Debian Codegraph @

pkg/spec3/encoding_test.go

raw
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 TestEncodingRoundtrip(t *testing.T) {
31 cases := []jsontesting.RoundTripTestCase{
32 {
33 Name: "Basic Roundtrip",
34 Object: &spec3.Encoding{
35 spec3.EncodingProps{
36 ContentType: "image/png",
37 },
38 spec.VendorExtensible{Extensions: spec.Extensions{
39 "x-framework": "go-swagger",
40 }},
41 },
42 },
43 }
44
45 for _, tcase := range cases {
46 t.Run(tcase.Name, func(t *testing.T) {
47 require.NoError(t, tcase.RoundTripTest(&spec3.Encoding{}))
48 })
49 }
50 }
51
52 func TestEncodingJSONSerialization(t *testing.T) {
53 cases := []struct {
54 name string
55 target *spec3.Encoding
56 expectedOutput string
57 }{
58 // scenario 1
59 {
60 name: "basic",
61 target: &spec3.Encoding{
62 EncodingProps: spec3.EncodingProps{
63 ContentType: "image/png",
64 Headers: map[string]*spec3.Header{
65 "X-Rate-Limit-Limit": {
66 HeaderProps: spec3.HeaderProps{
67 Description: "The number of allowed requests in the current period",
68 Schema: &spec.Schema{
69 SchemaProps: spec.SchemaProps{
70 Type: []string{"integer"},
71 },
72 },
73 },
74 },
75 },
76 },
77 },
78 expectedOutput: `{"contentType":"image/png","headers":{"X-Rate-Limit-Limit":{"description":"The number of allowed requests in the current period","schema":{"type":"integer"}}}}`,
79 },
80 }
81 for _, tc := range cases {
82 t.Run(tc.name, func(t *testing.T) {
83 rawTarget, err := json.Marshal(tc.target)
84 if err != nil {
85 t.Fatal(err)
86 }
87 serializedTarget := string(rawTarget)
88 if !cmp.Equal(serializedTarget, tc.expectedOutput) {
89 t.Fatalf("diff %s", cmp.Diff(serializedTarget, tc.expectedOutput))
90 }
91 })
92 }
93 }