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