| 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 |
"reflect" |
| 22 |
"testing" |
| 23 |
|
| 24 |
"github.com/google/go-cmp/cmp" |
| 25 |
"github.com/stretchr/testify/require" |
| 26 |
jsonv2 "k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json" |
| 27 |
"k8s.io/kube-openapi/pkg/spec3" |
| 28 |
jsontesting "k8s.io/kube-openapi/pkg/util/jsontesting" |
| 29 |
"k8s.io/kube-openapi/pkg/validation/spec" |
| 30 |
) |
| 31 |
|
| 32 |
func TestResponsesRoundTrip(t *testing.T) { |
| 33 |
cases := []jsontesting.RoundTripTestCase{ |
| 34 |
{ |
| 35 |
Name: "Basic Test With Extensions", |
| 36 |
Object: &spec3.Responses{ |
| 37 |
VendorExtensible: spec.VendorExtensible{ |
| 38 |
Extensions: spec.Extensions{ |
| 39 |
"x-framework": "swagger-go", |
| 40 |
}, |
| 41 |
}, |
| 42 |
ResponsesProps: spec3.ResponsesProps{ |
| 43 |
Default: &spec3.Response{ |
| 44 |
Refable: spec.Refable{Ref: spec.MustCreateRef("/components/some/ref.foo")}, |
| 45 |
}, |
| 46 |
}, |
| 47 |
}, |
| 48 |
}, |
| 49 |
} |
| 50 |
for _, tcase := range cases { |
| 51 |
t.Run(tcase.Name, func(t *testing.T) { |
| 52 |
require.NoError(t, tcase.RoundTripTest(&spec3.Responses{})) |
| 53 |
}) |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
func TestResponseRoundTrip(t *testing.T) { |
| 58 |
cases := []jsontesting.RoundTripTestCase{ |
| 59 |
{ |
| 60 |
Name: "Basic Roundtrip", |
| 61 |
Object: &spec3.Response{ |
| 62 |
spec.Refable{Ref: spec.MustCreateRef("Dog")}, |
| 63 |
spec3.ResponseProps{ |
| 64 |
Description: "foo", |
| 65 |
}, |
| 66 |
spec.VendorExtensible{Extensions: spec.Extensions{ |
| 67 |
"x-framework": "go-swagger", |
| 68 |
}}, |
| 69 |
}, |
| 70 |
}, |
| 71 |
} |
| 72 |
|
| 73 |
for _, tcase := range cases { |
| 74 |
t.Run(tcase.Name, func(t *testing.T) { |
| 75 |
require.NoError(t, tcase.RoundTripTest(&spec3.Response{})) |
| 76 |
}) |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
func TestResponseJSONSerialization(t *testing.T) { |
| 81 |
cases := []struct { |
| 82 |
name string |
| 83 |
target *spec3.Response |
| 84 |
expectedOutput string |
| 85 |
}{ |
| 86 |
// scenario 1 |
| 87 |
{ |
| 88 |
name: "basic", |
| 89 |
target: &spec3.Response{ |
| 90 |
ResponseProps: spec3.ResponseProps{ |
| 91 |
Content: map[string]*spec3.MediaType{ |
| 92 |
"text/plain": { |
| 93 |
MediaTypeProps: spec3.MediaTypeProps{ |
| 94 |
Schema: &spec.Schema{ |
| 95 |
SchemaProps: spec.SchemaProps{ |
| 96 |
Type: []string{"string"}, |
| 97 |
}, |
| 98 |
}, |
| 99 |
}, |
| 100 |
}, |
| 101 |
}, |
| 102 |
}, |
| 103 |
}, |
| 104 |
expectedOutput: `{"content":{"text/plain":{"schema":{"type":"string"}}}}`, |
| 105 |
}, |
| 106 |
} |
| 107 |
for _, tc := range cases { |
| 108 |
t.Run(tc.name, func(t *testing.T) { |
| 109 |
rawTarget, err := json.Marshal(tc.target) |
| 110 |
if err != nil { |
| 111 |
t.Fatal(err) |
| 112 |
} |
| 113 |
serializedTarget := string(rawTarget) |
| 114 |
if !cmp.Equal(serializedTarget, tc.expectedOutput) { |
| 115 |
t.Fatalf("diff %s", cmp.Diff(serializedTarget, tc.expectedOutput)) |
| 116 |
} |
| 117 |
}) |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
func TestResponsesNullUnmarshal(t *testing.T) { |
| 122 |
nullByte := []byte(`null`) |
| 123 |
|
| 124 |
expected := spec3.Responses{} |
| 125 |
test := spec3.Responses{ |
| 126 |
ResponsesProps: spec3.ResponsesProps{ |
| 127 |
Default: &spec3.Response{}, |
| 128 |
}, |
| 129 |
} |
| 130 |
jsonv2.Unmarshal(nullByte, &test) |
| 131 |
if !reflect.DeepEqual(test, expected) { |
| 132 |
t.Error("Expected unmarshal of null to reset the Responses struct") |
| 133 |
} |
| 134 |
} |