-
-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathengine_test.go
95 lines (83 loc) · 2.51 KB
/
engine_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
package fuego
import (
"errors"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestWithErrorHandler(t *testing.T) {
t.Run("default engine", func(t *testing.T) {
e := NewEngine()
err := NotFoundError{
Err: errors.New("Not Found :c"),
}
errResponse := e.ErrorHandler(err)
require.ErrorAs(t, errResponse, &HTTPError{})
})
t.Run("custom handler", func(t *testing.T) {
e := NewEngine(
WithErrorHandler(func(err error) error {
return fmt.Errorf("%w foobar", err)
}),
)
err := NotFoundError{
Err: errors.New("Not Found :c"),
}
errResponse := e.ErrorHandler(err)
require.ErrorAs(t, errResponse, &HTTPError{})
require.ErrorContains(t, errResponse, "Not Found :c foobar")
})
t.Run("should be fatal", func(t *testing.T) {
require.Panics(t, func() {
NewEngine(
WithErrorHandler(nil),
)
})
})
t.Run("disable error handler", func(t *testing.T) {
e := NewEngine(DisableErrorHandler())
err := NotFoundError{
Err: errors.New("Not Found"),
}
errResponse := e.ErrorHandler(err)
require.Equal(t, "Not Found", errResponse.Error())
})
t.Run("nil returning handler", func(t *testing.T) {
e := NewEngine(
WithErrorHandler(func(err error) error {
return nil
}),
)
err := NotFoundError{
Err: errors.New("Not Found"),
}
errResponse := e.ErrorHandler(err)
require.Nil(t, errResponse, "error handler can return nil, which might lead to unexpected behavior")
})
}
func TestWithRequestContentType(t *testing.T) {
t.Run("base", func(t *testing.T) {
e := NewEngine()
require.Nil(t, e.requestContentTypes)
})
t.Run("input", func(t *testing.T) {
arr := []string{"application/json", "application/xml"}
e := NewEngine(WithRequestContentType("application/json", "application/xml"))
require.ElementsMatch(t, arr, e.requestContentTypes)
})
t.Run("ensure applied to route", func(t *testing.T) {
s := NewServer(WithEngineOptions(
WithRequestContentType("application/json", "application/xml")),
)
route := Post(s, "/test", dummyController)
content := route.Operation.RequestBody.Value.Content
require.NotNil(t, content["application/json"])
assert.Equal(t, "#/components/schemas/ReqBody", content["application/json"].Schema.Ref)
require.NotNil(t, content["application/xml"])
assert.Equal(t, "#/components/schemas/ReqBody", content["application/xml"].Schema.Ref)
require.Nil(t, content["application/x-yaml"])
_, ok := s.OpenAPI.Description().Components.RequestBodies["ReqBody"]
require.False(t, ok)
})
}