forked from danielgtaylor/huma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter_test.go
464 lines (389 loc) · 11.9 KB
/
router_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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
package huma
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/danielgtaylor/huma/schema"
"github.com/stretchr/testify/assert"
)
func newTestRouter() *Router {
app := New("Test API", "1.0.0")
return app
}
func TestRouterServiceLink(t *testing.T) {
r := newTestRouter()
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/", nil)
r.ServeHTTP(w, req)
assert.Contains(t, w.Header().Get("Link"), `</openapi.json>; rel="service-desc"`)
assert.Contains(t, w.Header().Get("Link"), `</docs>; rel="service-doc"`)
}
func TestRouterHello(t *testing.T) {
r := New("Test", "1.0.0")
r.Resource("/test").Get("test", "Test",
NewResponse(http.StatusNoContent, "test"),
).Run(func(ctx Context) {
ctx.WriteHeader(http.StatusNoContent)
})
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/test", nil)
r.ServeHTTP(w, req)
// Assert the response is as expected.
assert.Equal(t, http.StatusNoContent, w.Code)
}
func TestStreamingInput(t *testing.T) {
r := New("Test", "1.0.0")
r.Resource("/stream").Post("stream", "Stream test",
NewResponse(http.StatusNoContent, "test"),
NewResponse(http.StatusInternalServerError, "error"),
).Run(func(ctx Context, input struct {
Body io.Reader
}) {
_, err := ioutil.ReadAll(input.Body)
if err != nil {
ctx.WriteError(http.StatusInternalServerError, "Problem reading input", err)
}
ctx.WriteHeader(http.StatusNoContent)
})
w := httptest.NewRecorder()
body := bytes.NewReader(make([]byte, 1024))
req, _ := http.NewRequest(http.MethodPost, "/stream", body)
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusNoContent, w.Code)
}
func TestModelInputOutput(t *testing.T) {
type Response struct {
Category string `json:"category"`
Hidden bool `json:"hidden"`
Auth string `json:"auth"`
ID string `json:"id"`
Age int `json:"age"`
}
r := New("Test", "1.0.0")
r.Resource("/players/{category}").Post("player", "Create player",
NewResponse(http.StatusOK, "test").Model(Response{}),
).Run(func(ctx Context, input struct {
Category string `path:"category"`
Hidden bool `query:"hidden"`
Auth string `header:"Authorization"`
Body struct {
ID string `json:"id"`
Age int `json:"age" minimum:"16"`
}
}) {
ctx.WriteModel(http.StatusOK, Response{
Category: input.Category,
Hidden: input.Hidden,
Auth: input.Auth,
ID: input.Body.ID,
Age: input.Body.Age,
})
})
w := httptest.NewRecorder()
body := bytes.NewReader([]byte(`{"id": "abc123", "age": 25}`))
req, _ := http.NewRequest(http.MethodPost, "/players/fps?hidden=true", body)
req.Header.Set("Authorization", "dummy")
req.Host = "example.com"
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
assert.JSONEq(t, `{
"$schema": "https://example.com/schemas/Response.json",
"category": "fps",
"hidden": true,
"auth": "dummy",
"id": "abc123",
"age": 25
}`, w.Body.String())
// Should be able to get OpenAPI describing this API with its resource,
// operation, schema, etc.
w = httptest.NewRecorder()
req, _ = http.NewRequest(http.MethodGet, "/openapi.json", nil)
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
// Should be able to get model referenced by the response.
w = httptest.NewRecorder()
req, _ = http.NewRequest(http.MethodGet, "/schemas/Response.json", nil)
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
// Missing schema should return 404
w = httptest.NewRecorder()
req, _ = http.NewRequest(http.MethodGet, "/schemas/Missing.json", nil)
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusNotFound, w.Code)
}
func TestRouterEmbeddedStructOutput(t *testing.T) {
type CreatedField struct {
Created time.Time `json:"created,omitempty"`
}
type Resp struct {
CreatedField
Another string `json:"another"`
Ignored string `json:"-"`
}
now := time.Now()
r := New("Test", "1.0.0")
r.Resource("/test").Get("test", "Test",
NewResponse(http.StatusOK, "test").Model(&Resp{}),
).Run(func(ctx Context) {
ctx.WriteModel(http.StatusOK, &Resp{
CreatedField: CreatedField{
Created: now,
},
Another: "foo",
})
})
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/test", nil)
req.Host = "example.com"
r.ServeHTTP(w, req)
// Assert the response is as expected.
assert.Equal(t, http.StatusOK, w.Code)
assert.JSONEq(t, fmt.Sprintf(`{
"$schema": "https://example.com/schemas/Resp.json",
"created": "%s",
"another": "foo"
}`, now.Format(time.RFC3339Nano)), w.Body.String())
}
func TestTooBigBody(t *testing.T) {
app := newTestRouter()
type Input struct {
Body struct {
ID string `json:"id"`
}
}
op := app.Resource("/test").Put("put", "desc",
NewResponse(http.StatusNoContent, "desc"),
)
op.MaxBodyBytes(5)
op.Run(func(ctx Context, input Input) {
// Do nothing...
})
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodPut, "/test", strings.NewReader(`{"id": "foo"}`))
app.ServeHTTP(w, req)
assert.Equal(t, http.StatusBadRequest, w.Code)
assert.Contains(t, w.Body.String(), "Request body too large")
// With content length
w = httptest.NewRecorder()
req, _ = http.NewRequest(http.MethodPut, "/test", strings.NewReader(`{"id": "foo"}`))
req.Header.Set("Content-Length", "13")
app.ServeHTTP(w, req)
assert.Equal(t, http.StatusBadRequest, w.Code)
assert.Contains(t, w.Body.String(), "Request body too large")
}
type timeoutError struct{}
func (e *timeoutError) Error() string {
return "timed out"
}
func (e *timeoutError) Timeout() bool {
return true
}
func (e *timeoutError) Temporary() bool {
return false
}
type slowReader struct{}
func (r *slowReader) Read(p []byte) (int, error) {
return 0, &timeoutError{}
}
func TestBodySlow(t *testing.T) {
app := newTestRouter()
type Input struct {
Body struct {
ID string
}
}
op := app.Resource("/test").Put("put", "desc",
NewResponse(http.StatusNoContent, "desc"),
)
op.BodyReadTimeout(1 * time.Millisecond)
op.Run(func(ctx Context, input Input) {
// Do nothing...
})
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodPut, "/test", &slowReader{})
app.ServeHTTP(w, req)
assert.Equal(t, http.StatusBadRequest, w.Code)
assert.Contains(t, w.Body.String(), "timed out")
}
func TestErrorHandlers(t *testing.T) {
app := newTestRouter()
app.Resource("/").Get("root", "desc",
NewResponse(http.StatusNoContent, "desc"),
).Run(func(ctx Context) {
// Do nothing
})
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/notfound", nil)
app.ServeHTTP(w, req)
assert.Equal(t, http.StatusNotFound, w.Code)
assert.Equal(t, "application/problem+json", w.Header().Get("Content-Type"))
assert.Contains(t, w.Body.String(), "/notfound")
w = httptest.NewRecorder()
req, _ = http.NewRequest(http.MethodPut, "/", nil)
app.ServeHTTP(w, req)
assert.Equal(t, http.StatusMethodNotAllowed, w.Code)
assert.Equal(t, "application/problem+json", w.Header().Get("Content-Type"))
assert.Contains(t, w.Body.String(), "PUT")
}
func TestInvalidPathParam(t *testing.T) {
type Input struct {
ThingID string `path:"thing-if"`
}
app := newTestRouter()
// The router has no middleware, so no panic recovery will happen. This lets
// us test via a simple assertion that it would panic, and the actual test
// to ensure a 5xx error happens in the `middleware` package instead.
assert.Panics(t, func() {
app.Resource("/things/{thing-id}").Get("get", "Test",
NewResponse(http.StatusNoContent, "desc"),
).Run(func(ctx Context, input Input) {
// Do nothing
})
})
}
func TestRouterSecurity(t *testing.T) {
app := newTestRouter()
// Document that the API gateway handles auth via OAuth2 Authorization Code.
app.GatewayAuthCode("default", "https://example.com/authorize", "https://example.com/token", nil)
app.GatewayClientCredentials("m2m", "https://example.com/token", nil)
app.GatewayBasicAuth("basic")
// Every call must be authenticated using the default auth mechanism
// registered above.
app.SecurityRequirement("default")
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/openapi.json", nil)
app.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
var parsed map[string]interface{}
err := json.Unmarshal(w.Body.Bytes(), &parsed)
assert.Nil(t, err)
assert.Equal(t, parsed["security"], []interface{}{
map[string]interface{}{
"default": []interface{}{},
}})
assert.Equal(t, parsed["components"].(map[string]interface{})["securitySchemes"], map[string]interface{}{
"default": map[string]interface{}{
"type": "oauth2",
"flows": map[string]interface{}{
"authorizationCode": map[string]interface{}{
"authorizationUrl": "https://example.com/authorize",
"tokenUrl": "https://example.com/token",
},
},
},
"m2m": map[string]interface{}{
"type": "oauth2",
"flows": map[string]interface{}{
"clientCredentials": map[string]interface{}{
"tokenUrl": "https://example.com/token",
},
},
},
"basic": map[string]interface{}{
"type": "http",
"scheme": "basic",
"flows": map[string]interface{}{},
},
})
}
// TODO: test app.AutoConfig
func TestRouterAutoConfig(t *testing.T) {
app := newTestRouter()
app.GatewayAuthCode("authcode", "https://example.com/authorize", "https://example.com/token", nil)
app.SecurityRequirement("authcode")
app.AutoConfig(AutoConfig{
Security: "authcode",
Prompt: map[string]AutoConfigVar{
"extra": {
Description: "Some extra value",
Example: "abc123",
},
},
Params: map[string]string{
"another": "https://example.com/extras/{extra}",
},
})
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/openapi.json", nil)
app.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
var parsed map[string]interface{}
err := json.Unmarshal(w.Body.Bytes(), &parsed)
assert.Nil(t, err)
assert.Equal(t, parsed["x-cli-config"], map[string]interface{}{
"security": "authcode",
"prompt": map[string]interface{}{
"extra": map[string]interface{}{
"description": "Some extra value",
"example": "abc123",
},
},
"params": map[string]interface{}{
"another": "https://example.com/extras/{extra}",
},
})
}
func TestCustomRequestSchema(t *testing.T) {
app := newTestRouter()
op := app.Resource("/foo").Post("id", "doc", NewResponse(http.StatusOK, "ok"))
op.RequestSchema(&schema.Schema{
Type: schema.TypeInteger,
Minimum: schema.F(0),
Maximum: schema.F(100),
})
op.Run(func(ctx Context, input struct {
Body int
}) {
// Custom schema should be used so we never get here.
ctx.WriteHeader(http.StatusOK)
})
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodPost, "/foo", strings.NewReader("1234"))
app.ServeHTTP(w, req)
assert.Equal(t, http.StatusBadRequest, w.Result().StatusCode)
}
func TestGetOperationName(t *testing.T) {
app := newTestRouter()
var opInfo *OperationInfo
app.Middleware(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
next.ServeHTTP(w, r)
opInfo = GetOperationInfo(r.Context())
})
})
app.Resource("/").Get("test-id", "doc",
NewResponse(http.StatusOK, "ok"),
).Run(func(ctx Context) {
// Do nothing!
})
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/", nil)
app.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Result().StatusCode)
assert.Equal(t, "test-id", opInfo.ID)
assert.Equal(t, "/", opInfo.URITemplate)
assert.Equal(t, "doc", opInfo.Summary)
assert.Equal(t, []string{}, opInfo.Tags)
}
func TestGetOperationDoesNotCrash(t *testing.T) {
ctx := context.Background()
assert.NotPanics(t, func() {
info := GetOperationInfo(ctx)
assert.NotNil(t, info)
})
}
func TestSubResource(t *testing.T) {
app := newTestRouter()
// This should not crash.
app.Resource("/").SubResource("/foo").SubResource("/bar").Get("get-bar", "docs", NewResponse(http.StatusOK, "ok")).Run(func(ctx Context) {
// Do nothing
})
}