1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package spec
16
17 import (
18 "encoding/json"
19 "testing"
20
21 "github.com/stretchr/testify/assert"
22 "github.com/stretchr/testify/require"
23 )
24
25 var spec = Swagger{
26 SwaggerProps: SwaggerProps{
27 ID: "http://localhost:3849/api-docs",
28 Swagger: "2.0",
29 Consumes: []string{"application/json", "application/x-yaml"},
30 Produces: []string{"application/json"},
31 Schemes: []string{"http", "https"},
32 Info: &info,
33 Host: "some.api.out.there",
34 BasePath: "/",
35 Paths: &paths,
36 Definitions: map[string]Schema{"Category": {SchemaProps: SchemaProps{Type: []string{"string"}}}},
37 Parameters: map[string]Parameter{
38 "categoryParam": {ParamProps: ParamProps{Name: "category", In: "query"}, SimpleSchema: SimpleSchema{Type: "string"}},
39 },
40 Responses: map[string]Response{
41 "EmptyAnswer": {
42 ResponseProps: ResponseProps{
43 Description: "no data to return for this operation",
44 },
45 },
46 },
47 SecurityDefinitions: map[string]*SecurityScheme{
48 "internalApiKey": APIKeyAuth("api_key", "header"),
49 },
50 Security: []map[string][]string{
51 {"internalApiKey": {}},
52 },
53 Tags: []Tag{NewTag("pets", "", nil)},
54 ExternalDocs: &ExternalDocumentation{Description: "the name", URL: "the url"},
55 },
56 VendorExtensible: VendorExtensible{Extensions: map[string]interface{}{
57 "x-some-extension": "vendor",
58 "x-schemes": []interface{}{"unix", "amqp"},
59 }},
60 }
61
62 const specJSON = `{
63 "id": "http://localhost:3849/api-docs",
64 "consumes": ["application/json", "application/x-yaml"],
65 "produces": ["application/json"],
66 "schemes": ["http", "https"],
67 "swagger": "2.0",
68 "info": {
69 "contact": {
70 "name": "wordnik api team",
71 "url": "http://developer.wordnik.com"
72 },
73 "description": "A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0` +
74 ` specification",
75 "license": {
76 "name": "Creative Commons 4.0 International",
77 "url": "http://creativecommons.org/licenses/by/4.0/"
78 },
79 "termsOfService": "http://helloreverb.com/terms/",
80 "title": "Swagger Sample API",
81 "version": "1.0.9-abcd",
82 "x-framework": "go-swagger"
83 },
84 "host": "some.api.out.there",
85 "basePath": "/",
86 "paths": {"x-framework":"go-swagger","/":{"$ref":"cats"}},
87 "definitions": { "Category": { "type": "string"} },
88 "parameters": {
89 "categoryParam": {
90 "name": "category",
91 "in": "query",
92 "type": "string"
93 }
94 },
95 "responses": { "EmptyAnswer": { "description": "no data to return for this operation" } },
96 "securityDefinitions": {
97 "internalApiKey": {
98 "type": "apiKey",
99 "in": "header",
100 "name": "api_key"
101 }
102 },
103 "security": [{"internalApiKey":[]}],
104 "tags": [{"name":"pets"}],
105 "externalDocs": {"description":"the name","url":"the url"},
106 "x-some-extension": "vendor",
107 "x-schemes": ["unix","amqp"]
108 }`
109
110
111
112
113
114
115
116
117
118
119
120
121
208
209 func assertSpecs(t testing.TB, actual, expected Swagger) bool {
210 expected.Swagger = "2.0"
211 return assert.Equal(t, expected, actual)
212 }
213
214
238
239 func TestSwaggerSpec_Serialize(t *testing.T) {
240 expected := make(map[string]interface{})
241 _ = json.Unmarshal([]byte(specJSON), &expected)
242 b, err := json.MarshalIndent(spec, "", " ")
243 require.NoError(t, err)
244 var actual map[string]interface{}
245 require.NoError(t, json.Unmarshal(b, &actual))
246 assert.EqualValues(t, expected, actual)
247 }
248
249 func TestSwaggerSpec_Deserialize(t *testing.T) {
250 var actual Swagger
251 require.NoError(t, json.Unmarshal([]byte(specJSON), &actual))
252 assert.EqualValues(t, actual, spec)
253 }
254
255 func TestVendorExtensionStringSlice(t *testing.T) {
256 var actual Swagger
257 require.NoError(t, json.Unmarshal([]byte(specJSON), &actual))
258 schemes, ok := actual.Extensions.GetStringSlice("x-schemes")
259 require.True(t, ok)
260 assert.EqualValues(t, []string{"unix", "amqp"}, schemes)
261
262 notSlice, ok := actual.Extensions.GetStringSlice("x-some-extension")
263 assert.Nil(t, notSlice)
264 assert.False(t, ok)
265
266 actual.AddExtension("x-another-ext", 100)
267 notString, ok := actual.Extensions.GetStringSlice("x-another-ext")
268 assert.Nil(t, notString)
269 assert.False(t, ok)
270
271 actual.AddExtension("x-another-slice-ext", []interface{}{100, 100})
272 notStringSlice, ok := actual.Extensions.GetStringSlice("x-another-slice-ext")
273 assert.Nil(t, notStringSlice)
274 assert.False(t, ok)
275
276 _, ok = actual.Extensions.GetStringSlice("x-notfound-ext")
277 assert.False(t, ok)
278 }
279
280 func TestOptionalSwaggerProps_Serialize(t *testing.T) {
281 minimalJSONSpec := []byte(`{
282 "swagger": "2.0",
283 "info": {
284 "version": "0.0.0",
285 "title": "Simple API"
286 },
287 "paths": {
288 "/": {
289 "get": {
290 "responses": {
291 "200": {
292 "description": "OK"
293 }
294 }
295 }
296 }
297 }
298 }`)
299
300 var minimalSpec Swagger
301 err := json.Unmarshal(minimalJSONSpec, &minimalSpec)
302 require.NoError(t, err)
303 bytes, err := json.Marshal(&minimalSpec)
304 require.NoError(t, err)
305
306 var ms map[string]interface{}
307 require.NoError(t, json.Unmarshal(bytes, &ms))
308
309 assert.NotContains(t, ms, "consumes")
310 assert.NotContains(t, ms, "produces")
311 assert.NotContains(t, ms, "schemes")
312 assert.NotContains(t, ms, "host")
313 assert.NotContains(t, ms, "basePath")
314 assert.NotContains(t, ms, "definitions")
315 assert.NotContains(t, ms, "parameters")
316 assert.NotContains(t, ms, "responses")
317 assert.NotContains(t, ms, "securityDefinitions")
318 assert.NotContains(t, ms, "security")
319 assert.NotContains(t, ms, "tags")
320 assert.NotContains(t, ms, "externalDocs")
321 }
322
323 var minimalJSONSpec = []byte(`{
324 "swagger": "2.0",
325 "info": {
326 "version": "0.0.0",
327 "title": "Simple API"
328 },
329 "securityDefinitions": {
330 "basic": {
331 "type": "basic"
332 },
333 "apiKey": {
334 "type": "apiKey",
335 "in": "header",
336 "name": "X-API-KEY"
337 },
338 "queryKey": {
339 "type": "apiKey",
340 "in": "query",
341 "name": "api_key"
342 }
343 },
344 "paths": {
345 "/": {
346 "get": {
347 "security": [
348 {
349 "apiKey": [],
350 "basic": []
351 },
352 {},
353 {
354 "queryKey": [],
355 "basic": []
356 }
357 ],
358 "responses": {
359 "200": {
360 "description": "OK"
361 }
362 }
363 }
364 }
365 }
366 }`)
367
368 func TestSecurityRequirements(t *testing.T) {
369 var minimalSpec Swagger
370 require.NoError(t, json.Unmarshal(minimalJSONSpec, &minimalSpec))
371
372 sec := minimalSpec.Paths.Paths["/"].Get.Security
373 require.Len(t, sec, 3)
374 assert.Contains(t, sec[0], "basic")
375 assert.Contains(t, sec[0], "apiKey")
376 assert.NotNil(t, sec[1])
377 assert.Empty(t, sec[1])
378 assert.Contains(t, sec[2], "queryKey")
379 }
380
381 func TestSwaggerGobEncoding(t *testing.T) {
382 doTestSwaggerGobEncoding(t, specJSON)
383
384 doTestSwaggerGobEncoding(t, string(minimalJSONSpec))
385 }
386
387 func doTestSwaggerGobEncoding(t *testing.T, fixture string) {
388 var src, dst Swagger
389 require.NoError(t, json.Unmarshal([]byte(fixture), &src))
390
391 doTestAnyGobEncoding(t, &src, &dst)
392 }
393
View as plain text