...
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
26 "k8s.io/kube-openapi/pkg/spec3"
27 jsontesting "k8s.io/kube-openapi/pkg/util/jsontesting"
28 "k8s.io/kube-openapi/pkg/validation/spec"
29 )
30
31 func TestServerRoundTrip(t *testing.T) {
32 cases := []jsontesting.RoundTripTestCase{
33 {
34 Name: "Basic Roundtrip",
35 Object: &spec3.Server{
36 spec3.ServerProps{
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.Server{}))
49 })
50 }
51 }
52
53 func TestServerJSONSerialization(t *testing.T) {
54 cases := []struct {
55 name string
56 target *spec3.Server
57 expectedOutput string
58 }{
59
60 {
61 name: "basic",
62 target: &spec3.Server{
63 ServerProps: spec3.ServerProps{
64 URL: "https://development.gigantic-server.com/v1",
65 Description: "Development server",
66 },
67 },
68 expectedOutput: `{"description":"Development server","url":"https://development.gigantic-server.com/v1"}`,
69 },
70 }
71 for _, tc := range cases {
72 t.Run(tc.name, func(t *testing.T) {
73 rawTarget, err := json.Marshal(tc.target)
74 if err != nil {
75 t.Fatal(err)
76 }
77 serializedTarget := string(rawTarget)
78 if !cmp.Equal(serializedTarget, tc.expectedOutput) {
79 t.Fatalf("diff %s", cmp.Diff(serializedTarget, tc.expectedOutput))
80 }
81 })
82 }
83 }
84
View as plain text