From ee7086fb57da51dbc1ceb56cf86b5468e1818cbc Mon Sep 17 00:00:00 2001 From: Umputun Date: Sun, 13 May 2018 18:53:41 -0500 Subject: [PATCH] rss controllers to separate mount --- app/rest/api/rest.go | 2 +- app/rest/api/rss.go | 7 +++++ app/rest/api/rss_test.go | 60 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 app/rest/api/rss_test.go diff --git a/app/rest/api/rest.go b/app/rest/api/rest.go index 42fb4cd6..8405c530 100644 --- a/app/rest/api/rest.go +++ b/app/rest/api/rest.go @@ -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) { diff --git a/app/rest/api/rss.go b/app/rest/api/rss.go index 00084aab..140fabcb 100644 --- a/app/rest/api/rss.go +++ b/app/rest/api/rss.go @@ -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")} diff --git a/app/rest/api/rss_test.go b/app/rest/api/rss_test.go new file mode 100644 index 00000000..a76dec16 --- /dev/null +++ b/app/rest/api/rss_test.go @@ -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(` + + Remark42 comments + https://radio-t.com/blah1 + comment updates + %s + + developer one + https://radio-t.com/blah1 + <p>test 123</p> + developer one + %s + + + `, 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) + +}