|
| 1 | +// Copyright 2017 go-swagger maintainers |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package spec |
| 16 | + |
| 17 | +import ( |
| 18 | + "encoding/json" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/stretchr/testify/assert" |
| 22 | +) |
| 23 | + |
| 24 | +var responses = Responses{ |
| 25 | + VendorExtensible: VendorExtensible{ |
| 26 | + Extensions: map[string]interface{}{ |
| 27 | + "x-go-name": "PutDogExists", |
| 28 | + }, |
| 29 | + }, |
| 30 | + ResponsesProps: ResponsesProps{ |
| 31 | + StatusCodeResponses: map[int]Response{ |
| 32 | + 200: { |
| 33 | + Refable: Refable{Ref: MustCreateRef("Dog")}, |
| 34 | + VendorExtensible: VendorExtensible{ |
| 35 | + Extensions: map[string]interface{}{ |
| 36 | + "x-go-name": "PutDogExists", |
| 37 | + }, |
| 38 | + }, |
| 39 | + ResponseProps: ResponseProps{ |
| 40 | + Description: "Dog exists", |
| 41 | + Schema: &Schema{SchemaProps: SchemaProps{Type: []string{"string"}}}, |
| 42 | + }, |
| 43 | + }, |
| 44 | + }, |
| 45 | + }, |
| 46 | +} |
| 47 | + |
| 48 | +const responsesJSON = `{ |
| 49 | + "x-go-name": "PutDogExists", |
| 50 | + "200": { |
| 51 | + "$ref": "Dog", |
| 52 | + "x-go-name": "PutDogExists", |
| 53 | + "description": "Dog exists", |
| 54 | + "schema": { |
| 55 | + "type": "string" |
| 56 | + } |
| 57 | + } |
| 58 | +}` |
| 59 | + |
| 60 | +func TestIntegrationResponses(t *testing.T) { |
| 61 | + var actual Responses |
| 62 | + if assert.NoError(t, json.Unmarshal([]byte(responsesJSON), &actual)) { |
| 63 | + assert.EqualValues(t, actual, responses) |
| 64 | + } |
| 65 | + |
| 66 | + assertParsesJSON(t, responsesJSON, responses) |
| 67 | +} |
| 68 | + |
| 69 | +func TestJSONLookupResponses(t *testing.T) { |
| 70 | + resp200, ok := responses.StatusCodeResponses[200] |
| 71 | + if !assert.True(t, ok) { |
| 72 | + t.FailNow() |
| 73 | + return |
| 74 | + } |
| 75 | + |
| 76 | + res, err := resp200.JSONLookup("$ref") |
| 77 | + if !assert.NoError(t, err) { |
| 78 | + t.FailNow() |
| 79 | + return |
| 80 | + } |
| 81 | + if assert.IsType(t, &Ref{}, res) { |
| 82 | + ref := res.(*Ref) |
| 83 | + assert.EqualValues(t, MustCreateRef("Dog"), *ref) |
| 84 | + } |
| 85 | + |
| 86 | + var def string |
| 87 | + res, err = resp200.JSONLookup("description") |
| 88 | + if !assert.NoError(t, err) || !assert.NotNil(t, res) || !assert.IsType(t, def, res) { |
| 89 | + t.FailNow() |
| 90 | + return |
| 91 | + } |
| 92 | + def = res.(string) |
| 93 | + assert.Equal(t, "Dog exists", def) |
| 94 | + |
| 95 | + var x *interface{} |
| 96 | + res, err = responses.JSONLookup("x-go-name") |
| 97 | + if !assert.NoError(t, err) || !assert.NotNil(t, res) || !assert.IsType(t, x, res) { |
| 98 | + t.FailNow() |
| 99 | + return |
| 100 | + } |
| 101 | + |
| 102 | + x = res.(*interface{}) |
| 103 | + assert.EqualValues(t, "PutDogExists", *x) |
| 104 | + |
| 105 | + res, err = responses.JSONLookup("unknown") |
| 106 | + if !assert.Error(t, err) || !assert.Nil(t, res) { |
| 107 | + t.FailNow() |
| 108 | + return |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func TestResponsesBuild(t *testing.T) { |
| 113 | + resp := NewResponse(). |
| 114 | + WithDescription("some response"). |
| 115 | + WithSchema(new(Schema).Typed("object", "")). |
| 116 | + AddHeader("x-header", ResponseHeader().Typed("string", "")). |
| 117 | + AddExample("application/json", `{"key":"value"}`) |
| 118 | + jazon, _ := json.MarshalIndent(resp, "", " ") |
| 119 | + assert.JSONEq(t, `{ |
| 120 | + "description": "some response", |
| 121 | + "schema": { |
| 122 | + "type": "object" |
| 123 | + }, |
| 124 | + "headers": { |
| 125 | + "x-header": { |
| 126 | + "type": "string" |
| 127 | + } |
| 128 | + }, |
| 129 | + "examples": { |
| 130 | + "application/json": "{\"key\":\"value\"}" |
| 131 | + } |
| 132 | + }`, string(jazon)) |
| 133 | +} |
0 commit comments