Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(294)

Side by Side Diff: test/web_socket_test.dart

Issue 1734773002: Copy the web socket stuff from http_parser. (Closed) Base URL: git@github.com:dart-lang/web_socket_channel.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « pubspec.yaml ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 import 'dart:io';
8
9 import 'package:stream_channel/stream_channel.dart';
10 import 'package:test/test.dart';
11
12 import 'package:web_socket_channel/web_socket_channel.dart';
13
14 void main() {
15 group("using WebSocketChannel", () {
16 test("a client can communicate with a WebSocket server", () async {
17 var server = await HttpServer.bind("localhost", 0);
18 server.transform(new WebSocketTransformer()).listen((webSocket) {
19 webSocket.add("hello!");
20 webSocket.listen((request) {
21 expect(request, equals("ping"));
22 webSocket.add("pong");
23 webSocket.close();
24 });
25 });
26
27 var client = new HttpClient();
28 var request = await client.openUrl(
29 "GET", Uri.parse("http://localhost:${server.port}"));
30 request.headers
31 ..set("Connection", "Upgrade")
32 ..set("Upgrade", "websocket")
33 ..set("Sec-WebSocket-Key", "x3JJHMbDL1EzLkh9GBhXDw==")
34 ..set("Sec-WebSocket-Version", "13");
35
36 var response = await request.close();
37 var socket = await response.detachSocket();
38 var innerChannel = new StreamChannel(socket, socket);
39 var webSocket = new WebSocketChannel(innerChannel, serverSide: false);
40
41 var n = 0;
42 await webSocket.stream.listen((message) {
43 if (n == 0) {
44 expect(message, equals("hello!"));
45 webSocket.sink.add("ping");
46 } else if (n == 1) {
47 expect(message, equals("pong"));
48 webSocket.sink.close();
49 server.close();
50 } else {
51 fail("Only expected two messages.");
52 }
53 n++;
54 }).asFuture();
55 });
56
57 test("a server can communicate with a WebSocket client", () async {
58 var server = await HttpServer.bind("localhost", 0);
59 server.listen((request) async {
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", WebSocketChannel
66 .signKey(request.headers.value('Sec-WebSocket-Key')));
67 response.contentLength = 0;
68
69 var socket = await response.detachSocket();
70 var innerChannel = new StreamChannel(socket, socket);
71 var webSocket = new WebSocketChannel(innerChannel);
72 webSocket.sink.add("hello!");
73
74 var message = await webSocket.stream.first;
75 expect(message, equals("ping"));
76 webSocket.sink.add("pong");
77 webSocket.sink.close();
78 });
79
80 var webSocket = await WebSocket.connect('ws://localhost:${server.port}');
81 var n = 0;
82 await webSocket.listen((message) {
83 if (n == 0) {
84 expect(message, equals("hello!"));
85 webSocket.add("ping");
86 } else if (n == 1) {
87 expect(message, equals("pong"));
88 webSocket.close();
89 server.close();
90 } else {
91 fail("Only expected two messages.");
92 }
93 n++;
94 }).asFuture();
95 });
96 });
97 }
OLDNEW
« no previous file with comments | « pubspec.yaml ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698