diff --git a/conformance/tests/http b/conformance/tests/http new file mode 100644 index 0000000000..e69de29bb2 diff --git a/conformance/tests/httproute-backend-request-redirect.go b/conformance/tests/httproute-backend-request-redirect.go new file mode 100644 index 0000000000..f79f6e401a --- /dev/null +++ b/conformance/tests/httproute-backend-request-redirect.go @@ -0,0 +1,72 @@ +/* +Copyright 2022 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 tests + +import ( + "testing" + + "k8s.io/apimachinery/pkg/types" + + "sigs.k8s.io/gateway-api/conformance/utils/http" + "sigs.k8s.io/gateway-api/conformance/utils/kubernetes" + "sigs.k8s.io/gateway-api/conformance/utils/suite" + "sigs.k8s.io/gateway-api/pkg/features" +) + +func init() { + ConformanceTests = append(ConformanceTests, HTTPRouteBackendPathRedirect) +} + +var HTTPRouteBackendPathRedirect = suite.ConformanceTest{ + ShortName: "HTTPRouteBackendPathRedirect", + Description: "A single HTTPRoute with a redirection filter for different backends", + Features: []features.SupportedFeature{ + features.SupportGateway, + features.SupportHTTPRoute, + features.SupportHTTPRouteBackendPathRedirect, + }, + Manifests: []string{"tests/httproute-backend-request-redirect.yaml"}, + Test: func(t *testing.T, suite *suite.ConformanceTestSuite) { + ns := "gateway-conformance-infra" + routeNN := types.NamespacedName{Name: "backend-request-redirect", Namespace: ns} + gwNN := types.NamespacedName{Name: "same-namespace", Namespace: ns} + gwAddr := kubernetes.GatewayAndHTTPRoutesMustBeAccepted(t, suite.Client, suite.TimeoutConfig, suite.ControllerName, kubernetes.NewGatewayRef(gwNN), routeNN) + kubernetes.HTTPRouteMustHaveResolvedRefsConditionsTrue(t, suite.Client, suite.TimeoutConfig, routeNN, gwNN) + + testCases := []http.ExpectedResponse{{ + Request: http.Request{Path: "/"}, + Backend: "infra-backend-v1", + Namespace: ns, + Response: http.Response{StatusCode: 302, Headers: map[string]string{"Location": "https://gateway-api.sigs.k8s.io"}}, + }, { + Request: http.Request{Path: "/"}, + Backend: "infra-backend-v2", + Namespace: ns, + Response: http.Response{StatusCode: 302, Headers: map[string]string{"Location": "http://www.redirect-example.com"}}, + }} + + for i := range testCases { + // Declare tc here to avoid loop variable + // reuse issues across parallel tests. + tc := testCases[i] + t.Run(tc.GetTestCaseName(i), func(t *testing.T) { + t.Parallel() + http.MakeRequestAndExpectEventuallyConsistentResponse(t, suite.RoundTripper, suite.TimeoutConfig, gwAddr, tc) + }) + } + }, +} diff --git a/conformance/tests/httproute-backend-request-redirect.yaml b/conformance/tests/httproute-backend-request-redirect.yaml new file mode 100644 index 0000000000..9a4faaa479 --- /dev/null +++ b/conformance/tests/httproute-backend-request-redirect.yaml @@ -0,0 +1,32 @@ +apiVersion: gateway.networking.k8s.io/v1 +kind: HTTPRoute +metadata: + name: backend-request-redirect + namespace: gateway-conformance-infra +spec: + parentRefs: + - name: same-namespace + rules: + - matches: + - path: + type: PathPrefix + value: / + backendRefs: + - name: infra-backend-v1 + port: 8080 + filters: + - type: RequestRedirect + requestRedirect: + scheme: https + hostname: redirect-example.com + port: 443 + statusCode: 302 + - name: infra-backend-v2 + port: 8080 + filters: + - type: RequestRedirect + requestRedirect: + scheme: http + hostname: redirect-example.com + port: 80 + statusCode: 301 diff --git a/pkg/features/features.go b/pkg/features/features.go index c9e3c1c52d..479d40c1b0 100644 --- a/pkg/features/features.go +++ b/pkg/features/features.go @@ -150,6 +150,9 @@ const ( // This option indicates support for HTTPRoute with a backendref with an appProtoocol 'kubernetes.io/ws' (extended conformance) SupportHTTPRouteBackendProtocolWebSocket SupportedFeature = "HTTPRouteBackendProtocolWebSocket" + + // This option indicates support for HTTPRoute backend path redirect (extended conformance) + SupportHTTPRouteBackendPathRedirect SupportedFeature = "HTTPRouteBackendPathRedirect" ) // HTTPRouteExtendedFeatures includes all extended features for HTTPRoute @@ -172,6 +175,7 @@ var HTTPRouteExtendedFeatures = sets.New( SupportHTTPRouteBackendRequestHeaderModification, SupportHTTPRouteBackendProtocolH2C, SupportHTTPRouteBackendProtocolWebSocket, + SupportHTTPRouteBackendPathRedirect, ) // -----------------------------------------------------------------------------