OLD | NEW |
| (Empty) |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library multipart_test; | |
6 | |
7 import 'dart:async'; | |
8 | |
9 import 'package:http/http.dart' as http; | |
10 import 'package:http_parser/http_parser.dart'; | |
11 import 'package:unittest/unittest.dart'; | |
12 | |
13 import 'utils.dart'; | |
14 | |
15 void main() { | |
16 test('empty', () { | |
17 var request = new http.MultipartRequest('POST', dummyUrl); | |
18 expect(request, bodyMatches(''' | |
19 --{{boundary}}-- | |
20 ''')); | |
21 }); | |
22 | |
23 test('with fields and files', () { | |
24 var request = new http.MultipartRequest('POST', dummyUrl); | |
25 request.fields['field1'] = 'value1'; | |
26 request.fields['field2'] = 'value2'; | |
27 request.files.add(new http.MultipartFile.fromString("file1", "contents1", | |
28 filename: "filename1.txt")); | |
29 request.files.add(new http.MultipartFile.fromString("file2", "contents2")); | |
30 | |
31 expect(request, bodyMatches(''' | |
32 --{{boundary}} | |
33 content-disposition: form-data; name="field1" | |
34 | |
35 value1 | |
36 --{{boundary}} | |
37 content-disposition: form-data; name="field2" | |
38 | |
39 value2 | |
40 --{{boundary}} | |
41 content-type: text/plain; charset=utf-8 | |
42 content-disposition: form-data; name="file1"; filename="filename1.txt" | |
43 | |
44 contents1 | |
45 --{{boundary}} | |
46 content-type: text/plain; charset=utf-8 | |
47 content-disposition: form-data; name="file2" | |
48 | |
49 contents2 | |
50 --{{boundary}}-- | |
51 ''')); | |
52 }); | |
53 | |
54 test('with a unicode field name', () { | |
55 var request = new http.MultipartRequest('POST', dummyUrl); | |
56 request.fields['fïēld'] = 'value'; | |
57 | |
58 expect(request, bodyMatches(''' | |
59 --{{boundary}} | |
60 content-disposition: form-data; name="fïēld" | |
61 | |
62 value | |
63 --{{boundary}}-- | |
64 ''')); | |
65 }); | |
66 | |
67 test('with a field name with newlines', () { | |
68 var request = new http.MultipartRequest('POST', dummyUrl); | |
69 request.fields['foo\nbar\rbaz\r\nbang'] = 'value'; | |
70 | |
71 expect(request, bodyMatches(''' | |
72 --{{boundary}} | |
73 content-disposition: form-data; name="foo%0D%0Abar%0D%0Abaz%0D%0Abang" | |
74 | |
75 value | |
76 --{{boundary}}-- | |
77 ''')); | |
78 }); | |
79 | |
80 test('with a field name with a quote', () { | |
81 var request = new http.MultipartRequest('POST', dummyUrl); | |
82 request.fields['foo"bar'] = 'value'; | |
83 | |
84 expect(request, bodyMatches(''' | |
85 --{{boundary}} | |
86 content-disposition: form-data; name="foo%22bar" | |
87 | |
88 value | |
89 --{{boundary}}-- | |
90 ''')); | |
91 }); | |
92 | |
93 test('with a unicode field value', () { | |
94 var request = new http.MultipartRequest('POST', dummyUrl); | |
95 request.fields['field'] = 'vⱥlūe'; | |
96 | |
97 expect(request, bodyMatches(''' | |
98 --{{boundary}} | |
99 content-disposition: form-data; name="field" | |
100 content-type: text/plain; charset=utf-8 | |
101 | |
102 vⱥlūe | |
103 --{{boundary}}-- | |
104 ''')); | |
105 }); | |
106 | |
107 test('with a unicode filename', () { | |
108 var request = new http.MultipartRequest('POST', dummyUrl); | |
109 request.files.add(new http.MultipartFile.fromString('file', 'contents', | |
110 filename: 'fïlēname.txt')); | |
111 | |
112 expect(request, bodyMatches(''' | |
113 --{{boundary}} | |
114 content-type: text/plain; charset=utf-8 | |
115 content-disposition: form-data; name="file"; filename="fïlēname.txt" | |
116 | |
117 contents | |
118 --{{boundary}}-- | |
119 ''')); | |
120 }); | |
121 | |
122 test('with a filename with newlines', () { | |
123 var request = new http.MultipartRequest('POST', dummyUrl); | |
124 request.files.add(new http.MultipartFile.fromString('file', 'contents', | |
125 filename: 'foo\nbar\rbaz\r\nbang')); | |
126 | |
127 expect(request, bodyMatches(''' | |
128 --{{boundary}} | |
129 content-type: text/plain; charset=utf-8 | |
130 content-disposition: form-data; name="file"; filename="foo%0D%0Abar%0D%0
Abaz%0D%0Abang" | |
131 | |
132 contents | |
133 --{{boundary}}-- | |
134 ''')); | |
135 }); | |
136 | |
137 test('with a filename with a quote', () { | |
138 var request = new http.MultipartRequest('POST', dummyUrl); | |
139 request.files.add(new http.MultipartFile.fromString('file', 'contents', | |
140 filename: 'foo"bar')); | |
141 | |
142 expect(request, bodyMatches(''' | |
143 --{{boundary}} | |
144 content-type: text/plain; charset=utf-8 | |
145 content-disposition: form-data; name="file"; filename="foo%22bar" | |
146 | |
147 contents | |
148 --{{boundary}}-- | |
149 ''')); | |
150 }); | |
151 | |
152 test('with a string file with a content-type but no charset', () { | |
153 var request = new http.MultipartRequest('POST', dummyUrl); | |
154 var file = new http.MultipartFile.fromString('file', '{"hello": "world"}', | |
155 contentType: new MediaType('application', 'json')); | |
156 request.files.add(file); | |
157 | |
158 expect(request, bodyMatches(''' | |
159 --{{boundary}} | |
160 content-type: application/json; charset=utf-8 | |
161 content-disposition: form-data; name="file" | |
162 | |
163 {"hello": "world"} | |
164 --{{boundary}}-- | |
165 ''')); | |
166 }); | |
167 | |
168 test('with a file with a iso-8859-1 body', () { | |
169 var request = new http.MultipartRequest('POST', dummyUrl); | |
170 // "Ã¥" encoded as ISO-8859-1 and then read as UTF-8 results in "å". | |
171 var file = new http.MultipartFile.fromString('file', 'non-ascii: "Ã¥"', | |
172 contentType: new MediaType('text', 'plain', {'charset': 'iso-8859-1'})); | |
173 request.files.add(file); | |
174 | |
175 expect(request, bodyMatches(''' | |
176 --{{boundary}} | |
177 content-type: text/plain; charset=iso-8859-1 | |
178 content-disposition: form-data; name="file" | |
179 | |
180 non-ascii: "å" | |
181 --{{boundary}}-- | |
182 ''')); | |
183 }); | |
184 | |
185 test('with a stream file', () { | |
186 var request = new http.MultipartRequest('POST', dummyUrl); | |
187 var controller = new StreamController(sync: true); | |
188 request.files.add(new http.MultipartFile('file', controller.stream, 5)); | |
189 | |
190 expect(request, bodyMatches(''' | |
191 --{{boundary}} | |
192 content-type: application/octet-stream | |
193 content-disposition: form-data; name="file" | |
194 | |
195 hello | |
196 --{{boundary}}-- | |
197 ''')); | |
198 | |
199 controller.add([104, 101, 108, 108, 111]); | |
200 controller.close(); | |
201 }); | |
202 | |
203 test('with an empty stream file', () { | |
204 var request = new http.MultipartRequest('POST', dummyUrl); | |
205 var controller = new StreamController(sync: true); | |
206 request.files.add(new http.MultipartFile('file', controller.stream, 0)); | |
207 | |
208 expect(request, bodyMatches(''' | |
209 --{{boundary}} | |
210 content-type: application/octet-stream | |
211 content-disposition: form-data; name="file" | |
212 | |
213 | |
214 --{{boundary}}-- | |
215 ''')); | |
216 | |
217 controller.close(); | |
218 }); | |
219 | |
220 test('with a byte file', () { | |
221 var request = new http.MultipartRequest('POST', dummyUrl); | |
222 var file = new http.MultipartFile.fromBytes( | |
223 'file', [104, 101, 108, 108, 111]); | |
224 request.files.add(file); | |
225 | |
226 expect(request, bodyMatches(''' | |
227 --{{boundary}} | |
228 content-type: application/octet-stream | |
229 content-disposition: form-data; name="file" | |
230 | |
231 hello | |
232 --{{boundary}}-- | |
233 ''')); | |
234 }); | |
235 } | |
OLD | NEW |