-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrss_test.go
79 lines (71 loc) · 2.11 KB
/
rss_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package blawg
import (
"encoding/xml"
"html/template"
"strings"
"testing"
)
func TestNewRSS(t *testing.T) {
posts := Posts{
Post{
Body: template.HTML("<p>hello</p>"),
TitleText: "a bum title",
Metadata: Metadata{
Categories: []string{"unix", "fun"},
Description: "The best post ever",
},
},
}
config := Config{
URL: "http://test-url",
Description: "an amazing site",
Title: "Test URL",
}
rss, err := newRSS(&posts, config)
if err != nil {
t.Errorf("Couldn't even make a new RSS")
}
t.Run("RSS uses the config description", func(t *testing.T) {
if rss.Channel.Description != config.Description {
t.Errorf("Expected %+s to be %+s\n", rss.Channel.Description, config.Description)
}
})
t.Run("RSS uses the config title", func(t *testing.T) {
if rss.Channel.Title != config.Title {
t.Errorf("Expected %+s to be %+s\n", rss.Channel.Description, config.Description)
}
})
t.Run("creates the expected item", func(t *testing.T) {
want := RSSItem{
Category: strings.Join(posts[0].Metadata.Categories, ","),
Description: posts[0].Metadata.Description,
Title: posts[0].TitleText,
Link: "http://test-urlposts/1/1/1/a-bum-title/",
Guid: "http://test-urlposts/1/1/1/a-bum-title/",
PubDate: "Mon, 01 Jan 0001 00:00:00 +0000",
Content: Content{Text: string(posts[0].Body)},
}
got := rss.Channel.Item[0]
if got != want {
t.Errorf("wanted\n\t%#v\nbut got\n\t%#v\n", want, got)
}
})
t.Run("RSS actually makes some RSS", func(t *testing.T) {
sb := strings.Builder{}
xml.NewEncoder(&sb).Encode(rss)
rssXML := sb.String()
want := `<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"`
if !strings.Contains(rssXML, want) {
t.Errorf("Expected %+s to contain %+s\n", rssXML, want)
}
})
t.Run("could we get the content in a CDATA tag?", func(t *testing.T) {
sb := strings.Builder{}
xml.NewEncoder(&sb).Encode(rss)
rssXML := sb.String()
want := `<content:encoding><![CDATA[<p>hello</p>]]`
if !strings.Contains(rssXML, want) {
t.Errorf("Expected %+s to contain %+s\n", rssXML, want)
}
})
}