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

Side by Side Diff: pkg/shelf_web_socket/test/web_socket_test.dart

Issue 297593003: Add a shelf_web_socket package. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: fixes Created 6 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 | Annotate | Revision Log
« no previous file with comments | « pkg/shelf_web_socket/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 library shelf_web_socket.web_socket_test;
6
7 import 'dart:io';
8
9 import 'package:http/http.dart' as http;
10 import 'package:shelf/shelf_io.dart' as shelf_io;
11 import 'package:shelf_web_socket/shelf_web_socket.dart';
12 import 'package:unittest/unittest.dart';
13
14 Map<String, String> get _handshakeHeaders => {
15 "Upgrade": "websocket",
16 "Connection": "Upgrade",
17 "Sec-WebSocket-Key": "x3JJHMbDL1EzLkh9GBhXDw==",
18 "Sec-WebSocket-Version": "13"
19 };
20
21 void main() {
22 test("can communicate with a dart:io WebSocket client", () {
23 return shelf_io.serve(webSocketHandler((webSocket) {
24 webSocket.add("hello!");
25 webSocket.first.then((request) {
26 expect(request, equals("ping"));
27 webSocket.add("pong");
28 webSocket.close();
29 });
30 }), "localhost", 0).then((server) {
31 return WebSocket.connect('ws://localhost:${server.port}')
32 .then((webSocket) {
33 var n = 0;
34 return webSocket.listen((message) {
35 if (n == 0) {
36 expect(message, equals("hello!"));
37 webSocket.add("ping");
38 } else if (n == 1) {
39 expect(message, equals("pong"));
40 webSocket.close();
41 server.close();
42 } else {
43 fail("Only expected two messages.");
44 }
45 n++;
46 }).asFuture();
47 }).whenComplete(server.close);
48 });
49 });
50
51 test("negotiates the sub-protocol", () {
52 return shelf_io.serve(webSocketHandler((webSocket, protocol) {
53 expect(protocol, equals("two"));
54 webSocket.close();
55 }, protocols: ["three", "two", "x"]), "localhost", 0).then((server) {
56 return WebSocket.connect('ws://localhost:${server.port}',
57 protocols: ["one", "two", "three"]).then((webSocket) {
58 expect(webSocket.protocol, equals("two"));
59 return webSocket.close();
60 }).whenComplete(server.close);
61 });
62 });
63
64 group("with a set of allowed origins", () {
65 var server;
66 var url;
67 setUp(() {
68 return shelf_io.serve(webSocketHandler((webSocket) {
69 webSocket.close();
70 }, allowedOrigins: ["pub.dartlang.org", "GoOgLe.CoM"]), "localhost", 0)
71 .then((server_) {
72 server = server_;
73 url = 'http://localhost:${server.port}/';
74 });
75 });
76
77 tearDown(() => server.close());
78
79 test("allows access with an allowed origin", () {
80 var headers = _handshakeHeaders;
81 headers['Origin'] = 'pub.dartlang.org';
82 expect(http.get(url, headers: headers), hasStatus(101));
83 });
84
85 test("forbids access with a non-allowed origin", () {
86 var headers = _handshakeHeaders;
87 headers['Origin'] = 'dartlang.org';
88 expect(http.get(url, headers: headers), hasStatus(403));
89 });
90
91 test("allows access with no origin", () {
92 expect(http.get(url, headers: _handshakeHeaders), hasStatus(101));
93 });
94
95 test("ignores the case of the client origin", () {
96 var headers = _handshakeHeaders;
97 headers['Origin'] = 'PuB.DaRtLaNg.OrG';
98 expect(http.get(url, headers: headers), hasStatus(101));
99 });
100
101 test("ignores the case of the server origin", () {
102 var headers = _handshakeHeaders;
103 headers['Origin'] = 'google.com';
104 expect(http.get(url, headers: headers), hasStatus(101));
105 });
106 });
107
108 group("HTTP errors", () {
109 var server;
110 var url;
111 setUp(() {
112 return shelf_io.serve(webSocketHandler((_) {
113 fail("should not create a WebSocket");
114 }), "localhost", 0).then((server_) {
115 server = server_;
116 url = 'http://localhost:${server.port}/';
117 });
118 });
119
120 tearDown(() => server.close());
121
122 test("404s for non-GET requests", () {
123 expect(http.delete(url, headers: _handshakeHeaders), hasStatus(404));
124 });
125
126 test("404s for non-Upgrade requests", () {
127 var headers = _handshakeHeaders;
128 headers.remove('Connection');
129 expect(http.get(url, headers: headers), hasStatus(404));
130 });
131
132 test("404s for non-websocket upgrade requests", () {
133 var headers = _handshakeHeaders;
134 headers['Upgrade'] = 'fblthp';
135 expect(http.get(url, headers: headers), hasStatus(404));
136 });
137
138 test("400s for a missing Sec-WebSocket-Version", () {
139 var headers = _handshakeHeaders;
140 headers.remove('Sec-WebSocket-Version');
141 expect(http.get(url, headers: headers), hasStatus(400));
142 });
143
144 test("404s for an unknown Sec-WebSocket-Version", () {
145 var headers = _handshakeHeaders;
146 headers['Sec-WebSocket-Version'] = '15';
147 expect(http.get(url, headers: headers), hasStatus(404));
148 });
149
150 test("400s for a missing Sec-WebSocket-Key", () {
151 var headers = _handshakeHeaders;
152 headers.remove('Sec-WebSocket-Key');
153 expect(http.get(url, headers: headers), hasStatus(400));
154 });
155 });
156
157 test("throws an error if a unary function is provided with protocols", () {
158 expect(() => webSocketHandler((_) => null, protocols: ['foo']),
159 throwsArgumentError);
160 });
161 }
162
163 Matcher hasStatus(int status) => completion(predicate((response) {
164 expect(response, new isInstanceOf<http.Response>());
165 expect(response.statusCode, equals(status));
166 return true;
167 }));
OLDNEW
« no previous file with comments | « pkg/shelf_web_socket/pubspec.yaml ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698