Skip to content

Commit d28cd59

Browse files
authored
conformance: add test to check for proper cors allow-credentials behvior (#3990)
* conformance: add test to check for proper cors allow-credentials behavior * chore: rephrase description * fix: SupportHTTPRouteCORS FeatureName
1 parent cd9f05f commit d28cd59

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
Copyright 2025 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, CORSAllowCredentialsBehavior)
32+
}
33+
34+
var CORSAllowCredentialsBehavior = suite.ConformanceTest{
35+
ShortName: "CORSAllowCredentialsBehavior",
36+
Description: "Validate ACA-Credentials responses",
37+
Manifests: []string{"tests/cors-allow-credentials-behavior.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-allow-credentials", 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+
origin := "https://app.example"
51+
52+
testCases := []http.ExpectedResponse{
53+
{
54+
Request: http.Request{
55+
Method: "GET",
56+
Path: "/cors-behavior-creds-false",
57+
Headers: map[string]string{
58+
"Origin": origin,
59+
"Cookie": "sid=abc123",
60+
"Authorization": "Bearer test",
61+
},
62+
},
63+
Response: http.Response{
64+
StatusCode: 200,
65+
AbsentHeaders: []string{"Access-Control-Allow-Credentials"},
66+
},
67+
Namespace: ns,
68+
},
69+
{
70+
Request: http.Request{
71+
Method: "GET",
72+
Path: "/cors-behavior-creds-true",
73+
Headers: map[string]string{
74+
"Origin": origin,
75+
"Cookie": "sid=abc123",
76+
"Authorization": "Bearer test",
77+
},
78+
},
79+
Response: http.Response{
80+
StatusCode: 200,
81+
Headers: map[string]string{
82+
"Access-Control-Allow-Credentials": "true",
83+
"Access-Control-Allow-Origin": origin,
84+
},
85+
},
86+
Namespace: ns,
87+
},
88+
}
89+
90+
for i := range testCases {
91+
// Declare tc here to avoid loop variable
92+
// reuse issues across parallel tests.
93+
tc := testCases[i]
94+
t.Run(tc.GetTestCaseName(i), func(t *testing.T) {
95+
t.Parallel()
96+
http.MakeRequestAndExpectEventuallyConsistentResponse(t, suite.RoundTripper, suite.TimeoutConfig, gwAddr, tc)
97+
})
98+
}
99+
},
100+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
apiVersion: gateway.networking.k8s.io/v1
2+
kind: HTTPRoute
3+
metadata:
4+
name: cors-allow-credentials
5+
namespace: gateway-conformance-infra
6+
spec:
7+
parentRefs:
8+
- name: same-namespace
9+
rules:
10+
- matches:
11+
- path:
12+
type: PathPrefix
13+
value: /cors-behavior-creds-false
14+
backendRefs:
15+
- name: infra-backend-v1
16+
port: 8080
17+
filters:
18+
- cors:
19+
allowCredentials: false
20+
type: CORS
21+
- matches:
22+
- path:
23+
type: PathPrefix
24+
value: /cors-behavior-creds-true
25+
backendRefs:
26+
- name: infra-backend-v1
27+
port: 8080
28+
filters:
29+
- cors:
30+
allowCredentials: true
31+
type: CORS
32+

pkg/features/httproute.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ const (
100100

101101
// This option indicates support for the name field in the HTTPRouteRule (extended conformance)
102102
SupportHTTPRouteNamedRouteRule FeatureName = "HTTPRouteNamedRouteRule"
103+
104+
// This option indicates support for the cors filter in the HTTPRouteFilter (extended conformance)
105+
SupportHTTPRouteCORS FeatureName = "HTTPRouteCORS"
103106
)
104107

105108
var (
@@ -198,6 +201,11 @@ var (
198201
Name: SupportHTTPRouteNamedRouteRule,
199202
Channel: FeatureChannelStandard,
200203
}
204+
// HTTPRouteCORS contains metadata for the SupportHTTPRouteCORS feature.
205+
HTTPRouteCORS = Feature{
206+
Name: SupportHTTPRouteCORS,
207+
Channel: FeatureChannelExperimental,
208+
}
201209
)
202210

203211
// HTTPRouteExtendedFeatures includes all extended features for HTTPRoute
@@ -223,4 +231,5 @@ var HTTPRouteExtendedFeatures = sets.New(
223231
HTTPRouteBackendProtocolH2CFeature,
224232
HTTPRouteBackendProtocolWebSocketFeature,
225233
HTTPRouteNamedRouteRule,
234+
HTTPRouteCORS,
226235
)

0 commit comments

Comments
 (0)