-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgopodcast_test.go
522 lines (462 loc) · 14.4 KB
/
gopodcast_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
515
516
517
518
519
520
521
522
package gopodcast_test
import (
"bytes"
"context"
"io"
"net/http"
"os"
"path"
"reflect"
"strings"
"testing"
"time"
"github.com/webbgeorge/gopodcast"
)
func TestParseFeed_RequiredFieldsOnly(t *testing.T) {
parser := gopodcast.NewParser()
f, err := os.Open("testdata/test-feed-minimum.xml")
if err != nil {
t.Fatal(err)
}
podcast, err := parser.ParseFeed(f)
if err != nil {
t.Fatal(err)
}
// channel fields
assertNotNil(t, podcast)
assertStr(t, "http://www.example.com/feed", podcast.AtomLink.Href)
assertStr(t, "self", podcast.AtomLink.Rel)
assertStr(t, "application/rss+xml", podcast.AtomLink.Type)
assertStr(t, "Test podcast 1", podcast.Title)
assertStr(t, "http://www.example.com/podcast-site", podcast.Link)
assertStr(t, "en", podcast.Language)
assertStr(t, "Test podcast description goes here", podcast.Description.Text)
assertBool(t, true, bool(podcast.ITunesExplicit))
assertStr(t, "http://www.example.com/image.jpg", podcast.ITunesImage.Href)
assertInt(t, 1, len(podcast.ITunesCategory))
assertStr(t, "Comedy", podcast.ITunesCategory[0].Text)
// non-required channel fields should be zero values
assertNil(t, podcast.ITunesCategory[0].SubCategory)
assertNil(t, podcast.PodcastLocked)
assertStr(t, "", podcast.PodcastGUID)
assertStr(t, "", podcast.ITunesAuthor)
assertStr(t, "", podcast.Copyright)
assertNil(t, podcast.PodcastText)
assertNil(t, podcast.PodcastFunding)
assertStr(t, "", podcast.ITunesType)
assertNil(t, podcast.ITunesComplete)
// item fields
assertInt(t, 2, len(podcast.Items))
item := podcast.Items[0]
item2 := podcast.Items[1]
assertStr(t, "Test episode 1", item.Title)
assertStr(t, "Test episode 2", item2.Title)
assertStr(t, "http://www.example.com/episode-1.mp3", item.Enclosure.URL)
assertStr(t, "audio/mpeg", item.Enclosure.Type)
assertInt(t, 1001, int(item.Enclosure.Length))
assertStr(t, "12345-67890-abcdef", item.GUID.Text)
// non-required item fields should be zero values
assertStr(t, "", item.Link)
assertNil(t, item.PubDate)
assertNil(t, item.Description)
assertStr(t, "", item.ITunesDuration)
assertNil(t, item.ITunesImage)
assertNil(t, item.ITunesExplicit)
assertInt(t, 0, len(item.PodcastTranscript))
assertStr(t, "", item.ITunesEpisode)
assertStr(t, "", item.ITunesSeason)
assertNil(t, item.ITunesBlock)
}
func TestParseFeed_AllFields(t *testing.T) {
parser := gopodcast.NewParser()
f, err := os.Open("testdata/test-feed-all.xml")
if err != nil {
t.Fatal(err)
}
podcast, err := parser.ParseFeed(f)
if err != nil {
t.Fatal(err)
}
// channel fields
assertNotNil(t, podcast)
assertStr(t, "http://www.example.com/feed", podcast.AtomLink.Href)
assertStr(t, "self", podcast.AtomLink.Rel)
assertStr(t, "application/rss+xml", podcast.AtomLink.Type)
assertStr(t, "Test podcast 1", podcast.Title)
assertStr(t, "http://www.example.com/podcast-site", podcast.Link)
assertStr(t, "en", podcast.Language)
assertStr(t, "Test podcast description goes here", podcast.Description.Text)
assertBool(t, true, bool(podcast.ITunesExplicit))
assertStr(t, "http://www.example.com/image.jpg", podcast.ITunesImage.Href)
assertInt(t, 2, len(podcast.ITunesCategory))
assertStr(t, "Comedy", podcast.ITunesCategory[0].Text)
assertStr(t, "Drama", podcast.ITunesCategory[1].Text)
assertStr(t, "Thriller", podcast.ITunesCategory[1].SubCategory.Text)
assertBool(t, true, bool(*podcast.PodcastLocked))
assertStr(t, "podcast-123456", podcast.PodcastGUID)
assertStr(t, "Dr Tester", podcast.ITunesAuthor)
assertStr(t, "Tester Inc.", podcast.Copyright)
assertStr(t, "abcdef", podcast.PodcastText.Text)
assertStr(t, "validation", podcast.PodcastText.Purpose)
assertStr(t, "Money please", podcast.PodcastFunding.Text)
assertStr(t, "http://www.example.com/money", podcast.PodcastFunding.URL)
assertStr(t, "Serialised", podcast.ITunesType)
assertBool(t, true, bool(*podcast.ITunesComplete))
// item fields
assertInt(t, 1, len(podcast.Items))
item := podcast.Items[0]
assertStr(t, "Test episode 1", item.Title)
assertStr(t, "http://www.example.com/episode-1.mp3", item.Enclosure.URL)
assertStr(t, "audio/mpeg", item.Enclosure.Type)
assertInt(t, 1001, int(item.Enclosure.Length))
assertStr(t, "12345-67890-abcdef", item.GUID.Text)
assertStr(t, "http://www.example.com/ep-link", item.Link)
assertStr(t, "2024-12-26T11:12:13Z", time.Time(*item.PubDate).Format(time.RFC3339))
assertStr(t, "Episode test description", item.Description.Text)
assertStr(t, "1234", item.ITunesDuration)
assertStr(t, "http://www.example.com/ep-image.png", item.ITunesImage.Href)
assertBool(t, false, bool(*item.ITunesExplicit))
assertInt(t, 2, len(item.PodcastTranscript))
assertStr(t, "http://www.example.com/transcript-1-en.txt", item.PodcastTranscript[0].URL)
assertStr(t, "text/plain", item.PodcastTranscript[0].Type)
assertStr(t, "self", item.PodcastTranscript[0].Rel)
assertStr(t, "en", item.PodcastTranscript[0].Language)
assertStr(t, "http://www.example.com/transcript-1-fr.txt", item.PodcastTranscript[1].URL)
assertStr(t, "text/plain", item.PodcastTranscript[1].Type)
assertStr(t, "self", item.PodcastTranscript[1].Rel)
assertStr(t, "fr", item.PodcastTranscript[1].Language)
assertStr(t, "1", item.ITunesEpisode)
assertStr(t, "2", item.ITunesSeason)
assertBool(t, false, bool(*item.ITunesBlock))
}
func TestWriteFeed_RequiredFieldsOnly(t *testing.T) {
podcast := &gopodcast.Podcast{
AtomLink: gopodcast.AtomLink{
Href: "http://www.example.com/feed",
Rel: "self",
Type: "application/rss+xml",
},
Title: "Test title",
Description: gopodcast.Description{Text: "Test description"},
Link: "http://www.example.com/podcast-site",
Language: "fr",
ITunesExplicit: true,
ITunesImage: gopodcast.ITunesImage{
Href: "http://www.example.com/image.png",
},
ITunesCategory: []gopodcast.ITunesCategory{{Text: "Drama"}},
Items: []*gopodcast.Item{
{
Title: "A podcast 1",
Enclosure: gopodcast.Enclosure{
URL: "http://www.example.com/pod1.mp3",
Type: "audio/mpeg",
Length: 2001,
},
GUID: gopodcast.ItemGUID{
Text: "abcdef-123456",
},
},
{
Title: "A podcast 2",
Enclosure: gopodcast.Enclosure{
URL: "http://www.example.com/pod2.mp3",
Type: "audio/mpeg",
Length: 2002,
},
GUID: gopodcast.ItemGUID{
Text: "abcdef-223456",
},
},
},
}
buf := &bytes.Buffer{}
podcast.WriteFeedXML(buf)
exp, err := os.ReadFile("testdata/test-feed-write-minimum.xml")
if err != nil {
t.Fatal(err)
}
assertStr(
t,
strings.TrimSpace(string(exp)),
strings.TrimSpace(buf.String()),
)
}
func TestWriteFeed_AllFields(t *testing.T) {
podcast := &gopodcast.Podcast{
AtomLink: gopodcast.AtomLink{
Href: "http://www.example.com/feed",
Rel: "self",
Type: "application/rss+xml",
},
Title: "Test title",
Description: gopodcast.Description{
Text: "Test description",
},
Link: "http://www.example.com/podcast-site",
Language: "fr",
ITunesExplicit: true,
ITunesImage: gopodcast.ITunesImage{
Href: "http://www.example.com/image.png",
},
ITunesCategory: []gopodcast.ITunesCategory{
{
Text: "Drama",
SubCategory: &gopodcast.ITunesCategory{Text: "Thriller"},
},
{
Text: "Comedy",
},
},
PodcastLocked: yesNoPtr(true),
PodcastGUID: "podcast-123-abc",
ITunesAuthor: "Mr Author",
Copyright: "Mr Author's Boss",
PodcastText: &gopodcast.PodcastText{
Purpose: "validation",
Text: "text test",
},
PodcastFunding: &gopodcast.PodcastFunding{
URL: "http://www.example.com/funding",
Text: "Money please",
},
ITunesType: "episodic",
ITunesComplete: yesNoPtr(true),
Items: []*gopodcast.Item{
{
Title: "A podcast 1",
Enclosure: gopodcast.Enclosure{
URL: "http://www.example.com/pod1.mp3",
Type: "audio/mpeg",
Length: 2001,
},
GUID: gopodcast.ItemGUID{
IsPermaLink: boolPtr(false),
Text: "abcdef-123456",
},
Link: "http://www.example.com/ep-link",
PubDate: timeFromStr("2024-12-25T10:11:12"),
Description: &gopodcast.Description{
Text: "Test episode description",
},
ITunesDuration: "12345",
ITunesImage: &gopodcast.ITunesImage{
Href: "http://www.example.com/ep-image.jpg",
},
ITunesExplicit: boolPtr(true),
PodcastTranscript: []gopodcast.PodcastTranscript{
{
URL: "http://www.example.com/ep/trans.fr.txt",
Type: "text/plain",
Rel: "something",
Language: "fr",
},
{
URL: "http://www.example.com/ep/trans.en.txt",
Type: "text/plain",
Rel: "something",
Language: "en",
},
},
ITunesEpisode: "1",
ITunesSeason: "2",
ITunesEpisodeType: "long",
ITunesBlock: yesNoPtr(false),
},
},
}
buf := &bytes.Buffer{}
podcast.WriteFeedXML(buf)
exp, err := os.ReadFile("testdata/test-feed-write-all.xml")
if err != nil {
t.Fatal(err)
}
assertStr(
t,
strings.TrimSpace(string(exp)),
strings.TrimSpace(buf.String()),
)
}
func TestParseFeedFromURL(t *testing.T) {
testFeedURL := "https://feeds.captivate.fm/elis-james-and-john-robins/"
parser := gopodcast.NewParser()
feed, err := parser.ParseFeedFromURL(context.Background(), testFeedURL)
if err != nil {
t.Fatal(err)
}
checkRequiredFeedValuesPresent(t, feed)
}
func TestParseFeedFromURL_Non200(t *testing.T) {
testFeedURL := "http://www.example.com/feed"
parser := gopodcast.NewParser()
parser.HTTPClient = newTestClient(500, "error")
feed, err := parser.ParseFeedFromURL(context.Background(), testFeedURL)
assertNil(t, feed)
assertStr(t, "non-200 http response '500'", err.Error())
}
func TestParseFeedFromURL_InvalidFeed(t *testing.T) {
testFeedURL := "http://www.example.com/feed"
parser := gopodcast.NewParser()
parser.HTTPClient = newTestClient(200, "NOT a valid feed")
feed, err := parser.ParseFeedFromURL(context.Background(), testFeedURL)
assertNil(t, feed)
assertNotNil(t, err)
}
func TestParseFeedFromURL_SendsAuthCreds(t *testing.T) {
interceptTransport := &interceptAuthTransport{
transport: http.DefaultTransport,
}
interceptClient := &http.Client{
Transport: interceptTransport,
}
testFeedURL := "http://www.example.com/feed"
parser := gopodcast.NewParser()
parser.HTTPClient = interceptClient
parser.AuthCredentials = &gopodcast.AuthCredentials{
Username: "user1",
Password: "password1",
}
_, _ = parser.ParseFeedFromURL(context.Background(), testFeedURL)
// basic auth: base64(user:pass)
assertStr(t, "Basic dXNlcjE6cGFzc3dvcmQx", interceptTransport.authHeader)
}
// TestParseFeed_TopPodcasts tests the parser against many different real podcasts,
// taken from the Apple charts.
func TestParseFeed_TopPodcasts(t *testing.T) {
files, err := os.ReadDir("testdata/top-podcasts")
if err != nil {
t.Fatal(err)
}
for _, file := range files {
if !file.Type().IsRegular() {
continue
}
t.Run(file.Name(), func(t *testing.T) {
parser := gopodcast.NewParser()
f, err := os.Open(path.Join("testdata/top-podcasts", file.Name()))
if err != nil {
t.Fatal(err)
}
podcast, err := parser.ParseFeed(f)
if err != nil {
t.Fatal(err)
}
checkRequiredFeedValuesPresent(t, podcast)
})
}
}
// checkRequiredFeedValuesPresent does some simple checks to make sure key
// fields are present in a podcast feed. This is used for running the parser
// tests across a large number of real podcast feeds.
//
// note that some podcast feeds don't include channel->link, enclosure->length,
// or channel->atom:link despite these being required by the PSP spec, so we
// don't test for them here.
func checkRequiredFeedValuesPresent(t *testing.T, podcast *gopodcast.Podcast) {
// channel fields
assertNotNil(t, podcast)
assertStrNotEmpty(t, podcast.Title)
assertStrNotEmpty(t, podcast.Language)
assertStrNotEmpty(t, podcast.Description.Text)
assertStrNotEmpty(t, podcast.ITunesImage.Href)
assertTrue(t, len(podcast.ITunesCategory) > 0)
assertStrNotEmpty(t, podcast.ITunesCategory[0].Text)
assertTrue(t, len(podcast.Items) > 0)
// use the first episode, as some feeds publish the latest episode ahead
// of release time, without the audio file.
item := podcast.Items[len(podcast.Items)-1]
// item fields
assertStrNotEmpty(t, item.Title)
assertStrNotEmpty(t, item.Enclosure.URL)
assertStrNotEmpty(t, item.Enclosure.Type)
assertStrNotEmpty(t, item.GUID.Text)
}
func boolPtr(b bool) *gopodcast.Bool {
bb := gopodcast.Bool(b)
return &bb
}
func yesNoPtr(b bool) *gopodcast.YesNo {
bb := gopodcast.YesNo(b)
return &bb
}
func timeFromStr(str string) *gopodcast.Time {
t, err := time.Parse("2006-01-02T15:04:05", str)
if err != nil {
panic(err)
}
tt := gopodcast.Time(t)
return &tt
}
// aim is for this library to have no dependencies, hence the assert funcs here
func assertTrue(t *testing.T, act bool) {
t.Helper()
if !act {
t.Fatal("expected to be true")
}
}
func assertNotNil(t *testing.T, act any) {
t.Helper()
if reflect.ValueOf(act).IsNil() {
t.Fatalf("expected '%+v' to not be nil", act)
}
}
func assertNil(t *testing.T, act any) {
t.Helper()
if !reflect.ValueOf(act).IsNil() {
t.Fatalf("expected '%+v' to be nil", act)
}
}
func assertStr(t *testing.T, exp string, act string) {
t.Helper()
if exp != act {
t.Fatalf("expected '%s', got '%s'", exp, act)
}
}
func assertBool(t *testing.T, exp bool, act bool) {
t.Helper()
if exp != act {
t.Fatalf("expected '%t', got '%t'", exp, act)
}
}
func assertInt(t *testing.T, exp int, act int) {
t.Helper()
if act != exp {
t.Fatalf("expected '%d', got '%d'", exp, act)
}
}
func assertStrNotEmpty(t *testing.T, act string) {
t.Helper()
if act == "" {
t.Fatal("expected string to not be empty")
}
}
func newTestClient(httpStatus int, content string) *http.Client {
return &http.Client{
Transport: &testTransport{
httpStatus: httpStatus,
content: content,
},
}
}
// testTransport returns the given http status code and content to enable testing of http clients
type testTransport struct {
httpStatus int
content string
}
func (t *testTransport) RoundTrip(r *http.Request) (*http.Response, error) {
buf := bytes.NewBufferString(t.content)
body := io.NopCloser(buf)
return &http.Response{
StatusCode: t.httpStatus,
Body: body,
}, nil
}
// interceptAuthTransport captures the value of the Authorization header to be used in tests
type interceptAuthTransport struct {
transport http.RoundTripper
authHeader string
}
func (t *interceptAuthTransport) RoundTrip(r *http.Request) (*http.Response, error) {
t.authHeader = r.Header.Get("Authorization")
return t.transport.RoundTrip(r)
}