-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwch_test.go
245 lines (210 loc) · 5.89 KB
/
twch_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
package twch
import (
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"testing"
)
var (
mux *http.ServeMux
server *httptest.Server
client *Client
)
func setup() {
mux = http.NewServeMux()
server = httptest.NewServer(mux)
client, _ = NewClient("test-client-key", nil)
client.BaseUrl, _ = url.Parse(server.URL)
}
func teardown() {
server.Close()
}
type params map[string]string
// testParams compares the request's query params and body query with a custom defined map of string key values
func testParams(t *testing.T, r *http.Request, par params) {
want := url.Values{}
for k, v := range par {
want.Add(k, v)
}
r.ParseForm()
if got := r.Form; !reflect.DeepEqual(got, want) {
t.Errorf("testParams: Request params failed to match:\nwant: %+v\ngot: %+v", want, got)
}
}
func testMethod(t *testing.T, r *http.Request, method string) {
if r.Method != method {
t.Errorf("Top Games: method = %s, want %v", r.Method, method)
}
}
func testListResponse(t *testing.T, r *Response, total *int, next *int, prev *int) {
if r == nil {
t.Fatal("ListResponse: response is nil")
}
if total != nil {
if r.Total == nil {
t.Errorf("ListResponse: response total is nil")
} else if *r.Total != *total {
t.Errorf("ListResponse: response total should not include value: %+v", *r.Total)
}
} else {
if r.Total != total {
t.Errorf("ListResponse: response total should not include value: %+v", *r.Total)
}
}
if next != nil {
if r.NextOffset == nil {
t.Errorf("ListResponse: response next is nil")
} else if *r.NextOffset != *next {
t.Errorf("ListResponse: response next should not include value: %+v", *r.NextOffset)
}
} else {
if r.NextOffset != next {
t.Errorf("ListResponse: response next should not include value: %+v", *r.NextOffset)
}
}
if prev != nil {
if r.PrevOffset == nil {
t.Errorf("ListResponse: response prev is nil")
} else if *r.PrevOffset != *prev {
t.Errorf("ListResponse: response prev should not include value: %+v", *r.PrevOffset)
}
} else {
if r.PrevOffset != prev {
t.Errorf("ListResponse: response prev should not include value: %+v", *r.PrevOffset)
}
}
}
func assetPtr() *Asset {
return &Asset{
Large: stringPtr("l"),
Medium: stringPtr("m"),
Small: stringPtr("s"),
Template: stringPtr("t"),
}
}
func channelPtr() *Channel {
return &Channel{
ID: intPtr(1),
DisplayName: stringPtr("d"),
Name: stringPtr("n"),
Game: stringPtr("g"),
Delay: intPtr(0),
Status: stringPtr("s"),
Teams: make([]Team, 0),
Banner: stringPtr("b"),
Background: stringPtr("b"),
Logo: stringPtr("l"),
URL: stringPtr("u"),
CreatedAt: stringPtr("2011-12-23T18:03:44Z"),
UpdatedAt: stringPtr("2013-02-15T15:22:24Z"),
}
}
func TestNewClient(t *testing.T) {
id := "test-id"
c, err := NewClient(id, nil)
if err != nil {
t.Errorf("NewClient returned error: %+v", err)
}
if c.ID != id {
t.Errorf("NewClient ID is invalid: %v != %s", c.ID, id)
}
if got := c.BaseUrl.String(); got != baseUrl {
t.Errorf("NewClient baseUrl is invalid: %v != %v", got, baseUrl)
}
}
func TestNewClient_CustomClient(t *testing.T) {
cl := &http.Client{}
c, err := NewClient("test", cl)
if err != nil {
t.Errorf("NewClient returned error: %v", err)
}
if c.client != cl {
t.Errorf("NewClient custom client is invalid: %v", c.client)
}
}
func TestNewRequest_Header(t *testing.T) {
req, err := client.NewRequest("GET", "uri")
if err != nil {
t.Errorf("client.NewRequest returned error: %v", err)
}
if got := req.Header.Get("Accept"); got != acceptHeader {
t.Errorf("client.NewRequest Accept header was incorrect. Got: %v", got)
}
if got := req.Header.Get("Client-ID"); got != client.ID {
t.Errorf("client.NewRequest Client-ID header was incorrect. Got: %v", got)
}
}
func TestListTotal(t *testing.T) {
l := listTotal{Total: intPtr(1)}
if v := l.ListTotal(); *v != 1 {
t.Errorf("listTotal.Total() did not return correct number. Got: %v", *v)
}
}
func TestListLinks(t *testing.T) {
l := listLinks{
Links: listPagingLinks{
Next: "https://api.twitch.tv/kraken/streams?channel=zisss%2Cvoyboy&game=Diablo+III&limit=100&offset=300",
Prev: "https://api.twitch.tv/kraken/streams?channel=zisss%2Cvoyboy&game=Diablo+III&limit=100&offset=100",
},
}
n, err := l.NextOffset()
if err != nil {
t.Errorf("ListLinks.NextOffset() returned an error: %+v\n", err)
}
if *n != 300 {
t.Errorf("listLinks.NextOffset() did not return correct value. Got: %v\n", *n)
}
p, err := l.PrevOffset()
if err != nil {
t.Errorf("ListLinks.PrevOffset() returned an error: %+v\n", err)
}
if *p != 100 {
t.Errorf("listLinks.NextOffset() did not return correct value. Got: %v\n", *p)
}
}
func TestListLinks_empty(t *testing.T) {
l := listLinks{}
n, err := l.NextOffset()
if err != nil {
t.Errorf("ListLinks.NextOffset() returned an error: %+v\n", err)
}
if n != nil {
t.Errorf("listLinks.NextOffset() did not return correct value. Got: %v\n", *n)
}
p, err := l.PrevOffset()
if err != nil {
t.Errorf("ListLinks.PrevOffset() returned an error: %+v\n", err)
}
if p != nil {
t.Errorf("listLinks.NextOffset() did not return correct value. Got: %v\n", *p)
}
}
type testResponseData struct {
*listLinks
*listTotal
}
func TestNewResponse(t *testing.T) {
resp := new(http.Response)
want := &Response{
Response: resp,
NextOffset: intPtr(300),
PrevOffset: intPtr(100),
Total: intPtr(1),
}
got, err := newResponse(resp, testResponseData{
listLinks: &listLinks{
Links: listPagingLinks{
Next: "https://api.twitch.tv/kraken/streams?limit=100&offset=300",
Prev: "https://api.twitch.tv/kraken/streams?limit=100&offset=100",
},
},
listTotal: &listTotal{Total: intPtr(1)},
})
if err != nil {
t.Errorf("newResponse returned with an error: %+v", err)
}
if !reflect.DeepEqual(got, want) {
t.Errorf("newResponse did not return correct value\nwant: %+v\n got: %+v", want, got)
}
}