-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_test.go
243 lines (219 loc) · 5.88 KB
/
parser_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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package rssreader
import (
"net/http"
"reflect"
"strings"
"testing"
"github.com/araddon/dateparse"
)
func TestParse(t *testing.T) {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Query().Get("id") {
case "valid":
w.Write([]byte(schema_valid))
case "valid-source":
w.Write([]byte(schema_valid_source))
case "valid-no-item":
w.Write([]byte(schema_valid_no_item))
case "multi-items":
w.Write([]byte(schema_multi_items))
case "invalid":
w.Write([]byte(schema_invalid))
case "invalid-date":
w.Write([]byte(schema_invalid_date))
}
})
go func() {
err := http.ListenAndServe(":3000", nil)
if err != nil {
t.Error("couldn't setup the test")
}
}()
urls := []string{
"http://localhost:3000?id=valid",
"http://localhost:3000?id=valid-source",
"http://localhost:3000?id=valid-no-item",
"http://localhost:3000?id=multi-items",
"http://localhost:3000?id=invalid",
"http://localhost:3000?id=invalid-date",
}
items := Parse(urls...)
if len(items) != 4 {
t.Errorf("the amount of items should be 4 but is %d", len(items))
}
items = Parse("http://localhost:3000/invalid")
if len(items) != 0 {
t.Errorf("the amount of items should be 0 but is %d", len(items))
}
}
func Test_parseData(t *testing.T) {
testCases := []struct {
desc string
xml string
itemLength int
items []RSSItem
}{
{
desc: "a valid schema",
xml: schema_valid,
itemLength: 1,
items: []RSSItem{
{
Title: "RSS Tutorial",
Link: "https://www.w3schools.com/xml/xml_rss.asp",
Description: "New RSS tutorial on W3Schools",
PublishDate: dateparse.MustParse("Thu, 27 Apr 2006"),
Source: "localhost",
SourceURL: "http://localhost:3001",
},
},
},
{
desc: "a valid schema (2 items)",
xml: schema_multi_items,
itemLength: 2,
items: []RSSItem{
{
Title: "RSS Tutorial",
Link: "https://www.w3schools.com/xml/xml_rss.asp",
Description: "New RSS tutorial on W3Schools",
PublishDate: dateparse.MustParse("Thu, 27 Apr 2006"),
Source: "localhost",
SourceURL: "http://localhost:3001",
},
{
Title: "RSS Tutorial",
Link: "https://www.w3schools.com/xml/xml_rss.asp",
Description: "New RSS tutorial on W3Schools",
PublishDate: dateparse.MustParse("Thu, 27 Apr 2006"),
Source: "localhost",
SourceURL: "http://localhost:3001",
},
},
},
{
desc: "a valid schema (source)",
xml: schema_valid_source,
itemLength: 1,
items: []RSSItem{
{
Title: "RSS Tutorial",
Link: "https://www.w3schools.com/xml/xml_rss.asp",
Description: "New RSS tutorial on W3Schools",
PublishDate: dateparse.MustParse("Thu, 27 Apr 2006"),
Source: "W3Schools.com",
SourceURL: "https://www.w3schools.com",
},
},
},
{
desc: "an invalid schema",
xml: schema_invalid,
itemLength: 0,
items: []RSSItem{},
},
{
desc: "an invalid schema (date)",
xml: schema_invalid_date,
itemLength: 0,
items: []RSSItem{},
},
{
desc: "an valid schema (no item)",
xml: schema_invalid,
itemLength: 0,
items: []RSSItem{},
},
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
data := strings.NewReader(tC.xml)
items := parseData(data, "http://localhost:3001")
if len(items) != tC.itemLength {
t.Errorf("the items parsed should be %d but are %d", tC.itemLength, len(items))
}
if !reflect.DeepEqual(items, tC.items) {
t.Errorf("the items should be %#v", tC.items)
}
})
}
}
var schema_valid = `
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>W3Schools Home Page</title>
<link>https://www.w3schools.com</link>
<description>Free web building tutorials</description>
<item>
<title>RSS Tutorial</title>
<link>https://www.w3schools.com/xml/xml_rss.asp</link>
<description>New RSS tutorial on W3Schools</description>
<pubDate>Thu, 27 Apr 2006</pubDate>
</item>
</channel>
</rss>
`
var schema_multi_items = `
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>W3Schools Home Page</title>
<link>https://www.w3schools.com</link>
<description>Free web building tutorials</description>
<item>
<title>RSS Tutorial</title>
<link>https://www.w3schools.com/xml/xml_rss.asp</link>
<description>New RSS tutorial on W3Schools</description>
<pubDate>Thu, 27 Apr 2006</pubDate>
</item>
<item>
<title>RSS Tutorial</title>
<link>https://www.w3schools.com/xml/xml_rss.asp</link>
<description>New RSS tutorial on W3Schools</description>
<pubDate>Thu, 27 Apr 2006</pubDate>
</item>
</channel>
</rss>
`
var schema_valid_source = `
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>W3Schools Home Page</title>
<link>https://www.w3schools.com</link>
<description>Free web building tutorials</description>
<item>
<title>RSS Tutorial</title>
<link>https://www.w3schools.com/xml/xml_rss.asp</link>
<description>New RSS tutorial on W3Schools</description>
<source url="https://www.w3schools.com">W3Schools.com</source>
<pubDate>Thu, 27 Apr 2006</pubDate>
</item>
</channel>
</rss>
`
var schema_invalid = ""
var schema_invalid_date = `
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>W3Schools Home Page</title>
<link>https://www.w3schools.com</link>
<description>Free web building tutorials</description>
<item>
<title>RSS Tutorial</title>
<link>https://www.w3schools.com/xml/xml_rss.asp</link>
<description>New RSS tutorial on W3Schools</description>
<pubDate></pubDate>
</item>
</channel>
</rss>
`
var schema_valid_no_item = `
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
</channel>
</rss>
`