mirror of
https://github.com/golang/go
synced 2024-11-20 05:14:41 -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:
parent
689d5b9163
commit
7f7a70f225
@ -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)
|
||||
|
@ -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) {
|
||||
mt, params, err := ParseMediaType("bogus ;=========")
|
||||
for _, tt := range badMediaTypeTests {
|
||||
mt, params, err := ParseMediaType(tt.in)
|
||||
if err == nil {
|
||||
t.Fatalf("expected an error parsing invalid media type; got type %q, params %#v", mt, params)
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user