| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library shelf_web_socket.web_socket_test; | |
| 6 | |
| 7 import 'dart:io'; | |
| 8 | |
| 9 import 'package:http/http.dart' as http; | |
| 10 import 'package:shelf/shelf_io.dart' as shelf_io; | |
| 11 import 'package:shelf_web_socket/shelf_web_socket.dart'; | |
| 12 import 'package:unittest/unittest.dart'; | |
| 13 | |
| 14 Map<String, String> get _handshakeHeaders => { | |
| 15 "Upgrade": "websocket", | |
| 16 "Connection": "Upgrade", | |
| 17 "Sec-WebSocket-Key": "x3JJHMbDL1EzLkh9GBhXDw==", | |
| 18 "Sec-WebSocket-Version": "13" | |
| 19 }; | |
| 20 | |
| 21 void main() { | |
| 22 test("can communicate with a dart:io WebSocket client", () { | |
| 23 return shelf_io.serve(webSocketHandler((webSocket) { | |
| 24 webSocket.add("hello!"); | |
| 25 webSocket.first.then((request) { | |
| 26 expect(request, equals("ping")); | |
| 27 webSocket.add("pong"); | |
| 28 webSocket.close(); | |
| 29 }); | |
| 30 }), "localhost", 0).then((server) { | |
| 31 return WebSocket.connect('ws://localhost:${server.port}') | |
| 32 .then((webSocket) { | |
| 33 var n = 0; | |
| 34 return webSocket.listen((message) { | |
| 35 if (n == 0) { | |
| 36 expect(message, equals("hello!")); | |
| 37 webSocket.add("ping"); | |
| 38 } else if (n == 1) { | |
| 39 expect(message, equals("pong")); | |
| 40 webSocket.close(); | |
| 41 server.close(); | |
| 42 } else { | |
| 43 fail("Only expected two messages."); | |
| 44 } | |
| 45 n++; | |
| 46 }).asFuture(); | |
| 47 }).whenComplete(server.close); | |
| 48 }); | |
| 49 }); | |
| 50 | |
| 51 test("negotiates the sub-protocol", () { | |
| 52 return shelf_io.serve(webSocketHandler((webSocket, protocol) { | |
| 53 expect(protocol, equals("two")); | |
| 54 webSocket.close(); | |
| 55 }, protocols: ["three", "two", "x"]), "localhost", 0).then((server) { | |
| 56 return WebSocket.connect('ws://localhost:${server.port}', | |
| 57 protocols: ["one", "two", "three"]).then((webSocket) { | |
| 58 expect(webSocket.protocol, equals("two")); | |
| 59 return webSocket.close(); | |
| 60 }).whenComplete(server.close); | |
| 61 }); | |
| 62 }); | |
| 63 | |
| 64 group("with a set of allowed origins", () { | |
| 65 var server; | |
| 66 var url; | |
| 67 setUp(() { | |
| 68 return shelf_io.serve(webSocketHandler((webSocket) { | |
| 69 webSocket.close(); | |
| 70 }, allowedOrigins: ["pub.dartlang.org", "GoOgLe.CoM"]), "localhost", 0) | |
| 71 .then((server_) { | |
| 72 server = server_; | |
| 73 url = 'http://localhost:${server.port}/'; | |
| 74 }); | |
| 75 }); | |
| 76 | |
| 77 tearDown(() => server.close()); | |
| 78 | |
| 79 test("allows access with an allowed origin", () { | |
| 80 var headers = _handshakeHeaders; | |
| 81 headers['Origin'] = 'pub.dartlang.org'; | |
| 82 expect(http.get(url, headers: headers), hasStatus(101)); | |
| 83 }); | |
| 84 | |
| 85 test("forbids access with a non-allowed origin", () { | |
| 86 var headers = _handshakeHeaders; | |
| 87 headers['Origin'] = 'dartlang.org'; | |
| 88 expect(http.get(url, headers: headers), hasStatus(403)); | |
| 89 }); | |
| 90 | |
| 91 test("allows access with no origin", () { | |
| 92 expect(http.get(url, headers: _handshakeHeaders), hasStatus(101)); | |
| 93 }); | |
| 94 | |
| 95 test("ignores the case of the client origin", () { | |
| 96 var headers = _handshakeHeaders; | |
| 97 headers['Origin'] = 'PuB.DaRtLaNg.OrG'; | |
| 98 expect(http.get(url, headers: headers), hasStatus(101)); | |
| 99 }); | |
| 100 | |
| 101 test("ignores the case of the server origin", () { | |
| 102 var headers = _handshakeHeaders; | |
| 103 headers['Origin'] = 'google.com'; | |
| 104 expect(http.get(url, headers: headers), hasStatus(101)); | |
| 105 }); | |
| 106 }); | |
| 107 | |
| 108 // Regression test for issue 21894. | |
| 109 test("allows a Connection header with multiple values", () { | |
| 110 return shelf_io.serve(webSocketHandler((webSocket) { | |
| 111 webSocket.close(); | |
| 112 }), "localhost", 0).then((server) { | |
| 113 var url = 'http://localhost:${server.port}/'; | |
| 114 | |
| 115 var headers = _handshakeHeaders; | |
| 116 headers['Connection'] = 'Other-Token, Upgrade'; | |
| 117 expect(http.get(url, headers: headers).whenComplete(server.close), | |
| 118 hasStatus(101)); | |
| 119 }); | |
| 120 }); | |
| 121 | |
| 122 group("HTTP errors", () { | |
| 123 var server; | |
| 124 var url; | |
| 125 setUp(() { | |
| 126 return shelf_io.serve(webSocketHandler((_) { | |
| 127 fail("should not create a WebSocket"); | |
| 128 }), "localhost", 0).then((server_) { | |
| 129 server = server_; | |
| 130 url = 'http://localhost:${server.port}/'; | |
| 131 }); | |
| 132 }); | |
| 133 | |
| 134 tearDown(() => server.close()); | |
| 135 | |
| 136 test("404s for non-GET requests", () { | |
| 137 expect(http.delete(url, headers: _handshakeHeaders), hasStatus(404)); | |
| 138 }); | |
| 139 | |
| 140 test("404s for non-Upgrade requests", () { | |
| 141 var headers = _handshakeHeaders; | |
| 142 headers.remove('Connection'); | |
| 143 expect(http.get(url, headers: headers), hasStatus(404)); | |
| 144 }); | |
| 145 | |
| 146 test("404s for non-websocket upgrade requests", () { | |
| 147 var headers = _handshakeHeaders; | |
| 148 headers['Upgrade'] = 'fblthp'; | |
| 149 expect(http.get(url, headers: headers), hasStatus(404)); | |
| 150 }); | |
| 151 | |
| 152 test("400s for a missing Sec-WebSocket-Version", () { | |
| 153 var headers = _handshakeHeaders; | |
| 154 headers.remove('Sec-WebSocket-Version'); | |
| 155 expect(http.get(url, headers: headers), hasStatus(400)); | |
| 156 }); | |
| 157 | |
| 158 test("404s for an unknown Sec-WebSocket-Version", () { | |
| 159 var headers = _handshakeHeaders; | |
| 160 headers['Sec-WebSocket-Version'] = '15'; | |
| 161 expect(http.get(url, headers: headers), hasStatus(404)); | |
| 162 }); | |
| 163 | |
| 164 test("400s for a missing Sec-WebSocket-Key", () { | |
| 165 var headers = _handshakeHeaders; | |
| 166 headers.remove('Sec-WebSocket-Key'); | |
| 167 expect(http.get(url, headers: headers), hasStatus(400)); | |
| 168 }); | |
| 169 }); | |
| 170 | |
| 171 test("throws an error if a unary function is provided with protocols", () { | |
| 172 expect(() => webSocketHandler((_) => null, protocols: ['foo']), | |
| 173 throwsArgumentError); | |
| 174 }); | |
| 175 } | |
| 176 | |
| 177 Matcher hasStatus(int status) => completion(predicate((response) { | |
| 178 expect(response, new isInstanceOf<http.Response>()); | |
| 179 expect(response.statusCode, equals(status)); | |
| 180 return true; | |
| 181 })); | |
| OLD | NEW |