-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcomponents_test.go
248 lines (242 loc) · 5.68 KB
/
components_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
package openapi_test
import (
"reflect"
"strconv"
"testing"
openapi "github.com/nasa9084/go-openapi"
yaml "gopkg.in/yaml.v2"
)
func TestComponents_Validate(t *testing.T) {
candidates := []candidate{
{"empty", openapi.Components{}, nil},
}
testValidater(t, candidates)
}
func TestComponents_ValidateComponentKeys(t *testing.T) {
candidates := []struct {
label string
in openapi.Components
err error
}{
{"empty", openapi.Components{}, nil},
{"invalidKey", openapi.Components{Parameters: map[string]*openapi.Parameter{"@": &openapi.Parameter{}}}, openapi.ErrMapKeyFormat},
{"validKey", openapi.Components{Parameters: map[string]*openapi.Parameter{"foo": &openapi.Parameter{}}}, nil},
}
for i, c := range candidates {
t.Run(strconv.Itoa(i)+"/"+c.label, func(t *testing.T) {
if err := openapi.ValidateComponentKeys(c.in); err != c.err {
t.Errorf("error should be %s, but %s", c.err, err)
}
})
}
}
func TestReduceComponentKeys(t *testing.T) {
candidates := []struct {
label string
in openapi.Components
expected []string
}{
{"empty", openapi.Components{}, []string{}},
}
for i, c := range candidates {
t.Run(strconv.Itoa(i)+"/"+c.label, func(t *testing.T) {
keys := openapi.ReduceComponentKeys(c.in)
if !reflect.DeepEqual(keys, c.expected) {
t.Errorf("%+v != %+v", keys, c.expected)
}
})
}
}
func TestReduceComponentObjects(t *testing.T) {
candidates := []struct {
label string
in openapi.Components
expected []openapi.Validater
}{
{"empty", openapi.Components{}, []openapi.Validater{}},
}
for i, c := range candidates {
t.Run(strconv.Itoa(i)+"/"+c.label, func(t *testing.T) {
objects := openapi.ReduceComponentObjects(c.in)
if !reflect.DeepEqual(objects, c.expected) {
t.Errorf("%+v != %+v", objects, c.expected)
}
})
}
}
func TestComponentsByExample(t *testing.T) {
example := `schemas:
GeneralError:
type: object
properties:
code:
type: integer
format: int32
message:
type: string
Category:
type: object
properties:
id:
type: integer
format: int64
name:
type: string
Tag:
type: object
properties:
id:
type: integer
format: int64
name:
type: string
parameters:
skipParam:
name: skip
in: query
description: number of items to skip
required: true
schema:
type: integer
format: int32
limitParam:
name: limit
in: query
description: max records to return
required: true
schema:
type: integer
format: int32
responses:
NotFound:
description: Entity not found.
IllegalInput:
description: Illegal input for operation.
GeneralError:
description: General Error
content:
application/json:
schema:
$ref: '#/components/schemas/GeneralError'
securitySchemes:
api_key:
type: apiKey
name: api_key
in: header
petstore_auth:
type: oauth2
flows:
implicit:
authorizationUrl: http://example.org/api/oauth/dialog
scopes:
write:pets: modify pets in your account
read:pets: read your pets`
var components openapi.Components
if err := yaml.Unmarshal([]byte(example), &components); err != nil {
t.Error(err)
return
}
expect := openapi.Components{
Schemas: map[string]*openapi.Schema{
"GeneralError": &openapi.Schema{
Type: "object",
Properties: map[string]*openapi.Schema{
"code": &openapi.Schema{
Type: "integer",
Format: "int32",
},
"message": &openapi.Schema{
Type: "string",
},
},
},
"Category": &openapi.Schema{
Type: "object",
Properties: map[string]*openapi.Schema{
"id": &openapi.Schema{
Type: "integer",
Format: "int64",
},
"name": &openapi.Schema{
Type: "string",
},
},
},
"Tag": &openapi.Schema{
Type: "object",
Properties: map[string]*openapi.Schema{
"id": &openapi.Schema{
Type: "integer",
Format: "int64",
},
"name": &openapi.Schema{
Type: "string",
},
},
},
},
Parameters: map[string]*openapi.Parameter{
"skipParam": &openapi.Parameter{
Name: "skip",
In: "query",
Description: "number of items to skip",
Required: true,
Schema: &openapi.Schema{
Type: "integer",
Format: "int32",
},
},
"limitParam": &openapi.Parameter{
Name: "limit",
In: "query",
Description: "max records to return",
Required: true,
Schema: &openapi.Schema{
Type: "integer",
Format: "int32",
},
},
},
Responses: openapi.Responses{
"NotFound": &openapi.Response{
Description: "Entity not found.",
},
"IllegalInput": &openapi.Response{
Description: "Illegal input for operation.",
},
"GeneralError": &openapi.Response{
Description: "General Error",
Content: map[string]*openapi.MediaType{
"application/json": &openapi.MediaType{
Schema: &openapi.Schema{
Ref: "#/components/schemas/GeneralError",
},
},
},
},
},
SecuritySchemes: map[string]*openapi.SecurityScheme{
"api_key": &openapi.SecurityScheme{
Type: "apiKey",
Name: "api_key",
In: "header",
},
"petstore_auth": &openapi.SecurityScheme{
Type: "oauth2",
Flows: &openapi.OAuthFlows{
Implicit: &openapi.OAuthFlow{
AuthorizationURL: "http://example.org/api/oauth/dialog",
Scopes: map[string]string{
"write:pets": "modify pets in your account",
"read:pets": "read your pets",
},
},
},
},
},
}
if !reflect.DeepEqual(components, expect) {
t.Errorf("%+v != %+v", components, expect)
return
}
}