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 http_parser.web_socket_test; |
| 6 |
| 7 import 'dart:io'; |
| 8 |
| 9 import 'package:http_parser/http_parser.dart'; |
| 10 import 'package:unittest/unittest.dart'; |
| 11 |
| 12 void main() { |
| 13 test("a client can communicate with a WebSocket server", () { |
| 14 return HttpServer.bind("localhost", 0).then((server) { |
| 15 server.transform(new WebSocketTransformer()).listen((webSocket) { |
| 16 webSocket.add("hello!"); |
| 17 webSocket.first.then((request) { |
| 18 expect(request, equals("ping")); |
| 19 webSocket.add("pong"); |
| 20 webSocket.close(); |
| 21 }); |
| 22 }); |
| 23 |
| 24 var client = new HttpClient(); |
| 25 return client.openUrl("GET", Uri.parse("http://localhost:${server.port}")) |
| 26 .then((request) { |
| 27 request.headers |
| 28 ..set("Connection", "Upgrade") |
| 29 ..set("Upgrade", "websocket") |
| 30 ..set("Sec-WebSocket-Key", "x3JJHMbDL1EzLkh9GBhXDw==") |
| 31 ..set("Sec-WebSocket-Version", "13"); |
| 32 return request.close(); |
| 33 }).then((response) => response.detachSocket()).then((socket) { |
| 34 var webSocket = new CompatibleWebSocket(socket, serverSide: false); |
| 35 |
| 36 var n = 0; |
| 37 return webSocket.listen((message) { |
| 38 if (n == 0) { |
| 39 expect(message, equals("hello!")); |
| 40 webSocket.add("ping"); |
| 41 } else if (n == 1) { |
| 42 expect(message, equals("pong")); |
| 43 webSocket.close(); |
| 44 server.close(); |
| 45 } else { |
| 46 expect(false, reason: "Only expected two messages."); |
| 47 } |
| 48 n++; |
| 49 }).asFuture(); |
| 50 }); |
| 51 }); |
| 52 }); |
| 53 |
| 54 test("a server can communicate with a WebSocket client", () { |
| 55 return HttpServer.bind("localhost", 0).then((server) { |
| 56 server.listen((request) { |
| 57 var response = request.response; |
| 58 response.statusCode = 101; |
| 59 response.headers |
| 60 ..set("Connection", "Upgrade") |
| 61 ..set("Upgrade", "websocket") |
| 62 ..set("Sec-WebSocket-Accept", CompatibleWebSocket.signKey( |
| 63 request.headers.value('Sec-WebSocket-Key'))); |
| 64 response.contentLength = 0; |
| 65 response.detachSocket().then((socket) { |
| 66 var webSocket = new CompatibleWebSocket(socket); |
| 67 webSocket.add("hello!"); |
| 68 webSocket.first.then((request) { |
| 69 expect(request, equals("ping")); |
| 70 webSocket.add("pong"); |
| 71 webSocket.close(); |
| 72 }); |
| 73 }); |
| 74 }); |
| 75 |
| 76 return WebSocket.connect('ws://localhost:${server.port}') |
| 77 .then((webSocket) { |
| 78 var n = 0; |
| 79 return webSocket.listen((message) { |
| 80 if (n == 0) { |
| 81 expect(message, equals("hello!")); |
| 82 webSocket.add("ping"); |
| 83 } else if (n == 1) { |
| 84 expect(message, equals("pong")); |
| 85 webSocket.close(); |
| 86 server.close(); |
| 87 } else { |
| 88 expect(false, reason: "Only expected two messages."); |
| 89 } |
| 90 n++; |
| 91 }).asFuture(); |
| 92 }); |
| 93 }); |
| 94 }); |
| 95 } |
OLD | NEW |