forked from go-fuego/fuego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
openapi_operations_test.go
149 lines (122 loc) · 5.1 KB
/
openapi_operations_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
package fuego
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestTags(t *testing.T) {
s := NewServer()
route := Get(s, "/test", testController).
Tags("my-tag").
Description("my description").
Summary("my summary").
Deprecated()
require.Equal(t, route.Operation.Tags, []string{"my-tag"})
require.Equal(t, route.Operation.Description, "my description")
require.Equal(t, route.Operation.Summary, "my summary")
require.Equal(t, route.Operation.Deprecated, true)
}
func TestAddTags(t *testing.T) {
s := NewServer()
route := Get(s, "/test", func(ctx *ContextNoBody) (string, error) {
return "test", nil
}).
AddTags("my-tag").
AddTags("my-other-tag")
require.Equal(t, route.Operation.Tags, []string{"my-tag", "my-other-tag"})
}
func TestRemoveTags(t *testing.T) {
s := NewServer()
route := Get(s, "/test", func(ctx *ContextNoBody) (string, error) {
return "test", nil
}).
AddTags("my-tag").
RemoveTags("my-tag", "string").
AddTags("my-other-tag")
require.Equal(t, route.Operation.Tags, []string{"my-other-tag"})
}
func TestQueryParams(t *testing.T) {
s := NewServer()
route := Get(s, "/test", func(ctx *ContextNoBody) (string, error) {
return "test", nil
}).
QueryParam("my-param", "my description")
require.Equal(t, "my description", route.Operation.Parameters.GetByInAndName("query", "my-param").Description)
}
func TestHeaderParams(t *testing.T) {
s := NewServer()
route := Get(s, "/test", testController).
Header("my-header", "my description")
require.Equal(t, "my description", route.Operation.Parameters.GetByInAndName("header", "my-header").Description)
}
func TestRequestContentType(t *testing.T) {
t.Run("base", func(t *testing.T) {
s := NewServer()
route := Post(s, "/test", testControllerWithBody).
RequestContentType("application/json")
content := route.Operation.RequestBody.Value.Content
require.NotNil(t, content.Get("application/json"))
require.Nil(t, content.Get("application/xml"))
require.Equal(t, "#/components/schemas/TestRequestBody", content.Get("application/json").Schema.Ref)
_, ok := s.OpenApiSpec.Components.RequestBodies["TestRequestBody"]
require.True(t, ok)
})
t.Run("variadic", func(t *testing.T) {
s := NewServer()
route := Post(s, "/test", testControllerWithBody).
RequestContentType("application/json", "my/content-type")
content := route.Operation.RequestBody.Value.Content
require.NotNil(t, content.Get("application/json"))
require.NotNil(t, content.Get("my/content-type"))
require.Nil(t, content.Get("application/xml"))
require.Equal(t, "#/components/schemas/TestRequestBody", content.Get("application/json").Schema.Ref)
_, ok := s.OpenApiSpec.Components.RequestBodies["TestRequestBody"]
require.True(t, ok)
})
}
func TestCustomError(t *testing.T) {
type MyError struct {
Message string
}
s := NewServer()
route := Get(s, "/test", testController).
AddError(400, "My Validation Error", MyError{})
require.Equal(t, "My Validation Error", *route.Operation.Responses.Map()["400"].Value.Description)
}
func TestCustomErrorGlobalAndOnRoute(t *testing.T) {
type MyGlobalError struct {
Message string
}
s := NewServer(
WithGlobalResponseTypes(400, "My Global Error", MyGlobalError{}),
WithGlobalResponseTypes(501, "Another Global Error", MyGlobalError{}),
)
type MyLocalError struct {
Message string
}
routeGlobal := Get(s, "/test-global", testController)
routeCustom := Get(s, "/test-custom", testController).
AddError(400, "My Local Error", MyLocalError{}).
AddError(419, "My Local Teapot")
require.Equal(t, "My Global Error", *routeGlobal.Operation.Responses.Map()["400"].Value.Description, "Overrides Fuego's default 400 error")
require.Equal(t, "Another Global Error", *routeGlobal.Operation.Responses.Map()["501"].Value.Description)
require.Equal(t, "My Local Error", *routeCustom.Operation.Responses.Map()["400"].Value.Description, "Local error overrides global error")
require.Equal(t, "My Local Teapot", *routeCustom.Operation.Responses.Map()["419"].Value.Description)
require.Equal(t, "Internal Server Error _(panics)_", *routeCustom.Operation.Responses.Map()["500"].Value.Description, "Global error set by default by Fuego")
require.Equal(t, "Another Global Error", *routeCustom.Operation.Responses.Map()["501"].Value.Description, "Global error is still available")
}
func TestCookieParams(t *testing.T) {
t.Run("basic cookie", func(t *testing.T) {
s := NewServer()
route := Get(s, "/test", testController).
Cookie("my-cookie", "my description")
require.Equal(t, "my description", route.Operation.Parameters.GetByInAndName("cookie", "my-cookie").Description)
})
t.Run("with more parameters", func(t *testing.T) {
s := NewServer()
route := Get(s, "/test", testController).
Cookie("my-cookie", "my description", OpenAPIParamOption{Required: true, Example: "my-example"})
require.Equal(t, "my description", route.Operation.Parameters.GetByInAndName("cookie", "my-cookie").Description)
require.Equal(t, true, route.Operation.Parameters.GetByInAndName("cookie", "my-cookie").Required)
require.Equal(t, "my-example", route.Operation.Parameters.GetByInAndName("cookie", "my-cookie").Example)
})
}