/
testing_client.go
241 lines (201 loc) · 8.66 KB
/
testing_client.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
package azugo
import (
"bytes"
"crypto/rand"
"fmt"
"mime/multipart"
"strings"
"time"
"azugo.io/azugo/internal/utils"
"github.com/goccy/go-json"
"github.com/oklog/ulid/v2"
"github.com/valyala/fasthttp"
)
// TestClient is a test client for testing purposes.
type TestClient struct {
app *TestApp
client *fasthttp.Client
}
// TestClientOption is a test client option.
type TestClientOption func(*TestClient, *fasthttp.Request)
func (c *TestClient) applyOptions(request *fasthttp.Request, options []TestClientOption) {
for _, option := range options {
option(c, request)
}
}
// WithHost sets host for the request.
func (c *TestClient) WithHost(host string) TestClientOption {
return func(tc *TestClient, r *fasthttp.Request) {
r.SetHost(host)
}
}
// WithHeader adds header to request.
func (c *TestClient) WithHeader(key, value string) TestClientOption {
return func(tc *TestClient, r *fasthttp.Request) {
r.Header.Add(key, value)
}
}
// WithMultiPartFormBoundary sets multipart form data boundary.
func (c *TestClient) WithMultiPartFormBoundary(boundary string) TestClientOption {
return func(tc *TestClient, r *fasthttp.Request) {
r.Header.SetMultipartFormBoundary(boundary)
}
}
// WithQuery adds query parameters from map to query arguments.
func (c *TestClient) WithQuery(params map[string]any) TestClientOption {
return func(tc *TestClient, r *fasthttp.Request) {
for key, value := range params {
var val string
switch v := value.(type) {
case []string:
val = strings.Join(v, ",")
default:
val = fmt.Sprintf("%v", v)
}
r.URI().QueryArgs().Add(key, val)
}
}
}
// Client returns the underlying fasthttp client.
func (c *TestClient) Client() *fasthttp.Client {
return c.client
}
// CallRaw calls the given method and endpoint with the given body and options.
func (c *TestClient) CallRaw(method, endpoint, body []byte, options ...TestClientOption) (*fasthttp.Response, error) {
req := fasthttp.AcquireRequest()
req.Header.SetMethodBytes(method)
req.SetRequestURIBytes(endpoint)
c.applyOptions(req, options)
if len(body) > fasthttp.DefaultMaxRequestBodySize {
req.SetBodyStream(bytes.NewReader(body), len(body))
} else if len(body) > 0 {
req.SetBodyRaw(body)
}
defer fasthttp.ReleaseRequest(req)
resp := fasthttp.AcquireResponse()
if err := c.client.Do(req, resp); err != nil {
return nil, err
}
return resp, nil
}
// Call calls the given method and endpoint with the given body and options.
func (c *TestClient) Call(method, endpoint string, body []byte, options ...TestClientOption) (*fasthttp.Response, error) {
return c.CallRaw([]byte(method), []byte(fmt.Sprintf("http://test%s", endpoint)), body, options...)
}
// Get calls GET method with given options.
func (c *TestClient) Get(endpoint string, options ...TestClientOption) (*fasthttp.Response, error) {
return c.Call(fasthttp.MethodGet, endpoint, nil, options...)
}
// Head calls HEAD method with given options.
func (c *TestClient) Head(endpoint string, options ...TestClientOption) (*fasthttp.Response, error) {
return c.Call(fasthttp.MethodHead, endpoint, nil, options...)
}
// Delete calls DELETE method with given options.
func (c *TestClient) Delete(endpoint string, options ...TestClientOption) (*fasthttp.Response, error) {
return c.Call(fasthttp.MethodDelete, endpoint, nil, options...)
}
// Patch calls PATCH method with given body and options.
func (c *TestClient) Patch(endpoint string, body []byte, options ...TestClientOption) (*fasthttp.Response, error) {
return c.Call(fasthttp.MethodPatch, endpoint, body, options...)
}
// PatchJSON calls PATCH method with given object marshaled as JSON and options.
func (c *TestClient) PatchJSON(endpoint string, body any, options ...TestClientOption) (*fasthttp.Response, error) {
b, err := json.Marshal(body)
if err != nil {
return nil, err
}
options = append(options, c.WithHeader("Content-Type", "application/json"))
return c.Patch(endpoint, b, options...)
}
// PatchForm calls PATCH method with given map marshaled as URL encoded form and options.
func (c *TestClient) PatchForm(endpoint string, params map[string]any, options ...TestClientOption) (*fasthttp.Response, error) {
options = append(options, c.WithHeader("Content-Type", "application/x-www-form-urlencoded"))
return c.Patch(endpoint, []byte(utils.MapToURLValues(params)), options...)
}
// PatchMultiPartForm calls PATCH method with given multipart form and options.
func (c *TestClient) PatchMultiPartForm(endpoint string, form *multipart.Form, options ...TestClientOption) (*fasthttp.Response, error) {
boundary, err := ulid.New(ulid.Timestamp(time.Now().UTC()), ulid.Monotonic(rand.Reader, 1))
if err != nil {
return nil, err
}
options = append(options, c.WithMultiPartFormBoundary(boundary.String()))
var body bytes.Buffer
if err = fasthttp.WriteMultipartForm(&body, form, boundary.String()); err != nil {
return nil, err
}
return c.Put(endpoint, body.Bytes(), options...)
}
// Put calls PUT method with given body and options.
func (c *TestClient) Put(endpoint string, body []byte, options ...TestClientOption) (*fasthttp.Response, error) {
return c.Call(fasthttp.MethodPut, endpoint, body, options...)
}
// PutJSON calls PUT method with given object marshaled as JSON and options.
func (c *TestClient) PutJSON(endpoint string, body any, options ...TestClientOption) (*fasthttp.Response, error) {
b, err := json.Marshal(body)
if err != nil {
return nil, err
}
options = append(options, c.WithHeader("Content-Type", "application/json"))
return c.Put(endpoint, b, options...)
}
// PutForm calls PUT method with given map marshaled as URL encoded form and options.
func (c *TestClient) PutForm(endpoint string, params map[string]any, options ...TestClientOption) (*fasthttp.Response, error) {
options = append(options, c.WithHeader("Content-Type", "application/x-www-form-urlencoded"))
return c.Put(endpoint, []byte(utils.MapToURLValues(params)), options...)
}
// PutMultiPartForm calls PUT method with given multipart form and options.
func (c *TestClient) PutMultiPartForm(endpoint string, form *multipart.Form, options ...TestClientOption) (*fasthttp.Response, error) {
boundary, err := ulid.New(ulid.Timestamp(time.Now().UTC()), ulid.Monotonic(rand.Reader, 1))
if err != nil {
return nil, err
}
options = append(options, c.WithMultiPartFormBoundary(boundary.String()))
var body bytes.Buffer
if err = fasthttp.WriteMultipartForm(&body, form, boundary.String()); err != nil {
return nil, err
}
return c.Put(endpoint, body.Bytes(), options...)
}
// Post calls POST method with given body and options.
func (c *TestClient) Post(endpoint string, body []byte, options ...TestClientOption) (*fasthttp.Response, error) {
return c.Call(fasthttp.MethodPost, endpoint, body, options...)
}
// PostJSON calls POST method with given object marshaled as JSON and options.
func (c *TestClient) PostJSON(endpoint string, body any, options ...TestClientOption) (*fasthttp.Response, error) {
b, err := json.Marshal(body)
if err != nil {
return nil, err
}
options = append(options, c.WithHeader("Content-Type", "application/json"))
return c.Post(endpoint, b, options...)
}
// PostForm calls POST method with given map marshaled as URL encoded form and options.
func (c *TestClient) PostForm(endpoint string, params map[string]any, options ...TestClientOption) (*fasthttp.Response, error) {
options = append(options, c.WithHeader("Content-Type", "application/x-www-form-urlencoded"))
return c.Post(endpoint, []byte(utils.MapToURLValues(params)), options...)
}
// PostMultiPartForm calls POST method with given multipart form and options.
func (c *TestClient) PostMultiPartForm(endpoint string, form *multipart.Form, options ...TestClientOption) (*fasthttp.Response, error) {
boundary, err := ulid.New(ulid.Timestamp(time.Now().UTC()), ulid.Monotonic(rand.Reader, 1))
if err != nil {
return nil, err
}
options = append(options, c.WithMultiPartFormBoundary(boundary.String()))
var body bytes.Buffer
if err = fasthttp.WriteMultipartForm(&body, form, boundary.String()); err != nil {
return nil, err
}
return c.Post(endpoint, body.Bytes(), options...)
}
// Connect calls CONNECT method with given options.
func (c *TestClient) Connect(endpoint string, options ...TestClientOption) (*fasthttp.Response, error) {
return c.Call(fasthttp.MethodConnect, endpoint, nil, options...)
}
// Options calls OPTIONS method with given options.
func (c *TestClient) Options(endpoint string, options ...TestClientOption) (*fasthttp.Response, error) {
return c.Call(fasthttp.MethodOptions, endpoint, nil, options...)
}
// Trace calls TRACE method with given options.
func (c *TestClient) Trace(endpoint string, options ...TestClientOption) (*fasthttp.Response, error) {
return c.Call(fasthttp.MethodTrace, endpoint, nil, options...)
}