Skip to content

Commit 51d1e15

Browse files
committed
add test for cors
Signed-off-by: Huabing (Robin) Zhao <[email protected]>
1 parent 3222422 commit 51d1e15

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed

conformance/tests/httproute-cors.go

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
Copyright 2022 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package tests
18+
19+
import (
20+
"testing"
21+
22+
"k8s.io/apimachinery/pkg/types"
23+
24+
"sigs.k8s.io/gateway-api/conformance/utils/http"
25+
"sigs.k8s.io/gateway-api/conformance/utils/kubernetes"
26+
"sigs.k8s.io/gateway-api/conformance/utils/suite"
27+
"sigs.k8s.io/gateway-api/pkg/features"
28+
)
29+
30+
func init() {
31+
ConformanceTests = append(ConformanceTests, HTTPRouteRewritePath)
32+
}
33+
34+
var HTTPRouteCORS = suite.ConformanceTest{
35+
ShortName: "HTTPRouteCORS",
36+
Description: "An HTTPRoute with CORS filter",
37+
Manifests: []string{"tests/httproute-cors.yaml"},
38+
Features: []features.FeatureName{
39+
features.SupportGateway,
40+
features.SupportHTTPRoute,
41+
features.SupportHTTPRouteCORS,
42+
},
43+
Test: func(t *testing.T, suite *suite.ConformanceTestSuite) {
44+
ns := "gateway-conformance-infra"
45+
routeNN := types.NamespacedName{Name: "cors", Namespace: ns}
46+
gwNN := types.NamespacedName{Name: "same-namespace", Namespace: ns}
47+
gwAddr := kubernetes.GatewayAndHTTPRoutesMustBeAccepted(t, suite.Client, suite.TimeoutConfig, suite.ControllerName, kubernetes.NewGatewayRef(gwNN), routeNN)
48+
kubernetes.HTTPRouteMustHaveResolvedRefsConditionsTrue(t, suite.Client, suite.TimeoutConfig, routeNN, gwNN)
49+
50+
testCases := []http.ExpectedResponse{
51+
{
52+
Request: http.Request{
53+
Path: "/",
54+
Method: "OPTIONS",
55+
Headers: map[string]string{
56+
"Origin": "https://www.foo.com",
57+
"access-control-request-method": "GET",
58+
"access-control-request-headers": "x-header-1, x-header-2",
59+
},
60+
},
61+
// Set the expected request properties to empty strings.
62+
// This is a workaround to avoid the test failure.
63+
// The response body is empty because the request is a preflight request.
64+
ExpectedRequest: &http.ExpectedRequest{
65+
Request: http.Request{
66+
Host: "",
67+
Method: "OPTIONS",
68+
Path: "",
69+
Headers: nil,
70+
},
71+
},
72+
Response: http.Response{
73+
StatusCode: 200,
74+
Headers: map[string]string{
75+
"access-control-allow-origin": "https://www.foo.com",
76+
"access-control-allow-methods": "GET, POST, PUT, PATCH, DELETE, OPTIONS",
77+
"access-control-allow-headers": "x-header-1, x-header-2",
78+
"access-control-expose-headers": "x-header-3, x-header-4",
79+
},
80+
},
81+
Namespace: "",
82+
},
83+
}
84+
for i := range testCases {
85+
// Declare tc here to avoid loop variable
86+
// reuse issues across parallel tests.
87+
tc := testCases[i]
88+
t.Run(tc.GetTestCaseName(i), func(t *testing.T) {
89+
t.Parallel()
90+
http.MakeRequestAndExpectEventuallyConsistentResponse(t, suite.RoundTripper, suite.TimeoutConfig, gwAddr, tc)
91+
})
92+
}
93+
},
94+
}

conformance/tests/httproute-cors.yaml

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
apiVersion: gateway.networking.k8s.io/v1
2+
kind: HTTPRoute
3+
metadata:
4+
name: cors
5+
namespace: gateway-conformance-infra
6+
spec:
7+
parentRefs:
8+
- name: same-namespace
9+
rules:
10+
- filters:
11+
- type: CORS
12+
cors:
13+
allowOrigins:
14+
- "https://www.foo.com"
15+
- "https://www.bar.com"
16+
- "https://*.foobar.com"
17+
allowMethods:
18+
- GET
19+
- POST
20+
- PUT
21+
- PATCH
22+
- DELETE
23+
- OPTIONS
24+
allowHeaders:
25+
- "x-header-1"
26+
- "x-header-2"
27+
exposeHeaders:
28+
- "x-header-3"
29+
- "x-header-4"
30+
allowCredentials: true
31+
maxAge: 1000
32+
backendRefs:
33+
- name: infra-backend-v1
34+
port: 8080

0 commit comments

Comments
 (0)