-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitunes_test.go
365 lines (304 loc) · 8.29 KB
/
itunes_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
package itunes_test
import (
"errors"
"fmt"
"io/ioutil"
"mime"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/deepilla/itunes"
)
func TestToRSS(t *testing.T) {
data := map[string]struct {
Paths []string
Feed string
Err error
}{
"Go Time": {
Paths: []string{
"podcasts/go-time/itunes-page",
},
Feed: "https://changelog.com/gotime/feed",
},
"Homecoming": {
Paths: []string{
"podcasts/homecoming/itunes-page",
"podcasts/homecoming/plist",
},
Feed: "http://feeds.gimletmedia.com/homecomingshow",
},
"Linux Voice": {
Paths: []string{
"podcasts/linux-voice/itunes-page",
},
Feed: "https://www.linuxvoice.com/podcast_mp3.rss",
},
"Longform": {
Paths: []string{
"podcasts/longform/itunes-page",
},
Feed: "http://longform.libsyn.com/rss",
},
"No Such Thing As A Fish": {
Paths: []string{
"podcasts/no-such-thing-as-a-fish/itunes-page",
"podcasts/no-such-thing-as-a-fish/plist",
},
Feed: "https://audioboom.com/channels/2399216.rss",
},
"Pod Save America": {
Paths: []string{
"podcasts/pod-save-america/itunes-page",
"podcasts/pod-save-america/plist",
},
Feed: "http://feeds.feedburner.com/pod-save-america",
},
"Revisionist History": {
Paths: []string{
"podcasts/revisionist-history/itunes-page",
},
Feed: "http://feeds.feedburner.com/RevisionistHistory",
},
"S-Town": {
Paths: []string{
"podcasts/s-town/itunes-page",
"podcasts/s-town/plist-1",
"podcasts/s-town/plist-2",
"podcasts/s-town/plist-3",
},
Feed: "http://feeds.stownpodcast.org/stownpodcast",
},
"Serial": {
Paths: []string{
"podcasts/serial/itunes-page",
"podcasts/serial/plist",
},
Feed: "http://feeds.serialpodcast.org/serialpodcast",
},
"The /Filmcast": {
Paths: []string{
"podcasts/filmcast/itunes-page",
},
Feed: "http://feeds.feedburner.com/filmcast",
},
"Wittertainment": {
Paths: []string{
"podcasts/wittertainment/itunes-page",
"podcasts/wittertainment/plist",
},
Feed: "https://podcasts.files.bbci.co.uk/b00lvdrj.rss",
},
"No Feed": {
Paths: []string{
"errors/no-feed/itunes-missing-user-agent",
"errors/no-feed/itunes-no-episodes",
"errors/no-feed/itunes-itunesu",
"errors/no-feed/plist-item-not-available",
"errors/no-feed/plist-incomplete",
"errors/no-feed/plist-blank-url",
},
Err: itunes.ErrNoFeed,
},
"Too Many Redirects": {
Paths: []string{
"errors/too-many-redirects/plist-4",
"errors/too-many-redirects/plist-recursive",
},
Err: errors.New("too many redirects"),
},
}
ts := httptest.NewServer(http.FileServer(http.Dir("testdata")))
defer ts.Close()
client := validateRequests(t, redirectRequests(ts, http.DefaultClient))
for name, test := range data {
for i, url := range test.Paths {
feed, err := itunes.ToRSSClient(url, client)
if !equalErrors(err, test.Err) {
t.Errorf("%s [%d/%d]: expected error %s, got %s", name, i+1, len(test.Paths), formatError(test.Err), formatError(err))
}
if feed != test.Feed {
t.Errorf("%s [%d/%d]: expected feed %q, got %q", name, i+1, len(test.Paths), test.Feed, feed)
}
}
}
}
func TestBadURL(t *testing.T) {
urls := []string{
"",
"https://",
"://itunes.apple.com/podcasts/123456789",
"1ttps://itunes.apple.com/podcasts/123456789",
"http://itunes.apple.com/podcasts/123456789#bad%%20escaping",
}
skipped := 0
for _, u := range urls {
_, err := url.Parse(u)
if err == nil {
t.Logf("Warning: Parse(%q) didn't return an error", u)
skipped++
if skipped == len(urls) {
t.Fatalf("No URLs tested")
}
continue
}
if e, ok := err.(*url.Error); ok {
err = e.Err
}
exp := fmt.Errorf("fetch error: bad URL: %s", err)
_, got := itunes.ToRSS(u)
if !equalErrors(got, exp) {
t.Errorf("URL %q: expected error %s, got %s", u, formatError(exp), formatError(got))
}
}
}
func TestClientError(t *testing.T) {
msgs := []string{
"it was nearly eleven when I started to return",
"the night was unexpectedly dark",
"to me, walking out of the lighted passage of my cousin's house",
"it seemed indeed black",
}
for _, s := range msgs {
client := clientFunc(func(*http.Request) (*http.Response, error) {
return nil, errors.New(s)
})
exp := fmt.Errorf("fetch error: %s", s)
_, got := itunes.ToRSSClient("", client)
if !equalErrors(got, exp) {
t.Errorf("expected error %s, got %s", formatError(exp), formatError(got))
}
}
}
func TestBadHTTPStatus(t *testing.T) {
statusCodes := []int{
http.StatusForbidden,
http.StatusNotFound,
http.StatusGone,
http.StatusTeapot,
http.StatusInternalServerError,
http.StatusBadGateway,
http.StatusInternalServerError,
420, // Enhance Your Calm (Twitter)
444, // No Response (nginx)
600, // Non-existent
}
for _, code := range statusCodes {
ts := httptest.NewServer(errorHandler(code))
client := redirectRequests(ts, http.DefaultClient)
msg := http.StatusText(code)
if msg == "" {
msg = fmt.Sprintf("status code %d", code) // Go's default status for unrecognised error codes
}
exp := fmt.Errorf("fetch error: %d %s", code, msg)
_, got := itunes.ToRSSClient("", client)
if !equalErrors(got, exp) {
t.Errorf("Status %d: expected error %s, got %s", code, formatError(exp), formatError(got))
}
ts.Close()
}
}
func TestBadContentType(t *testing.T) {
types := []string{
"",
"text/",
"text/xml; =",
}
skipped := 0
for _, ctype := range types {
_, _, err := mime.ParseMediaType(ctype)
if err == nil {
t.Logf("Warning: ParseMediaType(%q) didn't return an error", ctype)
skipped++
if skipped == len(types) {
t.Fatalf("No Content Types tested")
}
continue
}
ts := httptest.NewServer(contentTypeHandler(ctype))
client := redirectRequests(ts, http.DefaultClient)
exp := fmt.Errorf("bad Content Type %q: %s", ctype, err)
_, got := itunes.ToRSSClient("", client)
if !equalErrors(got, exp) {
t.Errorf("Content Type %q: expected error %s, got %s", ctype, formatError(exp), formatError(got))
}
ts.Close()
}
}
func TestUnsupportedContentType(t *testing.T) {
types := []string{
"image/png",
"text/plain; charset=utf-8",
"audio/mpeg",
}
for _, ctype := range types {
ts := httptest.NewServer(contentTypeHandler(ctype))
client := redirectRequests(ts, http.DefaultClient)
exp := fmt.Errorf("unsupported Content Type %q", ctype)
_, got := itunes.ToRSSClient("", client)
if !equalErrors(got, exp) {
t.Errorf("Content Type %q: expected error %s, got %s", ctype, formatError(exp), formatError(got))
}
ts.Close()
}
}
func contentTypeHandler(typ string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", typ)
})
}
func errorHandler(code int) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "helpful error message", code)
})
}
type clientFunc func(req *http.Request) (*http.Response, error)
func (f clientFunc) Do(req *http.Request) (*http.Response, error) {
return f(req)
}
func redirectRequests(ts *httptest.Server, client itunes.Client) itunes.Client {
return clientFunc(func(req *http.Request) (*http.Response, error) {
newURL := ts.URL + "/" + req.URL.Path
u, err := url.Parse(newURL)
if err != nil {
return nil, err
}
req.URL = u
return client.Do(req)
})
}
func validateRequests(t *testing.T, client itunes.Client) itunes.Client {
return clientFunc(func(req *http.Request) (*http.Response, error) {
if got, exp := req.Method, "GET"; got != exp {
t.Fatalf("Bad Request: expected Method %q, got %q", exp, got)
}
if got, exp := req.Header.Get("User-Agent"), "iTunes/10.1"; got != exp {
t.Fatalf("Bad Request: expected User Agent %q, got %q", exp, got)
}
if req.Body != nil {
b, err := ioutil.ReadAll(req.Body)
if err != nil {
t.Fatalf("Bad Request: Expected nil body, got something unreadable (read error: %s)", err)
}
t.Fatalf("Bad Request: Expected nil body, got %q", string(b))
}
return client.Do(req)
})
}
func formatError(err error) string {
if err == nil {
return "Nil"
}
return fmt.Sprintf("%q", err)
}
func equalErrors(err1, err2 error) bool {
switch {
case err1 == err2:
return true
case err1 == nil, err2 == nil:
return false
default:
return err1.Error() == err2.Error()
}
}