forked from Eun/go-hit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend_test.go
315 lines (280 loc) · 6.29 KB
/
send_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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package hit_test
import (
"testing"
"io"
"net/http"
"net/http/httptest"
"bytes"
"errors"
. "github.com/Eun/go-hit"
"github.com/stretchr/testify/require"
)
func TestSend(t *testing.T) {
s := EchoServer()
defer s.Close()
t.Run("func", func(t *testing.T) {
t.Run("with correct parameter (using Request)", func(t *testing.T) {
calledFunc := false
Test(t,
Post(s.URL),
Send(func(e Hit) {
calledFunc = true
e.Request().Body().SetString("Hello World")
}),
Expect().Body().Equal(`Hello World`),
)
require.True(t, calledFunc)
})
t.Run("with correct parameter (using Hit)", func(t *testing.T) {
calledFunc := false
Test(t,
Post(s.URL),
Send(func(e Hit) {
calledFunc = true
e.MustDo(Send(`Hello World`))
}),
Expect().Body().Equal(`Hello World`),
)
require.True(t, calledFunc)
})
t.Run("with correct parameter (using Hit) and error", func(t *testing.T) {
calledFunc := false
ExpectError(t,
Do(
Post(s.URL),
Send(func(e Hit) error {
calledFunc = true
return e.Do(Send(`Hello Earth`))
}),
Expect().Body().Equal(`Hello World`),
),
PtrStr("Not equal"), PtrStr(`expected: "Hello World"`), PtrStr(`actual: "Hello Earth"`), nil, nil, nil, nil,
)
require.True(t, calledFunc)
})
t.Run("with correct parameter (using Hit) and error (return an error)", func(t *testing.T) {
calledFunc := false
ExpectError(t,
Do(
Post(s.URL),
Send(func(e Hit) error {
calledFunc = true
return errors.New("whoops")
}),
),
PtrStr("whoops"),
)
require.True(t, calledFunc)
})
t.Run("with invalid parameter", func(t *testing.T) {
calledFunc := false
Test(t,
Post(s.URL),
Send(func() {
calledFunc = true
}),
Expect().Body().Equal(``),
)
require.True(t, calledFunc)
})
})
t.Run("bytes", func(t *testing.T) {
Test(t,
Post(s.URL),
Send([]byte("Hello World")),
Expect().Body().Equal(`Hello World`),
)
})
t.Run("string", func(t *testing.T) {
Test(t,
Post(s.URL),
Send("Hello World"),
Expect().Body().Equal(`Hello World`),
)
})
t.Run("reader", func(t *testing.T) {
Test(t,
Post(s.URL),
Send(bytes.NewBufferString("Hello World")),
Expect().Body().Equal(`Hello World`),
)
})
t.Run("slice", func(t *testing.T) {
Test(t,
Post(s.URL),
Send([]string{"A", "B"}),
Expect().Body().Equal(`["A","B"]`),
)
})
t.Run("int", func(t *testing.T) {
Test(t,
Post(s.URL),
Send(8),
Expect().Body().Equal(`8`),
)
})
}
func TestSend_Custom(t *testing.T) {
s := EchoServer()
defer s.Close()
t.Run("inline", func(t *testing.T) {
calledFunc := false
Test(t,
Post(s.URL),
Send().Custom(func(hit Hit) {
calledFunc = true
hit.Request().Body().SetString("Hello World")
}),
Expect().Body().Equal(`Hello World`),
)
require.True(t, calledFunc)
})
t.Run("MustDo", func(t *testing.T) {
calledFunc := false
Test(t,
Post(s.URL),
Send().Custom(func(hit Hit) {
calledFunc = true
hit.MustDo(Send("Hello World"))
}),
Expect().Body().Equal(`Hello World`),
)
require.True(t, calledFunc)
})
}
func TestSend_Custom_InvalidParameter(t *testing.T) {
s := EchoServer()
defer s.Close()
calledFunc := false
Test(t,
Post(s.URL),
Send(func() {
calledFunc = true
}),
)
require.True(t, calledFunc)
}
func TestSend_JSON(t *testing.T) {
s := EchoServer()
defer s.Close()
t.Run("string", func(t *testing.T) {
Test(t,
Post(s.URL),
Send().JSON("Hello World"),
Expect().Body().Equal(`"Hello World"`),
)
})
t.Run("slice", func(t *testing.T) {
Test(t,
Post(s.URL),
Send().JSON([]string{"A", "B"}),
Expect().Body().Equal(`["A","B"]`),
)
})
t.Run("object", func(t *testing.T) {
Test(t,
Post(s.URL),
Send().JSON(map[string]interface{}{"A": "1", "B": "2"}),
Expect().Body().Equal(`{"A":"1","B":"2"}`),
)
})
t.Run("int", func(t *testing.T) {
Test(t,
Post(s.URL),
Send().JSON(8),
Expect().Body().Equal(`8`),
)
})
t.Run("struct", func(t *testing.T) {
var user = struct {
Name string
ID int
}{
"Joe",
10,
}
Test(t,
Post(s.URL),
Send().JSON(user),
Expect().Body().Equal(`{"Name":"Joe","ID":10}`),
)
})
}
func TestSendHeader(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
_, _ = io.WriteString(writer, request.Header.Get("X-Headers"))
})
s := httptest.NewServer(mux)
defer s.Close()
Test(t,
Post(s.URL),
Send().Header("X-Headers", "World"),
Expect().Body().Equal("World"),
)
}
func TestSendHeader_DoubleSet(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
_, _ = io.WriteString(writer, request.Header.Get("X-Headers"))
})
s := httptest.NewServer(mux)
defer s.Close()
Test(t,
Post(s.URL),
Send().Header("X-Headers", "World"),
Send().Header("X-Headers", "Universe"),
Expect().Body().Equal("Universe"),
)
}
func TestSend_Final(t *testing.T) {
s := EchoServer()
defer s.Close()
t.Run("Send(value).Body()", func(t *testing.T) {
ExpectError(t,
Do(Send("Data").Body()),
PtrStr("only usable with Send() not with Send(value)"),
)
})
t.Run("Send(value).Body().JSON()", func(t *testing.T) {
ExpectError(t,
Do(Send("Data").Body().JSON(nil)),
PtrStr("only usable with Send() not with Send(value)"),
)
})
t.Run("Send(value).Interface()", func(t *testing.T) {
ExpectError(t,
Do(Send("Data").Interface(nil)),
PtrStr("only usable with Send() not with Send(value)"),
)
})
t.Run("Send(value).JSON()", func(t *testing.T) {
ExpectError(t,
Do(Send("Data").JSON(nil)),
PtrStr("only usable with Send() not with Send(value)"),
)
})
t.Run("Send(value).Header()", func(t *testing.T) {
ExpectError(t,
Do(Send("Data").Header("", "")),
PtrStr("only usable with Send() not with Send(value)"),
)
})
t.Run("Send(value).Custom()", func(t *testing.T) {
ExpectError(t,
Do(Send("Data").Custom(nil)),
PtrStr("only usable with Send() not with Send(value)"),
)
})
}
func TestSend_WithoutArgument(t *testing.T) {
s := EchoServer()
defer s.Close()
ExpectError(t,
Do(
Post(s.URL),
Send(),
),
PtrStr("unable to run Send() without an argument or without a chain. Please use Send(something) or Send().Something"),
)
}