-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_test.go
431 lines (382 loc) · 10.6 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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
package microdotblog
import (
"bytes"
"net/http"
"reflect"
"strconv"
"testing"
"time"
)
const posts string = `
{
"version": "https://jsonfeed.org/version/1",
"title": "Micro.blog - Ricco Førgaard",
"home_page_url": "https://micro.blog/",
"feed_url": "https://micro.blog/posts/ricco",
"_microblog": {
"about": "https://micro.blog/about/api",
"id": "735",
"username": "ricco",
"bio": "I like to code stuff, grow stuff, brew stuff, cook stuff, solder stuff, and stuff.",
"is_following": true,
"is_you": true,
"following_count": 33
},
"author": {
"name": "Ricco Førgaard",
"url": "https://github.com/fiskeben",
"avatar": "https://www.gravatar.com/avatar/5a1b964e34cc2b1d3e233fa387b791b0?s=96"
},
"items": [
{
"id": "218679",
"content_html": "<p>I’m testing my micro.blog API client right now, so you may see some wierd posts :)</p>\n",
"url": "http://micro.fiskeben.dk/2017/12/09/im-testing-my.html",
"date_published": "2017-12-09T18:46:00+00:00",
"author": {
"name": "Ricco Førgaard",
"url": "https://github.com/fiskeben",
"avatar": "https://www.gravatar.com/avatar/5a1b964e34cc2b1d3e233fa387b791b0?s=96",
"_microblog": {
"username": "ricco"
}
},
"_microblog": {
"date_relative": "7:46 pm",
"is_favorite": false,
"is_deletable": true
}
},
{
"id": "218680",
"content_html": "<p>Another test of my Go client library.</p>\n",
"url": "http://micro.fiskeben.dk/2017/12/09/another-test-of.html",
"date_published": "2017-12-09T18:44:00+00:00",
"author": {
"name": "Ricco Førgaard",
"url": "https://github.com/fiskeben",
"avatar": "https://www.gravatar.com/avatar/5a1b964e34cc2b1d3e233fa387b791b0?s=96",
"_microblog": {
"username": "ricco"
}
},
"_microblog": {
"date_relative": "7:44 pm",
"is_favorite": false,
"is_deletable": true
}
},
{
"id": "218675",
"content_html": "<p>Just testing how to post from my Go micro.blog API.</p>\n",
"url": "http://micro.fiskeben.dk/2017/12/09/just-testing-how.html",
"date_published": "2017-12-09T18:31:00+00:00",
"author": {
"name": "Ricco Førgaard",
"url": "https://github.com/fiskeben",
"avatar": "https://www.gravatar.com/avatar/5a1b964e34cc2b1d3e233fa387b791b0?s=96",
"_microblog": {
"username": "ricco"
}
},
"_microblog": {
"date_relative": "7:31 pm",
"is_favorite": false,
"is_deletable": true
}
}
]
}`
type mockClient struct {
responseData http.Response
}
type body struct {
buf *bytes.Buffer
}
func (b body) Read(p []byte) (int, error) {
return b.buf.Read(p)
}
func (b body) Close() error {
return nil
}
func (m mockClient) Do(req *http.Request) (*http.Response, error) {
return &m.responseData, nil
}
func makeMockClient(token, responseData string) APIClient {
body := body{bytes.NewBufferString(responseData)}
response := http.Response{Body: body, StatusCode: 200, Status: "OK"}
c := apiClient{
httpClient: aClient{
httpClient: mockClient{responseData: response},
token: token,
},
}
return c
}
func makeFailingMockClient(statusCode int, status string) APIClient {
body := body{bytes.NewBufferString(status)}
response := http.Response{Body: body, StatusCode: statusCode, Status: status}
c := apiClient{
httpClient: aClient{
httpClient: mockClient{responseData: response},
token: "",
},
}
return c
}
func getField(v interface{}, field string) string {
r := reflect.ValueOf(v)
f := reflect.Indirect(r).FieldByName(field)
val := f.Interface()
switch someV := val.(type) {
case int:
return strconv.FormatInt(int64(someV), 10)
case int64:
return strconv.FormatInt(someV, 10)
case string:
return f.String()
case bool:
if f.Bool() {
return "true"
}
return "false"
case time.Time:
return someV.String()
}
return ""
}
func TestGetPosts(t *testing.T) {
c := makeMockClient("ABCD12345", posts)
feed, err := c.GetPosts()
if err != nil {
t.Error(err)
}
feedPropertiesTestCases := []struct {
PropertyName string
ExpectedValue interface{}
}{
{"Version", "https://jsonfeed.org/version/1"},
{"Title", "Micro.blog - Ricco Førgaard"},
{"HomepageURL", "https://micro.blog/"},
{"FeedURL", "https://micro.blog/posts/ricco"},
}
for _, tc := range feedPropertiesTestCases {
if value := getField(feed, tc.PropertyName); value != tc.ExpectedValue {
t.Errorf("%s is not equal to %v (was '%s')", tc.PropertyName, tc.ExpectedValue, value)
}
}
microblogpropertiesTestCases := []struct {
PropertyName string
ExpectedValue interface{}
}{
{"ID", "735"},
{"Username", "ricco"},
{"About", "https://micro.blog/about/api"},
{"Bio", "I like to code stuff, grow stuff, brew stuff, cook stuff, solder stuff, and stuff."},
{"IsFollowing", "true"},
{"IsYou", "true"},
{"FollowingCount", "33"},
}
props := feed.MicroblogProperties
for _, tc := range microblogpropertiesTestCases {
if value := getField(props, tc.PropertyName); value != tc.ExpectedValue {
t.Errorf("%s is not equal to %v (was '%s')", tc.PropertyName, tc.ExpectedValue, value)
}
}
authorPropertiesTestCases := []struct {
PropertyName string
ExpectedValue interface{}
}{
{"Name", "Ricco Førgaard"},
{"URL", "https://github.com/fiskeben"},
{"Avatar", "https://www.gravatar.com/avatar/5a1b964e34cc2b1d3e233fa387b791b0?s=96"},
}
author := feed.Author
for _, tc := range authorPropertiesTestCases {
if value := getField(author, tc.PropertyName); value != tc.ExpectedValue {
t.Errorf("%s is not equal to %v (was '%s')", tc.PropertyName, tc.ExpectedValue, value)
}
}
if len(feed.Items) != 3 {
t.Errorf("Not 3 items in feed (was %d)", len(feed.Items))
}
postTestCases := []struct {
PropertyName string
ExpectedValue interface{}
}{
{"ID", "218679"},
{"ContentHTML", "<p>I’m testing my micro.blog API client right now, so you may see some wierd posts :)</p>\n"},
{"URL", "http://micro.fiskeben.dk/2017/12/09/im-testing-my.html"},
{"DatePublished", "2017-12-09 18:46:00 +0000 +0000"},
}
post := feed.Items[0]
for _, tc := range postTestCases {
if value := getField(post, tc.PropertyName); value != tc.ExpectedValue {
t.Errorf("%s is not equal to %v (was '%s')", tc.PropertyName, tc.ExpectedValue, value)
}
}
postAuthorTestCases := []struct {
PropertyName string
ExpectedValue interface{}
}{
{"Name", "Ricco Førgaard"},
{"URL", "https://github.com/fiskeben"},
{"Avatar", "https://www.gravatar.com/avatar/5a1b964e34cc2b1d3e233fa387b791b0?s=96"},
}
postAuthor := post.Author
for _, tc := range postAuthorTestCases {
if value := getField(postAuthor, tc.PropertyName); value != tc.ExpectedValue {
t.Errorf("%s is not equal to %v (was '%s')", tc.PropertyName, tc.ExpectedValue, value)
}
}
if postAuthor.MicroblogProperties.Username != "ricco" {
t.Errorf("Post author metadata username is not equal to '%s (was '%s')", "ricco", postAuthor.MicroblogProperties.Username)
}
postMetadataTestCases := []struct {
PropertyName string
ExpectedValue interface{}
}{
{"DateRelative", "7:46 pm"},
{"IsFavorite", "false"},
{"IsDeletable", "true"},
}
postMetadata := post.MicroblogProperties
for _, tc := range postMetadataTestCases {
if value := getField(postMetadata, tc.PropertyName); value != tc.ExpectedValue {
t.Errorf("%s is not equal to %v (was '%s')", tc.PropertyName, tc.ExpectedValue, value)
}
}
}
func TestGetMentions(t *testing.T) {
c := makeMockClient("ABCD12345", posts)
feed, err := c.GetMentions()
if err != nil {
t.Error(err)
}
if len(feed.Items) != 3 {
t.Errorf("Returned feed doesn't look right")
}
}
func TestGetFavourites(t *testing.T) {
c := makeMockClient("ABCD12345", posts)
faves, err := c.GetFavourites()
if err != nil {
t.Error(err)
}
if len(faves.Items) != 3 {
t.Errorf("Returned feed doesn't look right")
}
}
func TestDiscover(t *testing.T) {
c := makeMockClient("ABCD12345", posts)
interesting, err := c.Discover()
if err != nil {
t.Error(err)
}
if len(interesting.Items) != 3 {
t.Errorf("Returned feed doesn't look right")
}
}
func TestGetUserPosts(t *testing.T) {
c := makeMockClient("ABCD12345", posts)
userPosts, err := c.GetUserPosts("manton")
if err != nil {
t.Error(err)
}
if len(userPosts.Items) != 3 {
t.Errorf("Returned feed doesn't look right")
}
}
func TestGetConversation(t *testing.T) {
c := makeMockClient("ABCD12345", posts)
convo, err := c.GetConversation(215436)
if err != nil {
t.Error(err)
}
if len(convo.Items) != 3 {
t.Errorf("Returned feed doesn't look right")
}
}
func TestCheck(t *testing.T) {
responseBody := `{"count":5,"check_seconds":120}`
c := makeMockClient("ABCD12345", responseBody)
check, err := c.Check(215436)
if err != nil {
t.Error(err)
}
if check.Count != 5 {
t.Errorf("Check call failed, got '%d' items, expected 5", check.Count)
}
if check.CheckSeconds != 120 {
t.Errorf("Check call failed, got '%d' seconds, expected 120", check.CheckSeconds)
}
}
func TestFavourite(t *testing.T) {
c := makeMockClient("ABCD12345", "")
err := c.Favourite(1234)
if err != nil {
t.Error(err)
}
}
func TestUnfavourite(t *testing.T) {
c := makeMockClient("ABCD12345", "")
err := c.Unfavourite(1234)
if err != nil {
t.Error(err)
}
}
func TestPost(t *testing.T) {
c := makeMockClient("ABCD12345", "")
_, err := c.Post("Just testing how to post from my Go micro.blog API.")
if err != nil {
t.Error(err)
}
}
func TestDeletePost(t *testing.T) {
c := makeMockClient("ABCD12345", "")
err := c.DeletePost(1234)
if err != nil {
t.Error(err)
}
}
func TestFollow(t *testing.T) {
c := makeMockClient("ABCD12345", "")
if err := c.Follow("manton"); err != nil {
t.Error(err)
}
}
func TestNotFound(t *testing.T) {
c := makeFailingMockClient(404, "Not found")
_, err := c.GetPosts()
if _, ok := err.(NotFound); !ok {
t.Errorf("Expected HTTP 404 not found, got %v", err)
}
}
func TestForbidden(t *testing.T) {
c := makeFailingMockClient(403, "Forbidden")
_, err := c.Post("I'm not allowd to do this")
if _, ok := err.(Forbidden); !ok {
t.Errorf("Expected HTTP 403 forbidden, got %v", err)
}
}
func TestNotAuthorized(t *testing.T) {
c := makeFailingMockClient(401, "Not authorized")
err := c.DeletePost(123)
if _, ok := err.(NotAuthorized); !ok {
t.Errorf("Expected HTTP 401 not authorized, got %v", err)
}
}
func TestInternalServerError(t *testing.T) {
c := makeFailingMockClient(504, "Gateway timed out")
_, err := c.GetConversation(1234)
if _, ok := err.(ServerError); !ok {
t.Errorf("Expected internal server error, got %v", err)
}
}
func TestClientError(t *testing.T) {
c := makeFailingMockClient(418, "I'm a teapot")
err := c.Follow("manton")
if _, ok := err.(ClientError); !ok {
t.Errorf("Expected client error, got %v", err)
}
}