|
5 | 5 | package mime
|
6 | 6 |
|
7 | 7 | import (
|
| 8 | + "reflect" |
8 | 9 | "testing"
|
9 | 10 | )
|
10 | 11 |
|
@@ -85,23 +86,97 @@ func TestConsumeMediaParam(t *testing.T) {
|
85 | 86 | }
|
86 | 87 | }
|
87 | 88 |
|
| 89 | +type mediaTypeTest struct { |
| 90 | + in string |
| 91 | + t string |
| 92 | + p map[string]string |
| 93 | +} |
| 94 | + |
88 | 95 | func TestParseMediaType(t *testing.T) {
|
89 |
| - tests := [...]string{ |
90 |
| - `form-data; name="foo"`, |
91 |
| - ` form-data ; name=foo`, |
92 |
| - `FORM-DATA;name="foo"`, |
93 |
| - ` FORM-DATA ; name="foo"`, |
94 |
| - ` FORM-DATA ; name="foo"`, |
95 |
| - `form-data; key=value; blah="value";name="foo" `, |
| 96 | + // Convenience map initializer |
| 97 | + m := func(s ...string) map[string]string { |
| 98 | + sm := make(map[string]string) |
| 99 | + for i := 0; i < len(s); i += 2 { |
| 100 | + sm[s[i]] = s[i+1] |
| 101 | + } |
| 102 | + return sm |
| 103 | + } |
| 104 | + |
| 105 | + nameFoo := map[string]string{"name": "foo"} |
| 106 | + tests := []mediaTypeTest{ |
| 107 | + {`form-data; name="foo"`, "form-data", nameFoo}, |
| 108 | + {` form-data ; name=foo`, "form-data", nameFoo}, |
| 109 | + {`FORM-DATA;name="foo"`, "form-data", nameFoo}, |
| 110 | + {` FORM-DATA ; name="foo"`, "form-data", nameFoo}, |
| 111 | + {` FORM-DATA ; name="foo"`, "form-data", nameFoo}, |
| 112 | + |
| 113 | + {`form-data; key=value; blah="value";name="foo" `, |
| 114 | + "form-data", |
| 115 | + m("key", "value", "blah", "value", "name", "foo")}, |
| 116 | + |
| 117 | + // Tests from http://greenbytes.de/tech/tc2231/ |
| 118 | + // TODO(bradfitz): add the rest of the tests from that site. |
| 119 | + {`attachment; filename="f\oo.html"`, |
| 120 | + "attachment", |
| 121 | + m("filename", "foo.html")}, |
| 122 | + {`attachment; filename="\"quoting\" tested.html"`, |
| 123 | + "attachment", |
| 124 | + m("filename", `"quoting" tested.html`)}, |
| 125 | + {`attachment; filename="Here's a semicolon;.html"`, |
| 126 | + "attachment", |
| 127 | + m("filename", "Here's a semicolon;.html")}, |
| 128 | + {`attachment; foo="\"\\";filename="foo.html"`, |
| 129 | + "attachment", |
| 130 | + m("foo", "\"\\", "filename", "foo.html")}, |
| 131 | + {`attachment; filename=foo.html`, |
| 132 | + "attachment", |
| 133 | + m("filename", "foo.html")}, |
| 134 | + {`attachment; filename=foo.html ;`, |
| 135 | + "attachment", |
| 136 | + m("filename", "foo.html")}, |
| 137 | + {`attachment; filename='foo.html'`, |
| 138 | + "attachment", |
| 139 | + m("filename", "foo.html")}, |
| 140 | + {`attachment; filename="foo-%41.html"`, |
| 141 | + "attachment", |
| 142 | + m("filename", "foo-%41.html")}, |
| 143 | + {`attachment; filename="foo-%\41.html"`, |
| 144 | + "attachment", |
| 145 | + m("filename", "foo-%41.html")}, |
| 146 | + {`filename=foo.html`, |
| 147 | + "", m()}, |
| 148 | + {`x=y; filename=foo.html`, |
| 149 | + "", m()}, |
| 150 | + {`"foo; filename=bar;baz"; filename=qux`, |
| 151 | + "", m()}, |
| 152 | + {`inline; attachment; filename=foo.html`, |
| 153 | + "", m()}, |
| 154 | + {`attachment; filename="foo.html".txt`, |
| 155 | + "", m()}, |
| 156 | + {`attachment; filename="bar`, |
| 157 | + "", m()}, |
| 158 | + {`attachment; creation-date="Wed, 12 Feb 1997 16:29:51 -0500"`, |
| 159 | + "attachment", |
| 160 | + m("creation-date", "Wed, 12 Feb 1997 16:29:51 -0500")}, |
| 161 | + {`foobar`, "foobar", m()}, |
| 162 | + // TODO(bradfitz): rest of them, including RFC2231 encoded UTF-8 and |
| 163 | + // other charsets. |
96 | 164 | }
|
97 | 165 | for _, test := range tests {
|
98 |
| - mt, params := ParseMediaType(test) |
99 |
| - if mt != "form-data" { |
100 |
| - t.Errorf("expected type form-data for %s, got [%s]", test, mt) |
| 166 | + mt, params := ParseMediaType(test.in) |
| 167 | + if g, e := mt, test.t; g != e { |
| 168 | + t.Errorf("for input %q, expected type %q, got %q", |
| 169 | + test.in, e, g) |
| 170 | + continue |
| 171 | + } |
| 172 | + if len(params) == 0 && len(test.p) == 0 { |
101 | 173 | continue
|
102 | 174 | }
|
103 |
| - if params["name"] != "foo" { |
104 |
| - t.Errorf("expected name=foo for %s", test) |
| 175 | + if !reflect.DeepEqual(params, test.p) { |
| 176 | + t.Errorf("for input %q, wrong params.\n"+ |
| 177 | + "expected: %#v\n"+ |
| 178 | + " got: %#v", |
| 179 | + test.in, test.p, params) |
105 | 180 | }
|
106 | 181 | }
|
107 | 182 | }
|
|
0 commit comments