forked from emicklei/go-restful-openapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_path_test.go
234 lines (204 loc) · 7.66 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
package restfulspec
import (
"testing"
restful "github.com/emicklei/go-restful"
"github.com/go-openapi/spec"
)
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).
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{}).
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 p.Paths["/tests/{v}/a/{b}"].Get.Parameters[0].Type != "string" {
t.Error("Parameter type is 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")
}
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 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))
}
}
}