Commit 8d1b63ab authored by Matthew Cottingham's avatar Matthew Cottingham Committed by Brad Fitzpatrick

net/http: Return ErrNotMultipart from ParseMultipartForm if content-type isn't multipart/form-data.

Add test for multipart form requests with an invalid content-type to ensure
ErrNotMultipart is returned.

Change ParseMultipartForm to return ErrNotMultipart when it is returned by multipartReader.

Modify test for empty multipart request handling to use POST so that the body is checked.

Fixes #6334.

This is the first changeset working on multipart request handling. Further changesets
could add more tests and clean up the TODO.

LGTM=bradfitz
R=golang-codereviews, gobot, bradfitz, rsc
CC=golang-codereviews
https://golang.org/cl/44040043
parent 10a27319
...@@ -789,9 +789,7 @@ func (r *Request) ParseMultipartForm(maxMemory int64) error { ...@@ -789,9 +789,7 @@ func (r *Request) ParseMultipartForm(maxMemory int64) error {
} }
mr, err := r.multipartReader() mr, err := r.multipartReader()
if err == ErrNotMultipart { if err != nil {
return nil
} else if err != nil {
return err return err
} }
......
...@@ -154,7 +154,25 @@ func TestMultipartReader(t *testing.T) { ...@@ -154,7 +154,25 @@ func TestMultipartReader(t *testing.T) {
req.Header = Header{"Content-Type": {"text/plain"}} req.Header = Header{"Content-Type": {"text/plain"}}
multipart, err = req.MultipartReader() multipart, err = req.MultipartReader()
if multipart != nil { if multipart != nil {
t.Errorf("unexpected multipart for text/plain") t.Error("unexpected multipart for text/plain")
}
}
func TestParseMultipartForm(t *testing.T) {
req := &Request{
Method: "POST",
Header: Header{"Content-Type": {`multipart/form-data; boundary="foo123"`}},
Body: ioutil.NopCloser(new(bytes.Buffer)),
}
err := req.ParseMultipartForm(25)
if err == nil {
t.Error("expected multipart EOF, got nil")
}
req.Header = Header{"Content-Type": {"text/plain"}}
err = req.ParseMultipartForm(25)
if err != ErrNotMultipart {
t.Error("expected ErrNotMultipart for text/plain")
} }
} }
...@@ -220,16 +238,38 @@ func TestMultipartRequestAuto(t *testing.T) { ...@@ -220,16 +238,38 @@ func TestMultipartRequestAuto(t *testing.T) {
validateTestMultipartContents(t, req, true) validateTestMultipartContents(t, req, true)
} }
func TestEmptyMultipartRequest(t *testing.T) { func TestMissingFileMultipartRequest(t *testing.T) {
// Test that FormValue and FormFile automatically invoke // Test that FormFile returns an error if
// ParseMultipartForm and return the right values. // the named file is missing.
req, err := NewRequest("GET", "/", nil) req := newTestMultipartRequest(t)
if err != nil {
t.Errorf("NewRequest err = %q", err)
}
testMissingFile(t, req) testMissingFile(t, req)
} }
// Test that FormValue invokes ParseMultipartForm.
func TestFormValueCallsParseMultipartForm(t *testing.T) {
req, _ := NewRequest("POST", "http://www.google.com/", strings.NewReader("z=post"))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
if req.Form != nil {
t.Fatal("Unexpected request Form, want nil")
}
req.FormValue("z")
if req.Form == nil {
t.Fatal("ParseMultipartForm not called by FormValue")
}
}
// Test that FormFile invokes ParseMultipartForm.
func TestFormFileCallsParseMultipartForm(t *testing.T) {
req := newTestMultipartRequest(t)
if req.Form != nil {
t.Fatal("Unexpected request Form, want nil")
}
req.FormFile("")
if req.Form == nil {
t.Fatal("ParseMultipartForm not called by FormFile")
}
}
// Test that ParseMultipartForm errors if called // Test that ParseMultipartForm errors if called
// after MultipartReader on the same request. // after MultipartReader on the same request.
func TestParseMultipartFormOrder(t *testing.T) { func TestParseMultipartFormOrder(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