k8s.io/kube-openapi@v0.0.0-20240228011516-70dd3763d340/pkg/validation/spec/response_test.go (about)

     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  	jsontesting "k8s.io/kube-openapi/pkg/util/jsontesting"
    24  )
    25  
    26  var response = Response{
    27  	Refable: Refable{Ref: MustCreateRef("Dog")},
    28  	VendorExtensible: VendorExtensible{
    29  		Extensions: map[string]interface{}{
    30  			"x-go-name": "PutDogExists",
    31  		},
    32  	},
    33  	ResponseProps: ResponseProps{
    34  		Description: "Dog exists",
    35  		Schema:      &Schema{SchemaProps: SchemaProps{Type: []string{"string"}}},
    36  	},
    37  }
    38  
    39  const responseJSON = `{
    40  	"$ref": "Dog",
    41  	"x-go-name": "PutDogExists",
    42  	"description": "Dog exists",
    43  	"schema": {
    44  		"type": "string"
    45  	}
    46  }`
    47  
    48  func TestIntegrationResponse(t *testing.T) {
    49  	var actual Response
    50  	if assert.NoError(t, json.Unmarshal([]byte(responseJSON), &actual)) {
    51  		assert.EqualValues(t, actual, response)
    52  	}
    53  
    54  	assertParsesJSON(t, responseJSON, response)
    55  }
    56  
    57  func TestResponseRoundtrip(t *testing.T) {
    58  	cases := []jsontesting.RoundTripTestCase{
    59  		{
    60  			// Show at least one field from each embededd struct sitll allows
    61  			// roundtrips successfully
    62  			Name: "UnmarshalEmbedded",
    63  			JSON: `{
    64  				"$ref": "/components/ref/to/something.foo",
    65  				"x-framework": "swagger-go",
    66  				"description": "a really cool description"
    67  			  }`,
    68  			Object: &Response{
    69  				Refable{MustCreateRef("/components/ref/to/something.foo")},
    70  				ResponseProps{Description: "a really cool description"},
    71  				VendorExtensible{Extensions{"x-framework": "swagger-go"}},
    72  			},
    73  		}, {
    74  			Name:   "BasicCase",
    75  			JSON:   responseJSON,
    76  			Object: &response,
    77  		},
    78  	}
    79  
    80  	for _, tcase := range cases {
    81  		t.Run(tcase.Name, func(t *testing.T) {
    82  			require.NoError(t, tcase.RoundTripTest(&Response{}))
    83  		})
    84  	}
    85  }