forked from nicklaw5/helix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchannels_test.go
379 lines (341 loc) · 12.3 KB
/
channels_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
package helix
import (
"net/http"
"testing"
)
func TestSearchChannels(t *testing.T) {
t.Parallel()
testCases := []struct {
statusCode int
options *Options
First int
respBody string
parsed []Channel
}{
{
http.StatusOK,
&Options{ClientID: "my-client-id"},
2,
`{"data":[{"broadcaster_language":"en","display_name":"Ninja","game_id":"33214","game_name":"Loserfruit","id":"27833742640","is_live":false,"tag_ids":[],"thumbnail_url":"https://static-cdn.jtvnw.net/previews-ttv/live_user_ninja-{width}x{height}.jpg","title":"I have lost my voice D: | twitter.com/Ninja","started_at":"2018-03-06T15:07:45Z"},{"broadcaster_language":"en","display_name":"DrDisrespect","game_id":"33214","game_name":"Loserfruit","id":"27834185424","is_live":false,"tag_ids":[],"thumbnail_url":"https://static-cdn.jtvnw.net/previews-ttv/live_user_drdisrespectlive-{width}x{height}.jpg","title":"Turbo Treehouses || @DrDisRespect","started_at":"2018-03-06T16:05:00Z"}],"pagination":{"cursor":"eyJiIjpudWxsLCJhIjp7Ik9mZnNldCI6Mn19"}}`,
[]Channel{
{
ID: "27833742640",
GameID: "33214",
GameName: "Loserfruit",
BroadcasterLogin: "ninja",
DisplayName: "Ninja",
Language: "en",
Title: "I have lost my voice D: | twitter.com/Ninja",
ThumbnailURL: "https://static-cdn.jtvnw.net/previews-ttv/live_user_ninja-{width}x{height}.jpg",
IsLive: false,
TagIDs: []string{},
},
{
ID: "27834185424",
GameID: "33214",
GameName: "Loserfruit",
BroadcasterLogin: "drdisrespect",
DisplayName: "DrDisrespect",
Language: "en",
Title: "Turbo Treehouses || @DrDisRespect",
ThumbnailURL: "https://static-cdn.jtvnw.net/previews-ttv/live_user_drdisrespectlive-{width}x{height}.jpg",
IsLive: false,
TagIDs: []string{},
},
},
},
{
http.StatusBadRequest,
&Options{ClientID: "my-client-id"},
101,
`{"error":"Bad Request","status":400,"message":"The parameter \"first\" was malformed: the value must be less than or equal to 100"}`,
[]Channel{},
},
}
for _, testCase := range testCases {
c := newMockClient(testCase.options, newMockHandler(testCase.statusCode, testCase.respBody, nil))
resp, err := c.SearchChannels(&SearchChannelsParams{
First: testCase.First,
})
if err != nil {
t.Error(err)
}
// Test Bad Request Responses
if resp.StatusCode == http.StatusBadRequest {
firstErrStr := "The parameter \"first\" was malformed: the value must be less than or equal to 100"
if resp.ErrorMessage != firstErrStr {
t.Errorf("expected error message to be \"%s\", got \"%s\"", firstErrStr, resp.ErrorMessage)
}
continue
}
if resp.StatusCode != testCase.statusCode {
t.Errorf("expected status code to be \"%d\", got \"%d\"", testCase.statusCode, resp.StatusCode)
}
if len(resp.Data.Channels) != testCase.First {
t.Errorf("expected \"%d\" streams, got \"%d\"", testCase.First, len(resp.Data.Channels))
}
for i, channel := range resp.Data.Channels {
if channel.ID != testCase.parsed[i].ID {
t.Errorf("Expected struct field ID = %s, was %s", testCase.parsed[i].ID, channel.ID)
}
if channel.GameID != testCase.parsed[i].GameID {
t.Errorf("Expected struct field GameID = %s, was %s", testCase.parsed[i].GameID, channel.GameID)
}
if channel.DisplayName != testCase.parsed[i].DisplayName {
t.Errorf("Expected struct field DisplayName = %s, was %s", testCase.parsed[i].DisplayName, channel.DisplayName)
}
if channel.Language != testCase.parsed[i].Language {
t.Errorf("Expected struct field Language = %s, was %s", testCase.parsed[i].Language, channel.Language)
}
if channel.Title != testCase.parsed[i].Title {
t.Errorf("Expected struct field Title = %s, was %s", testCase.parsed[i].Title, channel.Title)
}
if channel.ThumbnailURL != testCase.parsed[i].ThumbnailURL {
t.Errorf("Expected struct field ThumbnailURL = %s, was %s", testCase.parsed[i].ThumbnailURL, channel.ThumbnailURL)
}
if channel.IsLive != testCase.parsed[i].IsLive {
t.Errorf("Expected struct field IsLive = %t, was %t", testCase.parsed[i].IsLive, channel.IsLive)
}
if len(channel.TagIDs) != len(testCase.parsed[i].TagIDs) {
t.Errorf("Expected struct field TagIDs length = %d, was %d", len(testCase.parsed[i].TagIDs), len(channel.TagIDs))
}
}
}
// Test with HTTP Failure
options := &Options{
ClientID: "my-client-id",
HTTPClient: &badMockHTTPClient{
newMockHandler(0, "", nil),
},
}
c := &Client{
opts: options,
}
_, err := c.SearchChannels(&SearchChannelsParams{})
if err == nil {
t.Error("expected error but got nil")
}
if err.Error() != "Failed to execute API request: Oops, that's bad :(" {
t.Error("expected error does match return error")
}
}
func TestGetChannelInformation(t *testing.T) {
t.Parallel()
testCases := []struct {
statusCode int
options *Options
BroadcasterID string
BroadcasterIDs []string
respBody string
parsed []ChannelInformation
}{
{
http.StatusOK,
&Options{ClientID: "my-client-id"},
"44445592",
[]string{},
`{"data":[{"broadcaster_id":"44445592","broadcaster_login":"pokimane","broadcaster_name":"pokimane","broadcaster_language":"en","game_id":"509658","game_name":"Just Chatting","title":"See you Wednesday 8am for Among Us ^_^", "delay": 2}]}`,
[]ChannelInformation{
{
BroadcasterID: "44445592",
BroadcasterName: "pokimane",
BroadcasterLanguage: "en",
GameID: "509658",
GameName: "Just Chatting",
Title: "See you Wednesday 8am for Among Us ^_^",
Delay: 2,
},
},
},
{
http.StatusBadRequest,
&Options{ClientID: "my-client-id"},
"9999999999999999999",
[]string{},
`{"error":"Bad Request","status":400,"message":"Invalid broadcasterID"}`,
[]ChannelInformation{},
},
{
http.StatusOK,
&Options{ClientID: "my-client-id"},
"",
[]string{"99631238", "11148817"},
`{"data":[{"broadcaster_id":"11148817","broadcaster_name":"pajlada","broadcaster_language":"en","game_id":"509670","game_name":"Science \u0026 Technology","title":"Programming.S04E71.1440p.MP4-XD.NVENC","delay":0},{"broadcaster_id":"99631238","broadcaster_name":"zneix","broadcaster_language":"en","game_id":"509670","game_name":"Science \u0026 Technology","title":"sunday coding, ranting and gaming (serious sam 4 later)","delay":0}]}`,
[]ChannelInformation{
{
BroadcasterID: "11148817",
BroadcasterName: "pajlada",
BroadcasterLanguage: "en",
GameID: "509670",
GameName: "Science & Technology",
Title: "Programming.S04E71.1440p.MP4-XD.NVENC",
Delay: 0,
},
{
BroadcasterID: "99631238",
BroadcasterName: "zneix",
BroadcasterLanguage: "en",
GameID: "509670",
GameName: "Science & Technology",
Title: "sunday coding, ranting and gaming (serious sam 4 later)",
Delay: 0,
},
},
},
{
http.StatusOK,
&Options{ClientID: "my-client-id"},
"",
[]string{"99631238", "forsen"},
`{"error": "Bad Request","status": 400,"message": "broadcaster_id \"forsen\" must be numeric"}`,
[]ChannelInformation{},
},
}
for _, testCase := range testCases {
c := newMockClient(testCase.options, newMockHandler(testCase.statusCode, testCase.respBody, nil))
var resp *GetChannelInformationResponse
var err error
if testCase.BroadcasterID != "" {
resp, err = c.GetChannelInformation(&GetChannelInformationParams{
BroadcasterID: testCase.BroadcasterID,
})
} else {
resp, err = c.GetChannelInformation(&GetChannelInformationParams{
BroadcasterIDs: testCase.BroadcasterIDs,
})
}
if err != nil {
t.Error(err)
}
// Test Bad Request Responses
if resp.StatusCode == http.StatusBadRequest {
broadcasterIDErrStr := "Invalid broadcasterID"
if resp.ErrorMessage != broadcasterIDErrStr {
t.Errorf("expected error message to be \"%s\", got \"%s\"", broadcasterIDErrStr, resp.ErrorMessage)
continue
}
}
if resp.StatusCode != testCase.statusCode {
t.Errorf("expected status code to be \"%d\", got \"%d\"", testCase.statusCode, resp.StatusCode)
}
for i, channel := range resp.Data.Channels {
if channel.BroadcasterID != testCase.parsed[i].BroadcasterID {
t.Errorf("Expected struct field ChannelID = %s, was %s", testCase.parsed[i].BroadcasterID, channel.BroadcasterID)
}
if channel.BroadcasterName != testCase.parsed[i].BroadcasterName {
t.Errorf("Expected struct field BroadcasterName = %s, was %s", testCase.parsed[i].BroadcasterName, channel.BroadcasterName)
}
if channel.BroadcasterLanguage != testCase.parsed[i].BroadcasterLanguage {
t.Errorf("Expected struct field BroadcasterLanguage = %s, was %s", testCase.parsed[i].BroadcasterLanguage, channel.BroadcasterLanguage)
}
if channel.GameID != testCase.parsed[i].GameID {
t.Errorf("Expected struct field GameID = %s, was %s", testCase.parsed[i].GameID, channel.GameID)
}
if channel.GameName != testCase.parsed[i].GameName {
t.Errorf("Expected struct field GameName = %s, was %s", testCase.parsed[i].GameName, channel.GameName)
}
if channel.Title != testCase.parsed[i].Title {
t.Errorf("Expected struct field Title = %s, was %s", testCase.parsed[i].Title, channel.Title)
}
}
}
// Test with HTTP Failure
options := &Options{
ClientID: "my-client-id",
HTTPClient: &badMockHTTPClient{
newMockHandler(0, "", nil),
},
}
c := &Client{
opts: options,
}
_, err := c.GetChannelInformation(&GetChannelInformationParams{})
if err == nil {
t.Error("expected error but got nil")
}
if err.Error() != "Failed to execute API request: Oops, that's bad :(" {
t.Error("expected error does match return error")
}
}
func TestEditChannelInformation(t *testing.T) {
t.Parallel()
testCases := []struct {
statusCode int
options *Options
params *EditChannelInformationParams
respBody string
}{
{
http.StatusOK,
&Options{ClientID: "my-client-id"},
&EditChannelInformationParams{
BroadcasterID: "123",
GameID: "456",
BroadcasterLanguage: "en",
Title: "Test title",
},
`{"data":[{"game_id":"498566","broadcaster_language":"en","title":"Test Twitch API"}]}`,
},
{
http.StatusBadRequest,
&Options{ClientID: "my-client-id"},
&EditChannelInformationParams{
BroadcasterID: "789",
GameID: "456",
BroadcasterLanguage: "en",
Title: "Test title",
Delay: 3,
},
`{"error":"Bad Request","status":400,"message":"the broadcaster is not partnered, failed to set delay"}`,
},
{
http.StatusBadRequest,
&Options{ClientID: "my-client-id"},
&EditChannelInformationParams{
BroadcasterID: "789",
GameID: "-1",
BroadcasterLanguage: "en",
Title: "Test title",
},
`{"error":"Bad Request","status":400,"message":"invalid game_id"}`,
},
}
for _, testCase := range testCases {
c := newMockClient(testCase.options, newMockHandler(testCase.statusCode, testCase.respBody, nil))
resp, err := c.EditChannelInformation(testCase.params)
if err != nil {
t.Error(err)
}
// Test Bad Request Responses
if resp.StatusCode == http.StatusBadRequest {
broadcasterIDErrStr := "the broadcaster is not partnered, failed to set delay"
if testCase.params.GameID == "-1" {
broadcasterIDErrStr = "invalid game_id"
}
if resp.ErrorMessage != broadcasterIDErrStr {
t.Errorf("expected error message to be \"%s\", got \"%s\"", broadcasterIDErrStr, resp.ErrorMessage)
continue
}
}
if resp.StatusCode != testCase.statusCode {
t.Errorf("expected status code to be \"%d\", got \"%d\"", testCase.statusCode, resp.StatusCode)
}
}
// Test with HTTP Failure
options := &Options{
ClientID: "my-client-id",
HTTPClient: &badMockHTTPClient{
newMockHandler(0, "", nil),
},
}
c := &Client{
opts: options,
}
_, err := c.EditChannelInformation(&EditChannelInformationParams{})
if err == nil {
t.Error("expected error but got nil")
}
if err.Error() != "Failed to execute API request: Oops, that's bad :(" {
t.Error("expected error does match return error")
}
}