|
| 1 | +/* |
| 2 | +Copyright 2023 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 annotations |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "net/http" |
| 22 | + "strings" |
| 23 | + |
| 24 | + "github.com/onsi/ginkgo/v2" |
| 25 | + "github.com/stretchr/testify/assert" |
| 26 | + "k8s.io/ingress-nginx/test/e2e/framework" |
| 27 | +) |
| 28 | + |
| 29 | +const ( |
| 30 | + relativeRedirectsHostname = "rr.foo.com" |
| 31 | + relativeRedirectsRedirectPath = "/something" |
| 32 | + relativeRedirectsRelativeRedirectURL = "/new-location" |
| 33 | +) |
| 34 | + |
| 35 | +var _ = framework.DescribeAnnotation("relative-redirects", func() { |
| 36 | + f := framework.NewDefaultFramework("relative-redirects") |
| 37 | + |
| 38 | + ginkgo.BeforeEach(func() { |
| 39 | + f.NewHttpbunDeployment() |
| 40 | + f.NewEchoDeployment() |
| 41 | + }) |
| 42 | + |
| 43 | + ginkgo.It("configures Nginx correctly", func() { |
| 44 | + annotations := map[string]string{ |
| 45 | + "nginx.ingress.kubernetes.io/relative-redirects": "true", |
| 46 | + } |
| 47 | + |
| 48 | + ing := framework.NewSingleIngress(relativeRedirectsHostname, "/", relativeRedirectsHostname, f.Namespace, framework.HTTPBunService, 80, annotations) |
| 49 | + f.EnsureIngress(ing) |
| 50 | + |
| 51 | + var serverConfig string |
| 52 | + f.WaitForNginxServer(relativeRedirectsHostname, func(srvCfg string) bool { |
| 53 | + serverConfig = srvCfg |
| 54 | + return strings.Contains(serverConfig, fmt.Sprintf("server_name %s", relativeRedirectsHostname)) |
| 55 | + }) |
| 56 | + |
| 57 | + ginkgo.By("turning off absolute_redirect directive") |
| 58 | + assert.Contains(ginkgo.GinkgoT(), serverConfig, "absolute_redirect off;") |
| 59 | + }) |
| 60 | + |
| 61 | + ginkgo.It("should respond with absolute URL in Location", func() { |
| 62 | + absoluteRedirectURL := fmt.Sprintf("http://%s%s", relativeRedirectsHostname, relativeRedirectsRelativeRedirectURL) |
| 63 | + annotations := map[string]string{ |
| 64 | + "nginx.ingress.kubernetes.io/permanent-redirect": relativeRedirectsRelativeRedirectURL, |
| 65 | + "nginx.ingress.kubernetes.io/relative-redirects": "false", |
| 66 | + } |
| 67 | + |
| 68 | + ginkgo.By("setup ingress") |
| 69 | + ing := framework.NewSingleIngress(relativeRedirectsHostname, relativeRedirectsRedirectPath, relativeRedirectsHostname, f.Namespace, framework.EchoService, 80, annotations) |
| 70 | + f.EnsureIngress(ing) |
| 71 | + |
| 72 | + f.WaitForNginxServer(relativeRedirectsHostname, func(srvCfg string) bool { |
| 73 | + return strings.Contains(srvCfg, fmt.Sprintf("server_name %s", relativeRedirectsHostname)) |
| 74 | + }) |
| 75 | + |
| 76 | + ginkgo.By("sending request to redirected URL path") |
| 77 | + f.HTTPTestClient(). |
| 78 | + GET(relativeRedirectsRedirectPath). |
| 79 | + WithHeader("Host", relativeRedirectsHostname). |
| 80 | + Expect(). |
| 81 | + Status(http.StatusMovedPermanently). |
| 82 | + Header("Location").Equal(absoluteRedirectURL) |
| 83 | + }) |
| 84 | + |
| 85 | + ginkgo.It("should respond with relative URL in Location", func() { |
| 86 | + annotations := map[string]string{ |
| 87 | + "nginx.ingress.kubernetes.io/permanent-redirect": relativeRedirectsRelativeRedirectURL, |
| 88 | + "nginx.ingress.kubernetes.io/relative-redirects": "true", |
| 89 | + } |
| 90 | + |
| 91 | + ginkgo.By("setup ingress") |
| 92 | + ing := framework.NewSingleIngress(relativeRedirectsHostname, relativeRedirectsRedirectPath, relativeRedirectsHostname, f.Namespace, framework.EchoService, 80, annotations) |
| 93 | + f.EnsureIngress(ing) |
| 94 | + |
| 95 | + f.WaitForNginxServer(relativeRedirectsHostname, func(srvCfg string) bool { |
| 96 | + return strings.Contains(srvCfg, fmt.Sprintf("server_name %s", relativeRedirectsHostname)) |
| 97 | + }) |
| 98 | + |
| 99 | + ginkgo.By("sending request to redirected URL path") |
| 100 | + f.HTTPTestClient(). |
| 101 | + GET(relativeRedirectsRedirectPath). |
| 102 | + WithHeader("Host", relativeRedirectsHostname). |
| 103 | + Expect(). |
| 104 | + Status(http.StatusMovedPermanently). |
| 105 | + Header("Location").Equal(relativeRedirectsRelativeRedirectURL) |
| 106 | + }) |
| 107 | +}) |
0 commit comments