forked from huandu/facebook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_test.go
170 lines (132 loc) · 3.97 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
// A facebook graph api client in go.
// https://github.com/huandu/facebook/
//
// Copyright 2012, Huan Du
// Licensed under the MIT license
// https://github.com/huandu/facebook/blob/master/LICENSE
package facebook
import (
"testing"
)
func TestApiGetUserInfoV2(t *testing.T) {
Version = FB_LATEST_VERSION
defer func() {
Version = ""
}()
// It's not allowed to get user info by name. So I get "me" with access token instead.
if FB_TEST_VALID_ACCESS_TOKEN != "" {
me, err := Api("me", GET, Params{
"access_token": FB_TEST_VALID_ACCESS_TOKEN,
})
if err != nil {
t.Fatalf("cannot get my info. [e:%v]", err)
}
if e := me.Err(); e != nil {
t.Fatalf("facebook returns error. [e:%v]", e)
}
t.Logf("my info. %v", me)
}
}
func TestBatchApiGetInfo(t *testing.T) {
if FB_TEST_VALID_ACCESS_TOKEN == "" {
t.Skipf("cannot call batch api without access token. skip this test.")
}
verifyBatchResult := func(t *testing.T, index int, res Result) {
batch, err := res.Batch()
if err != nil {
t.Fatalf("cannot parse batch api results[%v]. [e:%v] [result:%v]", index, err, res)
}
if batch.StatusCode != 200 {
t.Fatalf("facebook returns unexpected http status code in results[%v]. [code:%v] [result:%v]", index, batch.StatusCode, res)
}
contentType := batch.Header.Get("Content-Type")
if contentType == "" {
t.Fatalf("facebook returns unexpected http header in results[%v]. [header:%v]", index, batch.Header)
}
if batch.Body == "" {
t.Fatalf("facebook returns unexpected http body in results[%v]. [body:%v]", index, batch.Body)
}
var id string
err = batch.Result.DecodeField("id", &id)
if err != nil {
t.Fatalf("cannot get 'id' field in results[%v]. [result:%v]", index, res)
}
if id == "" {
t.Fatalf("facebook should return account id in results[%v].", index)
}
}
test := func(t *testing.T) {
params1 := Params{
"method": GET,
"relative_url": "me",
}
params2 := Params{
"method": GET,
"relative_url": "100002828925788", // id of my another facebook account
}
results, err := BatchApi(FB_TEST_VALID_ACCESS_TOKEN, params1, params2)
if err != nil {
t.Fatalf("cannot get batch result. [e:%v]", err)
}
if len(results) != 2 {
t.Fatalf("batch api should return results in an array with 2 entries. [len:%v]", len(results))
}
if Version == "" {
t.Log("use default facebook version.")
} else {
t.Logf("global facebook version: %v", Version)
}
for index, result := range results {
verifyBatchResult(t, index, result)
}
}
// Use default Version.
Version = ""
test(t)
// User FB_LATEST_VERSION.
Version = FB_LATEST_VERSION
defer func() {
Version = ""
}()
test(t)
// when providing an invalid access token, BatchApi should return a facebook error.
_, err := BatchApi("an_invalid_access_token", Params{
"method": GET,
"relative_url": "me",
})
if err == nil {
t.Fatalf("expect an error when providing an invalid access token to BatchApi.")
}
if _, ok := err.(*Error); !ok {
t.Fatalf("batch result error must be an *Error. [e:%v]", err)
}
}
func TestGraphError(t *testing.T) {
res, err := Get("/me", Params{
"access_token": "fake",
})
if err == nil {
t.Fatalf("facebook should return error for bad access token. [res:%v]", res)
}
fbErr, ok := err.(*Error)
if !ok {
t.Fatalf("error must be a *Error. [e:%v]", err)
}
t.Logf("facebook error. [e:%v] [message:%v] [type:%v] [code:%v] [subcode:%v]", err, fbErr.Message, fbErr.Type, fbErr.Code, fbErr.ErrorSubcode)
}
func TestGetAdaccounts(t *testing.T) {
if FB_TEST_VALID_ACCESS_TOKEN == "" {
t.Skipf("skip this case as we don't have a valid access token.")
}
result, e := Get("/v3.1/me/adaccounts", Params{
"fields": "id,name,currency",
"access_token": FB_TEST_VALID_ACCESS_TOKEN,
})
if e != nil {
t.Fatalf("cannot get my adaccounts info. [e:%v]", e)
}
if result.Get("data") == nil {
t.Fatalf("fail to get my adaccounts info. [result:%v]", result)
}
t.Logf("my adaccounts info is: %v", result)
}