diff --git a/test/ingress/conformance/tests/httproute-enable-cors.go b/test/ingress/conformance/tests/httproute-enable-cors.go new file mode 100644 index 0000000000..d7256e9401 --- /dev/null +++ b/test/ingress/conformance/tests/httproute-enable-cors.go @@ -0,0 +1,104 @@ +// Copyright (c) 2022 Alibaba Group Holding Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package tests + +import ( + "testing" + + "github.com/alibaba/higress/test/ingress/conformance/utils/http" + "github.com/alibaba/higress/test/ingress/conformance/utils/suite" +) + +func init() { + HigressConformanceTests = append(HigressConformanceTests, HTTPRouteEnableCors) +} + +var HTTPRouteEnableCors = suite.ConformanceTest{ + ShortName: "HTTPRouteEnableCors", + Description: "A single Ingress in the higress-conformance-infra namespace demonstrates enable cors ability", + Manifests: []string{"tests/httproute-enable-cors.yaml"}, + Test: func(t *testing.T, suite *suite.ConformanceTestSuite) { + testcases := []http.Assertion{ + { + Meta: http.AssertionMeta{ + TestCaseName: "case1: unable cors", + TargetBackend: "infra-backend-v1", + TargetNamespace: "higress-conformance-infra", + }, + Request: http.AssertionRequest{ + ActualRequest: http.Request{ + Path: "/foo", + Host: "foo1.com", + Method: "OPTIONS", + Headers: map[string]string{"Origin": "http://bar.com"}, + }, + }, + Response: http.AssertionResponse{ + ExpectedResponse: http.Response{ + StatusCode: 200, + AbsentHeaders: []string{"Access-Control-Allow-Credentials", "Access-Control-Allow-Origin"}, + }, + }, + }, { + Meta: http.AssertionMeta{ + TestCaseName: "case2: enable cors", + TargetBackend: "infra-backend-v2", + TargetNamespace: "higress-conformance-infra", + }, + Request: http.AssertionRequest{ + ActualRequest: http.Request{ + Path: "/foo", + Host: "foo2.com", + Method: "OPTIONS", + Headers: map[string]string{"Origin": "http://bar.com"}, + }, + }, + Response: http.AssertionResponse{ + ExpectedResponse: http.Response{ + StatusCode: 200, + Headers: map[string]string{"Access-Control-Allow-Credentials": "true", "Access-Control-Allow-Origin": "http://bar.com"}, + }, + }, + }, { + Meta: http.AssertionMeta{ + TestCaseName: "case3: enable cors and allow headers", + TargetBackend: "infra-backend-v3", + TargetNamespace: "higress-conformance-infra", + }, + Request: http.AssertionRequest{ + ActualRequest: http.Request{ + Path: "/foo", + Host: "foo3.com", + Method: "OPTIONS", + Headers: map[string]string{"Origin": "http://bar.com"}, + }, + }, + Response: http.AssertionResponse{ + ExpectedResponse: http.Response{ + StatusCode: 200, + Headers: map[string]string{"Access-Control-Allow-Credentials": "true", "Access-Control-Allow-Origin": "http://bar.com", "Access-Control-Expose-Headers": "*"}, + }, + }, + }, + } + + t.Run("Enable Cors Cases Split", func(t *testing.T) { + for _, testcase := range testcases { + http.MakeRequestAndExpectEventuallyConsistentResponse(t, suite.RoundTripper, suite.TimeoutConfig, suite.GatewayAddress, testcase) + } + }) + + }, +} diff --git a/test/ingress/conformance/tests/httproute-enable-cors.yaml b/test/ingress/conformance/tests/httproute-enable-cors.yaml new file mode 100644 index 0000000000..8ad192ad8e --- /dev/null +++ b/test/ingress/conformance/tests/httproute-enable-cors.yaml @@ -0,0 +1,82 @@ +# Copyright (c) 2022 Alibaba Group Holding Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: higress-conformance-infra-unable-cors-test + namespace: higress-conformance-infra + annotations: + nginx.ingress.kubernetes.io/enable-cors: "false" +spec: + ingressClassName: higress + rules: + - host: "foo1.com" + http: + paths: + - pathType: Prefix + path: "/foo" + backend: + service: + name: infra-backend-v1 + port: + number: 8080 +--- +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: higress-conformance-infra-enable-cors-test + namespace: higress-conformance-infra + annotations: + nginx.ingress.kubernetes.io/enable-cors: "true" +spec: + ingressClassName: higress + rules: + - host: "foo2.com" + http: + paths: + - pathType: Prefix + path: "/foo" + backend: + service: + name: infra-backend-v2 + port: + number: 8080 +--- +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: higress-conformance-infra-enable-cors-config-test + namespace: higress-conformance-infra + annotations: + nginx.ingress.kubernetes.io/enable-cors: "true" + nginx.ingress.kubernetes.io/cors-allow-methods: "POST" + nginx.ingress.kubernetes.io/cors-allow-headers: "Host,Origin" + nginx.ingress.kubernetes.io/cors-expose-headers: "*" + nginx.ingress.kubernetes.io/cors-allow-origin: "http://bar.com" + nginx.ingress.kubernetes.io/cors-allow-credentials: "true" + nginx.ingress.kubernetes.io/cors-max-age: "60" +spec: + ingressClassName: higress + rules: + - host: "foo3.com" + http: + paths: + - pathType: Prefix + path: "/foo" + backend: + service: + name: infra-backend-v3 + port: + number: 8080 \ No newline at end of file diff --git a/test/ingress/e2e_test.go b/test/ingress/e2e_test.go index 2ad7ac86eb..35393e54d9 100644 --- a/test/ingress/e2e_test.go +++ b/test/ingress/e2e_test.go @@ -55,6 +55,7 @@ func TestHigressConformanceTests(t *testing.T) { tests.HTTPRouteRewritePath, tests.HTTPRouteRewriteHost, tests.HTTPRouteCanaryHeader, + tests.HTTPRouteEnableCors, } cSuite.Run(t, higressTests)