-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_client_test.go
118 lines (96 loc) · 3.43 KB
/
http_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
package boomerang
import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
var (
defaultClientConfig = &ClientConfig{
Timeout: 10 * time.Millisecond,
Transport: DefaultTransport(),
MaxRetries: 5,
}
)
func TestHttpClient_Get(t *testing.T) {
client := NewHttpClient(defaultClientConfig)
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method)
w.WriteHeader(http.StatusOK)
}))
defer testServer.Close()
resp, err := client.Get(testServer.URL)
require.NoError(t, err, "Http client Get call failed")
assert.Equal(t, http.StatusOK, resp.StatusCode)
resp.Body.Close()
}
func TestHttpClient_Post(t *testing.T) {
cnf := &ClientConfig{
Timeout: 10 * time.Millisecond,
Transport: DefaultTransport(),
Backoff: NewExponentialBackoff(2*time.Millisecond, 10*time.Millisecond, 2.0),
MaxRetries: 2,
}
ctType := "application/json"
client := NewHttpClient(cnf)
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPost, r.Method)
assert.Equal(t, r.Header.Get("Content-Type"), ctType)
body, err := ioutil.ReadAll(r.Body)
assert.NoError(t, err, "error reading body of post request")
expected := `{"foo":"bar"}`
assert.Equal(t, string(body), expected)
w.WriteHeader(http.StatusOK)
}))
defer testServer.Close()
resp, err := client.Post(testServer.URL, ctType, strings.NewReader(`{"foo":"bar"}`))
require.NoError(t, err, "Http client Post call failed")
assert.Equal(t, http.StatusOK, resp.StatusCode)
resp.Body.Close()
}
func TestHttpClient_Do(t *testing.T) {
ctType := "application/json"
client := NewHttpClient(defaultClientConfig)
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPut, r.Method)
assert.Equal(t, r.Header.Get("Content-Type"), ctType)
body, err := ioutil.ReadAll(r.Body)
require.NoError(t, err, "error reading body of post request")
expected := `{"foo":"bar"}`
assert.Equal(t, string(body), expected)
w.WriteHeader(http.StatusOK)
}))
defer testServer.Close()
req, rErr := NewRequest("PUT", testServer.URL, strings.NewReader(`{"foo":"bar"}`))
req.Header.Set("Content-Type", ctType)
require.NoError(t, rErr, "error making a new request")
resp, err := client.Do(req)
require.NoError(t, err, "Http client PUT call failed")
assert.Equal(t, http.StatusOK, resp.StatusCode)
resp.Body.Close()
}
func TestHttpClient_Do_Fail(t *testing.T) {
ctType := "application/json"
client := NewHttpClient(defaultClientConfig)
client.QuietMode()
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPut, r.Method)
assert.Equal(t, r.Header.Get("Content-Type"), ctType)
body, err := ioutil.ReadAll(r.Body)
require.NoError(t, err, "error reading body of post request")
expected := `{"foo":"bar"}`
assert.Equal(t, string(body), expected)
w.WriteHeader(http.StatusBadGateway)
}))
defer testServer.Close()
req, rErr := NewRequest("PUT", testServer.URL, strings.NewReader(`{"foo":"bar"}`))
req.Header.Set("Content-Type", ctType)
require.NoError(t, rErr, "error making a new request")
_, err := client.Do(req)
require.Error(t, err, "Http client PUT call failed")
assert.Contains(t, err.Error(), "giving up")
}