Skip to content

Commit

Permalink
unit testing to response Content-Length.
Browse files Browse the repository at this point in the history
  • Loading branch information
telanflow authored and elazarl committed Aug 3, 2020
1 parent 153946a commit 57f4e53
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -969,3 +969,41 @@ func TestSimpleHttpRequest(t *testing.T) {

server.Shutdown(context.TODO())
}

func TestResponseContentLength(t *testing.T) {
// target server
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("hello world"))
}))
defer srv.Close()

// proxy server
proxy := goproxy.NewProxyHttpServer()
proxy.OnResponse().DoFunc(func(resp *http.Response, ctx *goproxy.ProxyCtx) *http.Response {
buf := &bytes.Buffer{}
buf.Write([]byte("change"))
resp.Body = ioutil.NopCloser(buf)
return resp
})
proxySrv := httptest.NewServer(proxy)
defer proxySrv.Close()

// send request
http.DefaultClient.Transport = &http.Transport{
Proxy: func(req *http.Request) (*url.URL, error) {
return url.Parse(proxySrv.URL)
},
}
req, _ := http.NewRequest(http.MethodGet, srv.URL, nil)
resp, _ := http.DefaultClient.Do(req)

body, _ := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()

if int64(len(body)) != resp.ContentLength {
t.Logf("response body: %s", string(body))
t.Logf("response body Length: %d", len(body))
t.Logf("response Content-Length: %d", resp.ContentLength)
t.Fatalf("Wrong response Content-Length.")
}
}

0 comments on commit 57f4e53

Please sign in to comment.