-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathutils_req.go
More file actions
140 lines (116 loc) · 3.29 KB
/
utils_req.go
File metadata and controls
140 lines (116 loc) · 3.29 KB
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
package got
import (
"bytes"
"context"
"encoding/json"
"io"
"mime"
"net/http"
"path/filepath"
)
// ReqMIME option type, it should be like ".json", "test.json", "a/b/c.jpg", etc
type ReqMIME string
type ReqClient interface {
Do(req *http.Request) (*http.Response, error)
}
type ClientDoFunc func(req *http.Request) (*http.Response, error)
func (f ClientDoFunc) Do(req *http.Request) (*http.Response, error) {
return f(req)
}
// Req is a helper method to send http request. It will handle errors automatically, so you don't need to check errors.
// The method is the http method, default value is "GET".
// If an option is [http.Header], it will be used as the request header.
// If an option is [ReqMIME], it will be used to set the Content-Type header.
// If an option is [context.Context], it will be used as the request context.
// If an option is [ReqClient], it will be used as the http client to send the request.
// Other option type will be treat as request body, it will be encoded by [Utils.Write].
// Some request examples:
//
// Req("GET", "http://example.com")
// Req("GET", "http://example.com", context.TODO())
// Req("POST", "http://example.com", map[string]any{"a": 1})
// Req("POST", "http://example.com", http.Header{"Host": "example.com"}, ReqMIME(".json"), map[string]any{"a": 1})
func (ut Utils) Req(method, url string, options ...interface{}) *ResHelper {
ut.Helper()
header := http.Header{}
var host string
var contentType string
var body io.Reader
var client ReqClient = http.DefaultClient
ctx := context.Background()
for _, item := range options {
switch val := item.(type) {
case http.Header:
host = val.Get("Host")
val.Del("Host")
header = val
case ReqMIME:
contentType = mime.TypeByExtension(filepath.Ext(string(val)))
case context.Context:
ctx = val
case ReqClient:
client = val
default:
buf := bytes.NewBuffer(nil)
ut.Write(val)(buf)
body = buf
}
}
req, err := http.NewRequestWithContext(ctx, method, url, body)
if err != nil {
return &ResHelper{ut, nil, err, nil}
}
if header != nil {
req.Header = header
}
req.Host = host
req.Header.Set("Content-Type", contentType)
res, err := client.Do(req)
return &ResHelper{ut, res, err, nil}
}
// ResHelper of the request
type ResHelper struct {
ut Utils
*http.Response
err error
read *bytes.Buffer
}
// Bytes parses body as [*bytes.Buffer] and returns the result
func (res *ResHelper) Bytes() *bytes.Buffer {
res.ut.Helper()
res.ut.err(res.err)
if res.read == nil {
res.read = res.ut.Read(res.Body)
}
return res.read
}
// String parses body as string and returns the result
func (res *ResHelper) String() string {
res.ut.Helper()
return res.Bytes().String()
}
// JSON parses body as json and returns the result
func (res *ResHelper) JSON() (v interface{}) {
res.ut.Helper()
res.ut.err(res.err)
return res.ut.JSON(res.String())
}
// Unmarshal body to v as json, it's like [json.Unmarshal].
func (res *ResHelper) Unmarshal(v interface{}) {
res.ut.Helper()
res.ut.err(json.Unmarshal(res.Bytes().Bytes(), v))
}
// Err of request protocol
func (res *ResHelper) Err() error {
return res.err
}
func (ut Utils) err(err error) {
ut.Helper()
if err != nil {
ut.Fatal(err)
}
}
// there no way to stop a blocking test from outside
var panicWithTrace = func(v interface{}) {
panic(v)
}