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 import 'dart:io'; | |
6 import 'dart:utf'; | |
7 | |
8 import 'package:expect/expect.dart'; | |
9 | |
10 void testHttpClientResponseBody() { | |
11 void test(String mimeType, | |
12 List<int> content, | |
13 dynamic expectedBody, | |
14 String type, | |
15 [bool shouldFail = false]) { | |
16 HttpServer.bind("127.0.0.1", 0).then((server) { | |
17 server.listen((request) { | |
18 request.listen( | |
19 (_) {}, | |
20 onDone: () { | |
21 request.response.headers.contentType = | |
22 ContentType.parse(mimeType); | |
23 request.response.add(content); | |
24 request.response.close(); | |
25 }); | |
26 }); | |
27 | |
28 var client = new HttpClient(); | |
29 client.get("127.0.0.1", server.port, "/") | |
30 .then((request) => request.close()) | |
31 .then(HttpBodyHandler.processResponse) | |
32 .then((body) { | |
33 if (shouldFail) Expect.fail("Error expected"); | |
34 Expect.equals(type, body.type); | |
35 Expect.isNotNull(body.response); | |
36 switch (type) { | |
37 case "text": | |
38 Expect.equals(expectedBody, body.body); | |
39 break; | |
40 | |
41 case "json": | |
42 Expect.mapEquals(expectedBody, body.body); | |
43 break; | |
44 | |
45 default: | |
46 Expect.fail("bad body type"); | |
47 } | |
48 }, onError: (error) { | |
49 if (!shouldFail) throw error; | |
50 }) | |
51 .whenComplete(() { | |
52 client.close(); | |
53 server.close(); | |
54 }); | |
55 }); | |
56 } | |
57 test("text/plain", "body".codeUnits, "body", "text"); | |
58 test("text/plain; charset=utf-8", | |
59 "body".codeUnits, | |
60 "body", | |
61 "text"); | |
62 test("text/plain; charset=iso-8859-1", | |
63 "body".codeUnits, | |
64 "body", | |
65 "text"); | |
66 test("text/plain; charset=us-ascii", | |
67 "body".codeUnits, | |
68 "body", | |
69 "text"); | |
70 test("text/plain; charset=utf-8", [42], "*", "text"); | |
71 test("text/plain; charset=us-ascii", [142], "?", "text"); | |
72 test("text/plain; charset=utf-8", | |
73 [142], | |
74 new String.fromCharCodes([UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]), | |
75 "text"); | |
76 | |
77 test("application/json", | |
78 '{"val": 5}'.codeUnits, | |
79 { "val" : 5 }, | |
80 "json"); | |
81 test("application/json", | |
82 '{ bad json }'.codeUnits, | |
83 null, | |
84 "json", | |
85 true); | |
86 } | |
87 | |
88 void testHttpServerRequestBody() { | |
89 void test(String mimeType, | |
90 List<int> content, | |
91 dynamic expectedBody, | |
92 String type, | |
93 {bool shouldFail: false, | |
94 Encoding defaultEncoding: Encoding.UTF_8}) { | |
95 HttpServer.bind("127.0.0.1", 0).then((server) { | |
96 server.transform(new HttpBodyHandler(defaultEncoding: defaultEncoding)) | |
97 .listen((body) { | |
98 if (shouldFail) Expect.fail("Error expected"); | |
99 Expect.equals(type, body.type); | |
100 switch (type) { | |
101 case "text": | |
102 Expect.equals(body.contentType.mimeType, "text/plain"); | |
103 Expect.equals(expectedBody, body.body); | |
104 break; | |
105 | |
106 case "json": | |
107 Expect.equals(body.contentType.mimeType, "application/json"); | |
108 Expect.mapEquals(expectedBody, body.body); | |
109 break; | |
110 | |
111 case "binary": | |
112 Expect.equals(body.contentType, null); | |
113 Expect.listEquals(expectedBody, body.body); | |
114 break; | |
115 | |
116 case "form": | |
117 var mimeType = body.contentType.mimeType; | |
118 Expect.isTrue( | |
119 mimeType == 'multipart/form-data' || | |
120 mimeType == 'application/x-www-form-urlencoded'); | |
121 Expect.setEquals(expectedBody.keys.toSet(), | |
122 body.body.keys.toSet()); | |
123 for (var key in expectedBody.keys) { | |
124 if (body.body[key] is HttpBodyFileUpload) { | |
125 Expect.equals(expectedBody[key]['contentType'], | |
126 body.body[key].contentType.toString()); | |
127 Expect.equals(expectedBody[key]['filename'], | |
128 body.body[key].filename); | |
129 if (body.body[key].content is String) { | |
130 Expect.equals(expectedBody[key]['content'], | |
131 body.body[key].content); | |
132 } else { | |
133 Expect.listEquals(expectedBody[key]['content'], | |
134 body.body[key].content); | |
135 } | |
136 } else { | |
137 Expect.equals(expectedBody[key], body.body[key]); | |
138 } | |
139 } | |
140 break; | |
141 | |
142 default: | |
143 Expect.fail("bad body type"); | |
144 } | |
145 body.response.close(); | |
146 }, onError: (error) { | |
147 if (!shouldFail) throw error; | |
148 }); | |
149 | |
150 var client = new HttpClient(); | |
151 client.post("127.0.0.1", server.port, "/") | |
152 .then((request) { | |
153 if (mimeType != null) { | |
154 request.headers.contentType = | |
155 ContentType.parse(mimeType); | |
156 } | |
157 request.add(content); | |
158 return request.close(); | |
159 }) | |
160 .then((response) { | |
161 if (shouldFail) { | |
162 Expect.equals(HttpStatus.BAD_REQUEST, response.statusCode); | |
163 } | |
164 response.fold(null, (x, y) {}); | |
165 client.close(); | |
166 server.close(); | |
167 }); | |
168 }); | |
169 } | |
170 | |
171 test("text/plain", "body".codeUnits, "body", "text"); | |
172 test("text/plain; charset=utf-8", | |
173 "body".codeUnits, | |
174 "body", | |
175 "text"); | |
176 test("text/plain; charset=utf-8", [42], "*", "text"); | |
177 test("text/plain; charset=us-ascii", [142], "?", "text"); | |
178 test("text/plain; charset=utf-8", | |
179 [142], | |
180 new String.fromCharCodes([UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]), | |
181 "text"); | |
182 | |
183 test("application/json", | |
184 '{"val": 5}'.codeUnits, | |
185 { "val" : 5 }, | |
186 "json"); | |
187 test("application/json", | |
188 '{ bad json }'.codeUnits, | |
189 null, | |
190 "json", | |
191 shouldFail: true); | |
192 | |
193 test(null, "body".codeUnits, "body".codeUnits, "binary"); | |
194 | |
195 test("multipart/form-data; boundary=AaB03x", | |
196 ''' | |
197 --AaB03x\r | |
198 Content-Disposition: form-data; name="name"\r | |
199 \r | |
200 Larry\r | |
201 --AaB03x--\r\n'''.codeUnits, | |
202 { "name": "Larry" }, | |
203 "form"); | |
204 | |
205 test("multipart/form-data; boundary=AaB03x", | |
206 ''' | |
207 --AaB03x\r | |
208 Content-Disposition: form-data; name="files"; filename="myfile"\r | |
209 Content-Type: application/octet-stream\r | |
210 \r | |
211 File content\r | |
212 --AaB03x--\r\n'''.codeUnits, | |
213 { "files": { 'filename': 'myfile', | |
214 'contentType': 'application/octet-stream', | |
215 'content': 'File content'.codeUnits} }, | |
216 "form"); | |
217 | |
218 test("multipart/form-data; boundary=AaB03x", | |
219 ''' | |
220 --AaB03x\r | |
221 Content-Disposition: form-data; name="files"; filename="myfile"\r | |
222 Content-Type: application/octet-stream\r | |
223 \r | |
224 File content\r | |
225 --AaB03x\r | |
226 Content-Disposition: form-data; name="files"; filename="myfile"\r | |
227 Content-Type: text/plain\r | |
228 \r | |
229 File content\r | |
230 --AaB03x--\r\n'''.codeUnits, | |
231 { "files": { 'filename': 'myfile', | |
232 'contentType': 'text/plain', | |
233 'content': 'File content'} }, | |
234 "form"); | |
235 | |
236 test("multipart/form-data; boundary=AaB03x", | |
237 ''' | |
238 --AaB03x\r | |
239 Content-Disposition: form-data; name="files"; filename="myfile"\r | |
240 Content-Type: application/json\r | |
241 \r | |
242 File content\r | |
243 --AaB03x--\r\n'''.codeUnits, | |
244 { "files": { 'filename': 'myfile', | |
245 'contentType': 'application/json', | |
246 'content': 'File content'} }, | |
247 "form"); | |
248 | |
249 test('application/x-www-form-urlencoded', | |
250 '%E5%B9%B3%3D%E4%BB%AE%E5%90%8D=%E5%B9%B3%E4%BB%AE%E5%90%8D&b' | |
251 '=%E5%B9%B3%E4%BB%AE%E5%90%8D'.codeUnits, | |
252 { 'b' : '平仮名', | |
253 '平=仮名' : '平仮名'}, | |
254 "form"); | |
255 | |
256 test('application/x-www-form-urlencoded', | |
257 'a=%F8+%26%23548%3B'.codeUnits, | |
258 { 'a' : '\u{FFFD} Ȥ' }, | |
259 "form"); | |
260 | |
261 test('application/x-www-form-urlencoded', | |
262 'a=%C0%A0'.codeUnits, | |
263 { 'a' : '\u{FFFD}' }, | |
264 "form"); | |
265 | |
266 test('application/x-www-form-urlencoded', | |
267 'a=x%A0x'.codeUnits, | |
268 { 'a' : 'x\u{FFFD}x' }, | |
269 "form"); | |
270 | |
271 test('application/x-www-form-urlencoded', | |
272 'a=x%C0x'.codeUnits, | |
273 { 'a' : 'x\u{FFFD}x' }, | |
274 "form"); | |
275 | |
276 test('application/x-www-form-urlencoded', | |
277 'a=%C3%B8+%C8%A4'.codeUnits, | |
278 { 'a' : 'ø Ȥ' }, | |
279 "form"); | |
280 | |
281 test('application/x-www-form-urlencoded', | |
282 'a=%F8+%26%23548%3B'.codeUnits, | |
283 { 'a' : 'ø Ȥ' }, | |
284 "form", | |
285 defaultEncoding: Encoding.ISO_8859_1); | |
286 | |
287 test('application/x-www-form-urlencoded', | |
288 'name=%26'.codeUnits, | |
289 { 'name' : '&' }, | |
290 "form", | |
291 defaultEncoding: Encoding.ISO_8859_1); | |
292 | |
293 test('application/x-www-form-urlencoded', | |
294 'name=%F8%26'.codeUnits, | |
295 { 'name' : 'ø&' }, | |
296 "form", | |
297 defaultEncoding: Encoding.ISO_8859_1); | |
298 | |
299 test('application/x-www-form-urlencoded', | |
300 'name=%26%3B'.codeUnits, | |
301 { 'name' : '&;' }, | |
302 "form", | |
303 defaultEncoding: Encoding.ISO_8859_1); | |
304 | |
305 test('application/x-www-form-urlencoded', | |
306 'name=%26%23548%3B%26%23548%3B'.codeUnits, | |
307 { 'name' : 'ȤȤ' }, | |
308 "form", | |
309 defaultEncoding: Encoding.ISO_8859_1); | |
310 | |
311 test('application/x-www-form-urlencoded', | |
312 'name=%26'.codeUnits, | |
313 { 'name' : '&' }, | |
314 "form"); | |
315 | |
316 test('application/x-www-form-urlencoded', | |
317 'name=%C3%B8%26'.codeUnits, | |
318 { 'name' : 'ø&' }, | |
319 "form"); | |
320 | |
321 test('application/x-www-form-urlencoded', | |
322 'name=%26%3B'.codeUnits, | |
323 { 'name' : '&;' }, | |
324 "form"); | |
325 | |
326 test('application/x-www-form-urlencoded', | |
327 'name=%C8%A4%26%23548%3B'.codeUnits, | |
328 { 'name' : 'ȤȤ' }, | |
329 "form"); | |
330 | |
331 test('application/x-www-form-urlencoded', | |
332 'name=%C8%A4%C8%A4'.codeUnits, | |
333 { 'name' : 'ȤȤ' }, | |
334 "form"); | |
335 } | |
336 | |
337 void main() { | |
338 testHttpClientResponseBody(); | |
339 testHttpServerRequestBody(); | |
340 } | |
OLD | NEW |