-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient_test.go
251 lines (229 loc) · 5.63 KB
/
client_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
package gwc_test
import (
"context"
"net/http"
"testing"
"reflect"
"github.com/delicb/cliware"
"github.com/delicb/gwc"
)
type mockTransport struct {
called bool
responseCode int
}
func (t *mockTransport) RoundTrip(req *http.Request) (*http.Response, error) {
t.called = true
return &http.Response{
StatusCode: 200,
Request: req,
}, nil
}
func newMockTransport(responseCode int) *mockTransport {
return &mockTransport{responseCode: responseCode}
}
type mockMiddleware struct {
called bool
count int
}
func (m *mockMiddleware) Exec(next cliware.Handler) cliware.Handler {
return cliware.HandlerFunc(func(req *http.Request) (*http.Response, error) {
m.called = true
m.count += 1
return next.Handle(req)
})
}
func dummyClient() *http.Client {
return &http.Client{
Transport: newMockTransport(200),
}
}
func TestNew(t *testing.T) {
mockTransport := newMockTransport(200)
httpClient := &http.Client{
Transport: mockTransport,
}
mockMiddleware := &mockMiddleware{}
client := gwc.New(httpClient, mockMiddleware)
client.Get().Send()
if !mockTransport.called {
t.Error("Request not send - RoundTripper not called.")
}
if !mockMiddleware.called {
t.Error("Middleware not called.")
}
}
func TestUse(t *testing.T) {
client := gwc.New(dummyClient())
mockMiddleware := &mockMiddleware{}
client.Use(mockMiddleware)
client.Get().Send()
if !mockMiddleware.called {
t.Error("Middleware set to client not called.")
}
}
func TestUseFunc(t *testing.T) {
client := gwc.New(dummyClient())
var called bool
client.UseFunc(func(next cliware.Handler) cliware.Handler {
return cliware.HandlerFunc(func(req *http.Request) (*http.Response, error) {
called = true
return next.Handle(req)
})
})
client.Get().Send()
if !called {
t.Error("Middleware that was added as function not called.")
}
}
func TestUsePost(t *testing.T) {
client := gwc.New(dummyClient())
mockMiddleware := &mockMiddleware{}
client.UsePost(mockMiddleware)
client.Get().Send()
if !mockMiddleware.called {
t.Error("Middleware added by UsePost not called.")
}
}
func TestUsePostFunc(t *testing.T) {
client := gwc.New(dummyClient())
var called bool
client.UsePostFunc(func(next cliware.Handler) cliware.Handler {
return cliware.HandlerFunc(func(req *http.Request) (*http.Response, error) {
called = true
return next.Handle(req)
})
})
client.Get().Send()
if !called {
t.Error("Middleware added by UsePostFunc not called.")
}
}
func TestMiddlewareOrder(t *testing.T) {
client := gwc.New(dummyClient())
order := []string{}
client.UseFunc(func(next cliware.Handler) cliware.Handler {
return cliware.HandlerFunc(func(req *http.Request) (*http.Response, error) {
order = append(order, "pre")
return next.Handle(req)
})
})
client.UsePostFunc(func(next cliware.Handler) cliware.Handler {
return cliware.HandlerFunc(func(req *http.Request) (*http.Response, error) {
order = append(order, "post")
return next.Handle(req)
})
})
client.Get().Send()
if !reflect.DeepEqual(order, []string{"pre", "post"}) {
t.Errorf("Wrong order of calling middlewares. Got: %v", order)
}
}
func TestRequest(t *testing.T) {
client := gwc.New(dummyClient())
req := client.Request()
// just check if we got non-nil request, most of the stuff are either private
// or tested with other tests
if req == nil {
t.Error("Got nil request.")
}
}
func TestClientMethods(t *testing.T) {
for _, data := range []struct {
Function func(*gwc.Client) *gwc.Request
Method string
}{
{
(*gwc.Client).Get,
"GET",
},
{
(*gwc.Client).Post,
"POST",
},
{
(*gwc.Client).Put,
"PUT",
},
{
(*gwc.Client).Delete,
"DELETE",
},
{
(*gwc.Client).Patch,
"PATCH",
},
{
(*gwc.Client).Head,
"HEAD",
},
{
(*gwc.Client).Options,
"OPTIONS",
},
} {
c := gwc.New(dummyClient())
req := data.Function(c)
resp, err := req.Send()
if err != nil {
t.Error("Got unexpected error: ", err)
}
if resp.Request.Method != data.Method {
t.Errorf("Wrong request method. Got: %s, expected: %s", resp.Request.Method, data.Method)
}
}
}
func TestDo(t *testing.T) {
client := gwc.New(dummyClient())
mockMiddleware := &mockMiddleware{}
_, err := client.Do(mockMiddleware)
if err != nil {
t.Error("Got unexpected error: ", err)
}
if !mockMiddleware.called {
t.Error("Middleware passed to DoCtx not called.")
}
}
func TestDoCtx(t *testing.T) {
client := gwc.New(dummyClient())
mockMiddleware := &mockMiddleware{}
ctx := context.WithValue(context.Background(), "key", "value")
resp, err := client.DoCtx(ctx, mockMiddleware)
if err != nil {
t.Error("Got unexpected error: ", err)
}
contextValue := resp.Request.Context().Value("key")
if contextValue != "value" {
t.Errorf("Wrong context value. Got: %s, expected: value", contextValue)
}
if !mockMiddleware.called {
t.Error("Middleware passed to DoCtx not called.")
}
}
func TestClientOnContext(t *testing.T) {
httpClient := dummyClient()
client := gwc.New(httpClient)
resp, err := client.Get().Send()
if err != nil {
t.Error("Got unexpected error: ", err)
}
cl := gwc.ClientFromContext(resp.Request.Context())
if cl != httpClient {
t.Error("Got wrong instance of http.Client.")
}
}
func TestClient_AfterCalledOnce(t *testing.T) {
client := gwc.New(dummyClient())
countingMockMiddleware := &mockMiddleware{}
client.UsePost(countingMockMiddleware)
workingMockMiddleware := &mockMiddleware{}
_, err := client.Do(workingMockMiddleware)
if err != nil {
t.Error("Got unexpected error:", err)
}
if !workingMockMiddleware.called {
t.Error("Working middleware not called.")
}
if countingMockMiddleware.count != 1 {
t.Error("Middleware not called only once.")
}
}