| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 | 6 |
| 7 import 'package:http/http.dart' as http; | 7 import 'package:http/http.dart' as http; |
| 8 import 'package:shelf/shelf_io.dart' as shelf_io; | 8 import 'package:shelf/shelf_io.dart' as shelf_io; |
| 9 import 'package:shelf_web_socket/shelf_web_socket.dart'; | 9 import 'package:shelf_web_socket/shelf_web_socket.dart'; |
| 10 import 'package:test/test.dart'; | 10 import 'package:test/test.dart'; |
| 11 | 11 |
| 12 Map<String, String> get _handshakeHeaders => { | 12 Map<String, String> get _handshakeHeaders => { |
| 13 "Upgrade": "websocket", | 13 "Upgrade": "websocket", |
| 14 "Connection": "Upgrade", | 14 "Connection": "Upgrade", |
| 15 "Sec-WebSocket-Key": "x3JJHMbDL1EzLkh9GBhXDw==", | 15 "Sec-WebSocket-Key": "x3JJHMbDL1EzLkh9GBhXDw==", |
| 16 "Sec-WebSocket-Version": "13" | 16 "Sec-WebSocket-Version": "13" |
| 17 }; | 17 }; |
| 18 | 18 |
| 19 void main() { | 19 void main() { |
| 20 test("can communicate with a dart:io WebSocket client", () { | 20 test("can communicate with a dart:io WebSocket client", () async { |
| 21 return shelf_io.serve(webSocketHandler((webSocket) { | 21 var server = await shelf_io.serve(webSocketHandler((webSocket) { |
| 22 webSocket.add("hello!"); | 22 webSocket.sink.add("hello!"); |
| 23 webSocket.first.then((request) { | 23 webSocket.stream.first.then((request) { |
| 24 expect(request, equals("ping")); | 24 expect(request, equals("ping")); |
| 25 webSocket.add("pong"); | 25 webSocket.sink.add("pong"); |
| 26 webSocket.close(); | 26 webSocket.sink.close(); |
| 27 }); | 27 }); |
| 28 }), "localhost", 0).then((server) { | 28 }), "localhost", 0); |
| 29 return WebSocket.connect('ws://localhost:${server.port}') | 29 |
| 30 .then((webSocket) { | 30 try { |
| 31 var n = 0; | 31 var webSocket = await WebSocket.connect('ws://localhost:${server.port}'); |
| 32 return webSocket.listen((message) { | 32 var n = 0; |
| 33 if (n == 0) { | 33 await webSocket.listen((message) { |
| 34 expect(message, equals("hello!")); | 34 if (n == 0) { |
| 35 webSocket.add("ping"); | 35 expect(message, equals("hello!")); |
| 36 } else if (n == 1) { | 36 webSocket.add("ping"); |
| 37 expect(message, equals("pong")); | 37 } else if (n == 1) { |
| 38 webSocket.close(); | 38 expect(message, equals("pong")); |
| 39 server.close(); | 39 webSocket.close(); |
| 40 } else { | 40 server.close(); |
| 41 fail("Only expected two messages."); | 41 } else { |
| 42 } | 42 fail("Only expected two messages."); |
| 43 n++; | 43 } |
| 44 }).asFuture(); | 44 n++; |
| 45 }).whenComplete(server.close); | 45 }).asFuture(); |
| 46 }); | 46 } finally { |
| 47 await server.close(); |
| 48 } |
| 47 }); | 49 }); |
| 48 | 50 |
| 49 test("negotiates the sub-protocol", () { | 51 test("negotiates the sub-protocol", () async { |
| 50 return shelf_io.serve(webSocketHandler((webSocket, protocol) { | 52 var server = await shelf_io.serve(webSocketHandler((webSocket, protocol) { |
| 51 expect(protocol, equals("two")); | 53 expect(protocol, equals("two")); |
| 52 webSocket.close(); | 54 webSocket.sink.close(); |
| 53 }, protocols: ["three", "two", "x"]), "localhost", 0).then((server) { | 55 }, protocols: ["three", "two", "x"]), "localhost", 0); |
| 54 return WebSocket.connect('ws://localhost:${server.port}', | 56 |
| 55 protocols: ["one", "two", "three"]).then((webSocket) { | 57 try { |
| 56 expect(webSocket.protocol, equals("two")); | 58 var webSocket = await WebSocket.connect( |
| 57 return webSocket.close(); | 59 'ws://localhost:${server.port}', |
| 58 }).whenComplete(server.close); | 60 protocols: ["one", "two", "three"]); |
| 59 }); | 61 expect(webSocket.protocol, equals("two")); |
| 62 return webSocket.close(); |
| 63 } finally { |
| 64 await server.close(); |
| 65 } |
| 60 }); | 66 }); |
| 61 | 67 |
| 62 group("with a set of allowed origins", () { | 68 group("with a set of allowed origins", () { |
| 63 var server; | 69 var server; |
| 64 var url; | 70 var url; |
| 65 setUp(() { | 71 setUp(() async { |
| 66 return shelf_io.serve(webSocketHandler((webSocket) { | 72 server = await shelf_io.serve(webSocketHandler((webSocket) { |
| 67 webSocket.close(); | 73 webSocket.sink.close(); |
| 68 }, allowedOrigins: ["pub.dartlang.org", "GoOgLe.CoM"]), "localhost", 0) | 74 }, allowedOrigins: ["pub.dartlang.org", "GoOgLe.CoM"]), "localhost", 0); |
| 69 .then((server_) { | 75 url = 'http://localhost:${server.port}/'; |
| 70 server = server_; | |
| 71 url = 'http://localhost:${server.port}/'; | |
| 72 }); | |
| 73 }); | 76 }); |
| 74 | 77 |
| 75 tearDown(() => server.close()); | 78 tearDown(() => server.close()); |
| 76 | 79 |
| 77 test("allows access with an allowed origin", () { | 80 test("allows access with an allowed origin", () { |
| 78 var headers = _handshakeHeaders; | 81 var headers = _handshakeHeaders; |
| 79 headers['Origin'] = 'pub.dartlang.org'; | 82 headers['Origin'] = 'pub.dartlang.org'; |
| 80 expect(http.get(url, headers: headers), hasStatus(101)); | 83 expect(http.get(url, headers: headers), hasStatus(101)); |
| 81 }); | 84 }); |
| 82 | 85 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 97 }); | 100 }); |
| 98 | 101 |
| 99 test("ignores the case of the server origin", () { | 102 test("ignores the case of the server origin", () { |
| 100 var headers = _handshakeHeaders; | 103 var headers = _handshakeHeaders; |
| 101 headers['Origin'] = 'google.com'; | 104 headers['Origin'] = 'google.com'; |
| 102 expect(http.get(url, headers: headers), hasStatus(101)); | 105 expect(http.get(url, headers: headers), hasStatus(101)); |
| 103 }); | 106 }); |
| 104 }); | 107 }); |
| 105 | 108 |
| 106 // Regression test for issue 21894. | 109 // Regression test for issue 21894. |
| 107 test("allows a Connection header with multiple values", () { | 110 test("allows a Connection header with multiple values", () async { |
| 108 return shelf_io.serve(webSocketHandler((webSocket) { | 111 var server = await shelf_io.serve(webSocketHandler((webSocket) { |
| 109 webSocket.close(); | 112 webSocket.sink.close(); |
| 110 }), "localhost", 0).then((server) { | 113 }), "localhost", 0); |
| 111 var url = 'http://localhost:${server.port}/'; | |
| 112 | 114 |
| 113 var headers = _handshakeHeaders; | 115 var url = 'http://localhost:${server.port}/'; |
| 114 headers['Connection'] = 'Other-Token, Upgrade'; | 116 var headers = _handshakeHeaders; |
| 115 expect(http.get(url, headers: headers).whenComplete(server.close), | 117 headers['Connection'] = 'Other-Token, Upgrade'; |
| 116 hasStatus(101)); | 118 expect(http.get(url, headers: headers).whenComplete(server.close), |
| 117 }); | 119 hasStatus(101)); |
| 118 }); | 120 }); |
| 119 | 121 |
| 120 group("HTTP errors", () { | 122 group("HTTP errors", () { |
| 121 var server; | 123 var server; |
| 122 var url; | 124 var url; |
| 123 setUp(() { | 125 setUp(() async { |
| 124 return shelf_io.serve(webSocketHandler((_) { | 126 server = await shelf_io.serve(webSocketHandler((_) { |
| 125 fail("should not create a WebSocket"); | 127 fail("should not create a WebSocket"); |
| 126 }), "localhost", 0).then((server_) { | 128 }), "localhost", 0); |
| 127 server = server_; | 129 url = 'http://localhost:${server.port}/'; |
| 128 url = 'http://localhost:${server.port}/'; | |
| 129 }); | |
| 130 }); | 130 }); |
| 131 | 131 |
| 132 tearDown(() => server.close()); | 132 tearDown(() => server.close()); |
| 133 | 133 |
| 134 test("404s for non-GET requests", () { | 134 test("404s for non-GET requests", () { |
| 135 expect(http.delete(url, headers: _handshakeHeaders), hasStatus(404)); | 135 expect(http.delete(url, headers: _handshakeHeaders), hasStatus(404)); |
| 136 }); | 136 }); |
| 137 | 137 |
| 138 test("404s for non-Upgrade requests", () { | 138 test("404s for non-Upgrade requests", () { |
| 139 var headers = _handshakeHeaders; | 139 var headers = _handshakeHeaders; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 170 expect(() => webSocketHandler((_) => null, protocols: ['foo']), | 170 expect(() => webSocketHandler((_) => null, protocols: ['foo']), |
| 171 throwsArgumentError); | 171 throwsArgumentError); |
| 172 }); | 172 }); |
| 173 } | 173 } |
| 174 | 174 |
| 175 Matcher hasStatus(int status) => completion(predicate((response) { | 175 Matcher hasStatus(int status) => completion(predicate((response) { |
| 176 expect(response, new isInstanceOf<http.Response>()); | 176 expect(response, new isInstanceOf<http.Response>()); |
| 177 expect(response.statusCode, equals(status)); | 177 expect(response.statusCode, equals(status)); |
| 178 return true; | 178 return true; |
| 179 })); | 179 })); |
| OLD | NEW |