...

Source file src/github.com/go-openapi/spec/responses_test.go

Documentation: github.com/go-openapi/spec

     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  	"github.com/stretchr/testify/require"
    23  )
    24  
    25  var responses = Responses{
    26  	VendorExtensible: VendorExtensible{
    27  		Extensions: map[string]interface{}{
    28  			"x-go-name": "PutDogExists",
    29  		},
    30  	},
    31  	ResponsesProps: ResponsesProps{
    32  		StatusCodeResponses: map[int]Response{
    33  			200: {
    34  				Refable: Refable{Ref: MustCreateRef("Dog")},
    35  				VendorExtensible: VendorExtensible{
    36  					Extensions: map[string]interface{}{
    37  						"x-go-name": "PutDogExists",
    38  					},
    39  				},
    40  				ResponseProps: ResponseProps{
    41  					Description: "Dog exists",
    42  					Schema:      &Schema{SchemaProps: SchemaProps{Type: []string{"string"}}},
    43  				},
    44  			},
    45  		},
    46  	},
    47  }
    48  
    49  const responsesJSON = `{
    50  	"x-go-name": "PutDogExists",
    51  	"200": {
    52  		"$ref": "Dog",
    53  		"x-go-name": "PutDogExists",
    54  		"description": "Dog exists",
    55  		"schema": {
    56  			"type": "string"
    57  		}
    58  	}
    59  }`
    60  
    61  func TestIntegrationResponses(t *testing.T) {
    62  	var actual Responses
    63  	require.NoError(t, json.Unmarshal([]byte(responsesJSON), &actual))
    64  	assert.EqualValues(t, actual, responses)
    65  
    66  	assertParsesJSON(t, responsesJSON, responses)
    67  }
    68  
    69  func TestJSONLookupResponses(t *testing.T) {
    70  	resp200, ok := responses.StatusCodeResponses[200]
    71  	require.True(t, ok)
    72  
    73  	res, err := resp200.JSONLookup("$ref")
    74  	require.NoError(t, err)
    75  	require.NotNil(t, res)
    76  	require.IsType(t, &Ref{}, res)
    77  
    78  	ref, ok := res.(*Ref)
    79  	require.True(t, ok)
    80  	assert.EqualValues(t, MustCreateRef("Dog"), *ref)
    81  
    82  	var def string
    83  	res, err = resp200.JSONLookup("description")
    84  	require.NoError(t, err)
    85  	require.NotNil(t, res)
    86  	require.IsType(t, def, res)
    87  
    88  	def, ok = res.(string)
    89  	require.True(t, ok)
    90  	assert.Equal(t, "Dog exists", def)
    91  
    92  	var x *interface{}
    93  	res, err = responses.JSONLookup("x-go-name")
    94  	require.NoError(t, err)
    95  	require.NotNil(t, res)
    96  	require.IsType(t, x, res)
    97  
    98  	x, ok = res.(*interface{})
    99  	require.True(t, ok)
   100  	assert.EqualValues(t, "PutDogExists", *x)
   101  
   102  	res, err = responses.JSONLookup("unknown")
   103  	require.Error(t, err)
   104  	require.Nil(t, res)
   105  }
   106  
   107  func TestResponsesBuild(t *testing.T) {
   108  	resp := NewResponse().
   109  		WithDescription("some response").
   110  		WithSchema(new(Schema).Typed("object", "")).
   111  		AddHeader("x-header", ResponseHeader().Typed("string", "")).
   112  		AddExample("application/json", `{"key":"value"}`)
   113  	jazon, _ := json.MarshalIndent(resp, "", " ")
   114  	assert.JSONEq(t, `{
   115           "description": "some response",
   116           "schema": {
   117            "type": "object"
   118           },
   119           "headers": {
   120            "x-header": {
   121             "type": "string"
   122            }
   123           },
   124           "examples": {
   125            "application/json": "{\"key\":\"value\"}"
   126           }
   127  			 }`, string(jazon))
   128  }
   129  

View as plain text