Do not set Content-Length in GET and HEAD requests according to pull request #31 (thanks to @buaazp )

This commit is contained in:
Aliaksandr Valialkin
2016-01-05 15:47:30 +02:00
parent 2633e1c90d
commit dbe26b3541
2 changed files with 24 additions and 2 deletions
+5 -2
View File
@@ -717,11 +717,14 @@ func (req *Request) Write(w *bufio.Writer) error {
req.Header.SetMultipartFormBoundary(req.multipartFormBoundary)
}
req.Header.SetContentLength(len(body))
hasBody := !req.Header.noBody()
if hasBody {
req.Header.SetContentLength(len(body))
}
if err = req.Header.Write(w); err != nil {
return err
}
if !req.Header.noBody() {
if hasBody {
_, err = w.Write(body)
} else if len(body) > 0 {
return fmt.Errorf("Non-zero body for non-POST request. body=%q", body)
+19
View File
@@ -10,6 +10,25 @@ import (
"testing"
)
func TestRequestNoContentLength(t *testing.T) {
var r Request
r.Header.SetMethod("HEAD")
r.Header.SetHost("foobar")
s := r.String()
if strings.Contains(s, "Content-Length: ") {
t.Fatalf("unexpected content-length in HEAD request %q", s)
}
r.Header.SetMethod("POST")
fmt.Fprintf(r.BodyWriter(), "foobar body")
s = r.String()
if !strings.Contains(s, "Content-Length: ") {
t.Fatalf("missing content-length header in non-GET request %q", s)
}
}
func TestRequestReadGzippedBody(t *testing.T) {
var r Request