-
Notifications
You must be signed in to change notification settings - Fork 217
/
Copy pathhandler_test.go
257 lines (225 loc) · 8.1 KB
/
handler_test.go
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package handler
import (
json "encoding/json"
"fmt"
"io"
"mime"
"net/http"
"net/http/httptest"
"os"
"reflect"
"testing"
"k8s.io/kube-openapi/pkg/cached"
"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 := normalizeSwaggerOrDie(returnedSwagger)
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 := NewOpenAPIService(&s)
o.RegisterOpenAPIVersionedService("/openapi/v2", mux)
server := httptest.NewServer(mux)
defer server.Close()
client := server.Client()
tcs := []struct {
acceptHeader string
respStatus int
responseContentTypeHeader string
respBody []byte
}{
{"", 200, "application/json", returnedJSON},
{"*/*", 200, "application/json", returnedJSON},
{"application/*", 200, "application/json", returnedJSON},
{"application/json", 200, "application/json", returnedJSON},
{"test/test", 406, "", []byte{}},
{"application/test", 406, "", []byte{}},
{"application/test, */*", 200, "application/json", returnedJSON},
{"application/test, application/json", 200, "application/json", returnedJSON},
{"application/com.github.proto-openapi.spec.v2.v1.0+protobuf", 200, "application/com.github.proto-openapi.spec.v2.v1.0+protobuf", returnedPb},
{"application/json, application/com.github.proto-openapi.spec.v2.v1.0+protobuf", 200, "application/json", returnedJSON},
{"application/com.github.proto-openapi.spec.v2.v1.0+protobuf, application/json", 200, "application/com.github.proto-openapi.spec.v2.v1.0+protobuf", returnedPb},
{"application/com.github.proto-openapi.spec.v2.v1.0+protobuf; q=0.5, application/json", 200, "application/json", returnedJSON},
{"application/com.github.proto-openapi.spec.v2@v1.0+protobuf", 200, "application/com.github.proto-openapi.spec.v2.v1.0+protobuf", returnedPb},
{"application/json, application/com.github.proto-openapi.spec.v2@v1.0+protobuf", 200, "application/json", returnedJSON},
{"application/com.github.proto-openapi.spec.v2@v1.0+protobuf, application/json", 200, "application/com.github.proto-openapi.spec.v2.v1.0+protobuf", returnedPb},
{"application/com.github.proto-openapi.spec.v2@v1.0+protobuf; q=0.5, application/json", 200, "application/json", 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)
}
if tc.respStatus != 200 {
continue
}
responseContentType := resp.Header.Get("Content-Type")
if responseContentType != tc.responseContentTypeHeader {
t.Errorf("Accept: %v: Unexpected content type in response, want: %v, got: %v", tc.acceptHeader, tc.responseContentTypeHeader, responseContentType)
}
_, _, err = mime.ParseMediaType(responseContentType)
if err != nil {
t.Errorf("Unexpected error in parsing response content type: %v, err: %v", responseContentType, err)
}
defer resp.Body.Close()
body, err := io.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))
}
}
}
var updatedSwagger = []byte(`{
"swagger": "2.0",
"info": {
"title": "Kubernetes",
"version": "v1.12.0"
}}`)
func getJSONBodyOrDie(server *httptest.Server) []byte {
return getBodyOrDie(server, "application/json")
}
func getProtoBodyOrDie(server *httptest.Server) []byte {
return getBodyOrDie(server, "application/com.github.proto-openapi.spec.v2.v1.0+protobuf")
}
func getBodyOrDie(server *httptest.Server, acceptHeader string) []byte {
req, err := http.NewRequest("GET", server.URL+"/openapi/v2", nil)
if err != nil {
panic(fmt.Errorf("Unexpected error in creating new request: %v", err))
}
req.Header.Add("Accept", acceptHeader)
resp, err := server.Client().Do(req)
if err != nil {
panic(fmt.Errorf("Unexpected error in serving HTTP request: %v", err))
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
panic(fmt.Errorf("Unexpected error in reading response body: %v", err))
}
return body
}
func normalizeSwaggerOrDie(j []byte) []byte {
var s spec.Swagger
err := s.UnmarshalJSON(j)
if err != nil {
panic(fmt.Errorf("Unexpected error in unmarshalling SwaggerJSON: %v", err))
}
rj, err := json.Marshal(s)
if err != nil {
panic(fmt.Errorf("Unexpected error in preparing returnedJSON: %v", err))
}
return rj
}
func TestUpdateSpecLazy(t *testing.T) {
returnedJSON := normalizeSwaggerOrDie(returnedSwagger)
var s spec.Swagger
err := s.UnmarshalJSON(returnedJSON)
if err != nil {
t.Errorf("Unexpected error in unmarshalling SwaggerJSON: %v", err)
}
mux := http.NewServeMux()
o := NewOpenAPIService(&s)
o.RegisterOpenAPIVersionedService("/openapi/v2", mux)
server := httptest.NewServer(mux)
defer server.Close()
body := string(getJSONBodyOrDie(server))
if body != string(returnedJSON) {
t.Errorf("Unexpected swagger received, got %q, expected %q", body, string(returnedSwagger))
}
o.UpdateSpecLazy(cached.Func(func() (*spec.Swagger, string, error) {
var s spec.Swagger
err := s.UnmarshalJSON(updatedSwagger)
if err != nil {
t.Errorf("Unexpected error in unmarshalling SwaggerJSON: %v", err)
}
return &s, "SOMEHASH", nil
}))
updatedJSON := normalizeSwaggerOrDie(updatedSwagger)
body = string(getJSONBodyOrDie(server))
if body != string(updatedJSON) {
t.Errorf("Unexpected swagger received, got %q, expected %q", body, string(updatedJSON))
}
}
func TestToProtoBinary(t *testing.T) {
bs, err := os.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
}
func TestConcurrentReadStaleCache(t *testing.T) {
// Number of requests sent in parallel
concurrency := 5
var s spec.Swagger
err := s.UnmarshalJSON(returnedSwagger)
if err != nil {
t.Errorf("Unexpected error in unmarshalling SwaggerJSON: %v", err)
}
mux := http.NewServeMux()
o := NewOpenAPIService(&s)
o.RegisterOpenAPIVersionedService("/openapi/v2", mux)
server := httptest.NewServer(mux)
defer server.Close()
returnedJSON := normalizeSwaggerOrDie(returnedSwagger)
returnedPb, err := ToProtoBinary(returnedJSON)
if err != nil {
t.Errorf("Unexpected error in preparing returnedPb: %v", err)
}
jsonResultsChan := make(chan []byte)
protoResultsChan := make(chan []byte)
updateSpecChan := make(chan struct{})
for i := 0; i < concurrency; i++ {
go func() {
sc := s
o.UpdateSpec(&sc)
updateSpecChan <- struct{}{}
}()
go func() { jsonResultsChan <- getJSONBodyOrDie(server) }()
go func() { protoResultsChan <- getProtoBodyOrDie(server) }()
}
for i := 0; i < concurrency; i++ {
r := <-jsonResultsChan
if !reflect.DeepEqual(r, returnedJSON) {
t.Errorf("Returned and expected JSON do not match: got %v, want %v", string(r), string(returnedJSON))
}
}
for i := 0; i < concurrency; i++ {
r := <-protoResultsChan
if !reflect.DeepEqual(r, returnedPb) {
t.Errorf("Returned and expected pb do not match: got %v, want %v", r, returnedPb)
}
}
for i := 0; i < concurrency; i++ {
<-updateSpecChan
}
}