]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: make NewRequest with empty method mean GET
authorBrad Fitzpatrick <bradfitz@golang.org>
Thu, 10 Dec 2015 18:24:03 +0000 (10:24 -0800)
committerBrad Fitzpatrick <bradfitz@golang.org>
Thu, 10 Dec 2015 18:51:50 +0000 (18:51 +0000)
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>

src/net/http/request.go
src/net/http/request_test.go

index c85713c42c60e6302717ae77bf042424df2d3e0c..9f740422ede4629648d7441069252fa8e599c30d 100644 (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)
        }
index a95a1d08c9274550cec27563e42be827d48dd6c4..ddbf8418e1aeed1dfc1ab3012c4575f7a82865d6 100644 (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) {