Repository navigation
Expand file tree
/
Copy pathroutes_test.go
More file actions
264 lines (236 loc) · 7.11 KB
/
Copy pathroutes_test.go
File metadata and controls
264 lines (236 loc) · 7.11 KB
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
// Copyright 2015 go-swagger maintainers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package scan
import (
goparser "go/parser"
"log"
"testing"
"github.com/go-openapi/spec"
"github.com/stretchr/testify/assert"
)
func TestRouteExpression(t *testing.T) {
assert.Regexp(t, rxRoute, "swagger:route DELETE /orders/{id} deleteOrder")
assert.Regexp(t, rxRoute, "swagger:route GET /v1.2/something deleteOrder")
}
func TestRoutesParser(t *testing.T) {
docFile := "../fixtures/goparsing/classification/operations/todo_operation.go"
fileTree, err := goparser.ParseFile(classificationProg.Fset, docFile, nil, goparser.ParseComments)
if err != nil {
log.Fatal(err)
}
rp := newRoutesParser(classificationProg)
var ops spec.Paths
err = rp.Parse(fileTree, &ops)
assert.NoError(t, err)
assert.Len(t, ops.Paths, 3)
po, ok := ops.Paths["/pets"]
assert.True(t, ok)
assert.NotNil(t, po.Get)
assertOperation(t,
po.Get,
"listPets",
"Lists pets filtered by some parameters.",
"This will show all available pets by default.\nYou can get the pets that are out of stock",
[]string{"pets", "users"},
[]string{"read", "write"},
)
assertOperation(t,
po.Post,
"createPet",
"Create a pet based on the parameters.",
"",
[]string{"pets", "users"},
[]string{"read", "write"},
)
po, ok = ops.Paths["/orders"]
assert.True(t, ok)
assert.NotNil(t, po.Get)
assertOperation(t,
po.Get,
"listOrders",
"lists orders filtered by some parameters.",
"",
[]string{"orders"},
[]string{"orders:read", "https://www.googleapis.com/auth/userinfo.email"},
)
assertOperation(t,
po.Post,
"createOrder",
"create an order based on the parameters.",
"",
[]string{"orders"},
[]string{"read", "write"},
)
po, ok = ops.Paths["/orders/{id}"]
assert.True(t, ok)
assert.NotNil(t, po.Get)
assertOperation(t,
po.Get,
"orderDetails",
"gets the details for an order.",
"",
[]string{"orders"},
[]string{"read", "write"},
)
assertOperation(t,
po.Put,
"updateOrder",
"Update the details for an order.",
"When the order doesn't exist this will return an error.",
[]string{"orders"},
[]string{"read", "write"},
)
assertOperation(t,
po.Delete,
"deleteOrder",
"delete a particular order.",
"",
nil,
[]string{"read", "write"},
)
}
func TestRoutesParserBody(t *testing.T) {
docFile := "../fixtures/goparsing/classification/operations_body/todo_operation_body.go"
fileTree, err := goparser.ParseFile(classificationProg.Fset, docFile, nil, goparser.ParseComments)
if err != nil {
log.Fatal(err)
}
rp := newRoutesParser(classificationProg)
var ops spec.Paths
err = rp.Parse(fileTree, &ops)
assert.NoError(t, err)
assert.Len(t, ops.Paths, 3)
po, ok := ops.Paths["/pets"]
assert.True(t, ok)
assert.NotNil(t, po.Get)
assertOperationBody(t,
po.Get,
"listPets",
"Lists pets filtered by some parameters.",
"This will show all available pets by default.\nYou can get the pets that are out of stock",
[]string{"pets", "users"},
[]string{"read", "write"},
)
assertOperationBody(t,
po.Post,
"createPet",
"Create a pet based on the parameters.",
"",
[]string{"pets", "users"},
[]string{"read", "write"},
)
po, ok = ops.Paths["/orders"]
assert.True(t, ok)
assert.NotNil(t, po.Get)
assertOperationBody(t,
po.Get,
"listOrders",
"lists orders filtered by some parameters.",
"",
[]string{"orders"},
[]string{"orders:read", "https://www.googleapis.com/auth/userinfo.email"},
)
assertOperationBody(t,
po.Post,
"createOrder",
"create an order based on the parameters.",
"",
[]string{"orders"},
[]string{"read", "write"},
)
po, ok = ops.Paths["/orders/{id}"]
assert.True(t, ok)
assert.NotNil(t, po.Get)
assertOperationBody(t,
po.Get,
"orderDetails",
"gets the details for an order.",
"",
[]string{"orders"},
[]string{"read", "write"},
)
assertOperationBody(t,
po.Put,
"updateOrder",
"Update the details for an order.",
"When the order doesn't exist this will return an error.",
[]string{"orders"},
[]string{"read", "write"},
)
assertOperationBody(t,
po.Delete,
"deleteOrder",
"delete a particular order.",
"",
nil,
[]string{"read", "write"},
)
}
func assertOperation(t *testing.T, op *spec.Operation, id, summary, description string, tags, scopes []string) {
assert.NotNil(t, op)
assert.Equal(t, summary, op.Summary)
assert.Equal(t, description, op.Description)
assert.Equal(t, id, op.ID)
assert.EqualValues(t, tags, op.Tags)
assert.EqualValues(t, []string{"application/json", "application/x-protobuf"}, op.Consumes)
assert.EqualValues(t, []string{"application/json", "application/x-protobuf"}, op.Produces)
assert.EqualValues(t, []string{"http", "https", "ws", "wss"}, op.Schemes)
assert.Len(t, op.Security, 2)
akv, ok := op.Security[0]["api_key"]
assert.True(t, ok)
// akv must be defined & not empty
assert.NotNil(t, akv)
assert.Empty(t, akv)
vv, ok := op.Security[1]["oauth"]
assert.True(t, ok)
assert.EqualValues(t, scopes, vv)
assert.NotNil(t, op.Responses.Default)
assert.Equal(t, "#/responses/genericError", op.Responses.Default.Ref.String())
rsp, ok := op.Responses.StatusCodeResponses[200]
assert.True(t, ok)
assert.Equal(t, "#/responses/someResponse", rsp.Ref.String())
rsp, ok = op.Responses.StatusCodeResponses[422]
assert.True(t, ok)
assert.Equal(t, "#/responses/validationError", rsp.Ref.String())
}
func assertOperationBody(t *testing.T, op *spec.Operation, id, summary, description string, tags, scopes []string) {
assert.NotNil(t, op)
assert.Equal(t, summary, op.Summary)
assert.Equal(t, description, op.Description)
assert.Equal(t, id, op.ID)
assert.EqualValues(t, tags, op.Tags)
assert.EqualValues(t, []string{"application/json", "application/x-protobuf"}, op.Consumes)
assert.EqualValues(t, []string{"application/json", "application/x-protobuf"}, op.Produces)
assert.EqualValues(t, []string{"http", "https", "ws", "wss"}, op.Schemes)
assert.Len(t, op.Security, 2)
akv, ok := op.Security[0]["api_key"]
assert.True(t, ok)
// akv must be defined & not empty
assert.NotNil(t, akv)
assert.Empty(t, akv)
vv, ok := op.Security[1]["oauth"]
assert.True(t, ok)
assert.EqualValues(t, scopes, vv)
assert.NotNil(t, op.Responses.Default)
assert.Equal(t, "", op.Responses.Default.Ref.String())
assert.Equal(t, "#/definitions/genericError", op.Responses.Default.Schema.Ref.String())
rsp, ok := op.Responses.StatusCodeResponses[200]
assert.True(t, ok)
assert.Equal(t, "", rsp.Ref.String())
assert.Equal(t, "#/definitions/someResponse", rsp.Schema.Ref.String())
rsp, ok = op.Responses.StatusCodeResponses[422]
assert.True(t, ok)
assert.Equal(t, "", rsp.Ref.String())
assert.Equal(t, "#/definitions/validationError", rsp.Schema.Ref.String())
}