1 // Copyright 2010 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
12 func TestConsumeToken(t *testing.T) {
13 tests := [...][3]string{
14 {"foo bar", "foo", " bar"},
19 for _, test := range tests {
20 token, rest := consumeToken(test[0])
21 expectedToken := test[1]
22 expectedRest := test[2]
23 if token != expectedToken {
24 t.Errorf("expected to consume token '%s', not '%s' from '%s'",
25 expectedToken, token, test[0])
26 } else if rest != expectedRest {
27 t.Errorf("expected to have left '%s', not '%s' after reading token '%s' from '%s'",
28 expectedRest, rest, token, test[0])
33 func TestConsumeValue(t *testing.T) {
34 tests := [...][3]string{
35 {"foo bar", "foo", " bar"},
37 {" bar ", "", " bar "},
38 {`"My value"end`, "My value", "end"},
39 {`"My value" end`, "My value", " end"},
40 {`"\\" rest`, "\\", " rest"},
41 {`"My \" value"end`, "My \" value", "end"},
42 {`"\" rest`, "", `"\" rest`},
44 for _, test := range tests {
45 value, rest := consumeValue(test[0])
46 expectedValue := test[1]
47 expectedRest := test[2]
48 if value != expectedValue {
49 t.Errorf("expected to consume value [%s], not [%s] from [%s]",
50 expectedValue, value, test[0])
51 } else if rest != expectedRest {
52 t.Errorf("expected to have left [%s], not [%s] after reading value [%s] from [%s]",
53 expectedRest, rest, value, test[0])
58 func TestConsumeMediaParam(t *testing.T) {
59 tests := [...][4]string{
60 {" ; foo=bar", "foo", "bar", ""},
61 {"; foo=bar", "foo", "bar", ""},
62 {";foo=bar", "foo", "bar", ""},
63 {";FOO=bar", "foo", "bar", ""},
64 {`;foo="bar"`, "foo", "bar", ""},
65 {`;foo="bar"; `, "foo", "bar", "; "},
66 {`;foo="bar"; foo=baz`, "foo", "bar", "; foo=baz"},
67 {` ; boundary=----CUT;`, "boundary", "----CUT", ";"},
68 {` ; key=value; blah="value";name="foo" `, "key", "value", `; blah="value";name="foo" `},
69 {`; blah="value";name="foo" `, "blah", "value", `;name="foo" `},
70 {`;name="foo" `, "name", "foo", ` `},
72 for _, test := range tests {
73 param, value, rest := consumeMediaParam(test[0])
74 expectedParam := test[1]
75 expectedValue := test[2]
76 expectedRest := test[3]
77 if param != expectedParam {
78 t.Errorf("expected to consume param [%s], not [%s] from [%s]",
79 expectedParam, param, test[0])
80 } else if value != expectedValue {
81 t.Errorf("expected to consume value [%s], not [%s] from [%s]",
82 expectedValue, value, test[0])
83 } else if rest != expectedRest {
84 t.Errorf("expected to have left [%s], not [%s] after reading [%s/%s] from [%s]",
85 expectedRest, rest, param, value, test[0])
90 type mediaTypeTest struct {
96 func TestParseMediaType(t *testing.T) {
97 // Convenience map initializer
98 m := func(s ...string) map[string]string {
99 sm := make(map[string]string)
100 for i := 0; i < len(s); i += 2 {
106 nameFoo := map[string]string{"name": "foo"}
107 tests := []mediaTypeTest{
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 {` FORM-DATA ; name="foo"`, "form-data", nameFoo},
114 {`form-data; key=value; blah="value";name="foo" `,
116 m("key", "value", "blah", "value", "name", "foo")},
118 {`foo; key=val1; key=the-key-appears-again-which-is-bogus`,
122 {`application/x-stuff; title*=us-ascii'en-us'This%20is%20%2A%2A%2Afun%2A%2A%2A`,
123 "application/x-stuff",
124 m("title", "This is ***fun***")},
126 {`message/external-body; access-type=URL; ` +
128 `URL*1="cs.utk.edu/pub/moore/bulk-mailer/bulk-mailer.tar"`,
129 "message/external-body",
130 m("access-type", "URL",
131 "url", "ftp://cs.utk.edu/pub/moore/bulk-mailer/bulk-mailer.tar")},
133 {`application/x-stuff; ` +
134 `title*0*=us-ascii'en'This%20is%20even%20more%20; ` +
135 `title*1*=%2A%2A%2Afun%2A%2A%2A%20; ` +
136 `title*2="isn't it!"`,
137 "application/x-stuff",
138 m("title", "This is even more ***fun*** isn't it!")},
140 // Tests from http://greenbytes.de/tech/tc2231/
141 // TODO(bradfitz): add the rest of the tests from that site.
142 {`attachment; filename="f\oo.html"`,
144 m("filename", "foo.html")},
145 {`attachment; filename="\"quoting\" tested.html"`,
147 m("filename", `"quoting" tested.html`)},
148 {`attachment; filename="Here's a semicolon;.html"`,
150 m("filename", "Here's a semicolon;.html")},
151 {`attachment; foo="\"\\";filename="foo.html"`,
153 m("foo", "\"\\", "filename", "foo.html")},
154 {`attachment; filename=foo.html`,
156 m("filename", "foo.html")},
157 {`attachment; filename=foo.html ;`,
159 m("filename", "foo.html")},
160 {`attachment; filename='foo.html'`,
162 m("filename", "foo.html")},
163 {`attachment; filename="foo-%41.html"`,
165 m("filename", "foo-%41.html")},
166 {`attachment; filename="foo-%\41.html"`,
168 m("filename", "foo-%41.html")},
169 {`filename=foo.html`,
171 {`x=y; filename=foo.html`,
173 {`"foo; filename=bar;baz"; filename=qux`,
175 {`inline; attachment; filename=foo.html`,
177 {`attachment; filename="foo.html".txt`,
179 {`attachment; filename="bar`,
181 {`attachment; creation-date="Wed, 12 Feb 1997 16:29:51 -0500"`,
183 m("creation-date", "Wed, 12 Feb 1997 16:29:51 -0500")},
184 {`foobar`, "foobar", m()},
185 {`attachment; filename* =UTF-8''foo-%c3%a4.html`,
187 m("filename", "foo-ä.html")},
188 {`attachment; filename*=UTF-8''A-%2541.html`,
190 m("filename", "A-%41.html")},
191 {`attachment; filename*0="foo."; filename*1="html"`,
193 m("filename", "foo.html")},
194 {`attachment; filename*0*=UTF-8''foo-%c3%a4; filename*1=".html"`,
196 m("filename", "foo-ä.html")},
197 {`attachment; filename*0="foo"; filename*01="bar"`,
199 m("filename", "foo")},
200 {`attachment; filename*0="foo"; filename*2="bar"`,
202 m("filename", "foo")},
203 {`attachment; filename*1="foo"; filename*2="bar"`,
205 {`attachment; filename*1="bar"; filename*0="foo"`,
207 m("filename", "foobar")},
208 {`attachment; filename="foo-ae.html"; filename*=UTF-8''foo-%c3%a4.html`,
210 m("filename", "foo-ä.html")},
211 {`attachment; filename*=UTF-8''foo-%c3%a4.html; filename="foo-ae.html"`,
213 m("filename", "foo-ä.html")},
215 // Browsers also just send UTF-8 directly without RFC 2231,
216 // at least when the source page is served with UTF-8.
217 {`form-data; firstname="Брэд"; lastname="Фицпатрик"`,
219 m("firstname", "Брэд", "lastname", "Фицпатрик")},
221 for _, test := range tests {
222 mt, params, err := ParseMediaType(test.in)
225 t.Errorf("for input %q, unexpected error: %v", test.in, err)
230 if g, e := mt, test.t; g != e {
231 t.Errorf("for input %q, expected type %q, got %q",
235 if len(params) == 0 && len(test.p) == 0 {
238 if !reflect.DeepEqual(params, test.p) {
239 t.Errorf("for input %q, wrong params.\n"+
242 test.in, test.p, params)
247 func TestParseMediaTypeBogus(t *testing.T) {
248 mt, params, err := ParseMediaType("bogus ;=========")
250 t.Fatalf("expected an error parsing invalid media type; got type %q, params %#v", mt, params)
252 if err.Error() != "mime: invalid media parameter" {
253 t.Errorf("expected invalid media parameter; got error %q", err)
257 type formatTest struct {
259 params map[string]string
263 var formatTests = []formatTest{
264 {"noslash", nil, ""},
265 {"foo/BAR", nil, "foo/bar"},
266 {"foo/BAR", map[string]string{"X": "Y"}, "foo/bar; x=Y"},
269 func TestFormatMediaType(t *testing.T) {
270 for i, tt := range formatTests {
271 got := FormatMediaType(tt.typ, tt.params)
273 t.Errorf("%d. FormatMediaType(%q, %v) = %q; want %q", i, tt.typ, tt.params, got, tt.want)