forked from ListenNotes/podcast-api-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecute_test.go
More file actions
216 lines (187 loc) · 4.8 KB
/
Copy pathexecute_test.go
File metadata and controls
216 lines (187 loc) · 4.8 KB
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
package listennotes
import (
"errors"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
)
func TestStandardClientExecuteNewReqFailure(t *testing.T) {
client := &standardHTTPClient{
baseURL: "http://localhost:bogus",
}
_, err := client.get("path", map[string]string{})
if err == nil || !strings.Contains(err.Error(), "invalid port ") {
t.Errorf("Expected url parse failure but got: %v", err)
}
}
func TestMappedErrors(t *testing.T) {
type toTest struct {
code int
err error
}
// expected code to error mappings -- dups the errMap however this means that the test is actually validating the map
errs := []toTest{
{code: 200, err: nil},
{code: 400, err: ErrBadRequest},
{code: 401, err: ErrUnauthorized},
{code: 404, err: ErrNotFound},
{code: 429, err: ErrTooManyRequests},
{code: 500, err: ErrInternalServerError},
}
for k, v := range errMap {
errs = append(errs, toTest{code: k, err: v})
}
var expectedCode int
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(expectedCode)
w.Write([]byte("{}"))
}))
defer ts.Close()
client := &standardHTTPClient{
httpClient: http.DefaultClient,
baseURL: ts.URL,
}
for _, e := range errs {
expectedCode = e.code
_, err := client.get("path", map[string]string{})
if (e.err == nil && err != nil) || (e.err != nil && !errors.Is(err, e.err)) {
t.Errorf("%d reponse code did not result in correct error: %s", e.code, err)
}
}
}
func TestDecodeError(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("not-json"))
}))
defer ts.Close()
client := &standardHTTPClient{
httpClient: http.DefaultClient,
baseURL: ts.URL,
}
_, err := client.get("path", map[string]string{})
if err == nil || !strings.Contains(err.Error(), "failed parsing the response") {
t.Errorf("Expected json parse failure but got: %v", err)
}
}
func TestGetQueryArguments(t *testing.T) {
called := false
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
called = true
if r.URL.RawQuery != "a=b&c=d" {
t.Errorf("Query parameters were not as expected: %s", r.URL.RawQuery)
}
w.Write([]byte(`{}`))
}))
defer ts.Close()
client := &standardHTTPClient{
httpClient: http.DefaultClient,
baseURL: ts.URL,
}
client.get("path", map[string]string{
"a": "b",
"c": "d",
})
if !called {
t.Errorf("Did not call expected httptest url")
}
}
func TestParsedResponse(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(searchPayload))
}))
defer ts.Close()
client := &standardHTTPClient{
httpClient: http.DefaultClient,
baseURL: ts.URL,
}
resp, err := client.get("path", map[string]string{
"a": "b",
"c": "d",
})
if err != nil {
t.Errorf("Expected no error but got: %v", err)
}
if resp == nil {
t.Fatalf("Expected resp but go nil")
return
}
if v := resp.Data["took"]; v != float64(0.693) {
t.Errorf("Wrong took value: %v", v)
}
resultZero := (resp.Data["results"].([]interface{}))[0].(map[string]interface{})
if v := resultZero["id"]; v != "ea09b575d07341599d8d5b71f205517b" {
t.Errorf("Wrong results[0].id value: %v", v)
}
}
const searchPayload = `{
"took": 0.693,
"count": 10,
"total": 9499,
"results": [
{
"id": "ea09b575d07341599d8d5b71f205517b"
}
],
"next_offset": 10
}`
func TestPost(t *testing.T) {
called := false
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
called = true
if err := r.ParseForm(); err != nil {
t.Errorf("Test form failed to parse: %s", err)
}
if formValue := r.Form.Get("k"); formValue != "v" {
t.Errorf("Form did not have proper k value: %s", formValue)
}
w.Write([]byte(`{}`))
}))
defer ts.Close()
client := &standardHTTPClient{
httpClient: http.DefaultClient,
baseURL: ts.URL,
}
client.post("path", map[string]string{}, url.Values{
"k": []string{"v"},
})
if !called {
t.Errorf("Did not call expected httptest url")
}
}
func TestResponseJSON(t *testing.T) {
resp := Response{
Data: map[string]interface{}{
"a": "b",
"c": []string{"1", "2"},
},
}
j := resp.ToJSON()
if j != `{"a":"b","c":["1","2"]}` {
t.Errorf("ToJSON had unexpected result: '%s'", j)
}
}
func TestNilResponseJSON(t *testing.T) {
var resp *Response
j := resp.ToJSON()
if j != "" {
t.Errorf("ToJSON had unexpected respons: '%s'", j)
}
}
func TestResponseJSONParseFailure(t *testing.T) {
resp := Response{
Data: map[string]interface{}{
"a": testNoParse{},
},
}
j := resp.ToJSON()
if j != "" {
t.Errorf("ToJSON error should have returned blank string")
}
}
type testNoParse struct{}
func (testNoParse) MarshalJSON() ([]byte, error) {
return nil, fmt.Errorf("no-json-marshal")
}