-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest_wrapper_test.go
163 lines (158 loc) · 3.65 KB
/
request_wrapper_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
package req
import (
"bytes"
"context"
"github.com/imroc/req/v3/internal/header"
"github.com/imroc/req/v3/internal/tests"
"net/http"
"testing"
"time"
)
func init() {
SetLogger(nil) // disable log
}
func TestGlobalWrapperForRequestSettings(t *testing.T) {
tests.AssertAllNotNil(t,
SetFiles(map[string]string{"test": "req.go"}),
SetFile("test", "req.go"),
SetFileReader("test", "test.txt", bytes.NewBufferString("test")),
SetFileBytes("test", "test.txt", []byte("test")),
SetFileUpload(FileUpload{}),
SetError(&ErrorMessage{}),
SetResult(&UserInfo{}),
SetOutput(new(bytes.Buffer)),
SetHeader("test", "test"),
SetHeaders(map[string]string{"test": "test"}),
SetCookies(&http.Cookie{
Name: "test",
Value: "test",
}),
SetBasicAuth("imroc", "123456"),
SetBearerAuthToken("123456"),
SetQueryString("test=test"),
SetQueryString("ksjlfjk?"),
SetQueryParam("test", "test"),
AddQueryParam("test", "test"),
SetQueryParams(map[string]string{"test": "test"}),
SetPathParam("test", "test"),
SetPathParams(map[string]string{"test": "test"}),
SetFormData(map[string]string{"test": "test"}),
SetURL(""),
SetFormDataFromValues(nil),
SetContentType(header.JsonContentType),
AddRetryCondition(func(rep *Response, err error) bool {
return err != nil
}),
SetRetryCondition(func(rep *Response, err error) bool {
return err != nil
}),
AddRetryHook(func(resp *Response, err error) {}),
SetRetryHook(func(resp *Response, err error) {}),
SetRetryBackoffInterval(0, 0),
SetRetryFixedInterval(0),
SetRetryInterval(func(resp *Response, attempt int) time.Duration {
return 1 * time.Millisecond
}),
SetRetryCount(3),
SetBodyXmlMarshal(0),
SetBodyString("test"),
SetBodyBytes([]byte("test")),
SetBodyJsonBytes([]byte(`{"user":"roc"}`)),
SetBodyJsonString(`{"user":"roc"}`),
SetBodyXmlBytes([]byte("test")),
SetBodyXmlString("test"),
SetBody("test"),
SetBodyJsonMarshal(User{
Name: "roc",
}),
EnableTrace(),
DisableTrace(),
SetContext(context.Background()),
SetUploadCallback(nil),
SetUploadCallbackWithInterval(nil, 0),
SetDownloadCallback(nil),
SetDownloadCallbackWithInterval(nil, 0),
)
}
func testGlobalWrapperMustSendMethods(t *testing.T) {
testCases := []struct {
SendReq func(string) *Response
ExpectMethod string
}{
{
SendReq: MustGet,
ExpectMethod: "GET",
},
{
SendReq: MustPost,
ExpectMethod: "POST",
},
{
SendReq: MustPatch,
ExpectMethod: "PATCH",
},
{
SendReq: MustPut,
ExpectMethod: "PUT",
},
{
SendReq: MustDelete,
ExpectMethod: "DELETE",
},
{
SendReq: MustOptions,
ExpectMethod: "OPTIONS",
},
{
SendReq: MustHead,
ExpectMethod: "HEAD",
},
}
url := getTestServerURL() + "/"
for _, tc := range testCases {
resp := tc.SendReq(url)
tests.AssertNotNil(t, resp.Response)
tests.AssertEqual(t, tc.ExpectMethod, resp.Header.Get("Method"))
}
}
func testGlobalWrapperSendMethods(t *testing.T) {
testCases := []struct {
SendReq func(string) (*Response, error)
ExpectMethod string
}{
{
SendReq: Get,
ExpectMethod: "GET",
},
{
SendReq: Post,
ExpectMethod: "POST",
},
{
SendReq: Patch,
ExpectMethod: "PATCH",
},
{
SendReq: Put,
ExpectMethod: "PUT",
},
{
SendReq: Delete,
ExpectMethod: "DELETE",
},
{
SendReq: Options,
ExpectMethod: "OPTIONS",
},
{
SendReq: Head,
ExpectMethod: "HEAD",
},
}
url := getTestServerURL() + "/"
for _, tc := range testCases {
resp, err := tc.SendReq(url)
assertSuccess(t, resp, err)
tests.AssertEqual(t, tc.ExpectMethod, resp.Header.Get("Method"))
}
}