]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: make Request.ParseForm parse form-urlencoded for method PATCH too
authorMatt Aimonetti <mattaimonetti@gmail.com>
Tue, 4 Mar 2014 19:58:21 +0000 (11:58 -0800)
committerBrad Fitzpatrick <bradfitz@golang.org>
Tue, 4 Mar 2014 19:58:21 +0000 (11:58 -0800)
Fixes #7454

LGTM=bradfitz
R=golang-codereviews, bradfitz
CC=golang-codereviews
https://golang.org/cl/70990044

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

index 7a97770314d7f824f87d754d53ac8c044b13fa5c..480baf3aee11f980b76a71722d1cb73735bb24e0 100644 (file)
@@ -728,7 +728,7 @@ func parsePostForm(r *Request) (vs url.Values, err error) {
 func (r *Request) ParseForm() error {
        var err error
        if r.PostForm == nil {
-               if r.Method == "POST" || r.Method == "PUT" {
+               if r.Method == "POST" || r.Method == "PUT" || r.Method == "PATCH" {
                        r.PostForm, err = parsePostForm(r)
                }
                if r.PostForm == nil {
index 68d141398aab3e2420c22112489cec0af6a57999..223e02c294521d7985bff71b4e4be1bb55e51093 100644 (file)
@@ -60,6 +60,37 @@ func TestPostQuery(t *testing.T) {
        }
 }
 
+func TestPatchQuery(t *testing.T) {
+       req, _ := NewRequest("PATCH", "http://www.google.com/search?q=foo&q=bar&both=x&prio=1&empty=not",
+               strings.NewReader("z=post&both=y&prio=2&empty="))
+       req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
+
+       if q := req.FormValue("q"); q != "foo" {
+               t.Errorf(`req.FormValue("q") = %q, want "foo"`, q)
+       }
+       if z := req.FormValue("z"); z != "post" {
+               t.Errorf(`req.FormValue("z") = %q, want "post"`, z)
+       }
+       if bq, found := req.PostForm["q"]; found {
+               t.Errorf(`req.PostForm["q"] = %q, want no entry in map`, bq)
+       }
+       if bz := req.PostFormValue("z"); bz != "post" {
+               t.Errorf(`req.PostFormValue("z") = %q, want "post"`, bz)
+       }
+       if qs := req.Form["q"]; !reflect.DeepEqual(qs, []string{"foo", "bar"}) {
+               t.Errorf(`req.Form["q"] = %q, want ["foo", "bar"]`, qs)
+       }
+       if both := req.Form["both"]; !reflect.DeepEqual(both, []string{"y", "x"}) {
+               t.Errorf(`req.Form["both"] = %q, want ["y", "x"]`, both)
+       }
+       if prio := req.FormValue("prio"); prio != "2" {
+               t.Errorf(`req.FormValue("prio") = %q, want "2" (from body)`, prio)
+       }
+       if empty := req.FormValue("empty"); empty != "" {
+               t.Errorf(`req.FormValue("empty") = %q, want "" (from body)`, empty)
+       }
+}
+
 type stringMap map[string][]string
 type parseContentTypeTest struct {
        shouldError bool