-
Notifications
You must be signed in to change notification settings - Fork 17
/
endpoints_test.go
73 lines (63 loc) · 2.31 KB
/
endpoints_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
package igdb
import (
"net/http"
"testing"
"github.com/pkg/errors"
)
func TestClient_GetFields(t *testing.T) {
var tests = []struct {
name string
status int
resp string
wantFields []string
wantErr error
}{
{"OK status with regular response", http.StatusOK, `["name", "slug", "url"]`, []string{"url", "slug", "name"}, nil},
{"OK status with empty response", http.StatusOK, "", nil, errInvalidJSON},
{"OK status with dot response", http.StatusOK, `["mugshot.width","name", "company.id"]`, []string{"company.id", "name", "mugshot.width"}, nil},
{"OK status with asterisk response", http.StatusOK, `["*"]`, []string{"*"}, nil},
{"Bad status with empty response", http.StatusBadRequest, "", nil, ErrBadRequest},
{"Not found status with error response", http.StatusNotFound, testErrNotFound, nil, ServerError{Status: 404, Msg: "status not found"}},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ts, c := testServerString(test.status, test.resp)
defer ts.Close()
f, err := c.getFields(testEndpoint)
if errors.Cause(err) != test.wantErr {
t.Errorf("got: <%v>, want: <%v>", errors.Cause(err), test.wantErr)
}
if !equalSlice(f, test.wantFields) {
t.Fatalf("Expected fields '%v', got '%v'", test.wantFields, f)
}
})
}
}
func TestClient_GetCount(t *testing.T) {
var tests = []struct {
name string
status int
resp string
wantCount int
wantErr error
}{
{"OK status with regular response", http.StatusOK, `{"count": 1234}`, 1234, nil},
{"OK status with count of zero response", http.StatusOK, `{"count": 0}`, 0, nil},
{"OK status with empty response", http.StatusOK, "", 0, errInvalidJSON},
{"Bad status with empty response", http.StatusBadRequest, "", 0, ErrBadRequest},
{"Not found status with error response", http.StatusNotFound, testErrNotFound, 0, ServerError{Status: 404, Msg: "status not found"}},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ts, c := testServerString(test.status, test.resp)
defer ts.Close()
count, err := c.getCount(testEndpoint)
if errors.Cause(err) != test.wantErr {
t.Errorf("got: <%v>, want: <%v>", errors.Cause(err), test.wantErr)
}
if count != test.wantCount {
t.Fatalf("Expected count %d, got %d", test.wantCount, count)
}
})
}
}