...

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

View as plain text