OSDN Git Service

Update to current version of Go library.
[pf3gnuchains/gcc-fork.git] / libgo / go / mime / mediatype_test.go
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.
4
5 package mime
6
7 import (
8         "reflect"
9         "testing"
10 )
11
12 func TestConsumeToken(t *testing.T) {
13         tests := [...][3]string{
14                 {"foo bar", "foo", " bar"},
15                 {"bar", "bar", ""},
16                 {"", "", ""},
17                 {" foo", "", " foo"},
18         }
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])
29                 }
30         }
31 }
32
33 func TestConsumeValue(t *testing.T) {
34         tests := [...][3]string{
35                 {"foo bar", "foo", " bar"},
36                 {"bar", "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`},
43         }
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])
54                 }
55         }
56 }
57
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", ` `},
70         }
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])
85                 }
86         }
87 }
88
89 type mediaTypeTest struct {
90         in string
91         t  string
92         p  map[string]string
93 }
94
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 {
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                 {`foo; key=val1; key=the-key-appears-again-which-is-bogus`,
118                         "", m()},
119
120                 // From RFC 2231:
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***")},
124
125                 {`message/external-body; access-type=URL; ` +
126                         `URL*0="ftp://";` +
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")},
131
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!")},
138
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"`,
142                         "attachment",
143                         m("filename", "foo.html")},
144                 {`attachment; filename="\"quoting\" tested.html"`,
145                         "attachment",
146                         m("filename", `"quoting" tested.html`)},
147                 {`attachment; filename="Here's a semicolon;.html"`,
148                         "attachment",
149                         m("filename", "Here's a semicolon;.html")},
150                 {`attachment; foo="\"\\";filename="foo.html"`,
151                         "attachment",
152                         m("foo", "\"\\", "filename", "foo.html")},
153                 {`attachment; filename=foo.html`,
154                         "attachment",
155                         m("filename", "foo.html")},
156                 {`attachment; filename=foo.html ;`,
157                         "attachment",
158                         m("filename", "foo.html")},
159                 {`attachment; filename='foo.html'`,
160                         "attachment",
161                         m("filename", "foo.html")},
162                 {`attachment; filename="foo-%41.html"`,
163                         "attachment",
164                         m("filename", "foo-%41.html")},
165                 {`attachment; filename="foo-%\41.html"`,
166                         "attachment",
167                         m("filename", "foo-%41.html")},
168                 {`filename=foo.html`,
169                         "", m()},
170                 {`x=y; filename=foo.html`,
171                         "", m()},
172                 {`"foo; filename=bar;baz"; filename=qux`,
173                         "", m()},
174                 {`inline; attachment; filename=foo.html`,
175                         "", m()},
176                 {`attachment; filename="foo.html".txt`,
177                         "", m()},
178                 {`attachment; filename="bar`,
179                         "", m()},
180                 {`attachment; creation-date="Wed, 12 Feb 1997 16:29:51 -0500"`,
181                         "attachment",
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`,
185                         "attachment",
186                         m("filename", "foo-ä.html")},
187                 {`attachment; filename*=UTF-8''A-%2541.html`,
188                         "attachment",
189                         m("filename", "A-%41.html")},
190                 {`attachment; filename*0="foo."; filename*1="html"`,
191                         "attachment",
192                         m("filename", "foo.html")},
193                 {`attachment; filename*0*=UTF-8''foo-%c3%a4; filename*1=".html"`,
194                         "attachment",
195                         m("filename", "foo-ä.html")},
196                 {`attachment; filename*0="foo"; filename*01="bar"`,
197                         "attachment",
198                         m("filename", "foo")},
199                 {`attachment; filename*0="foo"; filename*2="bar"`,
200                         "attachment",
201                         m("filename", "foo")},
202                 {`attachment; filename*1="foo"; filename*2="bar"`,
203                         "attachment", m()},
204                 {`attachment; filename*1="bar"; filename*0="foo"`,
205                         "attachment",
206                         m("filename", "foobar")},
207                 {`attachment; filename="foo-ae.html"; filename*=UTF-8''foo-%c3%a4.html`,
208                         "attachment",
209                         m("filename", "foo-ä.html")},
210                 {`attachment; filename*=UTF-8''foo-%c3%a4.html; filename="foo-ae.html"`,
211                         "attachment",
212                         m("filename", "foo-ä.html")},
213
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="Фицпатрик"`,
217                         "form-data",
218                         m("firstname", "Брэд", "lastname", "Фицпатрик")},
219         }
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",
224                                 test.in, e, g)
225                         continue
226                 }
227                 if len(params) == 0 && len(test.p) == 0 {
228                         continue
229                 }
230                 if !reflect.DeepEqual(params, test.p) {
231                         t.Errorf("for input %q, wrong params.\n"+
232                                 "expected: %#v\n"+
233                                 "     got: %#v",
234                                 test.in, test.p, params)
235                 }
236         }
237 }
238
239 func TestParseMediaTypeBogus(t *testing.T) {
240         mt, params := ParseMediaType("bogus ;=========")
241         if mt != "" {
242                 t.Error("expected empty type")
243         }
244         if params != nil {
245                 t.Error("expected nil params")
246         }
247 }