-
Notifications
You must be signed in to change notification settings - Fork 0
/
codec_test.go
234 lines (201 loc) · 5.7 KB
/
codec_test.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
package ep
import (
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"testing"
"github.com/advanderveer/ep/epcoding"
)
func BenchmarkHandlers(b *testing.B) {
for i, c := range []struct {
body string
left http.Handler
right http.Handler
}{
{ // baseline base-case overhead between ep handle and
body: ``,
left: New().Handle(func(ResponseWriter, *http.Request) {}),
right: http.HandlerFunc(func(http.ResponseWriter, *http.Request) {}),
},
{ // overhead between reflection and no-reflection
body: ``,
left: New().Handle(func() {}),
right: New().Handle(func(ResponseWriter, *http.Request) {}),
},
{ // overhead when something needs to be encoded/decoded: ~0.00323 ms
body: `{"Foo": "bar"}`,
left: New(
RequestDecoding(epcoding.JSON{}),
ResponseEncoding(epcoding.JSON{}),
).Handle(func(in struct{ Foo string }) (out struct{ Bar string }) {
out.Bar = in.Foo
return
}),
right: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var in struct{ Foo string }
dec := json.NewDecoder(r.Body)
err := dec.Decode(&in)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
out := struct{ Bar string }{in.Foo}
enc := json.NewEncoder(w)
err = enc.Encode(out)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
}),
},
} {
b.Run(strconv.Itoa(i)+"-left", func(b *testing.B) {
for i := 0; i < b.N; i++ {
r := httptest.NewRequest("POST", "/", strings.NewReader(c.body))
w := httptest.NewRecorder()
c.left.ServeHTTP(w, r)
}
})
b.Run(strconv.Itoa(i)+"-right", func(b *testing.B) {
for i := 0; i < b.N; i++ {
r := httptest.NewRequest("POST", "/", strings.NewReader(c.body))
w := httptest.NewRecorder()
c.right.ServeHTTP(w, r)
}
})
}
}
func TestCodecHandlePanic(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic")
}
}()
New().Handle(1) // first arg must be a function
}
func TestCodecHandleWithReflection(t *testing.T) {
for i, c := range []struct {
fn interface{}
body string
expCode int
expBody string
}{
{func() {}, ``, 200, ``},
{func(a string) string { return a }, `"foo"`, 200, `"foo"` + "\n"},
{func(a *string) string { return *a }, `"rab"`, 200, `"rab"` + "\n"},
{func(myCtx) string { return "bar" }, ``, 200, `"bar"` + "\n"},
} {
t.Run(strconv.Itoa(i), func(t *testing.T) {
w := httptest.NewRecorder()
r := httptest.NewRequest("POST", "/", strings.NewReader(c.body))
New(
RequestDecoding(epcoding.JSON{}),
ResponseEncoding(epcoding.JSON{}),
).Handle(c.fn).ServeHTTP(w, r)
if w.Code != c.expCode {
t.Fatalf("expected code: %d, got: %d", c.expCode, w.Code)
}
if w.Body.String() != c.expBody {
t.Fatalf("expected body: '%s', got: '%s'", c.expBody, w.Body.String())
}
})
}
}
func TestCodecHandleWithoutReflection(t *testing.T) {
errHook := func(err error) (out interface{}) {
return struct {
Message string `json:"message"`
}{err.Error()}
}
failingReqHook := func(r *http.Request, in interface{}) error {
return errors.New("failing request hook")
}
always404Hook := func(w http.ResponseWriter, r *http.Request, out interface{}) {
w.WriteHeader(404)
}
manualWrite := func(w ResponseWriter, r *http.Request) {
w.Write([]byte("foo"))
}
renderErr := func(w ResponseWriter, r *http.Request) {
w.Render(nil, errors.New("foo"))
}
panicHandle := func(w ResponseWriter, r *http.Request) {
panic("foo")
}
stream := func(w ResponseWriter, r *http.Request) {
for {
var in struct{ Foo string }
if !w.Bind(&in) {
break
}
w.Render(in, nil)
}
}
justBind := func(w ResponseWriter, r *http.Request) {
var in struct{}
w.Bind(&in)
}
for i, c := range []struct {
method string
body string
handle func(ResponseWriter, *http.Request)
opt Option
expBody string
expCode int
}{
{ // manual writing in handle shout trigger response hooks
opt: Options(nil, ResponseHook(always404Hook)),
method: "GET", handle: manualWrite,
expBody: "foo", expCode: 404,
},
{ // render an error should also call the hooks, and shoud encode
opt: Options(
ResponseEncoding(epcoding.JSON{}),
ResponseHook(always404Hook),
ErrorHook(errHook)),
method: "GET", handle: renderErr,
expBody: `{"message":"foo"}` + "\n", expCode: 404,
},
{ // steaming input to output should work as expected, with hook
opt: Options(
RequestDecoding(epcoding.JSON{}),
ResponseEncoding(epcoding.JSON{}),
ResponseHook(always404Hook)),
method: "POST", handle: stream,
body: `{"Foo": "bar"}` + "\n" + `{"Foo": "rab"}`,
expBody: `{"Foo":"bar"}` + "\n" + `{"Foo":"rab"}` + "\n",
expCode: 404,
},
{ // request hook error should render as error
opt: Options(
RequestHook(failingReqHook),
ResponseEncoding(epcoding.JSON{}),
ResponseHook(always404Hook),
ErrorHook(errHook)),
method: "GET", handle: justBind,
expBody: `{"message":"response.bind: request hook failed: failing request hook"}` + "\n", expCode: 404,
},
{ // panic should also render with encoder
opt: Options(
ResponseEncoding(epcoding.JSON{}),
ErrorHook(errHook)),
method: "GET", handle: panicHandle,
expBody: `{"message":"foo"}` + "\n", expCode: 200,
},
} {
t.Run(strconv.Itoa(i), func(t *testing.T) {
r := httptest.NewRequest(c.method, "/", strings.NewReader(c.body))
w := httptest.NewRecorder()
New(c.opt).Handle(c.handle).ServeHTTP(w, r)
if w.Code != c.expCode {
t.Fatalf("expected code: %d, got: %d", c.expCode, w.Code)
}
if w.Body.String() != c.expBody {
t.Fatalf("expected body: '%s', got: '%s'", c.expBody, w.Body.String())
}
})
}
}