| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 import 'dart:io'; | 5 import 'dart:io'; |
| 6 import 'dart:utf'; | 6 import 'dart:convert'; |
| 7 | 7 |
| 8 import 'package:http_server/http_server.dart'; | 8 import 'package:http_server/http_server.dart'; |
| 9 import 'package:unittest/unittest.dart'; | 9 import 'package:unittest/unittest.dart'; |
| 10 | 10 |
| 11 void testHttpClientResponseBody() { | 11 void testHttpClientResponseBody() { |
| 12 void test(String mimeType, | 12 void test(String mimeType, |
| 13 List<int> content, | 13 List<int> content, |
| 14 dynamic expectedBody, | 14 dynamic expectedBody, |
| 15 String type, | 15 String type, |
| 16 [bool shouldFail = false]) { | 16 [bool shouldFail = false]) { |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 "text"); | 59 "text"); |
| 60 test("text/plain; charset=iso-8859-1", | 60 test("text/plain; charset=iso-8859-1", |
| 61 "body".codeUnits, | 61 "body".codeUnits, |
| 62 "body", | 62 "body", |
| 63 "text"); | 63 "text"); |
| 64 test("text/plain; charset=us-ascii", | 64 test("text/plain; charset=us-ascii", |
| 65 "body".codeUnits, | 65 "body".codeUnits, |
| 66 "body", | 66 "body", |
| 67 "text"); | 67 "text"); |
| 68 test("text/plain; charset=utf-8", [42], "*", "text"); | 68 test("text/plain; charset=utf-8", [42], "*", "text"); |
| 69 test("text/plain; charset=us-ascii", [142], "?", "text"); | 69 test("text/plain; charset=us-ascii", [142], null, "text", true); |
| 70 test("text/plain; charset=utf-8", | 70 test("text/plain; charset=utf-8", [142], null, "text", true); |
| 71 [142], | |
| 72 new String.fromCharCodes([UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]), | |
| 73 "text"); | |
| 74 | 71 |
| 75 test("application/json", | 72 test("application/json", |
| 76 '{"val": 5}'.codeUnits, | 73 '{"val": 5}'.codeUnits, |
| 77 { "val" : 5 }, | 74 { "val" : 5 }, |
| 78 "json"); | 75 "json"); |
| 79 test("application/json", | 76 test("application/json", |
| 80 '{ bad json }'.codeUnits, | 77 '{ bad json }'.codeUnits, |
| 81 null, | 78 null, |
| 82 "json", | 79 "json", |
| 83 true); | 80 true); |
| 84 } | 81 } |
| 85 | 82 |
| 86 void testHttpServerRequestBody() { | 83 void testHttpServerRequestBody() { |
| 87 void test(String mimeType, | 84 void test(String mimeType, |
| 88 List<int> content, | 85 List<int> content, |
| 89 dynamic expectedBody, | 86 dynamic expectedBody, |
| 90 String type, | 87 String type, |
| 91 {bool shouldFail: false, | 88 {bool shouldFail: false, |
| 92 Encoding defaultEncoding: Encoding.UTF_8}) { | 89 Encoding defaultEncoding: UTF8}) { |
| 93 HttpServer.bind("127.0.0.1", 0).then((server) { | 90 HttpServer.bind("127.0.0.1", 0).then((server) { |
| 94 server.transform(new HttpBodyHandler(defaultEncoding: defaultEncoding)) | 91 server.transform(new HttpBodyHandler(defaultEncoding: defaultEncoding)) |
| 95 .listen((body) { | 92 .listen((body) { |
| 96 expect(shouldFail, isFalse); | 93 expect(shouldFail, isFalse); |
| 97 expect(body.type, equals(type)); | 94 expect(body.type, equals(type)); |
| 98 switch (type) { | 95 switch (type) { |
| 99 case "text": | 96 case "text": |
| 100 expect(body.contentType.mimeType, equals("text/plain")); | 97 expect(body.contentType.mimeType, equals("text/plain")); |
| 101 expect(body.body, equals(expectedBody)); | 98 expect(body.body, equals(expectedBody)); |
| 102 break; | 99 break; |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 }); | 159 }); |
| 163 }); | 160 }); |
| 164 } | 161 } |
| 165 | 162 |
| 166 test("text/plain", "body".codeUnits, "body", "text"); | 163 test("text/plain", "body".codeUnits, "body", "text"); |
| 167 test("text/plain; charset=utf-8", | 164 test("text/plain; charset=utf-8", |
| 168 "body".codeUnits, | 165 "body".codeUnits, |
| 169 "body", | 166 "body", |
| 170 "text"); | 167 "text"); |
| 171 test("text/plain; charset=utf-8", [42], "*", "text"); | 168 test("text/plain; charset=utf-8", [42], "*", "text"); |
| 172 test("text/plain; charset=us-ascii", [142], "?", "text"); | 169 test("text/plain; charset=us-ascii", [142], null, "text", shouldFail: true); |
| 173 test("text/plain; charset=utf-8", | 170 test("text/plain; charset=utf-8", [142], null, "text", shouldFail: true); |
| 174 [142], | |
| 175 new String.fromCharCodes([UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]), | |
| 176 "text"); | |
| 177 | 171 |
| 178 test("application/json", | 172 test("application/json", |
| 179 '{"val": 5}'.codeUnits, | 173 '{"val": 5}'.codeUnits, |
| 180 { "val" : 5 }, | 174 { "val" : 5 }, |
| 181 "json"); | 175 "json"); |
| 182 test("application/json", | 176 test("application/json", |
| 183 '{ bad json }'.codeUnits, | 177 '{ bad json }'.codeUnits, |
| 184 null, | 178 null, |
| 185 "json", | 179 "json", |
| 186 shouldFail: true); | 180 shouldFail: true); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 | 237 |
| 244 test('application/x-www-form-urlencoded', | 238 test('application/x-www-form-urlencoded', |
| 245 '%E5%B9%B3%3D%E4%BB%AE%E5%90%8D=%E5%B9%B3%E4%BB%AE%E5%90%8D&b' | 239 '%E5%B9%B3%3D%E4%BB%AE%E5%90%8D=%E5%B9%B3%E4%BB%AE%E5%90%8D&b' |
| 246 '=%E5%B9%B3%E4%BB%AE%E5%90%8D'.codeUnits, | 240 '=%E5%B9%B3%E4%BB%AE%E5%90%8D'.codeUnits, |
| 247 { 'b' : '平仮名', | 241 { 'b' : '平仮名', |
| 248 '平=仮名' : '平仮名'}, | 242 '平=仮名' : '平仮名'}, |
| 249 "form"); | 243 "form"); |
| 250 | 244 |
| 251 test('application/x-www-form-urlencoded', | 245 test('application/x-www-form-urlencoded', |
| 252 'a=%F8+%26%23548%3B'.codeUnits, | 246 'a=%F8+%26%23548%3B'.codeUnits, |
| 253 { 'a' : '\u{FFFD} Ȥ' }, | 247 null, |
| 254 "form"); | 248 "form", |
| 249 shouldFail: true); |
| 255 | 250 |
| 256 test('application/x-www-form-urlencoded', | 251 test('application/x-www-form-urlencoded', |
| 257 'a=%C0%A0'.codeUnits, | 252 'a=%C0%A0'.codeUnits, |
| 258 { 'a' : '\u{FFFD}' }, | 253 null, |
| 259 "form"); | 254 "form", |
| 255 shouldFail: true); |
| 260 | 256 |
| 261 test('application/x-www-form-urlencoded', | 257 test('application/x-www-form-urlencoded', |
| 262 'a=x%A0x'.codeUnits, | 258 'a=x%A0x'.codeUnits, |
| 263 { 'a' : 'x\u{FFFD}x' }, | 259 null, |
| 264 "form"); | 260 "form", |
| 261 shouldFail: true); |
| 265 | 262 |
| 266 test('application/x-www-form-urlencoded', | 263 test('application/x-www-form-urlencoded', |
| 267 'a=x%C0x'.codeUnits, | 264 'a=x%C0x'.codeUnits, |
| 268 { 'a' : 'x\u{FFFD}x' }, | 265 null, |
| 269 "form"); | 266 "form", |
| 267 shouldFail: true); |
| 270 | 268 |
| 271 test('application/x-www-form-urlencoded', | 269 test('application/x-www-form-urlencoded', |
| 272 'a=%C3%B8+%C8%A4'.codeUnits, | 270 'a=%C3%B8+%C8%A4'.codeUnits, |
| 273 { 'a' : 'ø Ȥ' }, | 271 { 'a' : 'ø Ȥ' }, |
| 274 "form"); | 272 "form"); |
| 275 | 273 |
| 276 test('application/x-www-form-urlencoded', | 274 test('application/x-www-form-urlencoded', |
| 277 'a=%F8+%26%23548%3B'.codeUnits, | 275 'a=%F8+%26%23548%3B'.codeUnits, |
| 278 { 'a' : 'ø Ȥ' }, | 276 { 'a' : 'ø Ȥ' }, |
| 279 "form", | 277 "form", |
| 280 defaultEncoding: Encoding.ISO_8859_1); | 278 defaultEncoding: LATIN1); |
| 281 | 279 |
| 282 test('application/x-www-form-urlencoded', | 280 test('application/x-www-form-urlencoded', |
| 283 'name=%26'.codeUnits, | 281 'name=%26'.codeUnits, |
| 284 { 'name' : '&' }, | 282 { 'name' : '&' }, |
| 285 "form", | 283 "form", |
| 286 defaultEncoding: Encoding.ISO_8859_1); | 284 defaultEncoding: LATIN1); |
| 287 | 285 |
| 288 test('application/x-www-form-urlencoded', | 286 test('application/x-www-form-urlencoded', |
| 289 'name=%F8%26'.codeUnits, | 287 'name=%F8%26'.codeUnits, |
| 290 { 'name' : 'ø&' }, | 288 { 'name' : 'ø&' }, |
| 291 "form", | 289 "form", |
| 292 defaultEncoding: Encoding.ISO_8859_1); | 290 defaultEncoding: LATIN1); |
| 293 | 291 |
| 294 test('application/x-www-form-urlencoded', | 292 test('application/x-www-form-urlencoded', |
| 295 'name=%26%3B'.codeUnits, | 293 'name=%26%3B'.codeUnits, |
| 296 { 'name' : '&;' }, | 294 { 'name' : '&;' }, |
| 297 "form", | 295 "form", |
| 298 defaultEncoding: Encoding.ISO_8859_1); | 296 defaultEncoding: LATIN1); |
| 299 | 297 |
| 300 test('application/x-www-form-urlencoded', | 298 test('application/x-www-form-urlencoded', |
| 301 'name=%26%23548%3B%26%23548%3B'.codeUnits, | 299 'name=%26%23548%3B%26%23548%3B'.codeUnits, |
| 302 { 'name' : 'ȤȤ' }, | 300 { 'name' : 'ȤȤ' }, |
| 303 "form", | 301 "form", |
| 304 defaultEncoding: Encoding.ISO_8859_1); | 302 defaultEncoding: LATIN1); |
| 305 | 303 |
| 306 test('application/x-www-form-urlencoded', | 304 test('application/x-www-form-urlencoded', |
| 307 'name=%26'.codeUnits, | 305 'name=%26'.codeUnits, |
| 308 { 'name' : '&' }, | 306 { 'name' : '&' }, |
| 309 "form"); | 307 "form"); |
| 310 | 308 |
| 311 test('application/x-www-form-urlencoded', | 309 test('application/x-www-form-urlencoded', |
| 312 'name=%C3%B8%26'.codeUnits, | 310 'name=%C3%B8%26'.codeUnits, |
| 313 { 'name' : 'ø&' }, | 311 { 'name' : 'ø&' }, |
| 314 "form"); | 312 "form"); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 326 test('application/x-www-form-urlencoded', | 324 test('application/x-www-form-urlencoded', |
| 327 'name=%C8%A4%C8%A4'.codeUnits, | 325 'name=%C8%A4%C8%A4'.codeUnits, |
| 328 { 'name' : 'ȤȤ' }, | 326 { 'name' : 'ȤȤ' }, |
| 329 "form"); | 327 "form"); |
| 330 } | 328 } |
| 331 | 329 |
| 332 void main() { | 330 void main() { |
| 333 testHttpClientResponseBody(); | 331 testHttpClientResponseBody(); |
| 334 testHttpServerRequestBody(); | 332 testHttpServerRequestBody(); |
| 335 } | 333 } |
| OLD | NEW |