Skip to content
This repository was archived by the owner on May 10, 2022. It is now read-only.

Commit 8050a55

Browse files
kevmoonatebosch
authored andcommitted
Fix newly enforced package:pedantic lints (#24)
- prefer_single_quotes Drop unused author field pubspec.
1 parent ed1dfc1 commit 8050a55

File tree

4 files changed

+55
-56
lines changed

4 files changed

+55
-56
lines changed

lib/shelf_web_socket.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ Handler webSocketHandler(Function onConnection,
4747
Duration pingInterval}) {
4848
if (onConnection is! _BinaryFunction) {
4949
if (protocols != null) {
50-
throw ArgumentError("If protocols is non-null, onConnection must "
51-
"take two arguments, the WebSocket and the protocol.");
50+
throw ArgumentError('If protocols is non-null, onConnection must '
51+
'take two arguments, the WebSocket and the protocol.');
5252
}
5353

5454
var innerOnConnection = onConnection;

lib/src/web_socket_handler.dart

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ class WebSocketHandler {
5454
if (key == null) return _badRequest('missing Sec-WebSocket-Key header.');
5555

5656
if (!request.canHijack) {
57-
throw ArgumentError("webSocketHandler may only be used with a server "
58-
"that supports request hijacking.");
57+
throw ArgumentError('webSocketHandler may only be used with a server '
58+
'that supports request hijacking.');
5959
}
6060

6161
// The Origin header is always set by browser connections. By filtering out
@@ -71,12 +71,12 @@ class WebSocketHandler {
7171
var protocol = _chooseProtocol(request);
7272
request.hijack((channel) {
7373
var sink = utf8.encoder.startChunkedConversion(channel.sink);
74-
sink.add("HTTP/1.1 101 Switching Protocols\r\n"
75-
"Upgrade: websocket\r\n"
76-
"Connection: Upgrade\r\n"
77-
"Sec-WebSocket-Accept: ${WebSocketChannel.signKey(key)}\r\n");
78-
if (protocol != null) sink.add("Sec-WebSocket-Protocol: $protocol\r\n");
79-
sink.add("\r\n");
74+
sink.add('HTTP/1.1 101 Switching Protocols\r\n'
75+
'Upgrade: websocket\r\n'
76+
'Connection: Upgrade\r\n'
77+
'Sec-WebSocket-Accept: ${WebSocketChannel.signKey(key)}\r\n');
78+
if (protocol != null) sink.add('Sec-WebSocket-Protocol: $protocol\r\n');
79+
sink.add('\r\n');
8080

8181
_onConnection(
8282
WebSocketChannel(channel, pingInterval: _pingInterval), protocol);
@@ -103,19 +103,19 @@ class WebSocketHandler {
103103

104104
/// Returns a 404 Not Found response.
105105
Response _notFound() => _htmlResponse(
106-
404, "404 Not Found", "Only WebSocket connections are supported.");
106+
404, '404 Not Found', 'Only WebSocket connections are supported.');
107107

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

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

120120
/// Creates an HTTP response with the given [statusCode] and an HTML body with
121121
/// [title] and [message].
@@ -124,7 +124,7 @@ class WebSocketHandler {
124124
Response _htmlResponse(int statusCode, String title, String message) {
125125
title = htmlEscape.convert(title);
126126
message = htmlEscape.convert(message);
127-
return Response(statusCode, body: """
127+
return Response(statusCode, body: '''
128128
<!doctype html>
129129
<html>
130130
<head><title>$title</title></head>
@@ -133,6 +133,6 @@ class WebSocketHandler {
133133
<p>$message</p>
134134
</body>
135135
</html>
136-
""", headers: {'content-type': 'text/html'});
136+
''', headers: {'content-type': 'text/html'});
137137
}
138138
}

pubspec.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
name: shelf_web_socket
2-
version: 0.2.3
2+
version: 0.2.4-dev
33

44
description: >-
55
A shelf handler that wires up a listener for every connection.
6-
author: Dart Team <[email protected]>
76
homepage: https://github.com/dart-lang/shelf_web_socket
87

98
environment:

test/web_socket_test.dart

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,36 @@ import 'package:shelf_web_socket/shelf_web_socket.dart';
1010
import 'package:test/test.dart';
1111

1212
Map<String, String> get _handshakeHeaders => {
13-
"Upgrade": "websocket",
14-
"Connection": "Upgrade",
15-
"Sec-WebSocket-Key": "x3JJHMbDL1EzLkh9GBhXDw==",
16-
"Sec-WebSocket-Version": "13"
13+
'Upgrade': 'websocket',
14+
'Connection': 'Upgrade',
15+
'Sec-WebSocket-Key': 'x3JJHMbDL1EzLkh9GBhXDw==',
16+
'Sec-WebSocket-Version': '13'
1717
};
1818

1919
void main() {
20-
test("can communicate with a dart:io WebSocket client", () async {
20+
test('can communicate with a dart:io WebSocket client', () async {
2121
var server = await shelf_io.serve(webSocketHandler((webSocket) {
22-
webSocket.sink.add("hello!");
22+
webSocket.sink.add('hello!');
2323
webSocket.stream.first.then((request) {
24-
expect(request, equals("ping"));
25-
webSocket.sink.add("pong");
24+
expect(request, equals('ping'));
25+
webSocket.sink.add('pong');
2626
webSocket.sink.close();
2727
});
28-
}), "localhost", 0);
28+
}), 'localhost', 0);
2929

3030
try {
3131
var webSocket = await WebSocket.connect('ws://localhost:${server.port}');
3232
var n = 0;
3333
await webSocket.listen((message) {
3434
if (n == 0) {
35-
expect(message, equals("hello!"));
36-
webSocket.add("ping");
35+
expect(message, equals('hello!'));
36+
webSocket.add('ping');
3737
} else if (n == 1) {
38-
expect(message, equals("pong"));
38+
expect(message, equals('pong'));
3939
webSocket.close();
4040
server.close();
4141
} else {
42-
fail("Only expected two messages.");
42+
fail('Only expected two messages.');
4343
}
4444
n++;
4545
}).asFuture();
@@ -48,74 +48,74 @@ void main() {
4848
}
4949
});
5050

51-
test("negotiates the sub-protocol", () async {
51+
test('negotiates the sub-protocol', () async {
5252
var server = await shelf_io.serve(
5353
webSocketHandler((webSocket, protocol) {
54-
expect(protocol, equals("two"));
54+
expect(protocol, equals('two'));
5555
webSocket.sink.close();
56-
}, protocols: ["three", "two", "x"]),
57-
"localhost",
56+
}, protocols: ['three', 'two', 'x']),
57+
'localhost',
5858
0);
5959

6060
try {
6161
var webSocket = await WebSocket.connect('ws://localhost:${server.port}',
62-
protocols: ["one", "two", "three"]);
63-
expect(webSocket.protocol, equals("two"));
62+
protocols: ['one', 'two', 'three']);
63+
expect(webSocket.protocol, equals('two'));
6464
return webSocket.close();
6565
} finally {
6666
await server.close();
6767
}
6868
});
6969

70-
group("with a set of allowed origins", () {
70+
group('with a set of allowed origins', () {
7171
var server;
7272
var url;
7373
setUp(() async {
7474
server = await shelf_io.serve(
7575
webSocketHandler((webSocket) {
7676
webSocket.sink.close();
77-
}, allowedOrigins: ["pub.dartlang.org", "GoOgLe.CoM"]),
78-
"localhost",
77+
}, allowedOrigins: ['pub.dartlang.org', 'GoOgLe.CoM']),
78+
'localhost',
7979
0);
8080
url = 'http://localhost:${server.port}/';
8181
});
8282

8383
tearDown(() => server.close());
8484

85-
test("allows access with an allowed origin", () {
85+
test('allows access with an allowed origin', () {
8686
var headers = _handshakeHeaders;
8787
headers['Origin'] = 'pub.dartlang.org';
8888
expect(http.get(url, headers: headers), hasStatus(101));
8989
});
9090

91-
test("forbids access with a non-allowed origin", () {
91+
test('forbids access with a non-allowed origin', () {
9292
var headers = _handshakeHeaders;
9393
headers['Origin'] = 'dartlang.org';
9494
expect(http.get(url, headers: headers), hasStatus(403));
9595
});
9696

97-
test("allows access with no origin", () {
97+
test('allows access with no origin', () {
9898
expect(http.get(url, headers: _handshakeHeaders), hasStatus(101));
9999
});
100100

101-
test("ignores the case of the client origin", () {
101+
test('ignores the case of the client origin', () {
102102
var headers = _handshakeHeaders;
103103
headers['Origin'] = 'PuB.DaRtLaNg.OrG';
104104
expect(http.get(url, headers: headers), hasStatus(101));
105105
});
106106

107-
test("ignores the case of the server origin", () {
107+
test('ignores the case of the server origin', () {
108108
var headers = _handshakeHeaders;
109109
headers['Origin'] = 'google.com';
110110
expect(http.get(url, headers: headers), hasStatus(101));
111111
});
112112
});
113113

114114
// Regression test for issue 21894.
115-
test("allows a Connection header with multiple values", () async {
115+
test('allows a Connection header with multiple values', () async {
116116
var server = await shelf_io.serve(webSocketHandler((webSocket) {
117117
webSocket.sink.close();
118-
}), "localhost", 0);
118+
}), 'localhost', 0);
119119

120120
var url = 'http://localhost:${server.port}/';
121121
var headers = _handshakeHeaders;
@@ -124,54 +124,54 @@ void main() {
124124
hasStatus(101));
125125
});
126126

127-
group("HTTP errors", () {
127+
group('HTTP errors', () {
128128
var server;
129129
var url;
130130
setUp(() async {
131131
server = await shelf_io.serve(webSocketHandler((_) {
132-
fail("should not create a WebSocket");
133-
}), "localhost", 0);
132+
fail('should not create a WebSocket');
133+
}), 'localhost', 0);
134134
url = 'http://localhost:${server.port}/';
135135
});
136136

137137
tearDown(() => server.close());
138138

139-
test("404s for non-GET requests", () {
139+
test('404s for non-GET requests', () {
140140
expect(http.delete(url, headers: _handshakeHeaders), hasStatus(404));
141141
});
142142

143-
test("404s for non-Upgrade requests", () {
143+
test('404s for non-Upgrade requests', () {
144144
var headers = _handshakeHeaders;
145145
headers.remove('Connection');
146146
expect(http.get(url, headers: headers), hasStatus(404));
147147
});
148148

149-
test("404s for non-websocket upgrade requests", () {
149+
test('404s for non-websocket upgrade requests', () {
150150
var headers = _handshakeHeaders;
151151
headers['Upgrade'] = 'fblthp';
152152
expect(http.get(url, headers: headers), hasStatus(404));
153153
});
154154

155-
test("400s for a missing Sec-WebSocket-Version", () {
155+
test('400s for a missing Sec-WebSocket-Version', () {
156156
var headers = _handshakeHeaders;
157157
headers.remove('Sec-WebSocket-Version');
158158
expect(http.get(url, headers: headers), hasStatus(400));
159159
});
160160

161-
test("404s for an unknown Sec-WebSocket-Version", () {
161+
test('404s for an unknown Sec-WebSocket-Version', () {
162162
var headers = _handshakeHeaders;
163163
headers['Sec-WebSocket-Version'] = '15';
164164
expect(http.get(url, headers: headers), hasStatus(404));
165165
});
166166

167-
test("400s for a missing Sec-WebSocket-Key", () {
167+
test('400s for a missing Sec-WebSocket-Key', () {
168168
var headers = _handshakeHeaders;
169169
headers.remove('Sec-WebSocket-Key');
170170
expect(http.get(url, headers: headers), hasStatus(400));
171171
});
172172
});
173173

174-
test("throws an error if a unary function is provided with protocols", () {
174+
test('throws an error if a unary function is provided with protocols', () {
175175
expect(() => webSocketHandler((_) => null, protocols: ['foo']),
176176
throwsArgumentError);
177177
});

0 commit comments

Comments
 (0)