/
api_test.go
356 lines (278 loc) · 9.91 KB
/
api_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
package goib
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"strconv"
"testing"
"github.com/stretchr/testify/assert"
)
func TestShouldFailGracefullyOnEmptyResponse(t *testing.T) {
svr, a := setupServerAndAPI(emptyJSON)
defer svr.Close()
assertGracefulFailOnAllAPIMethods(t, a)
}
func TestShouldFailGracefullyOnMalformedResponse(t *testing.T) {
svr, a := setupServerAndAPI(missingCloseBracketJSON)
defer svr.Close()
assertGracefulFailOnAllAPIMethods(t, a)
}
func TestShouldFailGracefullyOnHTMLResponse(t *testing.T) {
svr, a := setupServerAndAPI(badResponseHTML)
defer svr.Close()
assertGracefulFailOnAllAPIMethods(t, a)
}
func TestShouldFailGracefullyOn500Response(t *testing.T) {
svr, a := setupServerAndAPIWithHTTPStatus(badResponseHTML, 500)
defer svr.Close()
assertGracefulFailOnAllAPIMethods(t, a)
}
func TestShouldFailGracefullyOn404Response(t *testing.T) {
svr, a := setupServerAndAPIWithHTTPStatus(badResponseHTML, 404)
defer svr.Close()
assertGracefulFailOnAllAPIMethods(t, a)
}
func TestEntryApi(t *testing.T) {
svr, a := setupServerAndAPI(entryJSON)
defer svr.Close()
response, err := a.Entry("someKrazyChannel", "someKookyKollection", nil)
if err != nil {
t.Errorf("error getting entry: %v", err)
}
collection := response.(*Collection)
if len(collection.Items) == 0 {
t.Errorf("zero items returned from entry")
}
}
func TestSearchApi(t *testing.T) {
svr, a := setupServerAndAPI(searchJSON)
defer svr.Close()
search, err := a.Search("wkrp", "nfl", nil)
if err != nil {
t.Errorf("error getting content: %v", err)
}
if search.TotalCount == 0 {
t.Errorf("zero results returned from search")
}
if search.Keywords != "nfl" {
t.Errorf("expected keywords 'nfl' but got %s", search.Keywords)
}
}
func TestContentApiShouldParseCollectionsOfCollections(t *testing.T) {
svr, a := setupServerAndAPI(multitieredCollectionJSON)
defer svr.Close()
response, err := a.Content("someKrazyChannel", 12345, nil)
if err != nil {
t.Errorf("error getting collection: %v", err)
}
collection := response.(*Collection)
if len(collection.Items) == 0 {
t.Errorf("zero items returned from entry")
}
innerCollection := collection.Items[0].(*Collection)
assert.Equal(t, 8, len(innerCollection.Items), "first inner collection should have 8 items but only has %d", len(innerCollection.Items))
}
func TestContentAPIShouldParseImageType(t *testing.T) {
svr, a := setupServerAndAPI(imageJSON)
defer svr.Close()
response, err := a.Content("someKrazyChannel", 12345, nil)
if err != nil {
t.Errorf("error getting image: %v", err)
}
img := response.(*Image)
assert.Equal(t, 29283344, img.ContentID, "expected COID 29283344 but got %d", img.ContentID)
}
func TestContentAPIShouldParseGalleryType(t *testing.T) {
svr, a := setupServerAndAPI(galleryJSON)
defer svr.Close()
response, err := a.Content("someKrazyChannel", 12345, nil)
if err != nil {
t.Errorf("error getting gallery: %v", err)
}
g := response.(*Gallery)
assert.Equal(t, 29283428, g.ContentID, "expected COID 29283428 but got %d", g.ContentID)
assert.Equal(t, 53, len(g.Items), "expected 50 image items in gallery but got %d", len(g.Items))
}
func TestContentAPIShouldParseHTMLContentType(t *testing.T) {
svr, a := setupServerAndAPI(htmlContent)
defer svr.Close()
response, err := a.Content("someKrazyChannel", 12345, nil)
if err != nil {
t.Errorf("error getting HTML: %v", err)
}
h := response.(*HTMLContent)
assert.Equal(t, 14277264, h.ContentID, "expected COID 14277264 but got %d", h.ContentID)
assert.True(t, strings.HasPrefix(h.Code, "\n\n<!-- Slideshow Widget"), "code field should be populated")
}
func TestShouldUnmarshallArticleMedia(t *testing.T) {
svr, a := setupServerAndAPI(htmlContent)
defer svr.Close()
r := Receiver{
Type: ArticleType,
Media: []Receiver{
Receiver{Type: VideoType, ContentID: 123},
Receiver{Type: ImageType, ContentID: 456},
},
}
art := a.(*api).unmarshalArticle(r)
assert.Equal(t, 2, len(art.Media), "media should have two elements")
}
func TestShouldUnmarshallSettingsForCollection(t *testing.T) {
svr, a := setupServerAndAPI(collectionWithSettingsJSON)
defer svr.Close()
response, err := a.Content("someKrazyChannel", 12345, nil)
c := response.(*Collection)
assert.Nil(t, err, "err should be nil")
assert.Equal(t, 3, len(c.Settings[0]), "should have found 3 setings")
assert.Equal(t, "hourly", c.Settings[0]["collection.WeatherIndicatorType"], "should have 'hourly' for specific setting")
}
func TestClosingsCount(t *testing.T) {
svr, a := setupServerAndAPI(closingsCount)
defer svr.Close()
closings, err := a.Closings("someKrazyChannel", ClosingsCount)
assert.Nil(t, err, "err should be nil")
assert.Equal(t, 3, closings.Count.Total, "should have found 3 closings")
}
func TestClosingsAll(t *testing.T) {
svr, a := setupServerAndAPI(closingsAll)
defer svr.Close()
closings, err := a.Closings("someKrazyChannel", ClosingsAll)
assert.Nil(t, err, "err should be nil")
assert.Equal(t, 2, len(closings.Institutions), "should have found 2 institution letter groups")
assert.Equal(t, 3, len(closings.Institutions["a"]))
}
func TestClosingsClosed(t *testing.T) {
svr, a := setupServerAndAPI(closingsClosed)
defer svr.Close()
closings, err := a.Closings("someKrazyChannel", ClosingsClosed)
assert.Nil(t, err, "err should be nil")
assert.Equal(t, 2, len(closings.ClosedInstitutions), "should have found 2 institution letter groups")
assert.Equal(t, 3, len(closings.ClosedInstitutions["a"]))
}
func Test_Closing(t *testing.T) {
svr, a := setupServerAndAPI(closingInstJSON)
defer svr.Close()
inst, err := a.Closings("someKrazyChannel", ClosingsInst, "provider_id")
assert.Nil(t, err, "err should be nil")
assert.Equal(t, "Anne Arundel County Schools", inst.Institution.Name)
assert.Equal(t, int64(1424385395), inst.Institution.PublicationDate)
assert.Equal(t, "Annapolis", inst.Institution.City)
assert.Equal(t, "Anne Arundel", inst.Institution.County)
assert.Equal(t, "MD", inst.Institution.State)
assert.Equal(t, "urn:ibsys:institution:bal:ibstandard:31", inst.Institution.ProviderID)
assert.Equal(t, "Friday: 2 Hour Delay; No AM half-day PreK/ECI", inst.Institution.Status)
}
func TestContentAPIShouldParseMapType(t *testing.T) {
svr, a := setupServerAndAPI(mapJSON)
defer svr.Close()
response, err := a.Content("someKrazyChannel", 12345, nil)
if err != nil {
t.Errorf("error getting map: %v", err)
}
m := response.(*Map)
assert.Equal(t, 19969494, m.ContentID)
assert.Equal(t, expectedStaticMap, m.StaticMap)
assert.Equal(t, expectedInteractiveMap, m.InteractiveMap)
assert.Equal(t, expectedMapTeaserTitle, m.TeaserTitle)
}
func TestContentAPIShouldParsePersonType(t *testing.T) {
svr, a := setupServerAndAPI(personJSON)
defer svr.Close()
response, err := a.Content("someKrazyChannel", 12345, nil)
if err != nil {
panic(err)
}
assert.Nil(t, err, "error getting person")
p := response.(*Person)
assert.Equal(t, 24416014, p.ContentID)
assert.Equal(t, expectedPersonFullName, p.FullName)
assert.Equal(t, expectedPersonEmail, p.Email)
assert.Equal(t, expectedPersonBlurb, p.Blurb)
}
func TestExternalContentStructPopulates(t *testing.T) {
svr, a := setupServerAndAPI(externalContentJSON)
defer svr.Close()
response, err := a.Entry("someKrazyChannel", "sumpin", nil)
assert.Nil(t, err, "error getting external content")
e := response.(*ExternalContent)
assert.NotNil(t, e.Struct)
assert.Equal(t, 1, len(e.Struct))
innerObj := e.Struct[0]
innerMap := innerObj.(map[string]interface{})
assert.NotNil(t, innerMap["authors"])
}
func TestContentAPIShouldParseLivevideoType(t *testing.T) {
svr, a := setupServerAndAPI(livevideoJSON)
defer svr.Close()
response, err := a.Content("someKrazyChannel", 12345, nil)
if err != nil {
panic(err)
}
assert.Nil(t, err, "error getting livevideo")
l := response.(*Livevideo)
assert.Equal(t, 25837856, l.ContentID)
assert.Equal(t, expectedLivevideoTeaserTitle, l.TeaserTitle)
assert.Equal(t, expectedStream, l.Stream)
}
func TestContentAPIShouldParseTeaserType(t *testing.T) {
svr, a := setupServerAndAPI(teaserJSON)
defer svr.Close()
response, err := a.Content("someKrazyChannel", 12345, nil)
if err != nil {
panic(err)
}
assert.Nil(t, err, "error getting teaser")
tease := response.(*Teaser)
assert.Equal(t, 30678220, tease.ContentID)
assert.Equal(t, expectedTeaserTeaserTitle, tease.TeaserTitle)
assert.Equal(t, 1, len(tease.Media))
}
func Test_unmarshalGalleryCaptions(t *testing.T) {
var r Receiver
err := json.Unmarshal([]byte(galleryJSON), &r)
assert.Nil(t, err)
captions := unmarshalGalleryCaptions(r)
assert.Equal(t, 3, len(captions))
caption, ok := captions[strconv.Itoa(29283346)]
assert.True(t, ok)
assert.Equal(t, "Foo bar", caption)
}
//-------------------------
func setupServerAndAPI(cannedResponse string) (*httptest.Server, API) {
testSvr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/xml")
fmt.Fprintln(w, cannedResponse)
}))
testURL, err := url.Parse(testSvr.URL)
if err != nil {
panic(err) // sanity check
}
a := NewAPI().(*api)
a.host = testURL.Host
return testSvr, a
}
func setupServerAndAPIWithHTTPStatus(cannedResponse string, status int) (*httptest.Server, API) {
testSvr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "floopy/yowza")
w.WriteHeader(500)
fmt.Fprintln(w, cannedResponse)
}))
testURL, err := url.Parse(testSvr.URL)
if err != nil {
panic(err) // sanity check
}
a := NewAPI().(*api)
a.host = testURL.Host
return testSvr, a
}
func assertGracefulFailOnAllAPIMethods(t *testing.T, a API) {
_, err := a.Entry("someKrazyChannel", "someKookyKollection", nil)
assert.NotNil(t, err, "error should not be nil")
_, err = a.Content("someKrazyChannel", 12345, nil)
assert.NotNil(t, err, "error should not be nil")
_, err = a.Search("someKrazyChannel", "kweery", nil)
assert.NotNil(t, err, "error should not be nil")
}