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

Side by Side Diff: test/web_socket_test.dart

Issue 1046573002: pkg/http_parser: format code, prepare for +6 release (Closed) Base URL: https://github.com/dart-lang/http_parser.git@master
Patch Set: cl feedback Created 5 years, 8 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 | « test/media_type_test.dart ('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
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 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 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library http_parser.web_socket_test; 5 library http_parser.web_socket_test;
6 6
7 import 'dart:io'; 7 import 'dart:io';
8 8
9 import 'package:http_parser/http_parser.dart'; 9 import 'package:http_parser/http_parser.dart';
10 import 'package:unittest/unittest.dart'; 10 import 'package:unittest/unittest.dart';
11 11
12 void main() { 12 void main() {
13 test("a client can communicate with a WebSocket server", () { 13 test("a client can communicate with a WebSocket server", () {
14 return HttpServer.bind("localhost", 0).then((server) { 14 return HttpServer.bind("localhost", 0).then((server) {
15 server.transform(new WebSocketTransformer()).listen((webSocket) { 15 server.transform(new WebSocketTransformer()).listen((webSocket) {
16 webSocket.add("hello!"); 16 webSocket.add("hello!");
17 webSocket.first.then((request) { 17 webSocket.first.then((request) {
18 expect(request, equals("ping")); 18 expect(request, equals("ping"));
19 webSocket.add("pong"); 19 webSocket.add("pong");
20 webSocket.close(); 20 webSocket.close();
21 }); 21 });
22 }); 22 });
23 23
24 var client = new HttpClient(); 24 var client = new HttpClient();
25 return client.openUrl("GET", Uri.parse("http://localhost:${server.port}")) 25 return client
26 .openUrl("GET", Uri.parse("http://localhost:${server.port}"))
26 .then((request) { 27 .then((request) {
27 request.headers 28 request.headers
28 ..set("Connection", "Upgrade") 29 ..set("Connection", "Upgrade")
29 ..set("Upgrade", "websocket") 30 ..set("Upgrade", "websocket")
30 ..set("Sec-WebSocket-Key", "x3JJHMbDL1EzLkh9GBhXDw==") 31 ..set("Sec-WebSocket-Key", "x3JJHMbDL1EzLkh9GBhXDw==")
31 ..set("Sec-WebSocket-Version", "13"); 32 ..set("Sec-WebSocket-Version", "13");
32 return request.close(); 33 return request.close();
33 }).then((response) => response.detachSocket()).then((socket) { 34 }).then((response) => response.detachSocket()).then((socket) {
34 var webSocket = new CompatibleWebSocket(socket, serverSide: false); 35 var webSocket = new CompatibleWebSocket(socket, serverSide: false);
35 36
36 var n = 0; 37 var n = 0;
37 return webSocket.listen((message) { 38 return webSocket.listen((message) {
38 if (n == 0) { 39 if (n == 0) {
39 expect(message, equals("hello!")); 40 expect(message, equals("hello!"));
40 webSocket.add("ping"); 41 webSocket.add("ping");
41 } else if (n == 1) { 42 } else if (n == 1) {
42 expect(message, equals("pong")); 43 expect(message, equals("pong"));
43 webSocket.close(); 44 webSocket.close();
44 server.close(); 45 server.close();
45 } else { 46 } else {
46 fail("Only expected two messages."); 47 fail("Only expected two messages.");
47 } 48 }
48 n++; 49 n++;
49 }).asFuture(); 50 }).asFuture();
50 }); 51 });
51 }); 52 });
52 }); 53 });
53 54
54 test("a server can communicate with a WebSocket client", () { 55 test("a server can communicate with a WebSocket client", () {
55 return HttpServer.bind("localhost", 0).then((server) { 56 return HttpServer.bind("localhost", 0).then((server) {
56 server.listen((request) { 57 server.listen((request) {
57 var response = request.response; 58 var response = request.response;
58 response.statusCode = 101; 59 response.statusCode = 101;
59 response.headers 60 response.headers
60 ..set("Connection", "Upgrade") 61 ..set("Connection", "Upgrade")
61 ..set("Upgrade", "websocket") 62 ..set("Upgrade", "websocket")
62 ..set("Sec-WebSocket-Accept", CompatibleWebSocket.signKey( 63 ..set("Sec-WebSocket-Accept", CompatibleWebSocket
63 request.headers.value('Sec-WebSocket-Key'))); 64 .signKey(request.headers.value('Sec-WebSocket-Key')));
64 response.contentLength = 0; 65 response.contentLength = 0;
65 response.detachSocket().then((socket) { 66 response.detachSocket().then((socket) {
66 var webSocket = new CompatibleWebSocket(socket); 67 var webSocket = new CompatibleWebSocket(socket);
67 webSocket.add("hello!"); 68 webSocket.add("hello!");
68 webSocket.first.then((request) { 69 webSocket.first.then((request) {
69 expect(request, equals("ping")); 70 expect(request, equals("ping"));
70 webSocket.add("pong"); 71 webSocket.add("pong");
71 webSocket.close(); 72 webSocket.close();
72 }); 73 });
73 }); 74 });
74 }); 75 });
75 76
76 return WebSocket.connect('ws://localhost:${server.port}') 77 return WebSocket
78 .connect('ws://localhost:${server.port}')
77 .then((webSocket) { 79 .then((webSocket) {
78 var n = 0; 80 var n = 0;
79 return webSocket.listen((message) { 81 return webSocket.listen((message) {
80 if (n == 0) { 82 if (n == 0) {
81 expect(message, equals("hello!")); 83 expect(message, equals("hello!"));
82 webSocket.add("ping"); 84 webSocket.add("ping");
83 } else if (n == 1) { 85 } else if (n == 1) {
84 expect(message, equals("pong")); 86 expect(message, equals("pong"));
85 webSocket.close(); 87 webSocket.close();
86 server.close(); 88 server.close();
87 } else { 89 } else {
88 fail("Only expected two messages."); 90 fail("Only expected two messages.");
89 } 91 }
90 n++; 92 n++;
91 }).asFuture(); 93 }).asFuture();
92 }); 94 });
93 }); 95 });
94 }); 96 });
95 } 97 }
OLDNEW
« no previous file with comments | « test/media_type_test.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698