|
| 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 | +} |
0 commit comments