-
Notifications
You must be signed in to change notification settings - Fork 0
/
testy.go
282 lines (242 loc) · 6.69 KB
/
testy.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
package testy
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"strings"
)
const (
// MethodGet HTTP method
MethodGet = "GET"
// MethodPost HTTP method
MethodPost = "POST"
// MethodPut HTTP method
MethodPut = "PUT"
// MethodDelete HTTP method
MethodDelete = "DELETE"
// MethodPatch HTTP method
MethodPatch = "PATCH"
// MethodHead HTTP method
MethodHead = "HEAD"
// MethodOptions HTTP method
MethodOptions = "OPTIONS"
)
// Client ...
type Client struct {
handler http.Handler
QueryParam url.Values
FormData url.Values
Header http.Header
Body []byte
Result interface{}
Error interface{}
}
// Response ...
type Response struct {
RawResponse *http.Response
Body []byte
Status string
StatusCode int
Size int64
}
// New ...
func New(h http.Handler) *Client {
return &Client{
handler: h,
QueryParam: url.Values{},
FormData: url.Values{},
Header: http.Header{},
}
}
// Get ...
func (c *Client) Get(url string) *Response {
return c.Execute("GET", url)
}
// Patch ...
func (c *Client) Patch(url string) *Response {
return c.Execute("PATCH", url)
}
// Post ...
func (c *Client) Post(url string) *Response {
return c.Execute("POST", url)
}
// Delete ...
func (c *Client) Delete(url string) *Response {
return c.Execute("DELETE", url)
}
// Execute ...
func (c *Client) Execute(method, url string) *Response {
if len(c.QueryParam) > 0 {
url = fmt.Sprintf("%s?%s", url, c.QueryParam.Encode())
}
var reader io.Reader
if c.Body != nil {
reader = bytes.NewReader(c.Body)
}
request, _ := http.NewRequest(method, url, reader)
request.Header = c.Header
recorder := httptest.NewRecorder()
c.handler.ServeHTTP(recorder, request)
result := recorder.Result()
response := Response{
RawResponse: result,
Status: result.Status,
StatusCode: result.StatusCode,
}
var err error
if response.Body, err = ioutil.ReadAll(result.Body); err != nil {
panic(err)
}
response.Size = int64(len(response.Body))
if c.Result != nil {
err = json.Unmarshal(response.Body, c.Result)
if err != nil {
panic(err)
}
}
return &response
}
// SetHeader method is to set a single header field and its value in the current request.
//
// For Example: To set `Content-Type` and `Accept` as `application/json`.
// client.R().
// SetHeader("Content-Type", "application/json").
// SetHeader("Accept", "application/json")
//
// Also you can override header value, which was set at client instance level.
func (c *Client) SetHeader(header, value string) *Client {
c.Header.Set(header, value)
return c
}
// SetHeaders method sets multiple headers field and its values at one go in the current request.
//
// For Example: To set `Content-Type` and `Accept` as `application/json`
//
// client.R().
// SetHeaders(map[string]string{
// "Content-Type": "application/json",
// "Accept": "application/json",
// })
// Also you can override header value, which was set at client instance level.
func (c *Client) SetHeaders(headers map[string]string) *Client {
for h, v := range headers {
c.SetHeader(h, v)
}
return c
}
// SetQueryParam method sets single parameter and its value in the current request.
// It will be formed as query string for the request.
//
// For Example: `search=kitchen%20papers&size=large` in the URL after `?` mark.
// client.R().
// SetQueryParam("search", "kitchen papers").
// SetQueryParam("size", "large")
// Also you can override query params value, which was set at client instance level.
func (c *Client) SetQueryParam(param, value string) *Client {
c.QueryParam.Set(param, value)
return c
}
// SetQueryParams method sets multiple parameters and its values at one go in the current request.
// It will be formed as query string for the request.
//
// For Example: `search=kitchen%20papers&size=large` in the URL after `?` mark.
// client.R().
// SetQueryParams(map[string]string{
// "search": "kitchen papers",
// "size": "large",
// })
// Also you can override query params value, which was set at client instance level.
func (c *Client) SetQueryParams(params map[string]string) *Client {
for p, v := range params {
c.SetQueryParam(p, v)
}
return c
}
// SetQueryParamsFromValues method appends multiple parameters with multi-value
// (`url.Values`) at one go in the current request. It will be formed as
// query string for the request.
//
// For Example: `status=pending&status=approved&status=open` in the URL after `?` mark.
// client.R().
// SetQueryParamsFromValues(url.Values{
// "status": []string{"pending", "approved", "open"},
// })
// Also you can override query params value, which was set at client instance level.
func (c *Client) SetQueryParamsFromValues(params url.Values) *Client {
for p, v := range params {
for _, pv := range v {
c.QueryParam.Add(p, pv)
}
}
return c
}
// SetQueryString method provides ability to use string as an input to set URL query string for the request.
//
// Using String as an input
// client.R().
// SetQueryString("productId=232&template=fresh-sample&cat=resty&source=google&kw=buy a lot more")
func (c *Client) SetQueryString(query string) *Client {
params, err := url.ParseQuery(strings.TrimSpace(query))
if err == nil {
for p, v := range params {
for _, pv := range v {
c.QueryParam.Add(p, pv)
}
}
} else {
//c.client.log.Errorf("%v", err)
}
return c
}
// SetResult ...
func (c *Client) SetResult(result interface{}) *Client {
c.Result = result
return c
}
// SetBody method sets the request body for the request. Similar to resty.
// We can say its quite handy or powerful. Supported request body data types is `string`,
// `[]byte`, `struct`, `map` and `slice` (not io.Reader currently).
// Automatic marshalling for JSON (not XML), if it is `struct`, `map`, or `slice`.
func (c *Client) SetBody(body interface{}) *Client {
var bodyBytes []byte
//contentType := r.Header.Get("Content-Type")
kind := kindOf(body)
if b, ok := body.([]byte); ok {
bodyBytes = b
} else if s, ok := body.(string); ok {
bodyBytes = []byte(s)
} else if kind == reflect.Struct || kind == reflect.Map || kind == reflect.Slice {
var err error
bodyBytes, err = json.Marshal(body)
if err != nil {
panic(err)
}
}
if bodyBytes == nil {
panic("unsupported 'Body' type/value")
}
c.Body = bodyBytes
return c
}
func (r *Response) String() string {
return string(r.Body)
}
// Borrowed from resty/utils.go
func typeOf(i interface{}) reflect.Type {
return indirect(valueOf(i)).Type()
}
func valueOf(i interface{}) reflect.Value {
return reflect.ValueOf(i)
}
func indirect(v reflect.Value) reflect.Value {
return reflect.Indirect(v)
}
func kindOf(v interface{}) reflect.Kind {
return typeOf(v).Kind()
}