| 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 // VMOptions= | 5 // VMOptions= |
| 6 // VMOptions=--short_socket_read | 6 // VMOptions=--short_socket_read |
| 7 // VMOptions=--short_socket_write | 7 // VMOptions=--short_socket_write |
| 8 // VMOptions=--short_socket_read --short_socket_write | 8 // VMOptions=--short_socket_read --short_socket_write |
| 9 | 9 |
| 10 import "dart:async"; | 10 import "dart:async"; |
| 11 import "dart:convert"; |
| 11 import "dart:io"; | 12 import "dart:io"; |
| 12 import "dart:typed_data"; | 13 import "dart:typed_data"; |
| 13 | 14 |
| 14 import "package:async_helper/async_helper.dart"; | 15 import "package:async_helper/async_helper.dart"; |
| 15 import "package:crypto/crypto.dart"; | 16 import "package:crypto/crypto.dart"; |
| 16 import "package:expect/expect.dart"; | 17 import "package:expect/expect.dart"; |
| 17 import "package:path/path.dart"; | 18 import "package:path/path.dart"; |
| 18 | 19 |
| 19 const WEB_SOCKET_GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; | 20 const WEB_SOCKET_GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; |
| 20 | 21 |
| (...skipping 424 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 445 Expect.equals('my-value-1, my-value-2', header[0]); | 446 Expect.equals('my-value-1, my-value-2', header[0]); |
| 446 WebSocketTransformer.upgrade(request).then((webSocket) { | 447 WebSocketTransformer.upgrade(request).then((webSocket) { |
| 447 webSocket.listen((_) { webSocket.close(); }); | 448 webSocket.listen((_) { webSocket.close(); }); |
| 448 webSocket.add("Hello"); | 449 webSocket.add("Hello"); |
| 449 }); | 450 }); |
| 450 }); | 451 }); |
| 451 | 452 |
| 452 var url = '${secure ? "wss" : "ws"}://$HOST_NAME:${server.port}/'; | 453 var url = '${secure ? "wss" : "ws"}://$HOST_NAME:${server.port}/'; |
| 453 var headers = {'My-Header': 'my-value', | 454 var headers = {'My-Header': 'my-value', |
| 454 'My-Header-Multiple': ['my-value-1', 'my-value-2']}; | 455 'My-Header-Multiple': ['my-value-1', 'my-value-2']}; |
| 455 print(headers); | |
| 456 print(headers['My-Header-Multiple'] is Iterable); | |
| 457 print(headers['My-Header-Multiple'].length); | |
| 458 WebSocket.connect(url, headers: headers).then((websocket) { | 456 WebSocket.connect(url, headers: headers).then((websocket) { |
| 459 return websocket.listen((message) { | 457 return websocket.listen((message) { |
| 460 Expect.equals("Hello", message); | 458 Expect.equals("Hello", message); |
| 461 websocket.close(); | 459 websocket.close(); |
| 462 }).asFuture(); | 460 }).asFuture(); |
| 463 }).then((_) { | 461 }).then((_) { |
| 464 server.close(); | 462 server.close(); |
| 465 asyncEnd(); | 463 asyncEnd(); |
| 466 }); | 464 }); |
| 467 }); | 465 }); |
| 468 } | 466 } |
| 469 | 467 |
| 470 | 468 |
| 469 void testBasicAuthentication() { |
| 470 var userInfo = 'user:password'; |
| 471 |
| 472 asyncStart(); |
| 473 asyncStart(); |
| 474 createServer().then((server) { |
| 475 server.listen((request) { |
| 476 Expect.isTrue(WebSocketTransformer.isUpgradeRequest(request)); |
| 477 String auth = |
| 478 CryptoUtils.bytesToBase64(UTF8.encode(userInfo)); |
| 479 Expect.equals('Basic $auth', request.headers['Authorization'][0]); |
| 480 Expect.equals(1, request.headers['Authorization'].length); |
| 481 WebSocketTransformer.upgrade(request).then((webSocket) { |
| 482 webSocket.listen((_) { throw 'Unexpected'; }, |
| 483 onDone: () { asyncEnd(); }); |
| 484 webSocket.add("Hello"); |
| 485 }); |
| 486 }); |
| 487 |
| 488 var url = |
| 489 '${secure ? "wss" : "ws"}://$userInfo@$HOST_NAME:${server.port}/'; |
| 490 WebSocket.connect(url).then((websocket) { |
| 491 return websocket.listen((message) { |
| 492 Expect.equals("Hello", message); |
| 493 return websocket.close(); |
| 494 }).asFuture(); |
| 495 }).then((_) { |
| 496 return server.close(); |
| 497 }).whenComplete(() { |
| 498 asyncEnd(); |
| 499 }); |
| 500 }); |
| 501 } |
| 502 |
| 471 void runTests() { | 503 void runTests() { |
| 472 testRequestResponseClientCloses(2, null, null, 1); | 504 testRequestResponseClientCloses(2, null, null, 1); |
| 473 testRequestResponseClientCloses(2, 3001, null, 2); | 505 testRequestResponseClientCloses(2, 3001, null, 2); |
| 474 testRequestResponseClientCloses(2, 3002, "Got tired", 3); | 506 testRequestResponseClientCloses(2, 3002, "Got tired", 3); |
| 475 testRequestResponseServerCloses(2, null, null); | 507 testRequestResponseServerCloses(2, null, null); |
| 476 testRequestResponseServerCloses(2, 3001, null); | 508 testRequestResponseServerCloses(2, 3001, null); |
| 477 testRequestResponseServerCloses(2, 3002, "Got tired"); | 509 testRequestResponseServerCloses(2, 3002, "Got tired"); |
| 478 testMessageLength(125); | 510 testMessageLength(125); |
| 479 testMessageLength(126); | 511 testMessageLength(126); |
| 480 testMessageLength(127); | 512 testMessageLength(127); |
| 481 testMessageLength(65535); | 513 testMessageLength(65535); |
| 482 testMessageLength(65536); | 514 testMessageLength(65536); |
| 483 testDoubleCloseClient(); | 515 testDoubleCloseClient(); |
| 484 testDoubleCloseServer(); | 516 testDoubleCloseServer(); |
| 485 testImmediateCloseServer(); | 517 testImmediateCloseServer(); |
| 486 testImmediateCloseClient(); | 518 testImmediateCloseClient(); |
| 487 testNoUpgrade(); | 519 testNoUpgrade(); |
| 488 testUsePOST(); | 520 testUsePOST(); |
| 489 testConnections(10, 3002, "Got tired"); | 521 testConnections(10, 3002, "Got tired"); |
| 490 testIndividualUpgrade(5); | 522 testIndividualUpgrade(5); |
| 491 testFromUpgradedSocket(); | 523 testFromUpgradedSocket(); |
| 492 testAdditionalHeaders(); | 524 testAdditionalHeaders(); |
| 525 testBasicAuthentication(); |
| 493 } | 526 } |
| 494 } | 527 } |
| 495 | 528 |
| 496 | 529 |
| 497 void initializeSSL() { | 530 void initializeSSL() { |
| 498 var testPkcertDatabase = Platform.script.resolve('pkcert').toFilePath(); | 531 var testPkcertDatabase = Platform.script.resolve('pkcert').toFilePath(); |
| 499 SecureSocket.initialize(database: testPkcertDatabase, | 532 SecureSocket.initialize(database: testPkcertDatabase, |
| 500 password: "dartdart"); | 533 password: "dartdart"); |
| 501 } | 534 } |
| 502 | 535 |
| 503 | 536 |
| 504 main() { | 537 main() { |
| 505 new SecurityConfiguration(secure: false).runTests(); | 538 new SecurityConfiguration(secure: false).runTests(); |
| 506 initializeSSL(); | 539 initializeSSL(); |
| 507 new SecurityConfiguration(secure: true).runTests(); | 540 new SecurityConfiguration(secure: true).runTests(); |
| 508 } | 541 } |
| OLD | NEW |