k8s.io/kube-openapi@v0.0.0-20240228011516-70dd3763d340/pkg/spec3/parameter_test.go (about) 1 /* 2 Copyright 2021 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package spec3_test 18 19 import ( 20 "encoding/json" 21 "testing" 22 23 "github.com/google/go-cmp/cmp" 24 "github.com/stretchr/testify/require" 25 "k8s.io/kube-openapi/pkg/spec3" 26 jsontesting "k8s.io/kube-openapi/pkg/util/jsontesting" 27 "k8s.io/kube-openapi/pkg/validation/spec" 28 ) 29 30 func TestParameterRoundTrip(t *testing.T) { 31 cases := []jsontesting.RoundTripTestCase{ 32 { 33 Name: "Basic Roundtrip", 34 Object: &spec3.Parameter{ 35 spec.Refable{Ref: spec.MustCreateRef("Dog")}, 36 spec3.ParameterProps{ 37 Description: "foo", 38 }, 39 spec.VendorExtensible{Extensions: spec.Extensions{ 40 "x-framework": "go-swagger", 41 }}, 42 }, 43 }, 44 } 45 46 for _, tcase := range cases { 47 t.Run(tcase.Name, func(t *testing.T) { 48 require.NoError(t, tcase.RoundTripTest(&spec3.Parameter{})) 49 }) 50 } 51 } 52 53 func TestParameterJSONSerialization(t *testing.T) { 54 cases := []struct { 55 name string 56 target *spec3.Parameter 57 expectedOutput string 58 }{ 59 { 60 name: "header parameter", 61 target: &spec3.Parameter{ 62 ParameterProps: spec3.ParameterProps{ 63 Name: "token", 64 In: "header", 65 Description: "token to be passed as a header", 66 Required: true, 67 Schema: &spec.Schema{ 68 SchemaProps: spec.SchemaProps{ 69 Type: []string{"integer"}, 70 Format: "int64", 71 }, 72 }, 73 Style: "simple", 74 }, 75 }, 76 expectedOutput: `{"name":"token","in":"header","description":"token to be passed as a header","required":true,"style":"simple","schema":{"type":"integer","format":"int64"}}`, 77 }, 78 { 79 name: "path parameter", 80 target: &spec3.Parameter{ 81 ParameterProps: spec3.ParameterProps{ 82 Name: "username", 83 In: "path", 84 Description: "username to fetch", 85 Required: true, 86 Schema: &spec.Schema{ 87 SchemaProps: spec.SchemaProps{ 88 Type: []string{"string"}, 89 }, 90 }, 91 }, 92 }, 93 expectedOutput: `{"name":"username","in":"path","description":"username to fetch","required":true,"schema":{"type":"string"}}`, 94 }, 95 } 96 for _, tc := range cases { 97 t.Run(tc.name, func(t *testing.T) { 98 rawTarget, err := json.Marshal(tc.target) 99 if err != nil { 100 t.Fatal(err) 101 } 102 serializedTarget := string(rawTarget) 103 if !cmp.Equal(serializedTarget, tc.expectedOutput) { 104 t.Fatalf("diff %s", cmp.Diff(serializedTarget, tc.expectedOutput)) 105 } 106 }) 107 } 108 }