Skip to content

Commit 8110e9b

Browse files
committed
address comment
Signed-off-by: Huabing (Robin) Zhao <[email protected]>
1 parent ea3241b commit 8110e9b

File tree

2 files changed

+75
-31
lines changed

2 files changed

+75
-31
lines changed

conformance/tests/httproute-cors.go

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,22 @@ var HTTPRouteCORS = suite.ConformanceTest{
7676
Namespace: "",
7777
Response: http.Response{
7878
StatusCodes: []int{200, 204},
79-
Headers: map[string]string{
80-
"access-control-allow-origin": "https://www.foo.com",
81-
"access-control-allow-methods": "GET, OPTIONS",
82-
"access-control-allow-headers": "x-header-1, x-header-2",
83-
"access-control-expose-headers": "x-header-3, x-header-4",
84-
"access-control-max-age": "3600",
85-
"access-control-allow-credentials": "true",
79+
HeadersWithMultipleValues: map[string][]string{
80+
"access-control-allow-origin": {"https://www.foo.com"},
81+
"access-control-allow-methods": {
82+
"GET, OPTIONS",
83+
"OPTIONS, GET",
84+
},
85+
"access-control-allow-headers": {
86+
"x-header-1, x-header-2",
87+
"x-header-2, x-header-1",
88+
},
89+
"access-control-expose-headers": {
90+
"x-header-3, x-header-4",
91+
"x-header-4, x-header-3",
92+
},
93+
"access-control-max-age": {"3600"},
94+
"access-control-allow-credentials": {"true"},
8695
},
8796
// Ignore whitespace when comparing the response headers. This is because some
8897
// implementations add a space after each comma, and some don't. Both are valid.
@@ -114,14 +123,22 @@ var HTTPRouteCORS = suite.ConformanceTest{
114123
},
115124
Namespace: "",
116125
Response: http.Response{
117-
StatusCodes: []int{200, 204},
118-
Headers: map[string]string{
119-
"access-control-allow-origin": "https://www.bar.com",
120-
"access-control-allow-methods": "GET, OPTIONS",
121-
"access-control-allow-headers": "x-header-1, x-header-2",
122-
"access-control-expose-headers": "x-header-3, x-header-4",
123-
"access-control-max-age": "3600",
124-
"access-control-allow-credentials": "true",
126+
StatusCode: 200,
127+
HeadersWithMultipleValues: map[string][]string{
128+
"access-control-allow-origin": {"https://www.bar.com"},
129+
"access-control-allow-methods": {
130+
"GET, OPTIONS",
131+
"OPTIONS, GET"},
132+
"access-control-allow-headers": {
133+
"x-header-1, x-header-2",
134+
"x-header-2, x-header-1",
135+
},
136+
"access-control-expose-headers": {
137+
"x-header-3, x-header-4",
138+
"x-header-4, x-header-3",
139+
},
140+
"access-control-max-age": {"3600"},
141+
"access-control-allow-credentials": {"true"},
125142
},
126143
// Ignore whitespace when comparing the response headers. This is because some
127144
// implementations add a space after each comma, and some don't. Both are valid.

conformance/utils/http/http.go

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,11 @@ type ExpectedRequest struct {
8484

8585
// Response defines expected properties of a response from a backend.
8686
type Response struct {
87-
StatusCode int
88-
StatusCodes []int // alternative to StatusCode, allows multiple acceptable codes
89-
Headers map[string]string
90-
AbsentHeaders []string
87+
StatusCode int
88+
StatusCodes []int // alternative to StatusCode, allows multiple acceptable codes
89+
Headers map[string]string
90+
HeadersWithMultipleValues map[string][]string // allows multiple values for a header
91+
AbsentHeaders []string
9192

9293
// IgnoreWhitespace will cause whitespace to be ignored when comparing the respond
9394
// header values.
@@ -343,26 +344,52 @@ func CompareRequest(t *testing.T, req *roundtripper.Request, cReq *roundtripper.
343344
}
344345
}
345346

346-
if expected.Response.Headers != nil {
347+
if expected.Response.Headers != nil || expected.Response.HeadersWithMultipleValues != nil {
347348
if cRes.Headers == nil {
348349
return fmt.Errorf("no headers captured, expected %v", len(expected.Response.Headers))
349350
}
350351
for name, val := range cRes.Headers {
351352
cRes.Headers[strings.ToLower(name)] = val
352353
}
353354

354-
for name, expectedVal := range expected.Response.Headers {
355-
actualVal, ok := cRes.Headers[strings.ToLower(name)]
356-
if !ok {
357-
return fmt.Errorf("expected %s header to be set, actual headers: %v", name, cRes.Headers)
358-
}
359-
actualValStr := strings.Join(actualVal, ",")
360-
if expected.Response.IgnoreWhitespace {
361-
actualValStr = strings.ReplaceAll(actualValStr, " ", "")
362-
expectedVal = strings.ReplaceAll(expectedVal, " ", "")
355+
if expected.Response.HeadersWithMultipleValues != nil {
356+
for name, expectedVals := range expected.Response.HeadersWithMultipleValues {
357+
actualVal, ok := cRes.Headers[strings.ToLower(name)]
358+
if !ok {
359+
return fmt.Errorf("expected %s header to be set, actual headers: %v", name, cRes.Headers)
360+
}
361+
actualValStr := strings.Join(actualVal, ",")
362+
if expected.Response.IgnoreWhitespace {
363+
actualValStr = strings.ReplaceAll(actualValStr, " ", "")
364+
for i := range expectedVals {
365+
expectedVals[i] = strings.ReplaceAll(expectedVals[i], " ", "")
366+
}
367+
}
368+
matched := false
369+
for _, expectedVal := range expectedVals {
370+
if actualValStr == expectedVal {
371+
matched = true
372+
break
373+
}
374+
}
375+
if !matched {
376+
return fmt.Errorf("expected %s header to be set to one of %v, got %s", name, expectedVals, actualValStr)
377+
}
363378
}
364-
if actualValStr != expectedVal {
365-
return fmt.Errorf("expected %s header to be set to %s, got %s", name, expectedVal, actualValStr)
379+
} else {
380+
for name, expectedVal := range expected.Response.Headers {
381+
actualVal, ok := cRes.Headers[strings.ToLower(name)]
382+
if !ok {
383+
return fmt.Errorf("expected %s header to be set, actual headers: %v", name, cRes.Headers)
384+
}
385+
actualValStr := strings.Join(actualVal, ",")
386+
if expected.Response.IgnoreWhitespace {
387+
actualValStr = strings.ReplaceAll(actualValStr, " ", "")
388+
expectedVal = strings.ReplaceAll(expectedVal, " ", "")
389+
}
390+
if actualValStr != expectedVal {
391+
return fmt.Errorf("expected %s header to be set to %s, got %s", name, expectedVal, actualValStr)
392+
}
366393
}
367394
}
368395
}

0 commit comments

Comments
 (0)