1
0
mirror of https://github.com/golang/go synced 2024-11-23 18:00:06 -07:00

net/http: make NewRequest with empty method mean GET

Until recently, we always permitted an empty string to NewRequest.
Keep that property, since it broke tests within in Google when trying
out Go 1.6, and probably would've broken others too.

Change-Id: Idddab1ae7b9423d5caac00af2c897fe1065b600b
Reviewed-on: https://go-review.googlesource.com/17699
Reviewed-by: Russ Cox <rsc@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Brad Fitzpatrick 2015-12-10 10:24:03 -08:00
parent 9d6e4b7e3a
commit 8233ecd1b2
2 changed files with 13 additions and 0 deletions

View File

@ -581,6 +581,12 @@ func validMethod(method string) bool {
// type's documentation for the difference between inbound and outbound
// request fields.
func NewRequest(method, urlStr string, body io.Reader) (*Request, error) {
if method == "" {
// We document that "" means "GET" for Request.Method, and people have
// relied on that from NewRequest, so keep that working.
// We still enforce validMethod for non-empty methods.
method = "GET"
}
if !validMethod(method) {
return nil, fmt.Errorf("net/http: invalid method %q", method)
}

View File

@ -370,6 +370,13 @@ func TestRequestInvalidMethod(t *testing.T) {
if err == nil || !strings.Contains(err.Error(), "invalid method") {
t.Errorf("Transport error = %v; want invalid method", err)
}
req, err = NewRequest("", "http://foo.com/", nil)
if err != nil {
t.Errorf("NewRequest(empty method) = %v; want nil", err)
} else if req.Method != "GET" {
t.Errorf("NewRequest(empty method) has method %q; want GET", req.Method)
}
}
func TestNewRequestContentLength(t *testing.T) {