forked from gorilla/feeds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeed_test.go
514 lines (492 loc) · 19.4 KB
/
feed_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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
package feeds
import (
"bytes"
"testing"
"time"
)
var atomOutput = `<?xml version="1.0" encoding="UTF-8"?><feed xmlns="http://www.w3.org/2005/Atom">
<title>jmoiron.net blog</title>
<id>http://jmoiron.net/blog</id>
<updated>2013-01-16T21:52:35-05:00</updated>
<rights>This work is copyright © Benjamin Button</rights>
<subtitle>discussion about tech, footie, photos</subtitle>
<link href="http://jmoiron.net/blog"></link>
<author>
<name>Jason Moiron</name>
<email>[email protected]</email>
</author>
<entry>
<title>Limiting Concurrency in Go</title>
<updated>2013-01-16T21:52:35-05:00</updated>
<id>tag:jmoiron.net,2013-01-16:/blog/limiting-concurrency-in-go/</id>
<content type="html"><p>Go's goroutines make it easy to make <a href="http://collectiveidea.com/blog/archives/2012/12/03/playing-with-go-embarrassingly-parallel-scripts/">embarrassingly parallel programs</a>, but in many &quot;real world&quot; cases resources can be limited and attempting to do everything at once can exhaust your access to them.</p></content>
<link href="http://jmoiron.net/blog/limiting-concurrency-in-go/" rel="alternate"></link>
<summary type="html">A discussion on controlled parallelism in golang</summary>
<author>
<name>Jason Moiron</name>
<email>[email protected]</email>
</author>
</entry>
<entry>
<title>Logic-less Template Redux</title>
<updated>2013-01-16T21:52:35-05:00</updated>
<id>tag:jmoiron.net,2013-01-16:/blog/logicless-template-redux/</id>
<link href="http://jmoiron.net/blog/logicless-template-redux/" rel="alternate"></link>
<summary type="html">More thoughts on logicless templates</summary>
</entry>
<entry>
<title>Idiomatic Code Reuse in Go</title>
<updated>2013-01-16T21:52:35-05:00</updated>
<id>tag:jmoiron.net,2013-01-16:/blog/idiomatic-code-reuse-in-go/</id>
<link href="http://jmoiron.net/blog/idiomatic-code-reuse-in-go/" rel="alternate"></link>
<link href="http://example.com/cover.jpg" rel="enclosure" type="image/jpg" length="123456"></link>
<summary type="html">How to use interfaces <em>effectively</em></summary>
</entry>
<entry>
<title>Never Gonna Give You Up Mp3</title>
<updated>2013-01-16T21:52:35-05:00</updated>
<id>tag:example.com,2013-01-16:/RickRoll.mp3</id>
<link href="http://example.com/RickRoll.mp3" rel="alternate"></link>
<link href="http://example.com/RickRoll.mp3" rel="enclosure" type="audio/mpeg" length="123456"></link>
<summary type="html">Never gonna give you up - Never gonna let you down.</summary>
</entry>
<entry>
<title>String formatting in Go</title>
<updated>2013-01-16T21:52:35-05:00</updated>
<id>tag:example.com,2013-01-16:/strings</id>
<link href="http://example.com/strings" rel="alternate"></link>
<summary type="html">How to use things like %s, %v, %d, etc.</summary>
</entry>
</feed>`
var rssOutput = `<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
<title>jmoiron.net blog</title>
<link>http://jmoiron.net/blog</link>
<description>discussion about tech, footie, photos</description>
<copyright>This work is copyright © Benjamin Button</copyright>
<managingEditor>[email protected] (Jason Moiron)</managingEditor>
<pubDate>Wed, 16 Jan 2013 21:52:35 -0500</pubDate>
<item>
<title>Limiting Concurrency in Go</title>
<link>http://jmoiron.net/blog/limiting-concurrency-in-go/</link>
<description>A discussion on controlled parallelism in golang</description>
<content:encoded><![CDATA[<p>Go's goroutines make it easy to make <a href="http://collectiveidea.com/blog/archives/2012/12/03/playing-with-go-embarrassingly-parallel-scripts/">embarrassingly parallel programs</a>, but in many "real world" cases resources can be limited and attempting to do everything at once can exhaust your access to them.</p>]]></content:encoded>
<author>Jason Moiron</author>
<pubDate>Wed, 16 Jan 2013 21:52:35 -0500</pubDate>
</item>
<item>
<title>Logic-less Template Redux</title>
<link>http://jmoiron.net/blog/logicless-template-redux/</link>
<description>More thoughts on logicless templates</description>
<pubDate>Wed, 16 Jan 2013 21:52:35 -0500</pubDate>
</item>
<item>
<title>Idiomatic Code Reuse in Go</title>
<link>http://jmoiron.net/blog/idiomatic-code-reuse-in-go/</link>
<description>How to use interfaces <em>effectively</em></description>
<enclosure url="http://example.com/cover.jpg" length="123456" type="image/jpg"></enclosure>
<pubDate>Wed, 16 Jan 2013 21:52:35 -0500</pubDate>
</item>
<item>
<title>Never Gonna Give You Up Mp3</title>
<link>http://example.com/RickRoll.mp3</link>
<description>Never gonna give you up - Never gonna let you down.</description>
<enclosure url="http://example.com/RickRoll.mp3" length="123456" type="audio/mpeg"></enclosure>
<pubDate>Wed, 16 Jan 2013 21:52:35 -0500</pubDate>
</item>
<item>
<title>String formatting in Go</title>
<link>http://example.com/strings</link>
<description>How to use things like %s, %v, %d, etc.</description>
<pubDate>Wed, 16 Jan 2013 21:52:35 -0500</pubDate>
</item>
</channel>
</rss>`
var jsonOutput = `{
"version": "https://jsonfeed.org/version/1",
"title": "jmoiron.net blog",
"home_page_url": "http://jmoiron.net/blog",
"description": "discussion about tech, footie, photos",
"author": {
"name": "Jason Moiron"
},
"items": [
{
"id": "",
"url": "http://jmoiron.net/blog/limiting-concurrency-in-go/",
"title": "Limiting Concurrency in Go",
"content_html": "\u003cp\u003eGo's goroutines make it easy to make \u003ca href=\"http://collectiveidea.com/blog/archives/2012/12/03/playing-with-go-embarrassingly-parallel-scripts/\"\u003eembarrassingly parallel programs\u003c/a\u003e, but in many \u0026quot;real world\u0026quot; cases resources can be limited and attempting to do everything at once can exhaust your access to them.\u003c/p\u003e",
"summary": "A discussion on controlled parallelism in golang",
"date_published": "2013-01-16T21:52:35-05:00",
"author": {
"name": "Jason Moiron"
}
},
{
"id": "",
"url": "http://jmoiron.net/blog/logicless-template-redux/",
"title": "Logic-less Template Redux",
"summary": "More thoughts on logicless templates",
"date_published": "2013-01-16T21:52:35-05:00"
},
{
"id": "",
"url": "http://jmoiron.net/blog/idiomatic-code-reuse-in-go/",
"title": "Idiomatic Code Reuse in Go",
"summary": "How to use interfaces \u003cem\u003eeffectively\u003c/em\u003e",
"image": "http://example.com/cover.jpg",
"date_published": "2013-01-16T21:52:35-05:00"
},
{
"id": "",
"url": "http://example.com/RickRoll.mp3",
"title": "Never Gonna Give You Up Mp3",
"summary": "Never gonna give you up - Never gonna let you down.",
"date_published": "2013-01-16T21:52:35-05:00"
},
{
"id": "",
"url": "http://example.com/strings",
"title": "String formatting in Go",
"summary": "How to use things like %s, %v, %d, etc.",
"date_published": "2013-01-16T21:52:35-05:00"
}
]
}`
func TestFeed(t *testing.T) {
now, err := time.Parse(time.RFC3339, "2013-01-16T21:52:35-05:00")
if err != nil {
t.Error(err)
}
tz := time.FixedZone("EST", -5*60*60)
now = now.In(tz)
feed := &Feed{
Title: "jmoiron.net blog",
Link: &Link{Href: "http://jmoiron.net/blog"},
Description: "discussion about tech, footie, photos",
Author: &Author{Name: "Jason Moiron", Email: "[email protected]"},
Created: now,
Copyright: "This work is copyright © Benjamin Button",
}
feed.Items = []*Item{
{
Title: "Limiting Concurrency in Go",
Link: &Link{Href: "http://jmoiron.net/blog/limiting-concurrency-in-go/"},
Description: "A discussion on controlled parallelism in golang",
Author: &Author{Name: "Jason Moiron", Email: "[email protected]"},
Created: now,
Content: `<p>Go's goroutines make it easy to make <a href="http://collectiveidea.com/blog/archives/2012/12/03/playing-with-go-embarrassingly-parallel-scripts/">embarrassingly parallel programs</a>, but in many "real world" cases resources can be limited and attempting to do everything at once can exhaust your access to them.</p>`,
},
{
Title: "Logic-less Template Redux",
Link: &Link{Href: "http://jmoiron.net/blog/logicless-template-redux/"},
Description: "More thoughts on logicless templates",
Created: now,
},
{
Title: "Idiomatic Code Reuse in Go",
Link: &Link{Href: "http://jmoiron.net/blog/idiomatic-code-reuse-in-go/"},
Description: "How to use interfaces <em>effectively</em>",
Enclosure: &Enclosure{Url: "http://example.com/cover.jpg", Length: "123456", Type: "image/jpg"},
Created: now,
},
{
Title: "Never Gonna Give You Up Mp3",
Link: &Link{Href: "http://example.com/RickRoll.mp3"},
Enclosure: &Enclosure{Url: "http://example.com/RickRoll.mp3", Length: "123456", Type: "audio/mpeg"},
Description: "Never gonna give you up - Never gonna let you down.",
Created: now,
},
{
Title: "String formatting in Go",
Link: &Link{Href: "http://example.com/strings"},
Description: "How to use things like %s, %v, %d, etc.",
Created: now,
}}
atom, err := feed.ToAtom()
if err != nil {
t.Errorf("unexpected error encoding Atom: %v", err)
}
if atom != atomOutput {
t.Errorf("Atom not what was expected. Got:\n%s\n\nExpected:\n%s\n", atom, atomOutput)
}
var buf bytes.Buffer
if err := feed.WriteAtom(&buf); err != nil {
t.Errorf("unexpected error writing Atom: %v", err)
}
if got := buf.String(); got != atomOutput {
t.Errorf("Atom not what was expected. Got:\n%s\n\nExpected:\n%s\n", got, atomOutput)
}
rss, err := feed.ToRss()
if err != nil {
t.Errorf("unexpected error encoding RSS: %v", err)
}
if rss != rssOutput {
t.Errorf("Rss not what was expected. Got:\n%s\n\nExpected:\n%s\n", rss, rssOutput)
}
buf.Reset()
if err := feed.WriteRss(&buf); err != nil {
t.Errorf("unexpected error writing RSS: %v", err)
}
if got := buf.String(); got != rssOutput {
t.Errorf("Rss not what was expected. Got:\n%s\n\nExpected:\n%s\n", got, rssOutput)
}
json, err := feed.ToJSON()
if err != nil {
t.Errorf("unexpected error encoding JSON: %v", err)
}
if json != jsonOutput {
t.Errorf("JSON not what was expected. Got:\n%s\n\nExpected:\n%s\n", json, jsonOutput)
}
buf.Reset()
if err := feed.WriteJSON(&buf); err != nil {
t.Errorf("unexpected error writing JSON: %v", err)
}
if got := buf.String(); got != jsonOutput+"\n" { //json.Encode appends a newline after the JSON output: https://github.com/golang/go/commit/6f25f1d4c901417af1da65e41992d71c30f64f8f#diff-50848cbd686f250623a2ef6ddb07e157
t.Errorf("JSON not what was expected. Got:\n||%s||\n\nExpected:\n||%s||\n", got, jsonOutput)
}
}
var atomOutputSorted = `<?xml version="1.0" encoding="UTF-8"?><feed xmlns="http://www.w3.org/2005/Atom">
<title>jmoiron.net blog</title>
<id>http://jmoiron.net/blog</id>
<updated>2013-01-16T21:52:35-05:00</updated>
<rights>This work is copyright © Benjamin Button</rights>
<subtitle>discussion about tech, footie, photos</subtitle>
<link href="http://jmoiron.net/blog"></link>
<author>
<name>Jason Moiron</name>
<email>[email protected]</email>
</author>
<entry>
<title>Limiting Concurrency in Go</title>
<updated>2013-01-18T21:52:35-05:00</updated>
<id>tag:jmoiron.net,2013-01-18:/blog/limiting-concurrency-in-go/</id>
<link href="http://jmoiron.net/blog/limiting-concurrency-in-go/" rel="alternate"></link>
<summary type="html"></summary>
</entry>
<entry>
<title>Logic-less Template Redux</title>
<updated>2013-01-17T21:52:35-05:00</updated>
<id>tag:jmoiron.net,2013-01-17:/blog/logicless-template-redux/</id>
<link href="http://jmoiron.net/blog/logicless-template-redux/" rel="alternate"></link>
<summary type="html"></summary>
</entry>
<entry>
<title>Idiomatic Code Reuse in Go</title>
<updated>2013-01-17T09:52:35-05:00</updated>
<id>tag:jmoiron.net,2013-01-17:/blog/idiomatic-code-reuse-in-go/</id>
<link href="http://jmoiron.net/blog/idiomatic-code-reuse-in-go/" rel="alternate"></link>
<summary type="html"></summary>
</entry>
<entry>
<title>Never Gonna Give You Up Mp3</title>
<updated>2013-01-17T07:52:35-05:00</updated>
<id>tag:example.com,2013-01-17:/RickRoll.mp3</id>
<link href="http://example.com/RickRoll.mp3" rel="alternate"></link>
<summary type="html"></summary>
</entry>
<entry>
<title>String formatting in Go</title>
<updated>2013-01-16T21:52:35-05:00</updated>
<id>tag:example.com,2013-01-16:/strings</id>
<link href="http://example.com/strings" rel="alternate"></link>
<summary type="html"></summary>
</entry>
</feed>`
var rssOutputSorted = `<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
<title>jmoiron.net blog</title>
<link>http://jmoiron.net/blog</link>
<description>discussion about tech, footie, photos</description>
<copyright>This work is copyright © Benjamin Button</copyright>
<managingEditor>[email protected] (Jason Moiron)</managingEditor>
<pubDate>Wed, 16 Jan 2013 21:52:35 -0500</pubDate>
<item>
<title>Limiting Concurrency in Go</title>
<link>http://jmoiron.net/blog/limiting-concurrency-in-go/</link>
<description></description>
<pubDate>Fri, 18 Jan 2013 21:52:35 -0500</pubDate>
</item>
<item>
<title>Logic-less Template Redux</title>
<link>http://jmoiron.net/blog/logicless-template-redux/</link>
<description></description>
<pubDate>Thu, 17 Jan 2013 21:52:35 -0500</pubDate>
</item>
<item>
<title>Idiomatic Code Reuse in Go</title>
<link>http://jmoiron.net/blog/idiomatic-code-reuse-in-go/</link>
<description></description>
<pubDate>Thu, 17 Jan 2013 09:52:35 -0500</pubDate>
</item>
<item>
<title>Never Gonna Give You Up Mp3</title>
<link>http://example.com/RickRoll.mp3</link>
<description></description>
<pubDate>Thu, 17 Jan 2013 07:52:35 -0500</pubDate>
</item>
<item>
<title>String formatting in Go</title>
<link>http://example.com/strings</link>
<description></description>
<pubDate>Wed, 16 Jan 2013 21:52:35 -0500</pubDate>
</item>
</channel>
</rss>`
var jsonOutputSorted = `{
"version": "https://jsonfeed.org/version/1",
"title": "jmoiron.net blog",
"home_page_url": "http://jmoiron.net/blog",
"description": "discussion about tech, footie, photos",
"author": {
"name": "Jason Moiron"
},
"items": [
{
"id": "",
"url": "http://jmoiron.net/blog/limiting-concurrency-in-go/",
"title": "Limiting Concurrency in Go",
"date_published": "2013-01-18T21:52:35-05:00"
},
{
"id": "",
"url": "http://jmoiron.net/blog/logicless-template-redux/",
"title": "Logic-less Template Redux",
"date_published": "2013-01-17T21:52:35-05:00"
},
{
"id": "",
"url": "http://jmoiron.net/blog/idiomatic-code-reuse-in-go/",
"title": "Idiomatic Code Reuse in Go",
"date_published": "2013-01-17T09:52:35-05:00"
},
{
"id": "",
"url": "http://example.com/RickRoll.mp3",
"title": "Never Gonna Give You Up Mp3",
"date_published": "2013-01-17T07:52:35-05:00"
},
{
"id": "",
"url": "http://example.com/strings",
"title": "String formatting in Go",
"date_published": "2013-01-16T21:52:35-05:00"
}
]
}`
func TestFeedSorted(t *testing.T) {
now, err := time.Parse(time.RFC3339, "2013-01-16T21:52:35-05:00")
if err != nil {
t.Error(err)
}
tz := time.FixedZone("EST", -5*60*60)
now = now.In(tz)
feed := &Feed{
Title: "jmoiron.net blog",
Link: &Link{Href: "http://jmoiron.net/blog"},
Description: "discussion about tech, footie, photos",
Author: &Author{Name: "Jason Moiron", Email: "[email protected]"},
Created: now,
Copyright: "This work is copyright © Benjamin Button",
}
feed.Items = []*Item{
{
Title: "Limiting Concurrency in Go",
Link: &Link{Href: "http://jmoiron.net/blog/limiting-concurrency-in-go/"},
Created: now.Add(time.Duration(time.Hour * 48)),
},
{
Title: "Logic-less Template Redux",
Link: &Link{Href: "http://jmoiron.net/blog/logicless-template-redux/"},
Created: now.Add(time.Duration(time.Hour * 24)),
},
{
Title: "Idiomatic Code Reuse in Go",
Link: &Link{Href: "http://jmoiron.net/blog/idiomatic-code-reuse-in-go/"},
Created: now.Add(time.Duration(time.Hour * 12)),
},
{
Title: "Never Gonna Give You Up Mp3",
Link: &Link{Href: "http://example.com/RickRoll.mp3"},
Created: now.Add(time.Duration(time.Hour * 10)),
},
{
Title: "String formatting in Go",
Link: &Link{Href: "http://example.com/strings"},
Created: now,
}}
feed.Sort(func(a, b *Item) bool {
return a.Created.After(b.Created)
})
atom, err := feed.ToAtom()
if err != nil {
t.Errorf("unexpected error encoding Atom: %v", err)
}
if atom != atomOutputSorted {
t.Errorf("Atom not what was expected. Got:\n%s\n\nExpected:\n%s\n", atom, atomOutputSorted)
}
var buf bytes.Buffer
if err := feed.WriteAtom(&buf); err != nil {
t.Errorf("unexpected error writing Atom: %v", err)
}
if got := buf.String(); got != atomOutputSorted {
t.Errorf("Atom not what was expected. Got:\n%s\n\nExpected:\n%s\n", got, atomOutputSorted)
}
rss, err := feed.ToRss()
if err != nil {
t.Errorf("unexpected error encoding RSS: %v", err)
}
if rss != rssOutputSorted {
t.Errorf("Rss not what was expected. Got:\n%s\n\nExpected:\n%s\n", rss, rssOutputSorted)
}
buf.Reset()
if err := feed.WriteRss(&buf); err != nil {
t.Errorf("unexpected error writing RSS: %v", err)
}
if got := buf.String(); got != rssOutputSorted {
t.Errorf("Rss not what was expected. Got:\n%s\n\nExpected:\n%s\n", got, rssOutputSorted)
}
json, err := feed.ToJSON()
if err != nil {
t.Errorf("unexpected error encoding JSON: %v", err)
}
if json != jsonOutputSorted {
t.Errorf("JSON not what was expected. Got:\n%s\n\nExpected:\n%s\n", json, jsonOutputSorted)
}
buf.Reset()
if err := feed.WriteJSON(&buf); err != nil {
t.Errorf("unexpected error writing JSON: %v", err)
}
if got := buf.String(); got != jsonOutputSorted+"\n" { //json.Encode appends a newline after the JSON output: https://github.com/golang/go/commit/6f25f1d4c901417af1da65e41992d71c30f64f8f#diff-50848cbd686f250623a2ef6ddb07e157
t.Errorf("JSON not what was expected. Got:\n||%s||\n\nExpected:\n||%s||\n", got, jsonOutputSorted)
}
}
var jsonOutputHub = `{
"version": "https://jsonfeed.org/version/1",
"title": "feed title",
"hubs": [
{
"type": "WebSub",
"url": "https://websub-hub.example"
}
]
}`
func TestJSONHub(t *testing.T) {
feed := &JSONFeed{
Version: "https://jsonfeed.org/version/1",
Title: "feed title",
Hubs: []*JSONHub{
&JSONHub{
Type: "WebSub",
Url: "https://websub-hub.example",
},
},
}
json, err := feed.ToJSON()
if err != nil {
t.Errorf("unexpected error encoding JSON: %v", err)
}
if json != jsonOutputHub {
t.Errorf("JSON not what was expected. Got:\n%s\n\nExpected:\n%s\n", json, jsonOutputHub)
}
}