-
Notifications
You must be signed in to change notification settings - Fork 14
/
fluent_test.go
260 lines (221 loc) · 4.93 KB
/
fluent_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
package fluent
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
func readAllString(r io.Reader) (string, error) {
var buf bytes.Buffer
if _, err := buf.ReadFrom(r); err != nil {
return "", err
}
return buf.String(), nil
}
var copyHandlerFunc = http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
io.Copy(w, r.Body)
},
)
func TestGet(t *testing.T) {
ts := httptest.NewServer(copyHandlerFunc)
defer ts.Close()
res, err := New().Get(ts.URL).Send()
if err != nil {
t.Fatal(err)
}
if method := res.Request.Method; method != "GET" {
t.Fatal("Method sent is not GET")
}
}
func TestPost(t *testing.T) {
ts := httptest.NewServer(copyHandlerFunc)
defer ts.Close()
res, err := New().Post(ts.URL).Send()
if err != nil {
t.Fatal(err)
}
if res.Request.Method != "POST" {
t.Fatal("Method sent is not POST")
}
}
func TestPut(t *testing.T) {
ts := httptest.NewServer(copyHandlerFunc)
defer ts.Close()
res, err := New().Put(ts.URL).Send()
if err != nil {
t.Fatal(err)
}
if res.Request.Method != "PUT" {
t.Fatal("Method sent is not PUT")
}
}
func TestPatch(t *testing.T) {
ts := httptest.NewServer(copyHandlerFunc)
defer ts.Close()
res, err := New().Patch(ts.URL).Send()
if err != nil {
t.Fatal(err)
}
if res.Request.Method != "PATCH" {
t.Fatal("Method sent is not PATCH")
}
}
func TestDelete(t *testing.T) {
ts := httptest.NewServer(copyHandlerFunc)
defer ts.Close()
res, err := New().Delete(ts.URL).Send()
if err != nil {
t.Fatal(err)
}
if res.Request.Method != "DELETE" {
t.Fatal("Method sent is not DELETE")
}
}
func TestBody(t *testing.T) {
ts := httptest.NewServer(copyHandlerFunc)
defer ts.Close()
msg := "Hello world!"
res, err := New().
Post(ts.URL).
Body(strings.NewReader(msg)).
Send()
if err != nil {
t.Fatal(err)
}
defer res.Body.Close()
body, err := readAllString(res.Body)
if err != nil {
t.Fatal(err)
}
if body != msg {
t.Fatalf("Body sent %s doesn't match %s", msg, body)
}
}
func TestJson(t *testing.T) {
ts := httptest.NewServer(copyHandlerFunc)
defer ts.Close()
arr := []int{1, 2, 3}
res, err := New().Post(ts.URL).Json(arr).Send()
if err != nil {
t.Fatal(err)
}
defer res.Body.Close()
body, err := readAllString(res.Body)
if err != nil {
t.Fatal(err)
}
if body != "[1,2,3]" {
t.Fatalf("JSON sent doesn't match %s", body)
}
}
func TestRetries(t *testing.T) {
retry := 3
ts := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(500)
}),
)
defer ts.Close()
req := New()
req.Post(ts.URL).
InitialInterval(time.Millisecond).
Json([]int{1, 3, 4}).
Retry(retry)
if req.retry != retry {
t.Fatalf("Retries didn't apply!")
}
_, err := req.Send()
if err != nil {
fmt.Println("err", err)
}
if req.retry != 0 {
t.Fatalf("Fluent exited without finishing retries")
}
}
func TestTimeout(t *testing.T) {
ts := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(500)
}),
)
defer ts.Close()
req := New()
req.Get(ts.URL).Timeout(time.Duration(time.Second)).Send()
if req.timeout == 0 {
t.Fatal("timeout should be more than zero")
}
c := req.newClient()
if c.Timeout == 0 {
t.Fatal("Client timeout should be more than zero")
}
}
func TestRandomizationFactor(t *testing.T) {
req := New()
req.RandomizationFactor(0.6)
// 0.5 is the default that's why i'm testing against it
if req.backoff.RandomizationFactor != 0.6 {
t.Fatal("RandomizationFactor should be 0.6")
}
}
func TestMultiplier(t *testing.T) {
req := New()
req.Multiplier(2.0)
if req.backoff.Multiplier != 2.0 {
t.Fatal("Multiplier should be 2.0")
}
}
func TestMaxInterval(t *testing.T) {
interval := time.Duration(20 * time.Second)
req := New()
req.MaxInterval(interval)
if req.backoff.MaxInterval != interval {
t.Fatalf("MaxInterval should be %s", interval)
}
}
func TestMaxElapsedTime(t *testing.T) {
elapsed := time.Duration(20 * time.Second)
req := New()
req.MaxElapsedTime(elapsed)
if req.backoff.MaxElapsedTime != elapsed {
t.Fatalf("MaxElapsedTime should be %s", elapsed)
}
}
func TestProxy(t *testing.T) {
proxy := "http://localhost:8080"
req := New().Proxy(proxy)
if req.proxy != proxy {
t.Fatal("Proxy should be", proxy)
}
}
func TestProxiedRequest(t *testing.T) {
url := "http://github.com/"
ts := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(r.RequestURI))
}),
)
rsp, err := New().Proxy(ts.URL).Get(url).Send()
if err != nil {
t.Error(err)
}
body, err := ioutil.ReadAll(rsp.Body)
if err != nil {
t.Error(err)
}
rsp.Body.Close()
if string(body) != url {
t.Fatalf("Response from proxy server should be %s, got %s", url, body)
}
}
func TestInvalidProxyURL(t *testing.T) {
if _, err := New().Proxy("%gh&%ij").Get("/").Send(); err == nil {
t.Fatal("Expecting error due to invalid proxy URL")
}
}