This repository has been archived by the owner on Mar 15, 2024. It is now read-only.
forked from emicklei/go-restful-openapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_path_test.go
304 lines (266 loc) · 10.4 KB
/
build_path_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
package restfulspec
import (
"testing"
"github.com/go-openapi/spec"
"github.com/emicklei/go-restful/v3"
)
func TestRouteToPath(t *testing.T) {
description := "get the <strong>a</strong> <em>b</em> test\nthis is the test description"
notes := "notes\nblah blah"
ws := new(restful.WebService)
ws.Path("/tests/{v}")
ws.Param(ws.PathParameter("v", "value of v").DefaultValue("default-v"))
ws.Consumes(restful.MIME_JSON)
ws.Produces(restful.MIME_XML)
ws.Route(ws.GET("/a/{b}").To(dummy).
Doc(description).
Metadata(KeyOpenAPITags, []string{"tests"}).
AddExtension("x-restful-test", "test value").
Notes(notes).
Param(ws.PathParameter("b", "value of b").DefaultValue("default-b")).
Param(ws.QueryParameter("q", "value of q").DefaultValue("default-q")).
Returns(200, "list of a b tests", []Sample{}).
DefaultReturns("default", Sample{}).
Writes([]Sample{}))
ws.Route(ws.GET("/a/{b}/{c:[a-z]+}/{d:[1-9]+}/e").To(dummy).
Param(ws.PathParameter("b", "value of b").DefaultValue("default-b")).
Param(ws.PathParameter("c", "with regex").DefaultValue("abc")).
Param(ws.PathParameter("d", "with regex").DefaultValue("abcef")).
Param(ws.QueryParameter("q", "value of q").DataType("string").DataFormat("date").
DefaultValue("default-q").AllowMultiple(true)).
Returns(200, "list of a b tests", []Sample{}).
Writes([]Sample{}))
p := buildPaths(ws, Config{})
t.Log(asJSON(p))
if op := p.Paths["/tests/{v}/a/{b}"].Get; op.Parameters[0].Type != "string" {
t.Error("Parameter type is not set.")
} else if op.Extensions["x-restful-test"] != "test value" {
t.Error("Extensions not set.")
} else if len(op.Tags) != 1 || op.Tags[0] != "tests" {
t.Error("Metadata/openapi.tags not set.")
}
if _, exists := p.Paths["/tests/{v}/a/{b}/{c}/{d}/e"]; !exists {
t.Error("Expected path to exist after it was sanitized.")
}
q, exists := getParameter(p.Paths["/tests/{v}/a/{b}/{c}/{d}/e"], "q")
if !exists {
t.Errorf("get parameter q failed")
}
if q.Type != "array" || q.Items.Type != "string" || q.Format != "date" {
t.Errorf("parameter q expected to be a date array")
}
if p.Paths["/tests/{v}/a/{b}"].Get.Description != notes {
t.Errorf("GET description incorrect")
}
if p.Paths["/tests/{v}/a/{b}"].Get.Summary != "get the a b test\nthis is the test description" {
t.Errorf("GET summary incorrect")
}
response := p.Paths["/tests/{v}/a/{b}"].Get.Responses.StatusCodeResponses[200]
if response.Schema.Type[0] != "array" {
t.Errorf("response type incorrect")
}
defaultResponse := p.Paths["/tests/{v}/a/{b}"].Get.Responses.Default
if defaultResponse.Description != "default" {
t.Errorf("default response description incorrect")
}
if response.Schema.Items.Schema.Ref.String() != "#/definitions/restfulspec.Sample" {
t.Errorf("response element type incorrect")
}
// Test for patterns
path := p.Paths["/tests/{v}/a/{b}/{c}/{d}/e"]
checkPattern(t, path, "c", "[a-z]+")
checkPattern(t, path, "d", "[1-9]+")
checkPattern(t, path, "v", "")
}
func getParameter(path spec.PathItem, name string) (*spec.Parameter, bool) {
for _, param := range path.Get.Parameters {
if param.Name == name {
return ¶m, true
}
}
return nil, false
}
func checkPattern(t *testing.T, path spec.PathItem, paramName string, pattern string) {
param, exists := getParameter(path, paramName)
if !exists {
t.Errorf("Expected Parameter %s to exist", paramName)
}
if param.Pattern != pattern {
t.Errorf("Expected pattern %s to equal %s", param.Pattern, pattern)
}
}
func TestRouteToPathForAllowableValues(t *testing.T) {
description := "get the <strong>a</strong> <em>b</em> test\nthis is the test description"
notes := "notes\nblah blah"
allowedCheeses := map[string]string{"cheddar": "cheddar", "feta": "feta", "colby-jack": "colby-jack", "mozzerella": "mozzerella"}
ws := new(restful.WebService)
ws.Path("/tests/{v}")
ws.Param(ws.PathParameter("v", "value of v").DefaultValue("default-v"))
ws.Consumes(restful.MIME_JSON)
ws.Produces(restful.MIME_XML)
ws.Route(ws.GET("/a/{cheese}").To(dummy).
Doc(description).
Notes(notes).
Param(ws.PathParameter("cheese", "value of cheese").DefaultValue("cheddar").AllowableValues(allowedCheeses)).
Param(ws.QueryParameter("q", "value of q").DefaultValue("default-q")).
Returns(200, "list of a b tests", []Sample{}).
Writes([]Sample{}))
p := buildPaths(ws, Config{})
t.Log(asJSON(p))
if p.Paths["/tests/{v}/a/{cheese}"].Get.Parameters[0].Type != "string" {
t.Error("Parameter type is not set.")
}
path, exists := p.Paths["/tests/{v}/a/{cheese}"]
if !exists {
t.Error("Expected path to exist after it was sanitized.")
}
if path.Get.Description != notes {
t.Errorf("GET description incorrect")
}
if path.Get.Summary != "get the a b test\nthis is the test description" {
t.Errorf("GET summary incorrect")
}
if len(path.Get.Parameters) != 3 {
t.Errorf("Path expected to have 3 parameters but had %d", len(path.Get.Parameters))
}
if path.Get.Parameters[1].Name != "cheese" || len(path.Get.Parameters[1].Enum) != len(allowedCheeses) {
t.Errorf("Path parameter 'cheese' expected to have enum of allowable values of lenght %d but was of length %d", len(allowedCheeses), len(path.Get.Parameters[1].Enum))
}
for _, cheeseIface := range path.Get.Parameters[1].Enum {
cheese := cheeseIface.(string)
if _, found := allowedCheeses[cheese]; !found {
t.Errorf("Path parameter 'cheese' had enum value of %s which was not found in the allowableValues map", cheese)
}
}
response := path.Get.Responses.StatusCodeResponses[200]
if response.Schema.Type[0] != "array" {
t.Errorf("response type incorrect")
}
if response.Schema.Items.Schema.Ref.String() != "#/definitions/restfulspec.Sample" {
t.Errorf("response element type incorrect")
}
}
func TestMultipleMethodsRouteToPath(t *testing.T) {
ws := new(restful.WebService)
ws.Path("/tests/a")
ws.Consumes(restful.MIME_JSON)
ws.Produces(restful.MIME_XML)
ws.Route(ws.GET("/a/b").To(dummy).
Doc("get a b test").
Returns(200, "list of a b tests", []Sample{}).
Writes([]Sample{}))
ws.Route(ws.POST("/a/b").To(dummy).
Doc("post a b test").
Returns(200, "list of a b tests", []Sample{}).
Returns(500, "internal server error", []Sample{}).
Reads(Sample{}).
Writes([]Sample{}))
p := buildPaths(ws, Config{})
t.Log(asJSON(p))
if p.Paths["/tests/a/a/b"].Get.Summary != "get a b test" {
t.Errorf("GET summary incorrect")
}
if p.Paths["/tests/a/a/b"].Post.Summary != "post a b test" {
t.Errorf("POST summary incorrect")
}
if _, exists := p.Paths["/tests/a/a/b"].Post.Responses.StatusCodeResponses[500]; !exists {
t.Errorf("Response code 500 not added to spec.")
}
expectedRef := spec.MustCreateRef("#/definitions/restfulspec.Sample")
postBodyparam := p.Paths["/tests/a/a/b"].Post.Parameters[0]
postBodyRef := postBodyparam.Schema.Ref
if postBodyRef.String() != expectedRef.String() {
t.Errorf("Expected: %s, Got: %s", expectedRef.String(), postBodyRef.String())
}
if postBodyparam.Format != "" || postBodyparam.Type != "" || postBodyparam.Default != nil {
t.Errorf("Invalid parameter property is set on body property")
}
}
func TestReadArrayObjectInBody(t *testing.T) {
ws := new(restful.WebService)
ws.Path("/tests/a")
ws.Consumes(restful.MIME_JSON)
ws.Produces(restful.MIME_XML)
ws.Route(ws.POST("/a/b").To(dummy).
Doc("post a b test with array in body").
Returns(200, "list of a b tests", []Sample{}).
Returns(500, "internal server error", []Sample{}).
Reads([]Sample{}).
Writes([]Sample{}))
p := buildPaths(ws, Config{})
t.Log(asJSON(p))
postInfo := p.Paths["/tests/a/a/b"].Post
if postInfo.Summary != "post a b test with array in body" {
t.Errorf("POST description incorrect")
}
if _, exists := postInfo.Responses.StatusCodeResponses[500]; !exists {
t.Errorf("Response code 500 not added to spec.")
}
// indentify element model type in body array
expectedItemRef := spec.MustCreateRef("#/definitions/restfulspec.Sample")
postBody := postInfo.Parameters[0]
if postBody.Schema.Ref.String() != "" {
t.Errorf("you shouldn't have body Ref setting when using array in body!")
}
// check body array dy item ref
postBodyitems := postBody.Schema.Items.Schema.Ref
if postBodyitems.String() != expectedItemRef.String() {
t.Errorf("Expected: %s, Got: %s", expectedItemRef.String(), expectedItemRef.String())
}
if postBody.Format != "" || postBody.Type != "" || postBody.Default != nil {
t.Errorf("Invalid parameter property is set on body property")
}
}
// TestWritesPrimitive ensures that if an operation returns a primitive, then it
// is used as such (and not a ref to a definition).
func TestWritesPrimitive(t *testing.T) {
ws := new(restful.WebService)
ws.Path("/tests/returns")
ws.Consumes(restful.MIME_JSON)
ws.Produces(restful.MIME_JSON)
ws.Route(ws.POST("/primitive").To(dummy).
Doc("post that returns a string").
Returns(200, "primitive string", "(this is a string)").
Writes("(this is a string)"))
ws.Route(ws.POST("/custom").To(dummy).
Doc("post that returns a custom structure").
Returns(200, "sample object", Sample{}).
Writes(Sample{}))
p := buildPaths(ws, Config{})
t.Log(asJSON(p))
// Make sure that the operation that returns a primitive type is correct.
if pathInfo, okay := p.Paths["/tests/returns/primitive"]; !okay {
t.Errorf("Could not find path")
} else {
postInfo := pathInfo.Post
if postInfo.Summary != "post that returns a string" {
t.Errorf("POST description incorrect")
}
response := postInfo.Responses.StatusCodeResponses[200]
if response.Schema.Ref.String() != "" {
t.Errorf("Expected no ref; got: %s", response.Schema.Ref.String())
}
if len(response.Schema.Type) != 1 {
t.Errorf("Expected exactly one type; got: %d", len(response.Schema.Type))
}
if response.Schema.Type[0] != "string" {
t.Errorf("Expected a type of string; got: %s", response.Schema.Type[0])
}
}
// Make sure that the operation that returns a custom type is correct.
if pathInfo, okay := p.Paths["/tests/returns/custom"]; !okay {
t.Errorf("Could not find path")
} else {
postInfo := pathInfo.Post
if postInfo.Summary != "post that returns a custom structure" {
t.Errorf("POST description incorrect")
}
response := postInfo.Responses.StatusCodeResponses[200]
if response.Schema.Ref.String() != "#/definitions/restfulspec.Sample" {
t.Errorf("Expected ref '#/definitions/restfulspec.Sample'; got: %s", response.Schema.Ref.String())
}
if len(response.Schema.Type) != 0 {
t.Errorf("Expected exactly zero types; got: %d", len(response.Schema.Type))
}
}
}