package api
import (
"fmt"
"regexp"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/umputun/remark/app/store"
)
func TestServer_RssPost(t *testing.T) {
srv, ts := prep(t)
assert.NotNil(t, srv)
defer cleanup(ts)
waitOnSecChange()
c1 := store.Comment{
Text: "test 123",
Locator: store.Locator{URL: "https://radio-t.com/blah1", SiteID: "radio-t"},
}
id1 := addComment(t, c1, ts)
pubDate := time.Now().Format(time.RFC1123Z)
res, code := get(t, ts.URL+"/api/v1/rss/post?site=radio-t&url=https://radio-t.com/blah1")
assert.Equal(t, 200, code)
t.Log(res)
expected := fmt.Sprintf(`
Remark42 comments
https://radio-t.com/blah1
comment updates
%s
-
developer one
https://radio-t.com/blah1#remark42__comment-%s
<p>test 123</p>
developer one
%s
`, pubDate, id1, pubDate)
expected, res = cleanRssFormatting(expected, res)
assert.Equal(t, expected, res)
_, code = get(t, ts.URL+"/api/v1/rss/post?site=radio-t-bad&url=https://radio-t.com/blah1")
assert.Equal(t, 400, code)
}
func TestServer_RssSite(t *testing.T) {
srv, ts := prep(t)
assert.NotNil(t, srv)
defer cleanup(ts)
waitOnSecChange()
pubDate := time.Now().Format(time.RFC1123Z)
c1 := store.Comment{
Text: "test 123",
Locator: store.Locator{URL: "https://radio-t.com/blah10", SiteID: "radio-t"},
}
c2 := store.Comment{
Text: "xyz test",
Locator: store.Locator{URL: "https://radio-t.com/blah11", SiteID: "radio-t"},
}
id1 := addComment(t, c1, ts)
id2 := addComment(t, c2, ts)
res, code := get(t, ts.URL+"/api/v1/rss/site?site=radio-t")
assert.Equal(t, 200, code)
t.Log(res)
expected := fmt.Sprintf(`
Remark42 comments
radio-t
comment updates
%s
-
developer one
https://radio-t.com/blah11#remark42__comment-%s
<p>xyz test</p>
developer one
%s
-
developer one
https://radio-t.com/blah10#remark42__comment-%s
<p>test 123</p>
developer one
%s
`, pubDate, id2, pubDate, id1, pubDate)
expected, res = cleanRssFormatting(expected, res)
assert.Equal(t, expected, res)
_, code = get(t, ts.URL+"/api/v1/rss/site?site=bad-radio-t")
assert.Equal(t, 400, code)
}
func TestServer_RssWithReply(t *testing.T) {
srv, ts := prep(t)
assert.NotNil(t, srv)
defer cleanup(ts)
waitOnSecChange()
pubDate := time.Now().Format(time.RFC1123Z)
c1 := store.Comment{
Text: "test 123",
Locator: store.Locator{URL: "https://radio-t.com/blah10", SiteID: "radio-t"},
}
c2 := store.Comment{
Text: "xyz test",
Locator: store.Locator{URL: "https://radio-t.com/blah10", SiteID: "radio-t"},
}
id1 := addComment(t, c1, ts)
c2.ParentID = id1
id2 := addComment(t, c2, ts)
res, code := get(t, ts.URL+"/api/v1/rss/post?site=radio-t&url=https://radio-t.com/blah10")
assert.Equal(t, 200, code)
t.Log(res)
expected := fmt.Sprintf(`
Remark42 comments
https://radio-t.com/blah10
comment updates
%s
-
developer one > developer one
https://radio-t.com/blah10#remark42__comment-%s
<p>xyz test</p>
developer one
%s
-
developer one
https://radio-t.com/blah10#remark42__comment-%s
<p>test 123</p>
developer one
%s
`, pubDate, id2, pubDate, id1, pubDate)
expected, res = cleanRssFormatting(expected, res)
assert.Equal(t, expected, res)
}
func waitOnSecChange() {
for {
if time.Now().Nanosecond() < 100000000 {
break
}
time.Sleep(10 * time.Nanosecond)
}
}
// clean formatting, i.e. multiple spaces, \t, \n
func cleanRssFormatting(expected, actual string) (string, string) {
reSpaces := regexp.MustCompile(`[\s\p{Zs}]{2,}`)
expected = strings.Replace(expected, "\n", " ", -1)
expected = strings.Replace(expected, "\t", " ", -1)
expected = reSpaces.ReplaceAllString(expected, " ")
actual = strings.Replace(actual, "\n", " ", -1)
actual = reSpaces.ReplaceAllString(actual, " ")
return expected, actual
}