forked from globocom/httpclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request_test.go
133 lines (107 loc) · 3.02 KB
/
request_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
package httpclient_test
import (
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/globocom/httpclient"
"github.com/stretchr/testify/assert"
)
var gReq *http.Request
func handleFunc(rw http.ResponseWriter, req *http.Request) {
gReq = req
http.SetCookie(rw, &http.Cookie{Name: "testCookie", Value: "test"})
rw.Header().Add("testHeader", "test")
if _, err := rw.Write([]byte(`OK`)); err != nil {
rw.WriteHeader(http.StatusServiceUnavailable)
}
}
func TestRequest(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(handleFunc))
defer server.Close()
tests := map[string]func(*httpclient.Request) func(*testing.T){
"SetBody": testSetBody,
"SetHeader": testSetHeader,
"SetBasicAuth": testSetBasicAuth,
"Get": testGet,
"Post": testPost,
"Put": testPut,
"Delete": testDelete,
}
client := httpclient.NewHTTPClient(
&httpclient.LoggerAdapter{Writer: io.Discard},
httpclient.WithHostURL(server.URL),
)
for name, test := range tests {
target := client.NewRequest()
t.Run(name, test(target))
}
}
func testSetBody(target *httpclient.Request) func(*testing.T) {
return func(t *testing.T) {
target.SetBody([]byte("test"))
resp, err := target.Post("/")
assert.NoError(t, err)
if assert.NotNil(t, gReq.Method) {
assert.Equal(t, "POST", gReq.Method)
assert.NoError(t, err)
assert.Equal(t, []byte("OK"), resp.Body())
}
}
}
func testSetHeader(target *httpclient.Request) func(*testing.T) {
return func(t *testing.T) {
target.SetHeader("MyHeader", "MyValue")
_, err := target.Get("/")
assert.NoError(t, err)
if assert.NotNil(t, gReq.Method) {
assert.Equal(t, "GET", gReq.Method)
assert.NoError(t, err)
assert.Equal(t, "MyValue", gReq.Header.Get("MyHeader"))
}
}
}
func testSetBasicAuth(target *httpclient.Request) func(*testing.T) {
return func(t *testing.T) {
target.SetBasicAuth("Username", "Password")
_, err := target.Get("/")
assert.NoError(t, err)
if assert.NotNil(t, gReq.Method) {
assert.Equal(t, "GET", gReq.Method)
assert.NoError(t, err)
assert.Equal(t, "Basic VXNlcm5hbWU6UGFzc3dvcmQ=", gReq.Header.Get("Authorization"))
}
}
}
func testGet(target *httpclient.Request) func(*testing.T) {
return func(t *testing.T) {
target.SetBody([]byte("test"))
_, err := target.Get("/")
assert.NoError(t, err)
assert.Equal(t, "GET", gReq.Method)
}
}
func testPost(target *httpclient.Request) func(*testing.T) {
return func(t *testing.T) {
target.SetBody([]byte("test"))
_, err := target.Post("/")
assert.NoError(t, err)
assert.Equal(t, "POST", gReq.Method)
}
}
func testPut(target *httpclient.Request) func(*testing.T) {
return func(t *testing.T) {
target.SetBody([]byte("test"))
_, err := target.Put("/")
assert.NoError(t, err)
assert.Equal(t, "PUT", gReq.Method)
}
}
func testDelete(target *httpclient.Request) func(*testing.T) {
return func(t *testing.T) {
target.SetBody([]byte("test"))
_, err := target.Delete("/")
assert.NoError(t, err)
assert.Equal(t, "DELETE", gReq.Method)
}
}