-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
http.go
195 lines (160 loc) · 4.3 KB
/
http.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
package testttp
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"net/url"
"testing"
)
// Req is a request builder.
type Req struct {
t testing.TB
req *http.Request
}
// HTTP runs creates a request for an http call.
func HTTP(t testing.TB, method, addr string, body io.Reader) *Req {
return &Req{
t: t,
req: httptest.NewRequest(method, addr, body),
}
}
// Delete creates a DELETE request.
func Delete(t testing.TB, addr string) *Req {
return HTTP(t, http.MethodDelete, addr, nil)
}
// Get creates a GET request.
func Get(t testing.TB, addr string) *Req {
return HTTP(t, http.MethodGet, addr, nil)
}
// Patch creates a PATCH request.
func Patch(t testing.TB, addr string, body io.Reader) *Req {
return HTTP(t, http.MethodPatch, addr, body)
}
// PatchJSON creates a PATCH request with a json encoded body.
func PatchJSON(t testing.TB, addr string, v interface{}) *Req {
return HTTP(t, http.MethodPatch, addr, mustEncodeJSON(t, v))
}
// Post creates a POST request.
func Post(t testing.TB, addr string, body io.Reader) *Req {
return HTTP(t, http.MethodPost, addr, body)
}
// PostJSON returns a POST request with a json encoded body.
func PostJSON(t testing.TB, addr string, v interface{}) *Req {
return Post(t, addr, mustEncodeJSON(t, v))
}
// Put creates a PUT request.
func Put(t testing.TB, addr string, body io.Reader) *Req {
return HTTP(t, http.MethodPut, addr, body)
}
// PutJSON creates a PUT request with a json encoded body.
func PutJSON(t testing.TB, addr string, v interface{}) *Req {
return HTTP(t, http.MethodPut, addr, mustEncodeJSON(t, v))
}
// Do runs the request against the provided handler.
func (r *Req) Do(handler http.Handler) *Resp {
rec := httptest.NewRecorder()
handler.ServeHTTP(rec, r.req)
return &Resp{
t: r.t,
debug: true,
Req: r.req,
Rec: rec,
}
}
func (r *Req) SetFormValue(k, v string) *Req {
if r.req.Form == nil {
r.req.Form = make(url.Values)
}
r.req.Form.Set(k, v)
return r
}
// Headers allows the user to set headers on the http request.
func (r *Req) Headers(k, v string, rest ...string) *Req {
headers := append(rest, k, v)
for i := 0; i < len(headers); i += 2 {
if i+1 >= len(headers) {
break
}
k, v := headers[i], headers[i+1]
r.req.Header.Add(k, v)
}
return r
}
// WithCtx sets the ctx on the request.
func (r *Req) WithCtx(ctx context.Context) *Req {
r.req = r.req.WithContext(ctx)
return r
}
// WrapCtx provides means to wrap a request context. This is useful for stuffing in the
// auth stuffs that are required at times.
func (r *Req) WrapCtx(fn func(ctx context.Context) context.Context) *Req {
return r.WithCtx(fn(r.req.Context()))
}
// Resp is a http recorder wrapper.
type Resp struct {
t testing.TB
debug bool
Req *http.Request
Rec *httptest.ResponseRecorder
}
// Debug sets the debugger. If true, the debugger will print the body of the response
// when the expected status is not received.
func (r *Resp) Debug(b bool) *Resp {
r.debug = b
return r
}
// Expect allows the assertions against the raw Resp.
func (r *Resp) Expect(fn func(*Resp)) *Resp {
fn(r)
return r
}
// ExpectStatus compares the expected status code against the recorded status code.
func (r *Resp) ExpectStatus(code int) *Resp {
r.t.Helper()
if r.Rec.Code != code {
r.t.Errorf("unexpected status code: expected=%d got=%d", code, r.Rec.Code)
if r.debug {
r.t.Logf("body: %v", r.Rec.Body.String())
}
}
return r
}
// ExpectBody provides an assertion against the recorder body.
func (r *Resp) ExpectBody(fn func(body *bytes.Buffer)) *Resp {
fn(r.Rec.Body)
return r
}
// ExpectHeaders asserts that multiple headers with values exist in the recorder.
func (r *Resp) ExpectHeaders(h map[string]string) *Resp {
for k, v := range h {
r.ExpectHeader(k, v)
}
return r
}
// ExpectHeader asserts that the header is in the recorder.
func (r *Resp) ExpectHeader(k, v string) *Resp {
r.t.Helper()
vals, ok := r.Rec.Header()[k]
if !ok {
r.t.Errorf("did not find expected header: %q", k)
return r
}
for _, vv := range vals {
if vv == v {
return r
}
}
r.t.Errorf("did not find expected value for header %q; got: %v", k, vals)
return r
}
func mustEncodeJSON(t testing.TB, v interface{}) *bytes.Buffer {
t.Helper()
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(v); err != nil {
t.Fatal(err)
}
return &buf
}