diff --git a/src/pkg/mime/mediatype.go b/src/pkg/mime/mediatype.go index 9398dece1d..8396c0a155 100644 --- a/src/pkg/mime/mediatype.go +++ b/src/pkg/mime/mediatype.go @@ -99,7 +99,7 @@ func ParseMediaType(v string) (mediatype string, params map[string]string, err e err = checkMediaTypeDisposition(mediatype) if err != nil { - return + return "", nil, err } params = make(map[string]string) diff --git a/src/pkg/mime/mediatype_test.go b/src/pkg/mime/mediatype_test.go index 64ab291341..e41ead237a 100644 --- a/src/pkg/mime/mediatype_test.go +++ b/src/pkg/mime/mediatype_test.go @@ -244,13 +244,33 @@ func TestParseMediaType(t *testing.T) { } } +type badMediaTypeTest struct { + in string + err string +} + +var badMediaTypeTests = []badMediaTypeTest{ + {"bogus ;=========", "mime: invalid media parameter"}, + {"bogus/", "mime: expected token after slash"}, + {"bogus/bogus", "mime: unexpected content after media subtype"}, +} + func TestParseMediaTypeBogus(t *testing.T) { - mt, params, err := ParseMediaType("bogus ;=========") - if err == nil { - t.Fatalf("expected an error parsing invalid media type; got type %q, params %#v", mt, params) - } - if err.Error() != "mime: invalid media parameter" { - t.Errorf("expected invalid media parameter; got error %q", err) + for _, tt := range badMediaTypeTests { + mt, params, err := ParseMediaType(tt.in) + if err == nil { + t.Errorf("ParseMediaType(%q) = nil error; want parse error", tt.in) + continue + } + if err.Error() != tt.err { + t.Errorf("ParseMediaType(%q) = err %q; want %q", tt.in, err.Error(), tt.err) + } + if params != nil { + t.Errorf("ParseMediaType(%q): got non-nil params on error", tt.in) + } + if mt != "" { + t.Errorf("ParseMediaType(%q): got non-empty media type string on error", tt.in) + } } }