Skip to content

Commit

Permalink
tests: direct response
Browse files Browse the repository at this point in the history
Signed-off-by: Arko Dasgupta <arko@tetrate.io>
  • Loading branch information
arkodg committed Oct 31, 2024
1 parent b0c6f8c commit 45f164e
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 4 deletions.
64 changes: 64 additions & 0 deletions test/e2e/testdata/direct-response.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: direct-response
namespace: gateway-conformance-infra
spec:
parentRefs:
- name: same-namespace
rules:
- matches:
- path:
type: PathPrefix
value: /inline
filters:
- type: ExtensionRef
extensionRef:
group: gateway.envoyproxy.io
kind: HTTPRouteFilter
name: direct-response-inline
- matches:
- path:
type: PathPrefix
value: /value-ref
filters:
- type: ExtensionRef
extensionRef:
group: gateway.envoyproxy.io
kind: HTTPRouteFilter
name: direct-response-value-ref
---
apiVersion: v1
kind: ConfigMap
metadata:
name: response-value-ref
namespace: gateway-conformance-infra
data:
response.body: '{"error": "Internal Server Error"}'
---
apiVersion: gateway.envoyproxy.io/v1alpha1
kind: HTTPRouteFilter
metadata:
name: direct-response-inline
namespace: gateway-conformance-infra
spec:
directResponse:
contentType: text/plain
body:
type: Inline
inline: "Oops! Your request is not found."
---
apiVersion: gateway.envoyproxy.io/v1alpha1
kind: HTTPRouteFilter
metadata:
name: direct-response-value-ref
namespace: gateway-conformance-infra
spec:
directResponse:
contentType: application/json
body:
type: ValueRef
valueRef:
group: ""
kind: ConfigMap
name: value-ref-response
77 changes: 77 additions & 0 deletions test/e2e/tests/direct-response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright Envoy Gateway Authors
// SPDX-License-Identifier: Apache-2.0
// The full text of the Apache license is available in the LICENSE file at
// the root of the repo.

//go:build e2e

package tests

import (
"fmt"

Check failure on line 11 in test/e2e/tests/direct-response.go

View workflow job for this annotation

GitHub Actions / lint

"fmt" imported and not used
"io"
"net/http"
"net/url"
"testing"

"k8s.io/apimachinery/pkg/types"
gwapiv1 "sigs.k8s.io/gateway-api/apis/v1"

Check failure on line 18 in test/e2e/tests/direct-response.go

View workflow job for this annotation

GitHub Actions / lint

"sigs.k8s.io/gateway-api/apis/v1" imported as gwapiv1 and not used
gwapiv1a2 "sigs.k8s.io/gateway-api/apis/v1alpha2"

Check failure on line 19 in test/e2e/tests/direct-response.go

View workflow job for this annotation

GitHub Actions / lint

"sigs.k8s.io/gateway-api/apis/v1alpha2" imported as gwapiv1a2 and not used
httputils "sigs.k8s.io/gateway-api/conformance/utils/http"
"sigs.k8s.io/gateway-api/conformance/utils/kubernetes"
"sigs.k8s.io/gateway-api/conformance/utils/suite"

"github.com/envoyproxy/gateway/internal/gatewayapi"

Check failure on line 24 in test/e2e/tests/direct-response.go

View workflow job for this annotation

GitHub Actions / lint

"github.com/envoyproxy/gateway/internal/gatewayapi" imported and not used
"github.com/envoyproxy/gateway/internal/gatewayapi/resource"

Check failure on line 25 in test/e2e/tests/direct-response.go

View workflow job for this annotation

GitHub Actions / lint

"github.com/envoyproxy/gateway/internal/gatewayapi/resource" imported and not used (typecheck)
)

func init() {
ConformanceTests = append(ConformanceTests, DirectResponseTest)
}

var DirectResponseTest = suite.ConformanceTest{
ShortName: "DirectResponse",
Description: "Direct",
Manifests: []string{"testdata/direct-response.yaml"},
Test: func(t *testing.T, suite *suite.ConformanceTestSuite) {
t.Run("direct response", func(t *testing.T) {
ns := "gateway-conformance-infra"
routeNN := types.NamespacedName{Name: "direct-response", 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)
verifyCustomResponse(t, gwAddr, "/status/404", "text/plain", "Oops! Your request is not found.")
verifyCustomResponse(t, gwAddr, "/status/500", "application/json", `{"error": "Internal Server Error"}`)
})
},
}

func verifyCustomResponse(t *testing.T, gwAddr, path, expectedContentType, expectedBody string) {

Check failure on line 50 in test/e2e/tests/direct-response.go

View workflow job for this annotation

GitHub Actions / lint

other declaration of verifyCustomResponse
reqURL := url.URL{
Scheme: "http",
Host: httputils.CalculateHost(t, gwAddr, "http"),
Path: path,
}

rsp, err := http.Get(reqURL.String())
if err != nil {
t.Fatalf("failed to get response: %v", err)
}

// Verify that the response body is overridden
defer rsp.Body.Close()
body, err := io.ReadAll(rsp.Body)
if err != nil {
t.Fatalf("failed to read response body: %v", err)
}
if string(body) != expectedBody {
t.Errorf("expected response body to be %s but got %s", expectedBody, string(body))
}

// Verify that the content type is overridden
contentType := rsp.Header.Get("Content-Type")
if contentType != expectedContentType {
t.Errorf("expected content type to be %s but got %s", expectedContentType, contentType)
}
}
8 changes: 4 additions & 4 deletions test/e2e/tests/response-override.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,17 @@ var ResponseOverrideTest = suite.ConformanceTest{
Name: gwapiv1.ObjectName(gwNN.Name),
}
BackendTrafficPolicyMustBeAccepted(t, suite.Client, types.NamespacedName{Name: "response-override", Namespace: ns}, suite.ControllerName, ancestorRef)
verifyResponseOverride(t, gwAddr, 404, "text/plain", "Oops! Your request is not found.")
verifyResponseOverride(t, gwAddr, 500, "application/json", `{"error": "Internal Server Error"}`)
verifyCustomResponse(t, gwAddr, "/status/404", "text/plain", "Oops! Your request is not found.")
verifyCustomResponse(t, gwAddr, "/status/500", "application/json", `{"error": "Internal Server Error"}`)
})
},
}

func verifyResponseOverride(t *testing.T, gwAddr string, statusCode int, expectedContentType string, expectedBody string) {
func verifyCustomResponse(t *testing.T, gwAddr, path, expectedContentType, expectedBody string) {

Check failure on line 56 in test/e2e/tests/response-override.go

View workflow job for this annotation

GitHub Actions / lint

verifyCustomResponse redeclared in this block
reqURL := url.URL{
Scheme: "http",
Host: httputils.CalculateHost(t, gwAddr, "http"),
Path: fmt.Sprintf("/status/%d", statusCode),
Path: path,
}

rsp, err := http.Get(reqURL.String())
Expand Down

0 comments on commit 45f164e

Please sign in to comment.