1
2
3
4
5
6 package github
7
8 import (
9 "context"
10 "fmt"
11 "net/http"
12 "testing"
13
14 "github.com/google/go-cmp/cmp"
15 )
16
17 func TestUsersService_ListFollowers_authenticatedUser(t *testing.T) {
18 client, mux, _, teardown := setup()
19 defer teardown()
20
21 mux.HandleFunc("/user/followers", func(w http.ResponseWriter, r *http.Request) {
22 testMethod(t, r, "GET")
23 testFormValues(t, r, values{"page": "2"})
24 fmt.Fprint(w, `[{"id":1}]`)
25 })
26
27 opt := &ListOptions{Page: 2}
28 ctx := context.Background()
29 users, _, err := client.Users.ListFollowers(ctx, "", opt)
30 if err != nil {
31 t.Errorf("Users.ListFollowers returned error: %v", err)
32 }
33
34 want := []*User{{ID: Int64(1)}}
35 if !cmp.Equal(users, want) {
36 t.Errorf("Users.ListFollowers returned %+v, want %+v", users, want)
37 }
38
39 const methodName = "ListFollowers"
40 testBadOptions(t, methodName, func() (err error) {
41 _, _, err = client.Users.ListFollowers(ctx, "\n", opt)
42 return err
43 })
44
45 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
46 got, resp, err := client.Users.ListFollowers(ctx, "", opt)
47 if got != nil {
48 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
49 }
50 return resp, err
51 })
52 }
53
54 func TestUsersService_ListFollowers_specifiedUser(t *testing.T) {
55 client, mux, _, teardown := setup()
56 defer teardown()
57
58 mux.HandleFunc("/users/u/followers", func(w http.ResponseWriter, r *http.Request) {
59 testMethod(t, r, "GET")
60 fmt.Fprint(w, `[{"id":1}]`)
61 })
62
63 ctx := context.Background()
64 users, _, err := client.Users.ListFollowers(ctx, "u", nil)
65 if err != nil {
66 t.Errorf("Users.ListFollowers returned error: %v", err)
67 }
68
69 want := []*User{{ID: Int64(1)}}
70 if !cmp.Equal(users, want) {
71 t.Errorf("Users.ListFollowers returned %+v, want %+v", users, want)
72 }
73
74 const methodName = "ListFollowers"
75 testBadOptions(t, methodName, func() (err error) {
76 _, _, err = client.Users.ListFollowers(ctx, "\n", nil)
77 return err
78 })
79
80 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
81 got, resp, err := client.Users.ListFollowers(ctx, "u", nil)
82 if got != nil {
83 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
84 }
85 return resp, err
86 })
87 }
88
89 func TestUsersService_ListFollowers_invalidUser(t *testing.T) {
90 client, _, _, teardown := setup()
91 defer teardown()
92
93 ctx := context.Background()
94 _, _, err := client.Users.ListFollowers(ctx, "%", nil)
95 testURLParseError(t, err)
96 }
97
98 func TestUsersService_ListFollowing_authenticatedUser(t *testing.T) {
99 client, mux, _, teardown := setup()
100 defer teardown()
101
102 mux.HandleFunc("/user/following", func(w http.ResponseWriter, r *http.Request) {
103 testMethod(t, r, "GET")
104 testFormValues(t, r, values{"page": "2"})
105 fmt.Fprint(w, `[{"id":1}]`)
106 })
107
108 opts := &ListOptions{Page: 2}
109 ctx := context.Background()
110 users, _, err := client.Users.ListFollowing(ctx, "", opts)
111 if err != nil {
112 t.Errorf("Users.ListFollowing returned error: %v", err)
113 }
114
115 want := []*User{{ID: Int64(1)}}
116 if !cmp.Equal(users, want) {
117 t.Errorf("Users.ListFollowing returned %+v, want %+v", users, want)
118 }
119
120 const methodName = "ListFollowing"
121 testBadOptions(t, methodName, func() (err error) {
122 _, _, err = client.Users.ListFollowing(ctx, "\n", opts)
123 return err
124 })
125
126 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
127 got, resp, err := client.Users.ListFollowing(ctx, "", opts)
128 if got != nil {
129 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
130 }
131 return resp, err
132 })
133 }
134
135 func TestUsersService_ListFollowing_specifiedUser(t *testing.T) {
136 client, mux, _, teardown := setup()
137 defer teardown()
138
139 mux.HandleFunc("/users/u/following", func(w http.ResponseWriter, r *http.Request) {
140 testMethod(t, r, "GET")
141 fmt.Fprint(w, `[{"id":1}]`)
142 })
143
144 ctx := context.Background()
145 users, _, err := client.Users.ListFollowing(ctx, "u", nil)
146 if err != nil {
147 t.Errorf("Users.ListFollowing returned error: %v", err)
148 }
149
150 want := []*User{{ID: Int64(1)}}
151 if !cmp.Equal(users, want) {
152 t.Errorf("Users.ListFollowing returned %+v, want %+v", users, want)
153 }
154
155 const methodName = "ListFollowing"
156 testBadOptions(t, methodName, func() (err error) {
157 _, _, err = client.Users.ListFollowing(ctx, "\n", nil)
158 return err
159 })
160
161 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
162 got, resp, err := client.Users.ListFollowing(ctx, "u", nil)
163 if got != nil {
164 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
165 }
166 return resp, err
167 })
168 }
169
170 func TestUsersService_ListFollowing_invalidUser(t *testing.T) {
171 client, _, _, teardown := setup()
172 defer teardown()
173
174 ctx := context.Background()
175 _, _, err := client.Users.ListFollowing(ctx, "%", nil)
176 testURLParseError(t, err)
177 }
178
179 func TestUsersService_IsFollowing_authenticatedUser(t *testing.T) {
180 client, mux, _, teardown := setup()
181 defer teardown()
182
183 mux.HandleFunc("/user/following/t", func(w http.ResponseWriter, r *http.Request) {
184 testMethod(t, r, "GET")
185 w.WriteHeader(http.StatusNoContent)
186 })
187
188 ctx := context.Background()
189 following, _, err := client.Users.IsFollowing(ctx, "", "t")
190 if err != nil {
191 t.Errorf("Users.IsFollowing returned error: %v", err)
192 }
193 if want := true; following != want {
194 t.Errorf("Users.IsFollowing returned %+v, want %+v", following, want)
195 }
196
197 const methodName = "IsFollowing"
198 testBadOptions(t, methodName, func() (err error) {
199 _, _, err = client.Users.IsFollowing(ctx, "\n", "\n")
200 return err
201 })
202
203 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
204 got, resp, err := client.Users.IsFollowing(ctx, "", "t")
205 if got {
206 t.Errorf("testNewRequestAndDoFailure %v = %#v, want false", methodName, got)
207 }
208 return resp, err
209 })
210 }
211
212 func TestUsersService_IsFollowing_specifiedUser(t *testing.T) {
213 client, mux, _, teardown := setup()
214 defer teardown()
215
216 mux.HandleFunc("/users/u/following/t", func(w http.ResponseWriter, r *http.Request) {
217 testMethod(t, r, "GET")
218 w.WriteHeader(http.StatusNoContent)
219 })
220
221 ctx := context.Background()
222 following, _, err := client.Users.IsFollowing(ctx, "u", "t")
223 if err != nil {
224 t.Errorf("Users.IsFollowing returned error: %v", err)
225 }
226 if want := true; following != want {
227 t.Errorf("Users.IsFollowing returned %+v, want %+v", following, want)
228 }
229
230 const methodName = "IsFollowing"
231 testBadOptions(t, methodName, func() (err error) {
232 _, _, err = client.Users.IsFollowing(ctx, "\n", "\n")
233 return err
234 })
235
236 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
237 got, resp, err := client.Users.IsFollowing(ctx, "u", "t")
238 if got {
239 t.Errorf("testNewRequestAndDoFailure %v = %#v, want false", methodName, got)
240 }
241 return resp, err
242 })
243 }
244
245 func TestUsersService_IsFollowing_false(t *testing.T) {
246 client, mux, _, teardown := setup()
247 defer teardown()
248
249 mux.HandleFunc("/users/u/following/t", func(w http.ResponseWriter, r *http.Request) {
250 testMethod(t, r, "GET")
251 w.WriteHeader(http.StatusNotFound)
252 })
253
254 ctx := context.Background()
255 following, _, err := client.Users.IsFollowing(ctx, "u", "t")
256 if err != nil {
257 t.Errorf("Users.IsFollowing returned error: %v", err)
258 }
259 if want := false; following != want {
260 t.Errorf("Users.IsFollowing returned %+v, want %+v", following, want)
261 }
262
263 const methodName = "IsFollowing"
264 testBadOptions(t, methodName, func() (err error) {
265 _, _, err = client.Users.IsFollowing(ctx, "\n", "\n")
266 return err
267 })
268
269 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
270 got, resp, err := client.Users.IsFollowing(ctx, "u", "t")
271 if got {
272 t.Errorf("testNewRequestAndDoFailure %v = %#v, want false", methodName, got)
273 }
274 return resp, err
275 })
276 }
277
278 func TestUsersService_IsFollowing_error(t *testing.T) {
279 client, mux, _, teardown := setup()
280 defer teardown()
281
282 mux.HandleFunc("/users/u/following/t", func(w http.ResponseWriter, r *http.Request) {
283 testMethod(t, r, "GET")
284 http.Error(w, "BadRequest", http.StatusBadRequest)
285 })
286
287 ctx := context.Background()
288 following, _, err := client.Users.IsFollowing(ctx, "u", "t")
289 if err == nil {
290 t.Errorf("Expected HTTP 400 response")
291 }
292 if want := false; following != want {
293 t.Errorf("Users.IsFollowing returned %+v, want %+v", following, want)
294 }
295
296 const methodName = "IsFollowing"
297 testBadOptions(t, methodName, func() (err error) {
298 _, _, err = client.Users.IsFollowing(ctx, "u", "t")
299 return err
300 })
301
302 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
303 got, resp, err := client.Users.IsFollowing(ctx, "u", "t")
304 if got {
305 t.Errorf("testNewRequestAndDoFailure %v = %#v, want false", methodName, got)
306 }
307 return resp, err
308 })
309 }
310
311 func TestUsersService_IsFollowing_invalidUser(t *testing.T) {
312 client, _, _, teardown := setup()
313 defer teardown()
314
315 ctx := context.Background()
316 _, _, err := client.Users.IsFollowing(ctx, "%", "%")
317 testURLParseError(t, err)
318 }
319
320 func TestUsersService_Follow(t *testing.T) {
321 client, mux, _, teardown := setup()
322 defer teardown()
323
324 mux.HandleFunc("/user/following/u", func(w http.ResponseWriter, r *http.Request) {
325 testMethod(t, r, "PUT")
326 })
327
328 ctx := context.Background()
329 _, err := client.Users.Follow(ctx, "u")
330 if err != nil {
331 t.Errorf("Users.Follow returned error: %v", err)
332 }
333
334 const methodName = "Follow"
335 testBadOptions(t, methodName, func() (err error) {
336 _, err = client.Users.Follow(ctx, "\n")
337 return err
338 })
339
340 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
341 return client.Users.Follow(ctx, "u")
342 })
343 }
344
345 func TestUsersService_Follow_invalidUser(t *testing.T) {
346 client, _, _, teardown := setup()
347 defer teardown()
348
349 ctx := context.Background()
350 _, err := client.Users.Follow(ctx, "%")
351 testURLParseError(t, err)
352 }
353
354 func TestUsersService_Unfollow(t *testing.T) {
355 client, mux, _, teardown := setup()
356 defer teardown()
357
358 mux.HandleFunc("/user/following/u", func(w http.ResponseWriter, r *http.Request) {
359 testMethod(t, r, "DELETE")
360 })
361
362 ctx := context.Background()
363 _, err := client.Users.Unfollow(ctx, "u")
364 if err != nil {
365 t.Errorf("Users.Follow returned error: %v", err)
366 }
367
368 const methodName = "Unfollow"
369 testBadOptions(t, methodName, func() (err error) {
370 _, err = client.Users.Unfollow(ctx, "\n")
371 return err
372 })
373
374 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
375 return client.Users.Unfollow(ctx, "u")
376 })
377 }
378
379 func TestUsersService_Unfollow_invalidUser(t *testing.T) {
380 client, _, _, teardown := setup()
381 defer teardown()
382
383 ctx := context.Background()
384 _, err := client.Users.Unfollow(ctx, "%")
385 testURLParseError(t, err)
386 }
387
View as plain text