Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TT-13439] update response content-length when response body is modified by coprocess response hook #6732

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions coprocess/grpc/coprocess_grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ import (
"net"
"net/http"
"os"
"strconv"
"strings"
"testing"
"time"

"github.com/TykTechnologies/tyk/header"

"github.com/stretchr/testify/assert"

"google.golang.org/grpc"
Expand Down Expand Up @@ -567,6 +570,9 @@ func TestGRPCDispatch(t *testing.T) {
Code: http.StatusOK,
Headers: headers,
BodyMatch: "newbody",
HeadersMatch: map[string]string{
header.ContentLength: strconv.Itoa(len("newbody")),
},
})
})

Expand Down
6 changes: 6 additions & 0 deletions gateway/coprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,12 @@ func (h *CustomMiddlewareResponseHook) HandleResponse(rw http.ResponseWriter, re
bodyBuf := bytes.NewBuffer(retObject.Response.RawBody)
res.Body = ioutil.NopCloser(bodyBuf)

//set response body length with the size of response body returned from the hook
//so that it is updated accordingly in the response object
responseBodyLen := len(retObject.Response.RawBody)
res.ContentLength = int64(responseBodyLen)
res.Header.Set("Content-Length", fmt.Sprintf("%d", responseBodyLen))

res.StatusCode = int(retObject.Response.StatusCode)
return nil
}
Expand Down
Loading