...
1
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 TestHeaderRoundTrip(t *testing.T) {
31 cases := []jsontesting.RoundTripTestCase{
32 {
33 Name: "Basic Roundtrip",
34 Object: &spec3.Header{
35 spec.Refable{Ref: spec.MustCreateRef("Dog")},
36 spec3.HeaderProps{
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.Header{}))
49 })
50 }
51 }
52
53 func TestHeaderJSONSerialization(t *testing.T) {
54 cases := []struct {
55 name string
56 target *spec3.Header
57 expectedOutput string
58 }{
59
60 {
61 name: "basic",
62 target: &spec3.Header{
63 HeaderProps: spec3.HeaderProps{
64 Description: "The number of allowed requests in the current period",
65 Schema: &spec.Schema{
66 SchemaProps: spec.SchemaProps{
67 Type: []string{"integer"},
68 },
69 },
70 },
71 },
72 expectedOutput: `{"description":"The number of allowed requests in the current period","schema":{"type":"integer"}}`,
73 },
74 }
75 for _, tc := range cases {
76 t.Run(tc.name, func(t *testing.T) {
77 rawTarget, err := json.Marshal(tc.target)
78 if err != nil {
79 t.Fatal(err)
80 }
81 serializedTarget := string(rawTarget)
82 if !cmp.Equal(serializedTarget, tc.expectedOutput) {
83 t.Fatalf("diff %s", cmp.Diff(serializedTarget, tc.expectedOutput))
84 }
85 })
86 }
87 }
88
View as plain text