-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathgofight.go
480 lines (412 loc) · 10.7 KB
/
gofight.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
// Package gofight offers simple API http handler testing for Golang framework.
//
// Details about the gofight project are found in github page:
//
// https://github.com/appleboy/gofight
//
// Installation:
//
// $ go get -u github.com/appleboy/gofight
//
// Set Header: You can add custom header via SetHeader func.
//
// SetHeader(gofight.H{
// "X-Version": version,
// })
//
// Set Cookie: You can add custom cookie via SetCookie func.
//
// SetCookie(gofight.H{
// "foo": "bar",
// })
//
// Set query string: Using SetQuery to generate query string data.
//
// SetQuery(gofight.H{
// "a": "1",
// "b": "2",
// })
//
// POST FORM Data: Using SetForm to generate form data.
//
// SetForm(gofight.H{
// "a": "1",
// "b": "2",
// })
//
// POST JSON Data: Using SetJSON to generate json data.
//
// SetJSON(gofight.H{
// "a": "1",
// "b": "2",
// })
//
// POST RAW Data: Using SetBody to generate raw data.
//
// SetBody("a=1&b=1")
//
// For more details, see the documentation and example.
package gofight
import (
"bytes"
"context"
"encoding/json"
"io"
"log"
"mime/multipart"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"strings"
)
// Media types
const (
Version = "1.0"
UserAgent = "User-Agent"
ContentType = "Content-Type"
ApplicationJSON = "application/json"
ApplicationForm = "application/x-www-form-urlencoded"
)
// HTTPResponse wraps the httptest.ResponseRecorder to provide additional
// functionality or to simplify the response handling in tests.
type HTTPResponse struct {
*httptest.ResponseRecorder
}
// HTTPRequest is a wrapper around the standard http.Request.
// It embeds the http.Request struct, allowing you to use all the methods
// and fields of http.Request while also providing the ability to extend
// its functionality with additional methods or fields if needed.
type HTTPRequest struct {
*http.Request
}
// ResponseFunc is a type alias for a function that takes an HTTPResponse and an HTTPRequest as parameters.
// It is used to define a callback function that can handle or process HTTP responses and requests.
type ResponseFunc func(HTTPResponse, HTTPRequest)
// H is HTTP Header Type
type H map[string]string
// D is HTTP Data Type
type D map[string]interface{}
// RequestConfig provide user input request structure
type RequestConfig struct {
Method string
Path string
Body string
Headers H
Cookies H
Debug bool
ContentType string
Context context.Context
}
// UploadFile for upload file struct
type UploadFile struct {
Path string
Name string
Content []byte
}
// New supply initial structure
func New() *RequestConfig {
return &RequestConfig{
Context: context.Background(),
}
}
// SetDebug supply enable debug mode.
func (rc *RequestConfig) SetDebug(enable bool) *RequestConfig {
rc.Debug = enable
return rc
}
// SetContext sets the context for the RequestConfig.
// This allows the request to be aware of deadlines, cancellation signals, and other request-scoped values.
// It returns the updated RequestConfig instance.
//
// Parameters:
//
// ctx - the context to be set for the RequestConfig
//
// Returns:
//
// *RequestConfig - the updated RequestConfig instance with the new context
func (rc *RequestConfig) SetContext(ctx context.Context) *RequestConfig {
rc.Context = ctx
return rc
}
// GET is request method.
func (rc *RequestConfig) GET(path string) *RequestConfig {
rc.Path = path
rc.Method = "GET"
return rc
}
// POST is request method.
func (rc *RequestConfig) POST(path string) *RequestConfig {
rc.Path = path
rc.Method = "POST"
return rc
}
// PUT is request method.
func (rc *RequestConfig) PUT(path string) *RequestConfig {
rc.Path = path
rc.Method = "PUT"
return rc
}
// DELETE is request method.
func (rc *RequestConfig) DELETE(path string) *RequestConfig {
rc.Path = path
rc.Method = "DELETE"
return rc
}
// PATCH is request method.
func (rc *RequestConfig) PATCH(path string) *RequestConfig {
rc.Path = path
rc.Method = "PATCH"
return rc
}
// HEAD is request method.
func (rc *RequestConfig) HEAD(path string) *RequestConfig {
rc.Path = path
rc.Method = "HEAD"
return rc
}
// OPTIONS is request method.
func (rc *RequestConfig) OPTIONS(path string) *RequestConfig {
rc.Path = path
rc.Method = "OPTIONS"
return rc
}
// SetHeader supply http header what you defined.
func (rc *RequestConfig) SetHeader(headers H) *RequestConfig {
if len(headers) > 0 {
rc.Headers = headers
}
return rc
}
// SetJSON supply JSON body.
func (rc *RequestConfig) SetJSON(body D) *RequestConfig {
if b, err := json.Marshal(body); err == nil {
rc.Body = string(b)
}
return rc
}
// SetJSONInterface supply JSON body
func (rc *RequestConfig) SetJSONInterface(body interface{}) *RequestConfig {
if b, err := json.Marshal(body); err == nil {
rc.Body = string(b)
}
return rc
}
// SetForm sets the form data for the request configuration.
// It takes a map of string keys and values, converts it to url.Values,
// and encodes it as a URL-encoded form string, which is then assigned to the Body field.
//
// Parameters:
//
// body (H): A map containing the form data to be set.
//
// Returns:
//
// *RequestConfig: The updated request configuration.
func (rc *RequestConfig) SetForm(body H) *RequestConfig {
f := make(url.Values)
for k, v := range body {
f.Set(k, v)
}
rc.Body = f.Encode()
return rc
}
// SetFileFromPath upload new file.
func (rc *RequestConfig) SetFileFromPath(uploads []UploadFile, params ...H) *RequestConfig {
body := new(bytes.Buffer)
writer := multipart.NewWriter(body)
for _, f := range uploads {
reader := bytes.NewReader(f.Content)
if reader.Size() == 0 {
file, err := os.Open(f.Path)
if err != nil {
return rc
}
defer file.Close()
part, err := writer.CreateFormFile(f.Name, filepath.Base(f.Path))
if err != nil {
return rc
}
_, err = io.Copy(part, file)
if err != nil {
return rc
}
} else {
part, err := writer.CreateFormFile(f.Name, filepath.Base(f.Path))
if err != nil {
return rc
}
_, err = reader.WriteTo(part)
if err != nil {
return rc
}
}
}
if len(params) > 0 {
for key, val := range params[0] {
_ = writer.WriteField(key, val)
}
}
err := writer.Close()
if err != nil {
return rc
}
rc.ContentType = writer.FormDataContentType()
rc.Body = body.String()
return rc
}
// SetPath supply new request path to deal with path variable request
// ex. /reqpath/:book/:apple , usage: r.POST("/reqpath/").SetPath("book1/apple2")...
func (rc *RequestConfig) SetPath(str string) *RequestConfig {
rc.Path += str
return rc
}
// SetQueryD supply query string, support query using string array input.
// ex. /reqpath/?Ids[]=E&Ids[]=M usage:
// IDArray:=[]string{"E","M"} r.GET("reqpath").SetQueryD(gofight.D{`Ids[]`: IDArray})
func (rc *RequestConfig) SetQueryD(query D) *RequestConfig {
var buf strings.Builder
buf.WriteString("?")
for k, v := range query {
switch v := v.(type) {
case string:
buf.WriteString(k + "=" + v)
buf.WriteString("&")
case []string:
for _, info := range v {
buf.WriteString(k + "=" + info)
buf.WriteString("&")
}
}
}
rc.Path += buf.String()[:len(buf.String())-1]
return rc
}
// SetQuery sets the query parameters for the request configuration.
// It takes a map of query parameters and their values, and appends them
// to the existing path of the request configuration. If the path already
// contains query parameters, the new parameters are appended with an '&'.
// Otherwise, they are appended with a '?'.
//
// Parameters:
//
// query (H): A map containing the query parameters and their values.
//
// Returns:
//
// *RequestConfig: The updated request configuration with the query parameters set.
func (rc *RequestConfig) SetQuery(query H) *RequestConfig {
f := make(url.Values)
for k, v := range query {
f.Set(k, v)
}
if strings.Contains(rc.Path, "?") {
rc.Path = rc.Path + "&" + f.Encode()
} else {
rc.Path = rc.Path + "?" + f.Encode()
}
return rc
}
// SetBody sets the body of the request if the provided body string is not empty.
// It returns the updated RequestConfig instance.
//
// Parameters:
// - body: A string representing the body content to be set.
//
// Returns:
// - *RequestConfig: The updated RequestConfig instance.
func (rc *RequestConfig) SetBody(body string) *RequestConfig {
if len(body) > 0 {
rc.Body = body
}
return rc
}
// SetCookie sets the cookies for the request configuration.
// It takes a map of cookies and assigns it to the Cookies field of the RequestConfig
// if the provided map is not empty.
//
// Parameters:
// - cookies: A map of cookies to be set.
//
// Returns:
// - A pointer to the updated RequestConfig.
func (rc *RequestConfig) SetCookie(cookies H) *RequestConfig {
if len(cookies) > 0 {
rc.Cookies = cookies
}
return rc
}
func (rc *RequestConfig) initTest() (*http.Request, *httptest.ResponseRecorder) {
qs := ""
if strings.Contains(rc.Path, "?") {
ss := strings.Split(rc.Path, "?")
rc.Path = ss[0]
qs = ss[1]
}
body := bytes.NewBufferString(rc.Body)
req, _ := http.NewRequestWithContext(rc.Context, rc.Method, rc.Path, body)
req.RequestURI = req.URL.RequestURI()
if len(qs) > 0 {
req.URL.RawQuery = qs
}
// Auto add user agent
req.Header.Set(UserAgent, "Gofight-client/"+Version)
if rc.Method == "POST" || rc.Method == "PUT" || rc.Method == "PATCH" {
if strings.HasPrefix(rc.Body, "{") {
req.Header.Set(ContentType, ApplicationJSON)
} else {
req.Header.Set(ContentType, ApplicationForm)
}
}
if rc.ContentType != "" {
req.Header.Set(ContentType, rc.ContentType)
}
if len(rc.Headers) > 0 {
for k, v := range rc.Headers {
req.Header.Set(k, v)
}
}
if len(rc.Cookies) > 0 {
for k, v := range rc.Cookies {
req.AddCookie(&http.Cookie{
Name: k,
Value: v,
HttpOnly: true,
Secure: false,
})
}
}
if rc.Debug {
log.Printf("Request QueryString: %s", qs)
log.Printf("Request Method: %s", rc.Method)
log.Printf("Request Path: %s", rc.Path)
log.Printf("Request Body: %s", rc.Body)
log.Printf("Request Headers: %s", rc.Headers)
log.Printf("Request Cookies: %s", rc.Cookies)
log.Printf("Request Header: %s", req.Header)
}
w := httptest.NewRecorder()
return req, w
}
// Run executes the HTTP request using the provided http.Handler and processes
// the response using the given ResponseFunc. It initializes the test request
// and response writer, serves the HTTP request, and then passes the HTTP
// response and request to the response function.
//
// Parameters:
// - r: The http.Handler that will handle the HTTP request.
// - response: A function that processes the HTTP response and request.
func (rc *RequestConfig) Run(r http.Handler, response ResponseFunc) {
req, w := rc.initTest()
r.ServeHTTP(w, req)
response(
HTTPResponse{
w,
},
HTTPRequest{
req,
},
)
}