forked from maxcnunes/httpfake
/
request.go
185 lines (152 loc) · 4.72 KB
/
request.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
package httpfake
import (
"net/http"
"net/url"
"reflect"
"strings"
"sync"
"testing"
"github.com/tidwall/gjson"
)
// Request stores the settings for a request handler
// Such as how to match this handler for the incoming requests
// And how this request will respond back.
type Request struct {
sync.Mutex
Method string
URL *url.URL
Response *Response
CustomHandle Responder
assertions []Assertor
called int
}
// NewRequest creates a new Request.
func NewRequest() *Request {
return &Request{
URL: &url.URL{},
Response: NewResponse(),
called: 0,
}
}
// Get sets a GET request handler for a given path.
func (r *Request) Get(path string) *Request {
return r.method("GET", path)
}
// Post sets a POST request handler for a given path.
func (r *Request) Post(path string) *Request {
return r.method("POST", path)
}
// Put sets a PUT request handler for a given path.
func (r *Request) Put(path string) *Request {
return r.method("PUT", path)
}
// Patch sets a PATCH request handler for a given path.
func (r *Request) Patch(path string) *Request {
return r.method("PATCH", path)
}
// Delete ...
func (r *Request) Delete(path string) *Request {
return r.method("DELETE", path)
}
// Head sets a HEAD request handler for a given path.
func (r *Request) Head(path string) *Request {
return r.method("HEAD", path)
}
// Handle sets a custom handle
// By setting this responder it gives full control to the user over this request handler.
func (r *Request) Handle(handle Responder) {
r.CustomHandle = handle
}
// Reply sets a response status for this request
// And returns the Response struct to allow chaining the response settings.
func (r *Request) Reply(status int) *Response {
return r.Response.Status(status)
}
func (r *Request) method(method, path string) *Request {
if path != "/" {
u, err := url.Parse(path)
if err != nil {
panic(err)
}
r.URL = u
}
r.Method = strings.ToUpper(method)
return r
}
func (r *Request) runAssertions(t testing.TB, testReq *http.Request) {
for _, assertor := range r.assertions {
assertor.Log(t)
if err := assertor.Assert(testReq); err != nil {
assertor.Error(t, err)
}
}
}
// AssertQueries will assert that the provided query parameters are present in the requests to this handler.
func (r *Request) AssertQueries(key ...string) *Request {
r.assertions = append(r.assertions, &requiredQueries{Keys: key})
return r
}
// AssertQueryValue will assert that the provided query parameter and value are present in the requests to this handler.
func (r *Request) AssertQueryValue(key, value string) *Request {
r.assertions = append(r.assertions, &requiredQueryValue{Key: key, ExpectedValue: value})
return r
}
// AssertHeaders will assert that the provided header keys are present in the requests to this handler.
func (r *Request) AssertHeaders(keys ...string) *Request {
r.assertions = append(r.assertions, &requiredHeaders{Keys: keys})
return r
}
// AssertHeaderValue will assert that the provided header key and value are present in the requests to this handler.
func (r *Request) AssertHeaderValue(key, value string) *Request {
r.assertions = append(r.assertions, &requiredHeaderValue{Key: key, ExpectedValue: value})
return r
}
// AssertBody will assert that that the provided body matches in the requests to this handler.
func (r *Request) AssertBody(body []byte) *Request {
r.assertions = append(r.assertions, &requiredBody{ExpectedBody: body})
return r
}
// AssertCustom will run the provided assertor against requests to this handler.
func (r *Request) AssertCustom(assertor Assertor) *Request {
r.assertions = append(r.assertions, assertor)
return r
}
func (r *Request) AssertSubJSON(body string) *Request {
js := gjson.Parse(body)
for _, a := range r.assertions {
ja, ok := a.(*subJSON)
if ok {
js.ForEach(ja.fillFunc(""))
return r
}
}
jsonAssertor := &subJSON{necessaryFields: make(map[string]any)}
js.ForEach(jsonAssertor.fillFunc(""))
r.assertions = append(r.assertions, jsonAssertor)
return r
}
func (r *Request) AssertSubJSONBytes(body []byte) *Request {
return r.AssertSubJSON(string(body))
}
func (r *Request) AssertJsonField(key string, value any) *Request {
// because gjson only supports float64
switch reflectValue := reflect.ValueOf(value); {
case reflectValue.CanInt():
value = float64(reflectValue.Int())
case reflectValue.CanUint():
value = float64(reflectValue.Uint())
case reflectValue.CanFloat():
value = reflectValue.Float()
}
for _, a := range r.assertions {
ja, ok := a.(*subJSON)
if ok {
ja.necessaryFields[key] = value
return r
}
}
jsonAssertor := &subJSON{necessaryFields: make(map[string]any)}
jsonAssertor.necessaryFields[key] = value
r.assertions = append(r.assertions, jsonAssertor)
return r
}