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 // VMOptions= |
| 6 // VMOptions=--short_socket_read |
| 7 // VMOptions=--short_socket_write |
| 8 // VMOptions=--short_socket_read --short_socket_write |
| 9 |
| 10 import "package:expect/expect.dart"; |
| 11 import "dart:async"; |
| 12 import "dart:io"; |
| 13 |
| 14 |
| 15 testEmptyProtocol() { |
| 16 HttpServer.bind("127.0.0.1", 0).then((server) { |
| 17 server.listen((request) { |
| 18 WebSocketTransformer.upgrade(request).then((websocket) { |
| 19 websocket.close(); |
| 20 }); |
| 21 }); |
| 22 WebSocket.connect("ws://127.0.0.1:${server.port}/", |
| 23 protocols: []).then((client) { |
| 24 Expect.isNull(client.protocol); |
| 25 client.close(); |
| 26 server.close(); |
| 27 }); |
| 28 }); |
| 29 } |
| 30 |
| 31 |
| 32 testProtocol(List<String> protocols, String used) { |
| 33 selector(List<String> receivedProtocols) { |
| 34 Expect.listEquals(protocols, receivedProtocols); |
| 35 return used; |
| 36 } |
| 37 HttpServer.bind("127.0.0.1", 0).then((server) { |
| 38 server.listen((request) { |
| 39 WebSocketTransformer.upgrade(request, |
| 40 protocolSelector: selector) |
| 41 .then((websocket) { |
| 42 Expect.equals(used, websocket.protocol); |
| 43 websocket.close(); |
| 44 }); |
| 45 }); |
| 46 WebSocket.connect("ws://127.0.0.1:${server.port}/", |
| 47 protocols: protocols).then((client) { |
| 48 Expect.equals(used, client.protocol); |
| 49 client.close(); |
| 50 server.close(); |
| 51 }); |
| 52 }); |
| 53 } |
| 54 |
| 55 |
| 56 testProtocolHandler() { |
| 57 // Test throwing an error. |
| 58 HttpServer.bind("127.0.0.1", 0).then((server) { |
| 59 server.listen((request) { |
| 60 selector(List<String> receivedProtocols) { |
| 61 throw "error"; |
| 62 } |
| 63 WebSocketTransformer.upgrade(request, |
| 64 protocolSelector: selector) |
| 65 .then((websocket) { |
| 66 Expect.fail('error expected'); |
| 67 }, onError: (error) { |
| 68 Expect.equals('error', error); |
| 69 }); |
| 70 }); |
| 71 WebSocket.connect("ws://127.0.0.1:${server.port}/", |
| 72 protocols: ["v1.example.com"]) |
| 73 .then((client) { |
| 74 Expect.fail('error expected'); |
| 75 }, onError: (error) { |
| 76 server.close(); |
| 77 }); |
| 78 }); |
| 79 |
| 80 // Test returning another protocol. |
| 81 HttpServer.bind("127.0.0.1", 0).then((server) { |
| 82 server.listen((request) { |
| 83 selector(List<String> receivedProtocols) => "v2.example.com"; |
| 84 WebSocketTransformer.upgrade(request, |
| 85 protocolSelector: selector) |
| 86 .then((websocket) { |
| 87 Expect.fail('error expected'); |
| 88 }, onError: (error) { |
| 89 Expect.isTrue(error is WebSocketException); |
| 90 }); |
| 91 }); |
| 92 WebSocket.connect("ws://127.0.0.1:${server.port}/", |
| 93 protocols: ["v1.example.com"]) |
| 94 .then((client) { |
| 95 Expect.fail('error expected'); |
| 96 }, onError: (error) { |
| 97 server.close(); |
| 98 }); |
| 99 }); |
| 100 } |
| 101 |
| 102 |
| 103 void main() { |
| 104 testEmptyProtocol(); |
| 105 testProtocol(["v1.example.com", "v2.example.com"], "v1.example.com"); |
| 106 testProtocol(["v1.example.com", "v2.example.com"], "v2.example.com"); |
| 107 testProtocolHandler(); |
| 108 } |
| 109 |
OLD | NEW |