1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
package handler
import (
"io/ioutil"
"math"
"net/http"
"net/http/httptest"
"reflect"
"sort"
"testing"
"github.com/davecgh/go-spew/spew"
json "github.com/json-iterator/go"
yaml "gopkg.in/yaml.v2"
"k8s.io/kube-openapi/pkg/validation/spec"
)
var returnedSwagger = []byte(`{
"swagger": "2.0",
"info": {
"title": "Kubernetes",
"version": "v1.11.0"
}}`)
func TestRegisterOpenAPIVersionedService(t *testing.T) {
var s spec.Swagger
err := s.UnmarshalJSON(returnedSwagger)
if err != nil {
t.Errorf("Unexpected error in unmarshalling SwaggerJSON: %v", err)
}
returnedJSON, err := json.Marshal(s)
if err != nil {
t.Errorf("Unexpected error in preparing returnedJSON: %v", err)
}
var decodedJSON map[string]interface{}
if err := json.Unmarshal(returnedJSON, &decodedJSON); err != nil {
t.Fatal(err)
}
returnedPb, err := ToProtoBinary(returnedJSON)
if err != nil {
t.Errorf("Unexpected error in preparing returnedPb: %v", err)
}
mux := http.NewServeMux()
o, err := NewOpenAPIService(&s)
if err != nil {
t.Fatal(err)
}
if err = o.RegisterOpenAPIVersionedService("/openapi/v2", mux); err != nil {
t.Errorf("Unexpected error in register OpenAPI versioned service: %v", err)
}
server := httptest.NewServer(mux)
defer server.Close()
client := server.Client()
tcs := []struct {
acceptHeader string
respStatus int
respBody []byte
}{
{"", 200, returnedJSON},
{"*/*", 200, returnedJSON},
{"application/*", 200, returnedJSON},
{"application/json", 200, returnedJSON},
{"test/test", 406, []byte{}},
{"application/test", 406, []byte{}},
{"application/test, */*", 200, returnedJSON},
{"application/test, application/json", 200, returnedJSON},
{"application/com.github.proto-openapi.spec.v2@v1.0+protobuf", 200, returnedPb},
{"application/json, application/com.github.proto-openapi.spec.v2@v1.0+protobuf", 200, returnedJSON},
{"application/com.github.proto-openapi.spec.v2@v1.0+protobuf, application/json", 200, returnedPb},
{"application/com.github.proto-openapi.spec.v2@v1.0+protobuf; q=0.5, application/json", 200, returnedJSON},
}
for _, tc := range tcs {
req, err := http.NewRequest("GET", server.URL+"/openapi/v2", nil)
if err != nil {
t.Errorf("Accept: %v: Unexpected error in creating new request: %v", tc.acceptHeader, err)
}
req.Header.Add("Accept", tc.acceptHeader)
resp, err := client.Do(req)
if err != nil {
t.Errorf("Accept: %v: Unexpected error in serving HTTP request: %v", tc.acceptHeader, err)
}
if resp.StatusCode != tc.respStatus {
t.Errorf("Accept: %v: Unexpected response status code, want: %v, got: %v", tc.acceptHeader, tc.respStatus, resp.StatusCode)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Errorf("Accept: %v: Unexpected error in reading response body: %v", tc.acceptHeader, err)
}
if !reflect.DeepEqual(body, tc.respBody) {
t.Errorf("Accept: %v: Response body mismatches, \nwant: %s, \ngot: %s", tc.acceptHeader, string(tc.respBody), string(body))
}
}
}
func TestJsonToYAML(t *testing.T) {
intOrInt64 := func(i64 int64) interface{} {
if i := int(i64); i64 == int64(i) {
return i
}
return i64
}
tests := []struct {
name string
input map[string]interface{}
expected yaml.MapSlice
}{
{"nil", nil, nil},
{"empty", map[string]interface{}{}, yaml.MapSlice{}},
{
"values",
map[string]interface{}{
"bool": true,
"float64": float64(42.1),
"fractionless": float64(42),
"int": int(42),
"int64": int64(42),
"int64 big": float64(math.Pow(2, 62)),
"map": map[string]interface{}{"foo": "bar"},
"slice": []interface{}{"foo", "bar"},
"string": string("foo"),
"uint64 big": float64(math.Pow(2, 63)),
},
yaml.MapSlice{
{"bool", true},
{"float64", float64(42.1)},
{"fractionless", int(42)},
{"int", int(42)},
{"int64", int(42)},
{"int64 big", intOrInt64(int64(1) << 62)},
{"map", yaml.MapSlice{{"foo", "bar"}}},
{"slice", []interface{}{"foo", "bar"}},
{"string", string("foo")},
{"uint64 big", uint64(1) << 63},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := jsonToYAML(tt.input)
sortMapSlicesInPlace(tt.expected)
sortMapSlicesInPlace(got)
if !reflect.DeepEqual(got, tt.expected) {
t.Errorf("jsonToYAML() = %v, want %v", spew.Sdump(got), spew.Sdump(tt.expected))
}
})
}
}
func sortMapSlicesInPlace(x interface{}) {
switch x := x.(type) {
case []interface{}:
for i := range x {
sortMapSlicesInPlace(x[i])
}
case yaml.MapSlice:
sort.Slice(x, func(a, b int) bool {
return x[a].Key.(string) < x[b].Key.(string)
})
}
}
func TestToProtoBinary(t *testing.T) {
bs, err := ioutil.ReadFile("../../test/integration/testdata/aggregator/openapi.json")
if err != nil {
t.Fatal(err)
}
if _, err := ToProtoBinary(bs); err != nil {
t.Fatal()
}
// TODO: add some kind of roundtrip test here
}
|