]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: teach NewRequest that NoBody has ContentLength zero
authorBrad Fitzpatrick <bradfitz@golang.org>
Wed, 30 Nov 2016 18:45:56 +0000 (18:45 +0000)
committerBrad Fitzpatrick <bradfitz@golang.org>
Thu, 1 Dec 2016 20:36:18 +0000 (20:36 +0000)
NoBody is new in Go 1.8.

Found while investigating #18117

Change-Id: I6bda030f358e2270f090d108cb3a89c8a2665fcb
Reviewed-on: https://go-review.googlesource.com/33714
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/net/http/request.go
src/net/http/request_test.go

index 7a86322f94c532fbd8e1d69299e866c17cc4e982..2d65ca3c8a19595a142366805076f6211deab603 100644 (file)
@@ -785,7 +785,9 @@ func NewRequest(method, urlStr string, body io.Reader) (*Request, error) {
                                return ioutil.NopCloser(&r), nil
                        }
                default:
-                       req.ContentLength = -1 // unknown
+                       if body != NoBody {
+                               req.ContentLength = -1 // unknown
+                       }
                }
                // For client requests, Request.ContentLength of 0
                // means either actually 0, or unknown. The only way
index 3c965c1e8ad1ad6a48dd4882d7d415ae1b51c624..483c025fb09bd10778659634d99e7916c37ff593 100644 (file)
@@ -825,6 +825,16 @@ func TestNewRequestGetBody(t *testing.T) {
        }
 }
 
+func TestNewRequestNoBody(t *testing.T) {
+       req, err := NewRequest("GET", "http://foo.com/", NoBody)
+       if err != nil {
+               t.Fatal(err)
+       }
+       if req.ContentLength != 0 {
+               t.Errorf("ContentLength = %d; want 0", req.ContentLength)
+       }
+}
+
 func testMissingFile(t *testing.T, req *Request) {
        f, fh, err := req.FormFile("missing")
        if f != nil {