Commit 8233ecd1 authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

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/17699Reviewed-by: 's avatarRuss Cox <rsc@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 9d6e4b7e
......@@ -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)
}
......
......@@ -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) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment