Skip to content

Commit

Permalink
fix: remove unsafe b2s
Browse files Browse the repository at this point in the history
  • Loading branch information
li-jin-gou committed Aug 9, 2023
1 parent 2af5349 commit 12c6b54
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions reverse_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"context"
"errors"
"fmt"
"github.com/cloudwego/hertz/pkg/common/test/assert"
"net/http"
"strings"
"testing"
Expand Down Expand Up @@ -556,3 +557,34 @@ func TestReverseProxyTransferTrailer(t *testing.T) {
t.Errorf("handler got X-Trailer Trailer value %q; want 'trailer_value'", c)
}
}

func TestReverseProxySaveRespHeader(t *testing.T) {
const backendResponse = "I am the backend"
const backendStatus = 404
r := server.New(server.WithHostPorts("127.0.0.1:9997"))

r.GET("/proxy/backend", func(cc context.Context, ctx *app.RequestContext) {
ctx.Data(backendStatus, "application/json", []byte(backendResponse))
})

proxy, err := NewSingleHostReverseProxy("http://127.0.0.1:9997/proxy")
if err != nil {
t.Errorf("proxy error: %v", err)
}

r.GET("/backend", func(c context.Context, ctx *app.RequestContext) {
ctx.Response.Header.Set("aaa", "bbb")
proxy.ServeHTTP(c, ctx)
})
go r.Spin()
time.Sleep(time.Second)
cli, _ := client.NewClient()
req := protocol.AcquireRequest()
res := protocol.AcquireResponse()
req.SetRequestURI("http://localhost:9997/backend")
err = cli.Do(context.Background(), req, res)
if err != nil {
t.Fatalf("Get: %v", err)
}
assert.DeepEqual(t, "bbb", res.Header.Get("aaa"))
}

0 comments on commit 12c6b54

Please sign in to comment.