-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
198 lines (158 loc) · 3.92 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
196
197
198
package testutil
import (
"bytes"
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/alextanhongpin/core/http/httputil"
"github.com/alextanhongpin/core/internal"
"github.com/alextanhongpin/core/test/testdump"
"github.com/google/go-cmp/cmp"
)
type HTTPDump = testdump.HTTPDump
type HTTPHook = testdump.HTTPHook
type Path = internal.Path
type HTTPOption interface {
isHTTP()
}
func DumpHTTPHandler(t *testing.T, r *http.Request, handler http.HandlerFunc, opts ...HTTPOption) io.Reader {
t.Helper()
// Make a copy of the body.
var reset func()
//var br *bytes.Reader
if r.Body != nil {
b, err := io.ReadAll(r.Body)
if err != nil {
t.Fatal(err)
}
br := bytes.NewReader(b)
r.Body = io.NopCloser(br)
reset = func() {
br.Seek(0, 0)
}
}
wr := httptest.NewRecorder()
// Execute.
handler(wr, r)
w := wr.Result()
// Reset request for the handler.
if reset != nil {
reset()
}
return DumpHTTP(t, w, r, opts...)
}
func DumpHTTP(t *testing.T, w *http.Response, r *http.Request, opts ...HTTPOption) io.Reader {
t.Helper()
var fileName string
var hooks []testdump.Hook[*HTTPDump]
httpOpt := new(testdump.HTTPOption)
for _, opt := range opts {
switch o := opt.(type) {
case FileName:
fileName = string(o)
case *httpHookOption:
hooks = append(hooks, o.hook)
case *httpCmpOption:
httpOpt.Header = append(httpOpt.Header, o.header...)
httpOpt.Body = append(httpOpt.Body, o.body...)
httpOpt.Trailer = append(httpOpt.Trailer, o.trailer...)
default:
panic(fmt.Errorf("testutil: unhandled HTTP option: %#v", opt))
}
}
p := Path{
Dir: "testdata",
FilePath: t.Name(),
FileName: fileName,
FileExt: ".http",
}
// Read the body and reset it.
defer w.Body.Close()
b, err := io.ReadAll(w.Body)
if err != nil {
t.Fatal(err)
}
br := bytes.NewReader(b)
defer br.Seek(0, 0)
w.Body = io.NopCloser(br)
if err := testdump.HTTP(testdump.NewFile(p.String()), &HTTPDump{W: w, R: r}, httpOpt, hooks...); err != nil {
t.Fatal(err)
}
return br
}
type RoundTripper struct {
t *testing.T
opts []HTTPOption
}
func DumpRoundTrip(t *testing.T, opts ...HTTPOption) *RoundTripper {
return &RoundTripper{
t: t,
opts: opts,
}
}
func (t *RoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
r, err := httputil.CloneRequest(req)
if err != nil {
return nil, err
}
w, err := http.DefaultTransport.RoundTrip(req)
DumpHTTP(t.t, w, r, t.opts...)
return w, err
}
type httpHookOption struct {
hook testdump.Hook[*HTTPDump]
}
func (httpHookOption) isHTTP() {}
type httpCmpOption struct {
header []cmp.Option
body []cmp.Option
trailer []cmp.Option
}
func (httpCmpOption) isHTTP() {}
func IgnoreHeaders(headers ...string) *httpCmpOption {
return &httpCmpOption{
header: []cmp.Option{internal.IgnoreMapEntries(headers...)},
}
}
func IgnoreTrailers(headers ...string) *httpCmpOption {
return &httpCmpOption{
trailer: []cmp.Option{internal.IgnoreMapEntries(headers...)},
}
}
func IgnoreBodyFields(fields ...string) *httpCmpOption {
return &httpCmpOption{
body: []cmp.Option{internal.IgnoreMapEntries(fields...)},
}
}
func MaskRequestHeaders(headers ...string) *httpHookOption {
return &httpHookOption{
hook: testdump.MaskRequestHeaders(headers...),
}
}
func MaskResponseHeaders(headers ...string) *httpHookOption {
return &httpHookOption{
hook: testdump.MaskResponseHeaders(headers...),
}
}
func MaskRequestBody(fields ...string) *httpHookOption {
return &httpHookOption{
hook: testdump.MaskRequestBody(fields...),
}
}
func MaskResponseBody(fields ...string) *httpHookOption {
return &httpHookOption{
hook: testdump.MaskResponseBody(fields...),
}
}
func InspectHTTP(hook func(snapshot, received *HTTPDump) error) *httpHookOption {
return &httpHookOption{
hook: testdump.CompareHook(hook),
}
}
func InterceptHTTP(hook func(dump *HTTPDump) (*HTTPDump, error)) *httpHookOption {
return &httpHookOption{
hook: testdump.MarshalHook(hook),
}
}