-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathswagger_test.go
More file actions
267 lines (246 loc) · 7.22 KB
/
Copy pathswagger_test.go
File metadata and controls
267 lines (246 loc) · 7.22 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
265
266
267
package swagger_test
import (
"strings"
"testing"
"github.com/speakeasy-api/openapi/swagger"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestUnmarshal_Success(t *testing.T) {
t.Parallel()
tests := []struct {
name string
yaml string
}{
{
name: "minimal valid swagger document",
yaml: `swagger: "2.0"
info:
title: Test API
version: 1.0.0
paths: {}`,
},
{
name: "swagger with host and basePath",
yaml: `swagger: "2.0"
info:
title: Test API
version: 1.0.0
host: api.example.com
basePath: /v1
paths: {}`,
},
{
name: "swagger with schemes and consumes/produces",
yaml: `swagger: "2.0"
info:
title: Test API
version: 1.0.0
schemes:
- https
- http
consumes:
- application/json
produces:
- application/json
paths: {}`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
ctx := t.Context()
doc, validationErrs, err := swagger.Unmarshal(ctx, strings.NewReader(tt.yaml))
require.NoError(t, err, "unmarshal should succeed")
require.Empty(t, validationErrs, "should have no validation errors")
require.NotNil(t, doc, "document should not be nil")
assert.Equal(t, "2.0", doc.Swagger, "swagger version should be 2.0")
})
}
}
func TestUnmarshal_ValidationErrors(t *testing.T) {
t.Parallel()
tests := []struct {
name string
yaml string
expectedError string
}{
{
name: "missing swagger field",
yaml: `info:
title: Test API
version: 1.0.0
paths: {}`,
expectedError: "`swagger.swagger` is required",
},
{
name: "missing info field",
yaml: `swagger: "2.0"
paths: {}`,
expectedError: "`swagger.info` is required",
},
{
name: "missing paths field",
yaml: `swagger: "2.0"
info:
title: Test API
version: 1.0.0`,
expectedError: "`swagger.paths` is required",
},
{
name: "missing info.title",
yaml: `swagger: "2.0"
info:
version: 1.0.0
paths: {}`,
expectedError: "`info.title` is required",
},
{
name: "missing info.version",
yaml: `swagger: "2.0"
info:
title: Test API
paths: {}`,
expectedError: "`info.version` is required",
},
{
name: "invalid swagger version",
yaml: `swagger: "3.0"
info:
title: Test API
version: 1.0.0
paths: {}`,
expectedError: "swagger must be '2.0'",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
ctx := t.Context()
doc, validationErrs, err := swagger.Unmarshal(ctx, strings.NewReader(tt.yaml))
require.NoError(t, err, "unmarshal should not return error")
require.NotNil(t, doc, "document should not be nil")
require.NotEmpty(t, validationErrs, "should have validation errors")
found := false
var allErrors []string
for _, verr := range validationErrs {
allErrors = append(allErrors, verr.Error())
if strings.Contains(verr.Error(), tt.expectedError) {
found = true
break
}
}
assert.True(t, found, "should contain expected error: %s\nGot errors: %v", tt.expectedError, allErrors)
})
}
}
func TestMarshal_Success(t *testing.T) {
t.Parallel()
ctx := t.Context()
doc := &swagger.Swagger{
Swagger: swagger.Version,
Info: swagger.Info{
Title: "Test API",
Version: "1.0.0",
},
Paths: swagger.NewPaths(),
}
var buf strings.Builder
err := swagger.Marshal(ctx, doc, &buf)
require.NoError(t, err, "marshal should succeed")
expected := `swagger: "2.0"
info:
title: Test API
version: 1.0.0
paths: {}
`
assert.Equal(t, expected, buf.String(), "marshaled output should match expected YAML")
}
func TestSwagger_Getters_Success(t *testing.T) {
t.Parallel()
ctx := t.Context()
yml := `swagger: "2.0"
info:
title: Test API
version: 1.0.0
host: api.example.com
basePath: /v1
schemes:
- https
consumes:
- application/json
produces:
- application/xml
paths:
/users:
get:
responses:
"200":
description: Success
definitions:
User:
type: object
parameters:
limitParam:
name: limit
in: query
type: integer
responses:
NotFound:
description: Not found
securityDefinitions:
api_key:
type: apiKey
name: X-API-Key
in: header
security:
- api_key: []
tags:
- name: users
description: User operations
externalDocs:
description: External docs
url: https://example.com/docs
x-custom: value
`
doc, validationErrs, err := swagger.Unmarshal(ctx, strings.NewReader(yml))
require.NoError(t, err, "unmarshal should succeed")
require.Empty(t, validationErrs, "should have no validation errors")
assert.Equal(t, "2.0", doc.GetSwagger(), "GetSwagger should return 2.0")
assert.NotNil(t, doc.GetInfo(), "GetInfo should return non-nil Info")
assert.Equal(t, "Test API", doc.GetInfo().Title, "GetInfo should return correct Info")
assert.Equal(t, "api.example.com", doc.GetHost(), "GetHost should return correct value")
assert.Equal(t, "/v1", doc.GetBasePath(), "GetBasePath should return correct value")
assert.Equal(t, []string{"https"}, doc.GetSchemes(), "GetSchemes should return correct value")
assert.Equal(t, []string{"application/json"}, doc.GetConsumes(), "GetConsumes should return correct value")
assert.Equal(t, []string{"application/xml"}, doc.GetProduces(), "GetProduces should return correct value")
assert.NotNil(t, doc.GetPaths(), "GetPaths should return non-nil")
assert.NotNil(t, doc.GetDefinitions(), "GetDefinitions should return non-nil")
assert.NotNil(t, doc.GetParameters(), "GetParameters should return non-nil")
assert.NotNil(t, doc.GetResponses(), "GetResponses should return non-nil")
assert.NotNil(t, doc.GetSecurityDefinitions(), "GetSecurityDefinitions should return non-nil")
assert.NotNil(t, doc.GetSecurity(), "GetSecurity should return non-nil")
assert.NotNil(t, doc.GetTags(), "GetTags should return non-nil")
assert.NotNil(t, doc.GetExternalDocs(), "GetExternalDocs should return non-nil")
assert.NotNil(t, doc.GetExtensions(), "GetExtensions should return non-nil")
}
func TestSwagger_Getters_Nil(t *testing.T) {
t.Parallel()
var doc *swagger.Swagger
assert.Empty(t, doc.GetSwagger(), "GetSwagger should return empty string for nil")
assert.Nil(t, doc.GetInfo(), "GetInfo should return nil for nil doc")
assert.Empty(t, doc.GetHost(), "GetHost should return empty string for nil")
assert.Empty(t, doc.GetBasePath(), "GetBasePath should return empty string for nil")
assert.Nil(t, doc.GetSchemes(), "GetSchemes should return nil for nil doc")
assert.Nil(t, doc.GetConsumes(), "GetConsumes should return nil for nil doc")
assert.Nil(t, doc.GetProduces(), "GetProduces should return nil for nil doc")
assert.Nil(t, doc.GetPaths(), "GetPaths should return nil for nil doc")
assert.Nil(t, doc.GetDefinitions(), "GetDefinitions should return nil for nil doc")
assert.Nil(t, doc.GetParameters(), "GetParameters should return nil for nil doc")
assert.Nil(t, doc.GetResponses(), "GetResponses should return nil for nil doc")
assert.Nil(t, doc.GetSecurityDefinitions(), "GetSecurityDefinitions should return nil for nil doc")
assert.Nil(t, doc.GetSecurity(), "GetSecurity should return nil for nil doc")
assert.Nil(t, doc.GetTags(), "GetTags should return nil for nil doc")
assert.Nil(t, doc.GetExternalDocs(), "GetExternalDocs should return nil for nil doc")
assert.NotNil(t, doc.GetExtensions(), "GetExtensions should return empty extensions for nil doc")
}