-
Notifications
You must be signed in to change notification settings - Fork 11
/
scryfall_test.go
288 lines (259 loc) · 7.52 KB
/
scryfall_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
package scryfall
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"time"
)
func stringPointer(v string) *string {
return &v
}
func intPointer(v int) *int {
return &v
}
func setupTestServer(pattern string, handler func(http.ResponseWriter, *http.Request), clientOptions ...ClientOption) (*Client, *httptest.Server, error) {
mux := http.NewServeMux()
mux.HandleFunc(pattern, handler)
ts := httptest.NewServer(mux)
mergedClientOptions := []ClientOption{WithBaseURL(ts.URL), WithLimiter(nil)}
mergedClientOptions = append(mergedClientOptions, clientOptions...)
client, err := NewClient(mergedClientOptions...)
if err != nil {
ts.Close()
return nil, nil, err
}
return client, ts, nil
}
func TestDateUnmarshalJSON(t *testing.T) {
tests := []struct {
in []byte
out Date
}{
{
[]byte("null"),
Date{Time: time.Time{}},
},
{
[]byte("2018-04-27"),
Date{Time: time.Date(2018, 4, 27, 0, 0, 0, 0, time.FixedZone("UTC-8", -8*60*60))},
},
}
for _, test := range tests {
t.Run(string(test.in), func(t *testing.T) {
date := Date{}
err := date.UnmarshalJSON(test.in)
if err != nil {
t.Fatalf("Unexpected error while unmarshaling JSON date representation: %v", err)
}
if !date.Time.Equal(test.out.Time) {
t.Errorf("got: %s want: %s", date, test.out)
}
})
}
}
func TestDateMarshalJSON(t *testing.T) {
tests := []struct {
in Date
out []byte
}{
{
Date{Time: time.Date(2018, 4, 27, 0, 0, 0, 0, time.FixedZone("UTC-8", -8*60*60))},
[]byte("\"2018-04-27\""),
},
}
for _, test := range tests {
t.Run(string(test.out), func(t *testing.T) {
got, err := test.in.MarshalJSON()
if err != nil {
t.Fatalf("Unexpected error while marshaling date: %v", err)
}
if string(got) != string(test.out) {
t.Errorf("got: %s want: %s", got, test.out)
}
})
}
}
func TestDateUnmashalMarshaledJSON(t *testing.T) {
tests := []struct {
in Date
}{
{
Date{Time: time.Date(2018, 4, 27, 0, 0, 0, 0, time.FixedZone("UTC-8", -8*60*60))},
},
}
for _, test := range tests {
t.Run(string(test.in.Time.Format(dateFormat)), func(t *testing.T) {
marshaled, err := test.in.MarshalJSON()
if err != nil {
t.Fatalf("Unexpected error while marshaling date: %v", err)
}
var out Date
err = out.UnmarshalJSON(marshaled)
if err != nil {
t.Fatalf("Unexpected error while unmarshaling JSON date: %v", err)
}
if !out.Time.Equal(test.in.Time) {
t.Errorf("got: %s want: %s", out, test.in)
}
})
}
}
func TestTimestampUnmarshalJSON(t *testing.T) {
tests := []struct {
in []byte
out Timestamp
}{
{
[]byte("null"),
Timestamp{Time: time.Time{}},
},
{
[]byte("2018-12-01T14:31:43-05:00"),
Timestamp{Time: time.Date(2018, 12, 1, 14, 31, 43, 0, time.FixedZone("UTC-5", -5*60*60))},
},
{
[]byte("2018-12-31T09:05:07.949+00:00"),
Timestamp{Time: time.Date(2018, 12, 31, 9, 5, 7, 949000000, time.UTC)},
},
}
for _, test := range tests {
t.Run(string(test.in), func(t *testing.T) {
timestamp := Timestamp{}
err := timestamp.UnmarshalJSON(test.in)
if err != nil {
t.Fatalf("Unexpected error while unmarshaling timestamp: %v", err)
}
if !timestamp.Time.Equal(test.out.Time) {
t.Errorf("got: %s want: %s", timestamp, test.out)
}
})
}
}
func TestErrorError(t *testing.T) {
want := "not_found: The requested object or REST method was not found."
err := Error{
Status: 404,
Code: "not_found",
Details: "The requested object or REST method was not found.",
Type: nil,
Warnings: []string{},
}
if err.Error() != want {
t.Errorf("got: %s want: %s", err.Error(), want)
}
}
func TestError(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintln(w, `{"object": "error", "code": "not_found", "status": 404, "details": "The requested object or REST method was not found."}`)
})
client, ts, err := setupTestServer("/cards/nope", handler)
if err != nil {
t.Fatalf("Error setting up test server: %v", err)
}
defer ts.Close()
ctx := context.Background()
_, err = client.GetCard(ctx, "nope")
expectedErr := &Error{
Code: "not_found",
Status: 404,
Details: "The requested object or REST method was not found.",
}
if !reflect.DeepEqual(err, expectedErr) {
t.Errorf("got: %#v want: %#v", err, expectedErr)
}
}
func TestNewClientUserAgent(t *testing.T) {
tests := []struct {
name string
clientOptions []ClientOption
expectedUserAgent string
}{
{
name: "default user agent",
clientOptions: nil,
expectedUserAgent: defaultUserAgent,
},
{
name: "custom user agent",
clientOptions: []ClientOption{WithUserAgent("custom/1.2.3")},
expectedUserAgent: "custom/1.2.3",
},
}
for _, test := range tests {
t.Run(string(test.name), func(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
userAgent := r.Header.Get("User-Agent")
if userAgent != test.expectedUserAgent {
// I don't beleive they currently return an error for user agent
// issues but we want to make the test fail.
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintln(w, `{"object": "error", "code": "bad_request", "status": 400, "details": ""}`)
return
}
fmt.Fprintln(w, `{"object": "list", "has_more": false, "data": []}`)
})
client, ts, err := setupTestServer("/symbology", handler, test.clientOptions...)
if err != nil {
t.Fatalf("Error setting up test server: %v", err)
}
defer ts.Close()
ctx := context.Background()
_, err = client.ListCardSymbols(ctx)
if err != nil {
t.Fatalf("Error validating user agent: %v", err)
}
})
}
}
func TestNewClientWithClientSecret(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
authorizationHeader := r.Header.Get("Authorization")
if authorizationHeader != "Bearer cs-12345" {
w.WriteHeader(http.StatusForbidden)
fmt.Fprintln(w, `{"object": "error", "code": "forbidden", "status": 403, "details": ""}`)
return
}
fmt.Fprintln(w, `{"object": "list", "has_more": false, "data": []}`)
})
client, ts, err := setupTestServer("/symbology", handler, WithClientSecret("cs-12345"))
if err != nil {
t.Fatalf("Error setting up test server: %v", err)
}
defer ts.Close()
ctx := context.Background()
_, err = client.ListCardSymbols(ctx)
if err != nil {
t.Fatalf("Error listing card symbols using client with client secret set: %v", err)
}
}
func TestNewClientWithGrantSecret(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
authorizationHeader := r.Header.Get("Authorization")
if authorizationHeader != "Bearer 12345" {
w.WriteHeader(http.StatusForbidden)
fmt.Fprintln(w, `{"object": "error", "code": "forbidden", "status": 403, "details": ""}`)
return
}
fmt.Fprintln(w, `{"object": "list", "has_more": false, "data": []}`)
})
client, ts, err := setupTestServer("/symbology", handler, WithGrantSecret("12345"))
if err != nil {
t.Fatalf("Error setting up test server: %v", err)
}
defer ts.Close()
ctx := context.Background()
_, err = client.ListCardSymbols(ctx)
if err != nil {
t.Fatalf("Error listing card symbols using client with grant secret set: %v", err)
}
}
func TestNewClientMultipleSecrets(t *testing.T) {
_, err := NewClient(WithClientSecret("cs-12345"), WithGrantSecret("12345"))
if err != ErrMultipleSecrets {
t.Fatalf("Unexpected error %v received from NewClient when configured with multiple secrets", err)
}
}