2018-07-28 12:27:19 +00:00
|
|
|
/*
|
|
|
|
Copyright 2018 The Kubernetes Authors.
|
|
|
|
|
|
|
|
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 annotations
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
"github.com/onsi/ginkgo"
|
2018-07-28 12:27:19 +00:00
|
|
|
|
|
|
|
"k8s.io/ingress-nginx/test/e2e/framework"
|
|
|
|
)
|
|
|
|
|
2020-08-04 18:14:55 +00:00
|
|
|
var _ = framework.DescribeAnnotation("permanent-redirect permanent-redirect-code", func() {
|
2018-07-28 12:27:19 +00:00
|
|
|
f := framework.NewDefaultFramework("redirect")
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.It("should respond with a standard redirect code", func() {
|
|
|
|
ginkgo.By("setting permanent-redirect annotation")
|
2018-07-28 12:27:19 +00:00
|
|
|
|
|
|
|
host := "redirect"
|
|
|
|
redirectPath := "/something"
|
|
|
|
redirectURL := "http://redirect.example.com"
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
annotations := map[string]string{
|
|
|
|
"nginx.ingress.kubernetes.io/permanent-redirect": redirectURL,
|
|
|
|
}
|
2018-07-28 12:27:19 +00:00
|
|
|
|
2019-12-13 05:47:11 +00:00
|
|
|
ing := framework.NewSingleIngress(host, redirectPath, host, f.Namespace, framework.EchoService, 80, annotations)
|
2018-10-29 21:39:04 +00:00
|
|
|
f.EnsureIngress(ing)
|
2018-07-28 12:27:19 +00:00
|
|
|
|
2018-10-29 21:39:04 +00:00
|
|
|
f.WaitForNginxServer(host,
|
2018-07-28 12:27:19 +00:00
|
|
|
func(server string) bool {
|
2020-04-08 21:24:53 +00:00
|
|
|
return strings.Contains(server, fmt.Sprintf("return 301 %s;", redirectURL))
|
2018-07-28 12:27:19 +00:00
|
|
|
})
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.By("sending request to redirected URL path")
|
|
|
|
f.HTTPTestClient().
|
|
|
|
GET(redirectPath).
|
|
|
|
WithHeader("Host", host).
|
|
|
|
Expect().
|
|
|
|
Status(http.StatusMovedPermanently).
|
|
|
|
Header("Location").Equal(redirectURL)
|
2018-07-28 12:27:19 +00:00
|
|
|
})
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.It("should respond with a custom redirect code", func() {
|
|
|
|
ginkgo.By("setting permanent-redirect-code annotation")
|
2018-07-28 12:27:19 +00:00
|
|
|
|
|
|
|
host := "redirect"
|
|
|
|
redirectPath := "/something"
|
|
|
|
redirectURL := "http://redirect.example.com"
|
|
|
|
redirectCode := http.StatusFound
|
|
|
|
|
|
|
|
annotations := map[string]string{
|
|
|
|
"nginx.ingress.kubernetes.io/permanent-redirect": redirectURL,
|
|
|
|
"nginx.ingress.kubernetes.io/permanent-redirect-code": strconv.Itoa(redirectCode),
|
|
|
|
}
|
|
|
|
|
2019-12-13 05:47:11 +00:00
|
|
|
ing := framework.NewSingleIngress(host, redirectPath, host, f.Namespace, framework.EchoService, 80, annotations)
|
2018-10-29 21:39:04 +00:00
|
|
|
f.EnsureIngress(ing)
|
2018-07-28 12:27:19 +00:00
|
|
|
|
2018-10-29 21:39:04 +00:00
|
|
|
f.WaitForNginxServer(host,
|
2018-07-28 12:27:19 +00:00
|
|
|
func(server string) bool {
|
2020-04-08 21:24:53 +00:00
|
|
|
return strings.Contains(server, fmt.Sprintf("return %d %s;", redirectCode, redirectURL))
|
2018-07-28 12:27:19 +00:00
|
|
|
})
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.By("sending request to redirected URL path")
|
|
|
|
f.HTTPTestClient().
|
|
|
|
GET(redirectPath).
|
|
|
|
WithHeader("Host", host).
|
|
|
|
Expect().
|
|
|
|
Status(redirectCode).
|
|
|
|
Header("Location").Equal(redirectURL)
|
2018-07-28 12:27:19 +00:00
|
|
|
})
|
|
|
|
})
|