aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Ndungu <dnjuguna@gmail.com>2019-07-27 15:19:32 -0700
committerBrad Fitzpatrick <bradfitz@golang.org>2019-11-08 18:11:01 +0000
commit1fd3f8bd67a36e330c8be07941d1ab09870ff932 (patch)
tree30e8a6a0cd84926a6fd71570f57eddb5cf36ffdc
parentcfb13126f3798bcb46b4f947aaa16929ab5c147f (diff)
downloadgo-1fd3f8bd67a36e330c8be07941d1ab09870ff932.tar.xz
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/+/187823 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
-rw-r--r--src/net/http/request_test.go58
1 files changed, 30 insertions, 28 deletions
diff --git a/src/net/http/request_test.go b/src/net/http/request_test.go
index bb06d922f0..42c16d00ea 100644
--- a/src/net/http/request_test.go
+++ b/src/net/http/request_test.go
@@ -85,35 +85,37 @@ func TestParseFormQueryMethods(t *testing.T) {
}
}
-type stringMap map[string][]string
-type parseContentTypeTest struct {
- shouldError bool
- contentType stringMap
-}
-
-var parseContentTypeTests = []parseContentTypeTest{
- {false, stringMap{"Content-Type": {"text/plain"}}},
- // Empty content type is legal - may be treated as
- // application/octet-stream (RFC 7231, section 3.1.1.5)
- {false, stringMap{}},
- {true, stringMap{"Content-Type": {"text/plain; boundary="}}},
- {false, stringMap{"Content-Type": {"application/unknown"}}},
-}
-
func TestParseFormUnknownContentType(t *testing.T) {
- for i, test := range parseContentTypeTests {
- req := &Request{
- Method: "POST",
- Header: Header(test.contentType),
- Body: ioutil.NopCloser(strings.NewReader("body")),
- }
- err := req.ParseForm()
- switch {
- case err == nil && test.shouldError:
- t.Errorf("test %d should have returned error", i)
- case err != nil && !test.shouldError:
- t.Errorf("test %d should not have returned error, got %v", i, err)
- }
+ for _, test := range []struct {
+ name string
+ wantErr string
+ contentType Header
+ }{
+ {"text", "", Header{"Content-Type": {"text/plain"}}},
+ // Empty content type is legal - may be treated as
+ // application/octet-stream (RFC 7231, section 3.1.1.5)
+ {"empty", "", Header{}},
+ {"boundary", "mime: invalid media parameter", Header{"Content-Type": {"text/plain; boundary="}}},
+ {"unknown", "", Header{"Content-Type": {"application/unknown"}}},
+ } {
+ t.Run(test.name,
+ func(t *testing.T) {
+ req := &Request{
+ Method: "POST",
+ Header: test.contentType,
+ Body: ioutil.NopCloser(strings.NewReader("body")),
+ }
+ err := req.ParseForm()
+ switch {
+ case err == nil && test.wantErr != "":
+ t.Errorf("unexpected success; want error %q", test.wantErr)
+ case err != nil && test.wantErr == "":
+ 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)
+ }
+ },
+ )
}
}