From f762a37faf6abf9f2fa3c9ef3f4b63f28050a025 Mon Sep 17 00:00:00 2001 From: Tommy Situ Date: Wed, 17 Jul 2024 23:13:56 +0100 Subject: [PATCH] #1122 do not set transfer encoding header for 204 response while doing https proxy --- functional-tests/core/ft_proxy_https_test.go | 53 +++++++++++++++++-- go.mod | 4 +- go.sum | 8 +-- vendor/github.com/SpectoLabs/goproxy/https.go | 4 +- vendor/modules.txt | 6 +-- 5 files changed, 60 insertions(+), 15 deletions(-) diff --git a/functional-tests/core/ft_proxy_https_test.go b/functional-tests/core/ft_proxy_https_test.go index ac30fabcd..203330f3b 100644 --- a/functional-tests/core/ft_proxy_https_test.go +++ b/functional-tests/core/ft_proxy_https_test.go @@ -5,7 +5,7 @@ import ( "github.com/dghubble/sling" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" - "io/ioutil" + "io" "net/http" "net/http/httptest" ) @@ -87,7 +87,7 @@ var _ = Describe("When I run Hoverfly", func() { response := hoverfly.Proxy(sling.New().Get("https://hoverfly.io/path")) Expect(response.StatusCode).To(Equal(http.StatusOK)) - body, err := ioutil.ReadAll(response.Body) + body, err := io.ReadAll(response.Body) Expect(err).To(BeNil()) Expect(string(body)).To(Equal("OK")) @@ -131,7 +131,7 @@ var _ = Describe("When I run Hoverfly", func() { response := hoverfly.Proxy(sling.New().Get("https://hoverfly.io/path")) Expect(response.StatusCode).To(Equal(http.StatusOK)) - body, err := ioutil.ReadAll(response.Body) + body, err := io.ReadAll(response.Body) Expect(err).To(BeNil()) Expect(string(body)).To(Equal("OK")) @@ -173,7 +173,7 @@ var _ = Describe("When I run Hoverfly", func() { response := hoverfly.Proxy(sling.New().Get("https://hoverfly.io/path")) Expect(response.StatusCode).To(Equal(http.StatusOK)) - body, err := ioutil.ReadAll(response.Body) + body, err := io.ReadAll(response.Body) Expect(err).To(BeNil()) Expect(string(body)).To(Equal("OK")) @@ -183,6 +183,51 @@ var _ = Describe("When I run Hoverfly", func() { Expect(response.ContentLength).To(Equal(int64(-1))) Expect(response.TransferEncoding).To(Equal([]string{"chunked"})) }) + + It("should not set transfer encoding header for 204 response", func() { + + hoverfly.ImportSimulation(`{ + "data": { + "pairs": [ + { + "request": { + "path": [ + { + "matcher": "exact", + "value": "/path" + } + ], + "scheme": [ + { + "matcher": "exact", + "value": "https" + } + ] + }, + "response": { + "status": 204, + "body": "" + } + } + ] + }, + "meta": { + "schemaVersion": "v5.2" + } + }`) + response := hoverfly.Proxy(sling.New().Get("https://hoverfly.io/path")) + Expect(response.StatusCode).To(Equal(http.StatusNoContent)) + + body, err := io.ReadAll(response.Body) + Expect(err).To(BeNil()) + Expect(string(body)).To(Equal("")) + + Expect(response.Header.Get("Content-Length")).To(Equal("")) + Expect(response.Header.Get("Transfer-Encoding")).To(Equal("")) + + Expect(response.ContentLength).To(Equal(int64(0))) + Expect(response.TransferEncoding).To(BeNil()) + }) }) Context("and it uses default certificate and key configuration", func() { diff --git a/go.mod b/go.mod index bd53d2b51..72513e875 100644 --- a/go.mod +++ b/go.mod @@ -4,8 +4,8 @@ go 1.22.4 require ( github.com/ChrisTrenkamp/xsel v0.9.6 - github.com/SpectoLabs/goproxy v0.0.0-20220724221645-71c396c297b7 - github.com/SpectoLabs/goproxy/ext v0.0.0-20220724221645-71c396c297b7 + github.com/SpectoLabs/goproxy v0.0.0-20240717215706-55e01f38b2c9 + github.com/SpectoLabs/goproxy/ext v0.0.0-20220724222243-c982a2c966ae github.com/SpectoLabs/goxml2json v0.0.0-20240121223617-8e03292c14ea github.com/SpectoLabs/raymond v2.0.3-0.20240313210732-e0e216cf0920+incompatible github.com/antonholmquist/jason v1.0.1-0.20160829104012-962e09b85496 diff --git a/go.sum b/go.sum index 784907ed5..a3ba946b9 100644 --- a/go.sum +++ b/go.sum @@ -3,10 +3,10 @@ github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbi github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/ChrisTrenkamp/xsel v0.9.6 h1:0TmX81Wq2v2SnssP9SwBzz68KArsj5a8Dt9Ppj095QM= github.com/ChrisTrenkamp/xsel v0.9.6/go.mod h1:72u+GN8aQUQNyZ5B+QIqWy7WPUey921sjN1k9FJ6Hbo= -github.com/SpectoLabs/goproxy v0.0.0-20220724221645-71c396c297b7 h1:TTZvVLRa0sjoFrxBOQdmuktR/IueSVyPA1Q+D8pHr9w= -github.com/SpectoLabs/goproxy v0.0.0-20220724221645-71c396c297b7/go.mod h1:tj8G0iNIS9fkG5PSrzQYLUXcvhtqI8XbIld9PqpYaFw= -github.com/SpectoLabs/goproxy/ext v0.0.0-20220724221645-71c396c297b7 h1:MxMvFwL66nKnrmjHhp9GODbHVZ+/qS2MmvnXz/nDdR4= -github.com/SpectoLabs/goproxy/ext v0.0.0-20220724221645-71c396c297b7/go.mod h1:rnDN1CpKP9iJXwxRtnvM7d5wdPHQY0K5NkHeXWPlQ+o= +github.com/SpectoLabs/goproxy v0.0.0-20240717215706-55e01f38b2c9 h1:KLYZMZu6prwwPmCalgoE+ah7FxANPjMH9kERMN9TMfc= +github.com/SpectoLabs/goproxy v0.0.0-20240717215706-55e01f38b2c9/go.mod h1:43+j4Hye/DLXHbJHM0O5dcwOkQp7zGCgDKTq6nzGXQM= +github.com/SpectoLabs/goproxy/ext v0.0.0-20220724222243-c982a2c966ae h1:3Qfw4yZArPnqymR+G9sHRNBXHbrtj84wXaj6+EGkBjs= +github.com/SpectoLabs/goproxy/ext v0.0.0-20220724222243-c982a2c966ae/go.mod h1:rnDN1CpKP9iJXwxRtnvM7d5wdPHQY0K5NkHeXWPlQ+o= github.com/SpectoLabs/goxml2json v0.0.0-20240121223617-8e03292c14ea h1:VAu3xT0OShIqnm4K3TdPpxCd3k4uyXq/H6W76ueIWf0= github.com/SpectoLabs/goxml2json v0.0.0-20240121223617-8e03292c14ea/go.mod h1:P8Yk6l9gKxTfgnJeXVEQKCH7nGgvhNdeLs0zVs2h2KM= github.com/SpectoLabs/raymond v2.0.3-0.20240313210732-e0e216cf0920+incompatible h1:qI0OVNVzpRLUMHD3lxGOXSHsH87xHp09IuQm9hjTfTA= diff --git a/vendor/github.com/SpectoLabs/goproxy/https.go b/vendor/github.com/SpectoLabs/goproxy/https.go index fa54bd270..a460e2304 100644 --- a/vendor/github.com/SpectoLabs/goproxy/https.go +++ b/vendor/github.com/SpectoLabs/goproxy/https.go @@ -267,7 +267,7 @@ func (proxy *ProxyHttpServer) handleHttps(w http.ResponseWriter, r *http.Request return } - if resp.Request.Method == "HEAD" { + if resp.Request.Method == "HEAD" || resp.StatusCode == 204 { // don't change Content-Length for HEAD request } else { // Since we don't know the length of resp, return chunked encoded response @@ -286,7 +286,7 @@ func (proxy *ProxyHttpServer) handleHttps(w http.ResponseWriter, r *http.Request return } - if resp.Request.Method == "HEAD" { + if resp.Request.Method == "HEAD" || resp.StatusCode == 204 { // Don't write out a response body for HEAD request } else { chunked := newChunkedWriter(rawClientTls) diff --git a/vendor/modules.txt b/vendor/modules.txt index edf48507e..b57435b89 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -15,10 +15,10 @@ github.com/ChrisTrenkamp/xsel/grammar/token github.com/ChrisTrenkamp/xsel/node github.com/ChrisTrenkamp/xsel/parser github.com/ChrisTrenkamp/xsel/store -# github.com/SpectoLabs/goproxy v0.0.0-20220724221645-71c396c297b7 -## explicit +# github.com/SpectoLabs/goproxy v0.0.0-20240717215706-55e01f38b2c9 +## explicit; go 1.18 github.com/SpectoLabs/goproxy -# github.com/SpectoLabs/goproxy/ext v0.0.0-20220724221645-71c396c297b7 +# github.com/SpectoLabs/goproxy/ext v0.0.0-20220724222243-c982a2c966ae ## explicit github.com/SpectoLabs/goproxy/ext/auth # github.com/SpectoLabs/goxml2json v0.0.0-20240121223617-8e03292c14ea