1
0
mirror of https://github.com/golang/go synced 2024-11-20 06:44:40 -07:00

mime: make ParseMediaType return zero results on error

Fixes #3562

R=golang-dev, adg, rsc
CC=golang-dev
https://golang.org/cl/6119051
This commit is contained in:
Brad Fitzpatrick 2012-04-25 12:01:01 -07:00
parent 689d5b9163
commit 7f7a70f225
2 changed files with 27 additions and 7 deletions

View File

@ -99,7 +99,7 @@ func ParseMediaType(v string) (mediatype string, params map[string]string, err e
err = checkMediaTypeDisposition(mediatype) err = checkMediaTypeDisposition(mediatype)
if err != nil { if err != nil {
return return "", nil, err
} }
params = make(map[string]string) params = make(map[string]string)

View File

@ -244,13 +244,33 @@ func TestParseMediaType(t *testing.T) {
} }
} }
func TestParseMediaTypeBogus(t *testing.T) { type badMediaTypeTest struct {
mt, params, err := ParseMediaType("bogus ;=========") in string
if err == nil { err string
t.Fatalf("expected an error parsing invalid media type; got type %q, params %#v", mt, params) }
var badMediaTypeTests = []badMediaTypeTest{
{"bogus ;=========", "mime: invalid media parameter"},
{"bogus/<script>alert</script>", "mime: expected token after slash"},
{"bogus/bogus<script>alert</script>", "mime: unexpected content after media subtype"},
}
func TestParseMediaTypeBogus(t *testing.T) {
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)
} }
if err.Error() != "mime: invalid media parameter" {
t.Errorf("expected invalid media parameter; got error %q", err)
} }
} }