Commit 8f2d4899 authored by Russ Cox's avatar Russ Cox

[release-branch.go1] mime: make ParseMediaType return zero results on error

««« CL 6119051 / 1384d7510575
mime: make ParseMediaType return zero results on error

Fixes #3562

R=golang-dev, adg, rsc
CC=golang-dev
https://golang.org/cl/6119051
»»»

R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/6127045
parent a8cea4cd
...@@ -99,7 +99,7 @@ func ParseMediaType(v string) (mediatype string, params map[string]string, err e ...@@ -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)
......
...@@ -244,13 +244,33 @@ func TestParseMediaType(t *testing.T) { ...@@ -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/<script>alert</script>", "mime: expected token after slash"},
{"bogus/bogus<script>alert</script>", "mime: unexpected content after media subtype"},
}
func TestParseMediaTypeBogus(t *testing.T) { func TestParseMediaTypeBogus(t *testing.T) {
mt, params, err := ParseMediaType("bogus ;=========") for _, tt := range badMediaTypeTests {
if err == nil { mt, params, err := ParseMediaType(tt.in)
t.Fatalf("expected an error parsing invalid media type; got type %q, params %#v", mt, params) if err == nil {
} t.Errorf("ParseMediaType(%q) = nil error; want parse error", tt.in)
if err.Error() != "mime: invalid media parameter" { continue
t.Errorf("expected invalid media parameter; got error %q", err) }
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)
}
} }
} }
......
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