forked from hanzoai/gochimp3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_test.go
172 lines (145 loc) Β· 3.74 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
package gochimp3
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
var responder = func(w http.ResponseWriter, r *http.Request) {}
var testServer = "http://localhost:9999"
var delegate func(http.ResponseWriter, *http.Request)
func fatalIf(t *testing.T, err error) {
if err != nil {
t.Fatalf("Shouldn't have gotten an error %s", err)
}
}
func TestMain(m *testing.M) {
http.HandleFunc("/somewhere", func(w http.ResponseWriter, r *http.Request) {
delegate(w, r)
})
go http.ListenAndServe(":9999", nil)
os.Exit(m.Run())
}
func testAPI() *API {
api := New("apikey")
api.endpoint = testServer
api.Debug = true
return api
}
func TestGoodGet(t *testing.T) {
expected := map[string]interface{}{
"one": "thing",
"two": "thing",
}
delegate = func(w http.ResponseWriter, r *http.Request) {
// check the headers
assert.Equal(t, "application/json", r.Header.Get("Content-Type"))
// check auth
_, pass, ok := r.BasicAuth()
assert.Equal(t, "apikey", pass)
assert.True(t, ok)
// check the query params
assert.Empty(t, r.URL.Query())
// check we sent an empty body
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
fatalIf(t, err)
assert.Equal(t, 0, len(body))
// return something
data, _ := json.Marshal(expected)
fmt.Fprintf(w, string(data))
}
api := testAPI()
actual := make(map[string]interface{})
err := api.Request("GET", "/somewhere", nil, nil, &actual)
fatalIf(t, err)
assert.EqualValues(t, expected, actual)
}
func TestGetWithParams(t *testing.T) {
expected := map[string]interface{}{
"one": "thing",
"two": "thing",
}
params := BasicQueryParams{
Fields: []string{"marp", "parm"},
ExcludeFields: []string{"red", "fish"},
}
delegate = func(w http.ResponseWriter, r *http.Request) {
// check the query params
for k, v := range r.URL.Query() {
switch {
case k == "fields":
assert.EqualValues(t, []string{strings.Join(params.Fields, ",")}, v)
case k == "exclude_fields":
assert.EqualValues(t, []string{strings.Join(params.ExcludeFields, ",")}, v)
default:
t.Fail()
}
}
data, _ := json.Marshal(expected)
fmt.Fprintf(w, string(data))
}
api := testAPI()
actual := make(map[string]interface{})
err := api.Request("GET", "/somewhere", ¶ms, nil, &actual)
fatalIf(t, err)
assert.EqualValues(t, expected, actual)
}
func TestGetEmptyResponse(t *testing.T) {
delegate = func(w http.ResponseWriter, r *http.Request) {}
api := testAPI()
err := api.Request("GET", "/somewhere", nil, nil, nil)
fatalIf(t, err)
result, err := api.RequestOk("GET", "/somewhere")
fatalIf(t, err)
assert.True(t, result)
}
func TestGetWithBody(t *testing.T) {
s := struct {
A string
B string
}{"string1", "string2"}
delegate = func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "POST", r.Method)
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
fatalIf(t, err)
parsed := struct {
A string
B string
}{}
err = json.Unmarshal(body, &parsed)
fatalIf(t, err)
assert.EqualValues(t, s, parsed)
}
api := testAPI()
err := api.Request("POST", "/somewhere", nil, &s, nil)
fatalIf(t, err)
}
func TestGetWithNon200Response(t *testing.T) {
delegate = func(w http.ResponseWriter, r *http.Request) {
data, err := json.Marshal(&APIError{
Type: "some type",
Title: "a title",
Status: 500,
Detail: "you done screwed up",
Instance: "123123123",
})
fatalIf(t, err)
http.Error(w, string(data), 500)
}
api := testAPI()
ok, err := api.RequestOk("GET", "/somewhere")
assert.False(t, ok)
assert.NotNil(t, err)
}
func TestMissingEndpoint(t *testing.T) {
api := testAPI()
ok, err := api.RequestOk("GET", "/nowhere")
assert.False(t, ok)
assert.NotNil(t, err)
}