| 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:convert"; |
| 12 import "dart:io"; | 12 import "dart:io"; |
| 13 import "dart:typed_data"; | 13 import "dart:typed_data"; |
| 14 | 14 |
| 15 import "package:async_helper/async_helper.dart"; | 15 import "package:async_helper/async_helper.dart"; |
| 16 import "package:convert/convert.dart"; |
| 16 import "package:crypto/crypto.dart"; | 17 import "package:crypto/crypto.dart"; |
| 17 import "package:expect/expect.dart"; | 18 import "package:expect/expect.dart"; |
| 18 import "package:path/path.dart"; | 19 import "package:path/path.dart"; |
| 19 | 20 |
| 20 const WEB_SOCKET_GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; | 21 const WEB_SOCKET_GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; |
| 21 | 22 |
| 22 const String HOST_NAME = 'localhost'; | 23 const String HOST_NAME = 'localhost'; |
| 23 | 24 |
| 24 String localFile(path) => Platform.script.resolve(path).toFilePath(); | 25 String localFile(path) => Platform.script.resolve(path).toFilePath(); |
| 25 | 26 |
| (...skipping 434 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 460 } | 461 } |
| 461 | 462 |
| 462 testFromUpgradedSocket() { | 463 testFromUpgradedSocket() { |
| 463 asyncStart(); | 464 asyncStart(); |
| 464 createServer().then((server) { | 465 createServer().then((server) { |
| 465 server.listen((request) { | 466 server.listen((request) { |
| 466 Expect.equals('Upgrade', request.headers.value(HttpHeaders.CONNECTION)); | 467 Expect.equals('Upgrade', request.headers.value(HttpHeaders.CONNECTION)); |
| 467 Expect.equals('websocket', request.headers.value(HttpHeaders.UPGRADE)); | 468 Expect.equals('websocket', request.headers.value(HttpHeaders.UPGRADE)); |
| 468 | 469 |
| 469 var key = request.headers.value('Sec-WebSocket-Key'); | 470 var key = request.headers.value('Sec-WebSocket-Key'); |
| 470 var sha1 = new SHA1()..add("$key$WEB_SOCKET_GUID".codeUnits); | 471 var digest = sha1.convert("$key$WEB_SOCKET_GUID".codeUnits); |
| 471 var accept = CryptoUtils.bytesToBase64(sha1.close()); | 472 var accept = BASE64.encode(digest.bytes); |
| 472 request.response | 473 request.response |
| 473 ..statusCode = HttpStatus.SWITCHING_PROTOCOLS | 474 ..statusCode = HttpStatus.SWITCHING_PROTOCOLS |
| 474 ..headers.add(HttpHeaders.CONNECTION, "Upgrade") | 475 ..headers.add(HttpHeaders.CONNECTION, "Upgrade") |
| 475 ..headers.add(HttpHeaders.UPGRADE, "websocket") | 476 ..headers.add(HttpHeaders.UPGRADE, "websocket") |
| 476 ..headers.add("Sec-WebSocket-Accept", accept); | 477 ..headers.add("Sec-WebSocket-Accept", accept); |
| 477 request.response.contentLength = 0; | 478 request.response.contentLength = 0; |
| 478 request.response.detachSocket().then((socket) { | 479 request.response.detachSocket().then((socket) { |
| 479 return new WebSocket.fromUpgradedSocket(socket, serverSide: true); | 480 return new WebSocket.fromUpgradedSocket(socket, serverSide: true); |
| 480 }).then((websocket) { | 481 }).then((websocket) { |
| 481 websocket.add("Hello"); | 482 websocket.add("Hello"); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 528 | 529 |
| 529 void testBasicAuthentication() { | 530 void testBasicAuthentication() { |
| 530 var userInfo = 'user:password'; | 531 var userInfo = 'user:password'; |
| 531 | 532 |
| 532 asyncStart(); | 533 asyncStart(); |
| 533 asyncStart(); | 534 asyncStart(); |
| 534 createServer().then((server) { | 535 createServer().then((server) { |
| 535 server.listen((request) { | 536 server.listen((request) { |
| 536 Expect.isTrue(WebSocketTransformer.isUpgradeRequest(request)); | 537 Expect.isTrue(WebSocketTransformer.isUpgradeRequest(request)); |
| 537 String auth = | 538 String auth = |
| 538 CryptoUtils.bytesToBase64(UTF8.encode(userInfo)); | 539 BASE64.encode(UTF8.encode(userInfo)); |
| 539 Expect.equals('Basic $auth', request.headers['Authorization'][0]); | 540 Expect.equals('Basic $auth', request.headers['Authorization'][0]); |
| 540 Expect.equals(1, request.headers['Authorization'].length); | 541 Expect.equals(1, request.headers['Authorization'].length); |
| 541 WebSocketTransformer.upgrade(request).then((webSocket) { | 542 WebSocketTransformer.upgrade(request).then((webSocket) { |
| 542 webSocket.listen((_) { throw 'Unexpected'; }, | 543 webSocket.listen((_) { throw 'Unexpected'; }, |
| 543 onDone: () { asyncEnd(); }); | 544 onDone: () { asyncEnd(); }); |
| 544 webSocket.add("Hello"); | 545 webSocket.add("Hello"); |
| 545 }); | 546 }); |
| 546 }); | 547 }); |
| 547 | 548 |
| 548 var url = | 549 var url = |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 589 testBasicAuthentication(); | 590 testBasicAuthentication(); |
| 590 } | 591 } |
| 591 } | 592 } |
| 592 | 593 |
| 593 | 594 |
| 594 main() { | 595 main() { |
| 595 new SecurityConfiguration(secure: false).runTests(); | 596 new SecurityConfiguration(secure: false).runTests(); |
| 596 // TODO(whesse): Make WebSocket.connect() take an optional context: parameter. | 597 // TODO(whesse): Make WebSocket.connect() take an optional context: parameter. |
| 597 // new SecurityConfiguration(secure: true).runTests(); | 598 // new SecurityConfiguration(secure: true).runTests(); |
| 598 } | 599 } |
| OLD | NEW |