Skip to content

Commit ea3241b

Browse files
committed
support multiple status codes
Signed-off-by: Huabing (Robin) Zhao <[email protected]>
1 parent 29230f8 commit ea3241b

File tree

2 files changed

+80
-59
lines changed

2 files changed

+80
-59
lines changed

conformance/tests/httproute-cors.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ var HTTPRouteCORS = suite.ConformanceTest{
7575
},
7676
Namespace: "",
7777
Response: http.Response{
78-
StatusCode: 200,
78+
StatusCodes: []int{200, 204},
7979
Headers: map[string]string{
8080
"access-control-allow-origin": "https://www.foo.com",
8181
"access-control-allow-methods": "GET, OPTIONS",
@@ -114,7 +114,7 @@ var HTTPRouteCORS = suite.ConformanceTest{
114114
},
115115
Namespace: "",
116116
Response: http.Response{
117-
StatusCode: 200,
117+
StatusCodes: []int{200, 204},
118118
Headers: map[string]string{
119119
"access-control-allow-origin": "https://www.bar.com",
120120
"access-control-allow-methods": "GET, OPTIONS",
@@ -170,7 +170,7 @@ var HTTPRouteCORS = suite.ConformanceTest{
170170
},
171171
},
172172
Response: http.Response{
173-
StatusCode: 200,
173+
StatusCodes: []int{200, 204},
174174
Headers: map[string]string{
175175
"access-control-allow-origin": "https://www.foo.com",
176176
},
@@ -189,7 +189,7 @@ var HTTPRouteCORS = suite.ConformanceTest{
189189
},
190190
},
191191
Response: http.Response{
192-
StatusCode: 200,
192+
StatusCodes: []int{200, 204},
193193
Headers: map[string]string{
194194
"access-control-allow-origin": "https://www.bar.com",
195195
},
@@ -236,7 +236,7 @@ var HTTPRouteCORS = suite.ConformanceTest{
236236
},
237237
Namespace: "",
238238
Response: http.Response{
239-
StatusCode: 200,
239+
StatusCodes: []int{200, 204},
240240
Headers: map[string]string{
241241
"access-control-allow-origin": "https://www.foo.com",
242242
"access-control-allow-methods": "POST",

conformance/utils/http/http.go

Lines changed: 75 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ type ExpectedRequest struct {
8585
// Response defines expected properties of a response from a backend.
8686
type Response struct {
8787
StatusCode int
88+
StatusCodes []int // alternative to StatusCode, allows multiple acceptable codes
8889
Headers map[string]string
8990
AbsentHeaders []string
9091

@@ -263,54 +264,88 @@ func CompareRequest(t *testing.T, req *roundtripper.Request, cReq *roundtripper.
263264
return nil
264265
}
265266
}
266-
if expected.Response.StatusCode != cRes.StatusCode {
267+
268+
if len(expected.Response.StatusCodes) > 0 {
269+
matched := false
270+
for _, code := range expected.Response.StatusCodes {
271+
if code == cRes.StatusCode {
272+
matched = true
273+
break
274+
}
275+
}
276+
if !matched {
277+
return fmt.Errorf("expected status code to be one of %v, got %d", expected.Response.StatusCodes, cRes.StatusCode)
278+
}
279+
} else if expected.Response.StatusCode != cRes.StatusCode {
267280
return fmt.Errorf("expected status code to be %d, got %d", expected.Response.StatusCode, cRes.StatusCode)
268281
}
269-
if cRes.StatusCode == 200 {
270-
// The request expected to arrive at the backend is
271-
// the same as the request made, unless otherwise
272-
// specified.
273-
if expected.ExpectedRequest == nil {
274-
expected.ExpectedRequest = &ExpectedRequest{Request: expected.Request}
275-
}
276282

277-
if expected.ExpectedRequest.Method == "" {
278-
expected.ExpectedRequest.Method = "GET"
279-
}
283+
if cRes.StatusCode == 200 || cRes.StatusCode == 204 {
284+
if cRes.StatusCode == 200 {
285+
// The request expected to arrive at the backend is
286+
// the same as the request made, unless otherwise
287+
// specified.
288+
if expected.ExpectedRequest == nil {
289+
expected.ExpectedRequest = &ExpectedRequest{Request: expected.Request}
290+
}
280291

281-
if expected.ExpectedRequest.Host != "" && expected.ExpectedRequest.Host != cReq.Host {
282-
return fmt.Errorf("expected host to be %s, got %s", expected.ExpectedRequest.Host, cReq.Host)
283-
}
292+
if expected.ExpectedRequest.Method == "" {
293+
expected.ExpectedRequest.Method = "GET"
294+
}
284295

285-
if expected.ExpectedRequest.Path != cReq.Path {
286-
return fmt.Errorf("expected path to be %s, got %s", expected.ExpectedRequest.Path, cReq.Path)
287-
}
288-
if expected.ExpectedRequest.Method != cReq.Method {
289-
return fmt.Errorf("expected method to be %s, got %s", expected.ExpectedRequest.Method, cReq.Method)
290-
}
291-
if expected.Namespace != cReq.Namespace {
292-
return fmt.Errorf("expected namespace to be %s, got %s", expected.Namespace, cReq.Namespace)
293-
}
294-
if expected.ExpectedRequest.Headers != nil {
295-
if cReq.Headers == nil {
296-
return fmt.Errorf("no headers captured, expected %v", len(expected.ExpectedRequest.Headers))
296+
if expected.ExpectedRequest.Host != "" && expected.ExpectedRequest.Host != cReq.Host {
297+
return fmt.Errorf("expected host to be %s, got %s", expected.ExpectedRequest.Host, cReq.Host)
297298
}
298-
for name, val := range cReq.Headers {
299-
cReq.Headers[strings.ToLower(name)] = val
299+
300+
if expected.ExpectedRequest.Path != cReq.Path {
301+
return fmt.Errorf("expected path to be %s, got %s", expected.ExpectedRequest.Path, cReq.Path)
300302
}
301-
for name, expectedVal := range expected.ExpectedRequest.Headers {
302-
actualVal, ok := cReq.Headers[strings.ToLower(name)]
303-
if !ok {
304-
return fmt.Errorf("expected %s header to be set, actual headers: %v", name, cReq.Headers)
305-
} else if strings.Join(actualVal, ",") != expectedVal {
306-
return fmt.Errorf("expected %s header to be set to %s, got %s", name, expectedVal, strings.Join(actualVal, ","))
303+
if expected.ExpectedRequest.Method != cReq.Method {
304+
return fmt.Errorf("expected method to be %s, got %s", expected.ExpectedRequest.Method, cReq.Method)
305+
}
306+
if expected.Namespace != cReq.Namespace {
307+
return fmt.Errorf("expected namespace to be %s, got %s", expected.Namespace, cReq.Namespace)
308+
}
309+
if expected.ExpectedRequest.Headers != nil {
310+
if cReq.Headers == nil {
311+
return fmt.Errorf("no headers captured, expected %v", len(expected.ExpectedRequest.Headers))
312+
}
313+
for name, val := range cReq.Headers {
314+
cReq.Headers[strings.ToLower(name)] = val
315+
}
316+
for name, expectedVal := range expected.ExpectedRequest.Headers {
317+
actualVal, ok := cReq.Headers[strings.ToLower(name)]
318+
if !ok {
319+
return fmt.Errorf("expected %s header to be set, actual headers: %v", name, cReq.Headers)
320+
} else if strings.Join(actualVal, ",") != expectedVal {
321+
return fmt.Errorf("expected %s header to be set to %s, got %s", name, expectedVal, strings.Join(actualVal, ","))
322+
}
323+
}
324+
}
325+
326+
// Verify that headers expected *not* to be present on the
327+
// request are actually not present.
328+
if len(expected.ExpectedRequest.AbsentHeaders) > 0 {
329+
for name, val := range cReq.Headers {
330+
cReq.Headers[strings.ToLower(name)] = val
307331
}
332+
333+
for _, name := range expected.ExpectedRequest.AbsentHeaders {
334+
val, ok := cReq.Headers[strings.ToLower(name)]
335+
if ok {
336+
return fmt.Errorf("expected %s header to not be set, got %s", name, val)
337+
}
338+
}
339+
}
340+
341+
if !strings.HasPrefix(cReq.Pod, expected.Backend) {
342+
return fmt.Errorf("expected pod name to start with %s, got %s", expected.Backend, cReq.Pod)
308343
}
309344
}
310345

311346
if expected.Response.Headers != nil {
312347
if cRes.Headers == nil {
313-
return fmt.Errorf("no headers captured, expected %v", len(expected.ExpectedRequest.Headers))
348+
return fmt.Errorf("no headers captured, expected %v", len(expected.Response.Headers))
314349
}
315350
for name, val := range cRes.Headers {
316351
cRes.Headers[strings.ToLower(name)] = val
@@ -344,25 +379,6 @@ func CompareRequest(t *testing.T, req *roundtripper.Request, cReq *roundtripper.
344379
}
345380
}
346381
}
347-
348-
// Verify that headers expected *not* to be present on the
349-
// request are actually not present.
350-
if len(expected.ExpectedRequest.AbsentHeaders) > 0 {
351-
for name, val := range cReq.Headers {
352-
cReq.Headers[strings.ToLower(name)] = val
353-
}
354-
355-
for _, name := range expected.ExpectedRequest.AbsentHeaders {
356-
val, ok := cReq.Headers[strings.ToLower(name)]
357-
if ok {
358-
return fmt.Errorf("expected %s header to not be set, got %s", name, val)
359-
}
360-
}
361-
}
362-
363-
if !strings.HasPrefix(cReq.Pod, expected.Backend) {
364-
return fmt.Errorf("expected pod name to start with %s, got %s", expected.Backend, cReq.Pod)
365-
}
366382
} else if roundtripper.IsRedirect(cRes.StatusCode) {
367383
if expected.RedirectRequest == nil {
368384
return nil
@@ -424,6 +440,11 @@ func (er *ExpectedResponse) GetTestCaseName(i int) string {
424440
if er.Backend != "" {
425441
return fmt.Sprintf("%s should go to %s", reqStr, er.Backend)
426442
}
443+
444+
if len(er.Response.StatusCodes) > 0 {
445+
return fmt.Sprintf("%s should receive one of %v", reqStr, er.Response.StatusCodes)
446+
}
447+
427448
return fmt.Sprintf("%s should receive a %d", reqStr, er.Response.StatusCode)
428449
}
429450

0 commit comments

Comments
 (0)