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.
13 func TestConsumeToken(t
*testing
.T
) {
14 tests
:= [...][3]string{
15 {"foo bar", "foo", " bar"},
20 for _
, test
:= range tests
{
21 token
, rest
:= consumeToken(test
[0])
22 expectedToken
:= test
[1]
23 expectedRest
:= test
[2]
24 if token
!= expectedToken
{
25 t
.Errorf("expected to consume token '%s', not '%s' from '%s'",
26 expectedToken
, token
, test
[0])
27 } else if rest
!= expectedRest
{
28 t
.Errorf("expected to have left '%s', not '%s' after reading token '%s' from '%s'",
29 expectedRest
, rest
, token
, test
[0])
34 func TestConsumeValue(t
*testing
.T
) {
35 tests
:= [...][3]string{
36 {"foo bar", "foo", " bar"},
38 {" bar ", "", " bar "},
39 {`"My value"end`, "My value", "end"},
40 {`"My value" end`, "My value", " end"},
41 {`"\\" rest`, "\\", " rest"},
42 {`"My \" value"end`, "My \" value", "end"},
43 {`"\" rest`, "", `"\" rest`},
44 {`"C:\dev\go\robots.txt"`, `C:\dev\go\robots.txt`, ""},
45 {`"C:\新建文件件\中文第二次测试.mp4"`, `C:\新建文件件\中文第二次测试.mp4`, ""},
47 for _
, test
:= range tests
{
48 value
, rest
:= consumeValue(test
[0])
49 expectedValue
:= test
[1]
50 expectedRest
:= test
[2]
51 if value
!= expectedValue
{
52 t
.Errorf("expected to consume value [%s], not [%s] from [%s]",
53 expectedValue
, value
, test
[0])
54 } else if rest
!= expectedRest
{
55 t
.Errorf("expected to have left [%s], not [%s] after reading value [%s] from [%s]",
56 expectedRest
, rest
, value
, test
[0])
61 func TestConsumeMediaParam(t
*testing
.T
) {
62 tests
:= [...][4]string{
63 {" ; foo=bar", "foo", "bar", ""},
64 {"; foo=bar", "foo", "bar", ""},
65 {";foo=bar", "foo", "bar", ""},
66 {";FOO=bar", "foo", "bar", ""},
67 {`;foo="bar"`, "foo", "bar", ""},
68 {`;foo="bar"; `, "foo", "bar", "; "},
69 {`;foo="bar"; foo=baz`, "foo", "bar", "; foo=baz"},
70 {` ; boundary=----CUT;`, "boundary", "----CUT", ";"},
71 {` ; key=value; blah="value";name="foo" `, "key", "value", `; blah="value";name="foo" `},
72 {`; blah="value";name="foo" `, "blah", "value", `;name="foo" `},
73 {`;name="foo" `, "name", "foo", ` `},
75 for _
, test
:= range tests
{
76 param
, value
, rest
:= consumeMediaParam(test
[0])
77 expectedParam
:= test
[1]
78 expectedValue
:= test
[2]
79 expectedRest
:= test
[3]
80 if param
!= expectedParam
{
81 t
.Errorf("expected to consume param [%s], not [%s] from [%s]",
82 expectedParam
, param
, test
[0])
83 } else if value
!= expectedValue
{
84 t
.Errorf("expected to consume value [%s], not [%s] from [%s]",
85 expectedValue
, value
, test
[0])
86 } else if rest
!= expectedRest
{
87 t
.Errorf("expected to have left [%s], not [%s] after reading [%s/%s] from [%s]",
88 expectedRest
, rest
, param
, value
, test
[0])
93 type mediaTypeTest
struct {
99 func TestParseMediaType(t
*testing
.T
) {
100 // Convenience map initializer
101 m
:= func(s
...string) map[string]string {
102 sm
:= make(map[string]string)
103 for i
:= 0; i
< len(s
); i
+= 2 {
109 nameFoo
:= map[string]string{"name": "foo"}
110 tests
:= []mediaTypeTest
{
111 {`form-data; name="foo"`, "form-data", nameFoo
},
112 {` form-data ; name=foo`, "form-data", nameFoo
},
113 {`FORM-DATA;name="foo"`, "form-data", nameFoo
},
114 {` FORM-DATA ; name="foo"`, "form-data", nameFoo
},
115 {` FORM-DATA ; name="foo"`, "form-data", nameFoo
},
117 {`form-data; key=value; blah="value";name="foo" `,
119 m("key", "value", "blah", "value", "name", "foo")},
121 {`foo; key=val1; key=the-key-appears-again-which-is-bogus`,
125 {`application/x-stuff; title*=us-ascii'en-us'This%20is%20%2A%2A%2Afun%2A%2A%2A`,
126 "application/x-stuff",
127 m("title", "This is ***fun***")},
129 {`message/external-body; access-type=URL; ` +
131 `URL*1="cs.utk.edu/pub/moore/bulk-mailer/bulk-mailer.tar"`,
132 "message/external-body",
133 m("access-type", "URL",
134 "url", "ftp://cs.utk.edu/pub/moore/bulk-mailer/bulk-mailer.tar")},
136 {`application/x-stuff; ` +
137 `title*0*=us-ascii'en'This%20is%20even%20more%20; ` +
138 `title*1*=%2A%2A%2Afun%2A%2A%2A%20; ` +
139 `title*2="isn't it!"`,
140 "application/x-stuff",
141 m("title", "This is even more ***fun*** isn't it!")},
143 // Tests from http://greenbytes.de/tech/tc2231/
144 // Note: Backslash escape handling is a bit loose, like MSIE.
154 // #attwithasciifilename
155 {`attachment; filename="foo.html"`,
157 m("filename", "foo.html")},
158 // #attwithasciifilename25
159 {`attachment; filename="0000000000111111111122222"`,
161 m("filename", "0000000000111111111122222")},
162 // #attwithasciifilename35
163 {`attachment; filename="00000000001111111111222222222233333"`,
165 m("filename", "00000000001111111111222222222233333")},
166 // #attwithasciifnescapedchar
167 {`attachment; filename="f\oo.html"`,
169 m("filename", "f\\oo.html")},
170 // #attwithasciifnescapedquote
171 {`attachment; filename="\"quoting\" tested.html"`,
173 m("filename", `"quoting" tested.html`)},
174 // #attwithquotedsemicolon
175 {`attachment; filename="Here's a semicolon;.html"`,
177 m("filename", "Here's a semicolon;.html")},
178 // #attwithfilenameandextparam
179 {`attachment; foo="bar"; filename="foo.html"`,
181 m("foo", "bar", "filename", "foo.html")},
182 // #attwithfilenameandextparamescaped
183 {`attachment; foo="\"\\";filename="foo.html"`,
185 m("foo", "\"\\", "filename", "foo.html")},
186 // #attwithasciifilenameucase
187 {`attachment; FILENAME="foo.html"`,
189 m("filename", "foo.html")},
190 // #attwithasciifilenamenq
191 {`attachment; filename=foo.html`,
193 m("filename", "foo.html")},
194 // #attwithasciifilenamenqs
195 {`attachment; filename=foo.html ;`,
197 m("filename", "foo.html")},
199 {`attachment; filename='foo.html'`,
201 m("filename", "'foo.html'")},
202 // #attwithisofnplain
203 {`attachment; filename="foo-ä.html"`,
205 m("filename", "foo-ä.html")},
206 // #attwithutf8fnplain
207 {`attachment; filename="foo-ä.html"`,
209 m("filename", "foo-ä.html")},
210 // #attwithfnrawpctenca
211 {`attachment; filename="foo-%41.html"`,
213 m("filename", "foo-%41.html")},
214 // #attwithfnusingpct
215 {`attachment; filename="50%.html"`,
217 m("filename", "50%.html")},
218 // #attwithfnrawpctencaq
219 {`attachment; filename="foo-%\41.html"`,
221 m("filename", "foo-%\\41.html")},
223 {`attachment; name="foo-%41.html"`,
225 m("name", "foo-%41.html")},
226 // #attwithfilenamepctandiso
227 {`attachment; name="ä-%41.html"`,
229 m("name", "ä-%41.html")},
230 // #attwithfnrawpctenclong
231 {`attachment; filename="foo-%c3%a4-%e2%82%ac.html"`,
233 m("filename", "foo-%c3%a4-%e2%82%ac.html")},
234 // #attwithasciifilenamews1
235 {`attachment; filename ="foo.html"`,
237 m("filename", "foo.html")},
238 // #attmissingdisposition
239 {`filename=foo.html`,
241 // #attmissingdisposition2
242 {`x=y; filename=foo.html`,
244 // #attmissingdisposition3
245 {`"foo; filename=bar;baz"; filename=qux`,
247 // #attmissingdisposition4
248 {`filename=foo.html, filename=bar.html`,
251 {`; filename=foo.html`,
254 {`: inline; attachment; filename=foo.html`,
257 {`inline; attachment; filename=foo.html`,
260 {`attachment; inline; filename=foo.html`,
262 // #attbrokenquotedfn
263 {`attachment; filename="foo.html".txt`,
265 // #attbrokenquotedfn2
266 {`attachment; filename="bar`,
268 // #attbrokenquotedfn3
269 {`attachment; filename=foo"bar;baz"qux`,
272 {`attachment; filename=foo.html, attachment; filename=bar.html`,
275 {`attachment; foo=foo filename=bar`,
278 {`attachment; filename=bar foo=foo`,
281 {`attachment filename=bar`,
284 {`filename=foo.html; attachment`,
287 {`attachment; xfilename=foo.html`,
289 m("xfilename", "foo.html")},
291 {`attachment; creation-date="Wed, 12 Feb 1997 16:29:51 -0500"`,
293 m("creation-date", "Wed, 12 Feb 1997 16:29:51 -0500")},
295 {`attachment; modification-date="Wed, 12 Feb 1997 16:29:51 -0500"`,
297 m("modification-date", "Wed, 12 Feb 1997 16:29:51 -0500")},
299 {`foobar`, "foobar", m()},
301 {`attachment; example="filename=example.txt"`,
303 m("example", "filename=example.txt")},
304 // #attwithfn2231utf8
305 {`attachment; filename*=UTF-8''foo-%c3%a4-%e2%82%ac.html`,
307 m("filename", "foo-ä-€.html")},
309 {`attachment; filename*=''foo-%c3%a4-%e2%82%ac.html`,
312 // #attwithfn2231utf8comp
313 {`attachment; filename*=UTF-8''foo-a%cc%88.html`,
315 m("filename", "foo-ä.html")},
317 {`attachment; filename*= UTF-8''foo-%c3%a4.html`,
319 m("filename", "foo-ä.html")},
321 {`attachment; filename* =UTF-8''foo-%c3%a4.html`,
323 m("filename", "foo-ä.html")},
324 // #attwithfn2231quot
325 {`attachment; filename*="UTF-8''foo-%c3%a4.html"`,
327 m("filename", "foo-ä.html")},
328 // #attwithfn2231quot2
329 {`attachment; filename*="foo%20bar.html"`,
332 // #attwithfn2231singleqmissing
333 {`attachment; filename*=UTF-8'foo-%c3%a4.html`,
336 // #attwithfn2231nbadpct1
337 {`attachment; filename*=UTF-8''foo%`,
340 // #attwithfn2231nbadpct2
341 {`attachment; filename*=UTF-8''f%oo.html`,
344 // #attwithfn2231dpct
345 {`attachment; filename*=UTF-8''A-%2541.html`,
347 m("filename", "A-%41.html")},
349 {`attachment; filename*0="foo."; filename*1="html"`,
351 m("filename", "foo.html")},
353 {`attachment; filename*0*=UTF-8''foo-%c3%a4; filename*1=".html"`,
355 m("filename", "foo-ä.html")},
357 {`attachment; filename*0="foo"; filename*01="bar"`,
359 m("filename", "foo")},
361 {`attachment; filename*0="foo"; filename*2="bar"`,
363 m("filename", "foo")},
365 {`attachment; filename*1="foo."; filename*2="html"`,
368 {`attachment; filename*1="bar"; filename*0="foo"`,
370 m("filename", "foobar")},
372 {`attachment; filename="foo-ae.html"; filename*=UTF-8''foo-%c3%a4.html`,
374 m("filename", "foo-ä.html")},
376 {`attachment; filename*=UTF-8''foo-%c3%a4.html; filename="foo-ae.html"`,
378 m("filename", "foo-ä.html")},
380 {`attachment; filename*0*=ISO-8859-15''euro-sign%3d%a4; filename*=ISO-8859-1''currency-sign%3d%a4`,
384 {`attachment; foobar=x; filename="foo.html"`,
386 m("foobar", "x", "filename", "foo.html")},
388 // Browsers also just send UTF-8 directly without RFC 2231,
389 // at least when the source page is served with UTF-8.
390 {`form-data; firstname="Брэд"; lastname="Фицпатрик"`,
392 m("firstname", "Брэд", "lastname", "Фицпатрик")},
394 // Empty string used to be mishandled.
395 {`foo; bar=""`, "foo", m("bar", "")},
397 // Microsoft browers in intranet mode do not think they need to escape \ in file name.
398 {`form-data; name="file"; filename="C:\dev\go\robots.txt"`, "form-data", m("name", "file", "filename", `C:\dev\go\robots.txt`)},
399 {`form-data; name="file"; filename="C:\新建文件件\中文第二次测试.mp4"`, "form-data", m("name", "file", "filename", `C:\新建文件件\中文第二次测试.mp4`)},
401 for _
, test
:= range tests
{
402 mt
, params
, err
:= ParseMediaType(test
.in
)
405 t
.Errorf("for input %#q, unexpected error: %v", test
.in
, err
)
410 if g
, e
:= mt
, test
.t
; g
!= e
{
411 t
.Errorf("for input %#q, expected type %q, got %q",
415 if len(params
) == 0 && len(test
.p
) == 0 {
418 if !reflect
.DeepEqual(params
, test
.p
) {
419 t
.Errorf("for input %#q, wrong params.\n"+
422 test
.in
, test
.p
, params
)
427 type badMediaTypeTest
struct {
433 var badMediaTypeTests
= []badMediaTypeTest
{
434 {"bogus ;=========", "bogus", "mime: invalid media parameter"},
435 // The following example is from real email delivered by gmail (error: missing semicolon)
436 // and it is there to check behavior described in #19498
437 {"application/pdf; x-mac-type=\"3F3F3F3F\"; x-mac-creator=\"3F3F3F3F\" name=\"a.pdf\";",
438 "application/pdf", "mime: invalid media parameter"},
439 {"bogus/<script>alert</script>", "", "mime: expected token after slash"},
440 {"bogus/bogus<script>alert</script>", "", "mime: unexpected content after media subtype"},
441 // Tests from http://greenbytes.de/tech/tc2231/
442 {`"attachment"`, "attachment", "mime: no media type"},
443 {"attachment; filename=foo,bar.html", "attachment", "mime: invalid media parameter"},
444 {"attachment; ;filename=foo", "attachment", "mime: invalid media parameter"},
445 {"attachment; filename=foo bar.html", "attachment", "mime: invalid media parameter"},
446 {`attachment; filename="foo.html"; filename="bar.html"`, "attachment", "mime: duplicate parameter name"},
447 {"attachment; filename=foo[1](2).html", "attachment", "mime: invalid media parameter"},
448 {"attachment; filename=foo-ä.html", "attachment", "mime: invalid media parameter"},
449 {"attachment; filename=foo-ä.html", "attachment", "mime: invalid media parameter"},
450 {`attachment; filename *=UTF-8''foo-%c3%a4.html`, "attachment", "mime: invalid media parameter"},
453 func TestParseMediaTypeBogus(t
*testing
.T
) {
454 for _
, tt
:= range badMediaTypeTests
{
455 mt
, params
, err
:= ParseMediaType(tt
.in
)
457 t
.Errorf("ParseMediaType(%q) = nil error; want parse error", tt
.in
)
460 if err
.Error() != tt
.err
{
461 t
.Errorf("ParseMediaType(%q) = err %q; want %q", tt
.in
, err
.Error(), tt
.err
)
464 t
.Errorf("ParseMediaType(%q): got non-nil params on error", tt
.in
)
466 if err
!= ErrInvalidMediaParameter
&& mt
!= "" {
467 t
.Errorf("ParseMediaType(%q): got unexpected non-empty media type string", tt
.in
)
469 if err
== ErrInvalidMediaParameter
&& mt
!= tt
.mt
{
470 t
.Errorf("ParseMediaType(%q): in case of invalid parameters: expected type %q, got %q", tt
.in
, tt
.mt
, mt
)
475 type formatTest
struct {
477 params
map[string]string
481 var formatTests
= []formatTest
{
482 {"noslash", map[string]string{"X": "Y"}, "noslash; x=Y"}, // e.g. Content-Disposition values (RFC 2183); issue 11289
483 {"foo bar/baz", nil, ""},
484 {"foo/bar baz", nil, ""},
485 {"attachment", map[string]string{"filename": "ĄĄŽŽČČŠŠ"}, "attachment; filename*=utf-8''%C4%84%C4%84%C5%BD%C5%BD%C4%8C%C4%8C%C5%A0%C5%A0"},
486 {"attachment", map[string]string{"filename": "ÁÁÊÊÇÇÎÎ"}, "attachment; filename*=utf-8''%C3%81%C3%81%C3%8A%C3%8A%C3%87%C3%87%C3%8E%C3%8E"},
487 {"attachment", map[string]string{"filename": "数据统计.png"}, "attachment; filename*=utf-8''%E6%95%B0%E6%8D%AE%E7%BB%9F%E8%AE%A1.png"},
488 {"foo/BAR", nil, "foo/bar"},
489 {"foo/BAR", map[string]string{"X": "Y"}, "foo/bar; x=Y"},
490 {"foo/BAR", map[string]string{"space": "With space"}, `foo/bar; space="With space"`},
491 {"foo/BAR", map[string]string{"quote": `With "quote`}, `foo/bar; quote="With \"quote"`},
492 {"foo/BAR", map[string]string{"bslash": `With \backslash`}, `foo/bar; bslash="With \\backslash"`},
493 {"foo/BAR", map[string]string{"both": `With \backslash and "quote`}, `foo/bar; both="With \\backslash and \"quote"`},
494 {"foo/BAR", map[string]string{"": "empty attribute"}, ""},
495 {"foo/BAR", map[string]string{"bad attribute": "baz"}, ""},
496 {"foo/BAR", map[string]string{"nonascii": "not an ascii character: ä"}, "foo/bar; nonascii*=utf-8''not%20an%20ascii%20character%3A%20%C3%A4"},
497 {"foo/BAR", map[string]string{"ctl": "newline: \n nil: \000"}, "foo/bar; ctl*=utf-8''newline%3A%20%0A%20nil%3A%20%00"},
498 {"foo/bar", map[string]string{"a": "av", "b": "bv", "c": "cv"}, "foo/bar; a=av; b=bv; c=cv"},
499 {"foo/bar", map[string]string{"0": "'", "9": "'"}, "foo/bar; 0='; 9='"},
500 {"foo", map[string]string{"bar": ""}, `foo; bar=""`},
503 func TestFormatMediaType(t
*testing
.T
) {
504 for i
, tt
:= range formatTests
{
505 got
:= FormatMediaType(tt
.typ
, tt
.params
)
507 t
.Errorf("%d. FormatMediaType(%q, %v) = %q; want %q", i
, tt
.typ
, tt
.params
, got
, tt
.want
)
512 typ
, params
, err
:= ParseMediaType(got
)
514 t
.Errorf("%d. ParseMediaType(%q) err: %v", i
, got
, err
)
516 if typ
!= strings
.ToLower(tt
.typ
) {
517 t
.Errorf("%d. ParseMediaType(%q) typ = %q; want %q", i
, got
, typ
, tt
.typ
)
519 for k
, v
:= range tt
.params
{
520 k
= strings
.ToLower(k
)
522 t
.Errorf("%d. ParseMediaType(%q) params[%s] = %q; want %q", i
, got
, k
, params
[k
], v
)