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=baz`, "foo", "bar", "; foo=baz"},
66 {` ; boundary=----CUT;`, "boundary", "----CUT", ";"},
67 {` ; key=value; blah="value";name="foo" `, "key", "value", `; blah="value";name="foo" `},
68 {`; blah="value";name="foo" `, "blah", "value", `;name="foo" `},
69 {`;name="foo" `, "name", "foo", ` `},
71 for _, test := range tests {
72 param, value, rest := consumeMediaParam(test[0])
73 expectedParam := test[1]
74 expectedValue := test[2]
75 expectedRest := test[3]
76 if param != expectedParam {
77 t.Errorf("expected to consume param [%s], not [%s] from [%s]",
78 expectedParam, param, test[0])
79 } else if value != expectedValue {
80 t.Errorf("expected to consume value [%s], not [%s] from [%s]",
81 expectedValue, value, test[0])
82 } else if rest != expectedRest {
83 t.Errorf("expected to have left [%s], not [%s] after reading [%s/%s] from [%s]",
84 expectedRest, rest, param, value, test[0])
89 type mediaTypeTest struct {
95 func TestParseMediaType(t *testing.T) {
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 {
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},
113 {`form-data; key=value; blah="value";name="foo" `,
115 m("key", "value", "blah", "value", "name", "foo")},
117 {`foo; key=val1; key=the-key-appears-again-which-is-bogus`,
121 {`application/x-stuff; title*=us-ascii'en-us'This%20is%20%2A%2A%2Afun%2A%2A%2A`,
122 "application/x-stuff",
123 m("title", "This is ***fun***")},
125 {`message/external-body; access-type=URL; ` +
127 `URL*1="cs.utk.edu/pub/moore/bulk-mailer/bulk-mailer.tar"`,
128 "message/external-body",
129 m("access-type", "URL",
130 "URL", "ftp://cs.utk.edu/pub/moore/bulk-mailer/bulk-mailer.tar")},
132 {`application/x-stuff; ` +
133 `title*0*=us-ascii'en'This%20is%20even%20more%20; ` +
134 `title*1*=%2A%2A%2Afun%2A%2A%2A%20; ` +
135 `title*2="isn't it!"`,
136 "application/x-stuff",
137 m("title", "This is even more ***fun*** isn't it!")},
139 // Tests from http://greenbytes.de/tech/tc2231/
140 // TODO(bradfitz): add the rest of the tests from that site.
141 {`attachment; filename="f\oo.html"`,
143 m("filename", "foo.html")},
144 {`attachment; filename="\"quoting\" tested.html"`,
146 m("filename", `"quoting" tested.html`)},
147 {`attachment; filename="Here's a semicolon;.html"`,
149 m("filename", "Here's a semicolon;.html")},
150 {`attachment; foo="\"\\";filename="foo.html"`,
152 m("foo", "\"\\", "filename", "foo.html")},
153 {`attachment; filename=foo.html`,
155 m("filename", "foo.html")},
156 {`attachment; filename=foo.html ;`,
158 m("filename", "foo.html")},
159 {`attachment; filename='foo.html'`,
161 m("filename", "foo.html")},
162 {`attachment; filename="foo-%41.html"`,
164 m("filename", "foo-%41.html")},
165 {`attachment; filename="foo-%\41.html"`,
167 m("filename", "foo-%41.html")},
168 {`filename=foo.html`,
170 {`x=y; filename=foo.html`,
172 {`"foo; filename=bar;baz"; filename=qux`,
174 {`inline; attachment; filename=foo.html`,
176 {`attachment; filename="foo.html".txt`,
178 {`attachment; filename="bar`,
180 {`attachment; creation-date="Wed, 12 Feb 1997 16:29:51 -0500"`,
182 m("creation-date", "Wed, 12 Feb 1997 16:29:51 -0500")},
183 {`foobar`, "foobar", m()},
184 {`attachment; filename* =UTF-8''foo-%c3%a4.html`,
186 m("filename", "foo-ä.html")},
187 {`attachment; filename*=UTF-8''A-%2541.html`,
189 m("filename", "A-%41.html")},
190 {`attachment; filename*0="foo."; filename*1="html"`,
192 m("filename", "foo.html")},
193 {`attachment; filename*0*=UTF-8''foo-%c3%a4; filename*1=".html"`,
195 m("filename", "foo-ä.html")},
196 {`attachment; filename*0="foo"; filename*01="bar"`,
198 m("filename", "foo")},
199 {`attachment; filename*0="foo"; filename*2="bar"`,
201 m("filename", "foo")},
202 {`attachment; filename*1="foo"; filename*2="bar"`,
204 {`attachment; filename*1="bar"; filename*0="foo"`,
206 m("filename", "foobar")},
207 {`attachment; filename="foo-ae.html"; filename*=UTF-8''foo-%c3%a4.html`,
209 m("filename", "foo-ä.html")},
210 {`attachment; filename*=UTF-8''foo-%c3%a4.html; filename="foo-ae.html"`,
212 m("filename", "foo-ä.html")},
214 // Browsers also just send UTF-8 directly without RFC 2231,
215 // at least when the source page is served with UTF-8.
216 {`form-data; firstname="Брэд"; lastname="Фицпатрик"`,
218 m("firstname", "Брэд", "lastname", "Фицпатрик")},
220 for _, test := range tests {
221 mt, params := ParseMediaType(test.in)
222 if g, e := mt, test.t; g != e {
223 t.Errorf("for input %q, expected type %q, got %q",
227 if len(params) == 0 && len(test.p) == 0 {
230 if !reflect.DeepEqual(params, test.p) {
231 t.Errorf("for input %q, wrong params.\n"+
234 test.in, test.p, params)
239 func TestParseMediaTypeBogus(t *testing.T) {
240 mt, params := ParseMediaType("bogus ;=========")
242 t.Error("expected empty type")
245 t.Error("expected nil params")