Commit 1fd3f8bd authored by David Ndungu's avatar David Ndungu Committed by Brad Fitzpatrick

net/http: refactor test TestParseFormUnknownContentType

Use names to better communicate when a test case fails.

Change-Id: Id882783cb5e444b705443fbcdf612713f8a3b032
Reviewed-on: https://go-review.googlesource.com/c/go/+/187823Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
parent cfb13126
...@@ -85,35 +85,37 @@ func TestParseFormQueryMethods(t *testing.T) { ...@@ -85,35 +85,37 @@ func TestParseFormQueryMethods(t *testing.T) {
} }
} }
type stringMap map[string][]string func TestParseFormUnknownContentType(t *testing.T) {
type parseContentTypeTest struct { for _, test := range []struct {
shouldError bool name string
contentType stringMap wantErr string
} contentType Header
}{
var parseContentTypeTests = []parseContentTypeTest{ {"text", "", Header{"Content-Type": {"text/plain"}}},
{false, stringMap{"Content-Type": {"text/plain"}}},
// Empty content type is legal - may be treated as // Empty content type is legal - may be treated as
// application/octet-stream (RFC 7231, section 3.1.1.5) // application/octet-stream (RFC 7231, section 3.1.1.5)
{false, stringMap{}}, {"empty", "", Header{}},
{true, stringMap{"Content-Type": {"text/plain; boundary="}}}, {"boundary", "mime: invalid media parameter", Header{"Content-Type": {"text/plain; boundary="}}},
{false, stringMap{"Content-Type": {"application/unknown"}}}, {"unknown", "", Header{"Content-Type": {"application/unknown"}}},
} } {
t.Run(test.name,
func TestParseFormUnknownContentType(t *testing.T) { func(t *testing.T) {
for i, test := range parseContentTypeTests {
req := &Request{ req := &Request{
Method: "POST", Method: "POST",
Header: Header(test.contentType), Header: test.contentType,
Body: ioutil.NopCloser(strings.NewReader("body")), Body: ioutil.NopCloser(strings.NewReader("body")),
} }
err := req.ParseForm() err := req.ParseForm()
switch { switch {
case err == nil && test.shouldError: case err == nil && test.wantErr != "":
t.Errorf("test %d should have returned error", i) t.Errorf("unexpected success; want error %q", test.wantErr)
case err != nil && !test.shouldError: case err != nil && test.wantErr == "":
t.Errorf("test %d should not have returned error, got %v", i, err) t.Errorf("want success, got error: %v", err)
case test.wantErr != "" && test.wantErr != fmt.Sprint(err):
t.Errorf("got error %q; want %q", err, test.wantErr)
} }
},
)
} }
} }
......
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