Skip to content

Commit

Permalink
Add Travis-CI support (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevmoo authored Mar 2, 2018
1 parent d50ac26 commit 8cbd4e1
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 33 deletions.
24 changes: 24 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
language: dart

dart:
- dev
- stable

dart_task:
- test

matrix:
include:
# Only validate formatting using the dev release
- dart: dev
dart_task: dartfmt
- dart: dev
dart_task: analyzer

# Only building master means that we don't run two builds for each pull request.
branches:
only: [master]

cache:
directories:
- $HOME/.pub-cache
8 changes: 4 additions & 4 deletions lib/shelf_web_socket.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ typedef _BinaryFunction(arg1, arg2);
/// See also the WebSocket spec's discussion of [origin considerations][].
///
/// [origin considerations]: https://tools.ietf.org/html/rfc6455#section-10.2
Handler webSocketHandler(Function onConnection, {Iterable<String> protocols,
Iterable<String> allowedOrigins}) {
Handler webSocketHandler(Function onConnection,
{Iterable<String> protocols, Iterable<String> allowedOrigins}) {
if (protocols != null) protocols = protocols.toSet();
if (allowedOrigins != null) {
allowedOrigins = allowedOrigins
.map((origin) => origin.toLowerCase()).toSet();
allowedOrigins =
allowedOrigins.map((origin) => origin.toLowerCase()).toSet();
}

if (onConnection is! _BinaryFunction) {
Expand Down
22 changes: 11 additions & 11 deletions lib/src/web_socket_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class WebSocketHandler {

var connection = request.headers['Connection'];
if (connection == null) return _notFound();
var tokens = connection.toLowerCase().split(',')
.map((token) => token.trim());
var tokens =
connection.toLowerCase().split(',').map((token) => token.trim());
if (!tokens.contains('upgrade')) return _notFound();

var upgrade = request.headers['Upgrade'];
Expand Down Expand Up @@ -58,16 +58,16 @@ class WebSocketHandler {
// unexpected origins, we ensure that malicious JavaScript is unable to fake
// a WebSocket handshake.
var origin = request.headers['Origin'];
if (origin != null && _allowedOrigins != null &&
if (origin != null &&
_allowedOrigins != null &&
!_allowedOrigins.contains(origin.toLowerCase())) {
return _forbidden('invalid origin "$origin".');
}

var protocol = _chooseProtocol(request);
request.hijack((channel) {
var sink = UTF8.encoder.startChunkedConversion(channel.sink);
sink.add(
"HTTP/1.1 101 Switching Protocols\r\n"
sink.add("HTTP/1.1 101 Switching Protocols\r\n"
"Upgrade: websocket\r\n"
"Connection: Upgrade\r\n"
"Sec-WebSocket-Accept: ${WebSocketChannel.signKey(key)}\r\n");
Expand Down Expand Up @@ -97,20 +97,20 @@ class WebSocketHandler {
}

/// Returns a 404 Not Found response.
Response _notFound() => _htmlResponse(404, "404 Not Found",
"Only WebSocket connections are supported.");
Response _notFound() => _htmlResponse(
404, "404 Not Found", "Only WebSocket connections are supported.");

/// Returns a 400 Bad Request response.
///
/// [message] will be HTML-escaped before being included in the response body.
Response _badRequest(String message) => _htmlResponse(400, "400 Bad Request",
"Invalid WebSocket upgrade request: $message");
Response _badRequest(String message) => _htmlResponse(
400, "400 Bad Request", "Invalid WebSocket upgrade request: $message");

/// Returns a 403 Forbidden response.
///
/// [message] will be HTML-escaped before being included in the response body.
Response _forbidden(String message) => _htmlResponse(403, "403 Forbidden",
"WebSocket upgrade refused: $message");
Response _forbidden(String message) => _htmlResponse(
403, "403 Forbidden", "WebSocket upgrade refused: $message");

/// Creates an HTTP response with the given [statusCode] and an HTML body with
/// [title] and [message].
Expand Down
41 changes: 23 additions & 18 deletions test/web_socket_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import 'package:shelf_web_socket/shelf_web_socket.dart';
import 'package:test/test.dart';

Map<String, String> get _handshakeHeaders => {
"Upgrade": "websocket",
"Connection": "Upgrade",
"Sec-WebSocket-Key": "x3JJHMbDL1EzLkh9GBhXDw==",
"Sec-WebSocket-Version": "13"
};
"Upgrade": "websocket",
"Connection": "Upgrade",
"Sec-WebSocket-Key": "x3JJHMbDL1EzLkh9GBhXDw==",
"Sec-WebSocket-Version": "13"
};

void main() {
test("can communicate with a dart:io WebSocket client", () async {
Expand Down Expand Up @@ -49,14 +49,16 @@ void main() {
});

test("negotiates the sub-protocol", () async {
var server = await shelf_io.serve(webSocketHandler((webSocket, protocol) {
expect(protocol, equals("two"));
webSocket.sink.close();
}, protocols: ["three", "two", "x"]), "localhost", 0);
var server = await shelf_io.serve(
webSocketHandler((webSocket, protocol) {
expect(protocol, equals("two"));
webSocket.sink.close();
}, protocols: ["three", "two", "x"]),
"localhost",
0);

try {
var webSocket = await WebSocket.connect(
'ws://localhost:${server.port}',
var webSocket = await WebSocket.connect('ws://localhost:${server.port}',
protocols: ["one", "two", "three"]);
expect(webSocket.protocol, equals("two"));
return webSocket.close();
Expand All @@ -69,9 +71,12 @@ void main() {
var server;
var url;
setUp(() async {
server = await shelf_io.serve(webSocketHandler((webSocket) {
webSocket.sink.close();
}, allowedOrigins: ["pub.dartlang.org", "GoOgLe.CoM"]), "localhost", 0);
server = await shelf_io.serve(
webSocketHandler((webSocket) {
webSocket.sink.close();
}, allowedOrigins: ["pub.dartlang.org", "GoOgLe.CoM"]),
"localhost",
0);
url = 'http://localhost:${server.port}/';
});

Expand Down Expand Up @@ -173,7 +178,7 @@ void main() {
}

Matcher hasStatus(int status) => completion(predicate((response) {
expect(response, new isInstanceOf<http.Response>());
expect(response.statusCode, equals(status));
return true;
}));
expect(response, new isInstanceOf<http.Response>());
expect(response.statusCode, equals(status));
return true;
}));

0 comments on commit 8cbd4e1

Please sign in to comment.