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