Skip to content

Commit

Permalink
Add e2e test for cors annotations alibaba#171 (alibaba#179)
Browse files Browse the repository at this point in the history
Co-authored-by: Xunzhuo <bitliu@tencent.com>
  • Loading branch information
Hinsteny and Xunzhuo authored Feb 14, 2023
1 parent cdcfe24 commit 6efa393
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 0 deletions.
104 changes: 104 additions & 0 deletions test/ingress/conformance/tests/httproute-enable-cors.go
Original file line number Diff line number Diff line change
@@ -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)
}
})

},
}
82 changes: 82 additions & 0 deletions test/ingress/conformance/tests/httproute-enable-cors.yaml
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions test/ingress/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func TestHigressConformanceTests(t *testing.T) {
tests.HTTPRouteRewritePath,
tests.HTTPRouteRewriteHost,
tests.HTTPRouteCanaryHeader,
tests.HTTPRouteEnableCors,
}

cSuite.Run(t, higressTests)
Expand Down

0 comments on commit 6efa393

Please sign in to comment.