mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
net/http: teach NewRequest that NoBody has ContentLength zero
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>
This commit is contained in:
parent
2cfb6d5442
commit
1102c70bc4
2 changed files with 13 additions and 1 deletions
|
|
@ -785,7 +785,9 @@ func NewRequest(method, urlStr string, body io.Reader) (*Request, error) {
|
||||||
return ioutil.NopCloser(&r), nil
|
return ioutil.NopCloser(&r), nil
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
req.ContentLength = -1 // unknown
|
if body != NoBody {
|
||||||
|
req.ContentLength = -1 // unknown
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// For client requests, Request.ContentLength of 0
|
// For client requests, Request.ContentLength of 0
|
||||||
// means either actually 0, or unknown. The only way
|
// means either actually 0, or unknown. The only way
|
||||||
|
|
|
||||||
|
|
@ -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) {
|
func testMissingFile(t *testing.T, req *Request) {
|
||||||
f, fh, err := req.FormFile("missing")
|
f, fh, err := req.FormFile("missing")
|
||||||
if f != nil {
|
if f != nil {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue