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

Side by Side Diff: test/web_socket_test.dart

Issue 1950183003: Remove deprecated APIs. (Closed) Base URL: git@github.com:dart-lang/http_parser@master
Patch Set: Created 4 years, 7 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:http_parser/http_parser.dart';
10 import 'package:stream_channel/stream_channel.dart';
11 import 'package:test/test.dart';
12
13 void main() {
14 group("using WebSocketChannel", () {
15 test("a client can communicate with a WebSocket server", () async {
16 var server = await HttpServer.bind("localhost", 0);
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 var request = await client.openUrl(
28 "GET", Uri.parse("http://localhost:${server.port}"));
29 request.headers
30 ..set("Connection", "Upgrade")
31 ..set("Upgrade", "websocket")
32 ..set("Sec-WebSocket-Key", "x3JJHMbDL1EzLkh9GBhXDw==")
33 ..set("Sec-WebSocket-Version", "13");
34
35 var response = await request.close();
36 var socket = await response.detachSocket();
37 var innerChannel = new StreamChannel(socket, socket);
38 var webSocket = new WebSocketChannel(innerChannel, serverSide: false);
39
40 var n = 0;
41 await webSocket.stream.listen((message) {
42 if (n == 0) {
43 expect(message, equals("hello!"));
44 webSocket.sink.add("ping");
45 } else if (n == 1) {
46 expect(message, equals("pong"));
47 webSocket.sink.close();
48 server.close();
49 } else {
50 fail("Only expected two messages.");
51 }
52 n++;
53 }).asFuture();
54 });
55
56 test("a server can communicate with a WebSocket client", () async {
57 var server = await HttpServer.bind("localhost", 0);
58 server.listen((request) async {
59 var response = request.response;
60 response.statusCode = 101;
61 response.headers
62 ..set("Connection", "Upgrade")
63 ..set("Upgrade", "websocket")
64 ..set("Sec-WebSocket-Accept", CompatibleWebSocket
65 .signKey(request.headers.value('Sec-WebSocket-Key')));
66 response.contentLength = 0;
67
68 var socket = await response.detachSocket();
69 var innerChannel = new StreamChannel(socket, socket);
70 var webSocket = new WebSocketChannel(innerChannel);
71 webSocket.sink.add("hello!");
72
73 var message = await webSocket.stream.first;
74 expect(message, equals("ping"));
75 webSocket.sink.add("pong");
76 webSocket.sink.close();
77 });
78
79 var webSocket = await WebSocket.connect('ws://localhost:${server.port}');
80 var n = 0;
81 await webSocket.listen((message) {
82 if (n == 0) {
83 expect(message, equals("hello!"));
84 webSocket.add("ping");
85 } else if (n == 1) {
86 expect(message, equals("pong"));
87 webSocket.close();
88 server.close();
89 } else {
90 fail("Only expected two messages.");
91 }
92 n++;
93 }).asFuture();
94 });
95 });
96
97 group("using CompatibleWebSocket", () {
98 test("a client can communicate with a WebSocket server", () {
99 return HttpServer.bind("localhost", 0).then((server) {
100 server.transform(new WebSocketTransformer()).listen((webSocket) {
101 webSocket.add("hello!");
102 webSocket.listen((request) {
103 expect(request, equals("ping"));
104 webSocket.add("pong");
105 webSocket.close();
106 });
107 });
108
109 var client = new HttpClient();
110 return client
111 .openUrl("GET", Uri.parse("http://localhost:${server.port}"))
112 .then((request) {
113 request.headers
114 ..set("Connection", "Upgrade")
115 ..set("Upgrade", "websocket")
116 ..set("Sec-WebSocket-Key", "x3JJHMbDL1EzLkh9GBhXDw==")
117 ..set("Sec-WebSocket-Version", "13");
118 return request.close();
119 }).then((response) => response.detachSocket()).then((socket) {
120 var webSocket = new CompatibleWebSocket(socket, serverSide: false);
121
122 var n = 0;
123 return webSocket.listen((message) {
124 if (n == 0) {
125 expect(message, equals("hello!"));
126 webSocket.add("ping");
127 } else if (n == 1) {
128 expect(message, equals("pong"));
129 webSocket.close();
130 server.close();
131 } else {
132 fail("Only expected two messages.");
133 }
134 n++;
135 }).asFuture();
136 });
137 });
138 });
139
140 test("a server can communicate with a WebSocket client", () {
141 return HttpServer.bind("localhost", 0).then((server) {
142 server.listen((request) {
143 var response = request.response;
144 response.statusCode = 101;
145 response.headers
146 ..set("Connection", "Upgrade")
147 ..set("Upgrade", "websocket")
148 ..set("Sec-WebSocket-Accept", CompatibleWebSocket
149 .signKey(request.headers.value('Sec-WebSocket-Key')));
150 response.contentLength = 0;
151 response.detachSocket().then((socket) {
152 var webSocket = new CompatibleWebSocket(socket);
153 webSocket.add("hello!");
154 webSocket.first.then((request) {
155 expect(request, equals("ping"));
156 webSocket.add("pong");
157 webSocket.close();
158 });
159 });
160 });
161
162 return WebSocket
163 .connect('ws://localhost:${server.port}')
164 .then((webSocket) {
165 var n = 0;
166 return webSocket.listen((message) {
167 if (n == 0) {
168 expect(message, equals("hello!"));
169 webSocket.add("ping");
170 } else if (n == 1) {
171 expect(message, equals("pong"));
172 webSocket.close();
173 server.close();
174 } else {
175 fail("Only expected two messages.");
176 }
177 n++;
178 }).asFuture();
179 });
180 });
181 });
182 });
183 }
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