Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
Signed-off-by: Huabing Zhao <[email protected]>
  • Loading branch information
zhaohuabing committed Jan 8, 2025
1 parent db60df3 commit 1f85138
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 310 deletions.
117 changes: 116 additions & 1 deletion test/e2e/tests/compression.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,25 @@
package tests

import (
"compress/gzip"
"context"
"encoding/json"
"fmt"
"io"
"net"
nethttp "net/http"
"net/http/httputil"
"testing"

"k8s.io/apimachinery/pkg/types"
gwapiv1 "sigs.k8s.io/gateway-api/apis/v1"
gwapiv1a2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
"sigs.k8s.io/gateway-api/conformance/utils/config"
"sigs.k8s.io/gateway-api/conformance/utils/http"
"sigs.k8s.io/gateway-api/conformance/utils/kubernetes"
"sigs.k8s.io/gateway-api/conformance/utils/roundtripper"
"sigs.k8s.io/gateway-api/conformance/utils/suite"
"sigs.k8s.io/gateway-api/conformance/utils/tlog"

"github.com/envoyproxy/gateway/internal/gatewayapi"
"github.com/envoyproxy/gateway/internal/gatewayapi/resource"
Expand Down Expand Up @@ -59,7 +70,7 @@ var CompressionTest = suite.ConformanceTest{
},
Namespace: ns,
}
roundTripper := &DefaultRoundTripper{Debug: suite.Debug, TimeoutConfig: suite.TimeoutConfig}
roundTripper := &CompressionRoundTripper{Debug: suite.Debug, TimeoutConfig: suite.TimeoutConfig}
http.MakeRequestAndExpectEventuallyConsistentResponse(t, roundTripper, suite.TimeoutConfig, gwAddr, expectedResponse)
})

Expand Down Expand Up @@ -95,3 +106,107 @@ var CompressionTest = suite.ConformanceTest{
})
},
}

// CompressionRoundTripper implements roundtripper.RoundTripper and adds support for gzip encoding.
type CompressionRoundTripper struct {
Debug bool
TimeoutConfig config.TimeoutConfig
CustomDialContext func(context.Context, string, string) (net.Conn, error)
}

func (d *CompressionRoundTripper) CaptureRoundTrip(request roundtripper.Request) (*roundtripper.CapturedRequest, *roundtripper.CapturedResponse, error) {
return d.defaultRoundTrip(request, &nethttp.Transport{})
}

func (d *CompressionRoundTripper) defaultRoundTrip(request roundtripper.Request, transport nethttp.RoundTripper) (*roundtripper.CapturedRequest, *roundtripper.CapturedResponse, error) {
client := &nethttp.Client{}

client.Transport = transport

method := "GET"
if request.Method != "" {
method = request.Method
}
ctx, cancel := context.WithTimeout(context.Background(), d.TimeoutConfig.RequestTimeout)
defer cancel()
req, err := nethttp.NewRequestWithContext(ctx, method, request.URL.String(), nil)
if err != nil {
return nil, nil, err
}

if request.Host != "" {
req.Host = request.Host
}

if request.Headers != nil {
for name, value := range request.Headers {
req.Header.Set(name, value[0])
}
}

if d.Debug {
var dump []byte
dump, err = httputil.DumpRequestOut(req, true)
if err != nil {
return nil, nil, err
}

tlog.Logf(request.T, "Sending Request:\n%s\n\n", formatDump(dump))
}

resp, err := client.Do(req)
if err != nil {
return nil, nil, err
}
defer resp.Body.Close()

if d.Debug {
var dump []byte
dump, err = httputil.DumpResponse(resp, true)
if err != nil {
return nil, nil, err
}

tlog.Logf(request.T, "Received Response:\n%s\n\n", formatDump(dump))
}

cReq := &roundtripper.CapturedRequest{}
var body []byte
if resp.Header.Get("Content-Encoding") == "gzip" {
reader, err := gzip.NewReader(resp.Body)
if err != nil {
return nil, nil, err
}
defer reader.Close()
if body, err = io.ReadAll(reader); err != nil {
return nil, nil, err
}
} else {
if body, err = io.ReadAll(resp.Body); err != nil {
return nil, nil, err
}
}

// we cannot assume the response is JSON
if resp.Header.Get("Content-type") == "application/json" {
err = json.Unmarshal(body, cReq)
if err != nil {
return nil, nil, fmt.Errorf("unexpected error reading response: %w", err)
}
} else {
cReq.Method = method // assume it made the right request if the service being called isn't echoing
}

cRes := &roundtripper.CapturedResponse{
StatusCode: resp.StatusCode,
ContentLength: resp.ContentLength,
Protocol: resp.Proto,
Headers: resp.Header,
}

if resp.TLS != nil {
cRes.PeerCertificates = resp.TLS.PeerCertificates
}

return cReq, cRes, nil
}
Loading

0 comments on commit 1f85138

Please sign in to comment.