rss controllers to separate mount

This commit is contained in:
Umputun
2018-05-13 18:53:41 -05:00
parent 399d949e5a
commit ee7086fb57
3 changed files with 68 additions and 1 deletions
+1 -1
View File
@@ -97,7 +97,7 @@ func (s *Rest) Run(port int) {
rapi.Get("/list", s.listCtrl)
rapi.Get("/config", s.configCtrl)
rapi.Post("/preview", s.previewCommentCtrl)
rapi.Get("/rss", s.rssPostCommentsCtrl)
rapi.Mount("/rss", s.rssRoutes())
// protected routes, require auth
rapi.With(s.Authenticator.Auth(true)).Group(func(rauth chi.Router) {
+7
View File
@@ -5,12 +5,19 @@ import (
"net/http"
"time"
"github.com/go-chi/chi"
"github.com/go-chi/render"
"github.com/gorilla/feeds"
"github.com/umputun/remark/app/rest"
"github.com/umputun/remark/app/store"
)
func (s *Rest) rssRoutes() chi.Router {
router := chi.NewRouter()
router.Get("/post", s.rssPostCommentsCtrl)
return router
}
// GET /rss/post?site=siteID&url=post-url
func (s *Rest) rssPostCommentsCtrl(w http.ResponseWriter, r *http.Request) {
locator := store.Locator{SiteID: r.URL.Query().Get("site"), URL: r.URL.Query().Get("url")}
+60
View File
@@ -0,0 +1,60 @@
package api
import (
"fmt"
"net/http"
"regexp"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestServer_Rss(t *testing.T) {
srv, port := prep(t)
assert.NotNil(t, srv)
defer cleanup(srv)
// add one more comment
r := strings.NewReader(`{"text": "test 123", "locator":{"url": "https://radio-t.com/blah1", "site": "radio-t"}}`)
resp, err := http.Post(fmt.Sprintf("http://dev:password@127.0.0.1:%d/api/v1/comment", port), "application/json", r)
assert.Nil(t, err)
assert.Equal(t, http.StatusCreated, resp.StatusCode)
pubDate := time.Now().Format(time.RFC1123Z)
// get created comment by id
res, code := get(t, fmt.Sprintf("http://dev:password@127.0.0.1:%d/api/v1/rss/post?site=radio-t&url=https://radio-t.com/blah1", port))
assert.Equal(t, 200, code)
assert.Nil(t, err)
t.Log(res)
expected := fmt.Sprintf(`<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
<title>Remark42 comments</title>
<link>https://radio-t.com/blah1</link>
<description>comment updates</description>
<pubDate>%s</pubDate>
<item>
<title>developer one</title>
<link>https://radio-t.com/blah1</link>
<description>&lt;p&gt;test 123&lt;/p&gt;&#xA;</description>
<author>developer one</author>
<pubDate>%s</pubDate>
</item>
</channel>
</rss>`, pubDate, pubDate)
// clean formatting, i.e. multiple spaces, \t, \n
expected = strings.Replace(expected, "\n", " ", -1)
expected = strings.Replace(expected, "\t", " ", -1)
res = strings.Replace(res, "\n", " ", -1)
reSpaces := regexp.MustCompile(`[\s\p{Zs}]{2,}`)
expected = reSpaces.ReplaceAllString(expected, " ")
res = reSpaces.ReplaceAllString(res, " ")
assert.Equal(t, expected, res)
}