@@ -10,36 +10,36 @@ import 'package:shelf_web_socket/shelf_web_socket.dart';
10
10
import 'package:test/test.dart' ;
11
11
12
12
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'
17
17
};
18
18
19
19
void main () {
20
- test (" can communicate with a dart:io WebSocket client" , () async {
20
+ test (' can communicate with a dart:io WebSocket client' , () async {
21
21
var server = await shelf_io.serve (webSocketHandler ((webSocket) {
22
- webSocket.sink.add (" hello!" );
22
+ webSocket.sink.add (' hello!' );
23
23
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' );
26
26
webSocket.sink.close ();
27
27
});
28
- }), " localhost" , 0 );
28
+ }), ' localhost' , 0 );
29
29
30
30
try {
31
31
var webSocket = await WebSocket .connect ('ws://localhost:${server .port }' );
32
32
var n = 0 ;
33
33
await webSocket.listen ((message) {
34
34
if (n == 0 ) {
35
- expect (message, equals (" hello!" ));
36
- webSocket.add (" ping" );
35
+ expect (message, equals (' hello!' ));
36
+ webSocket.add (' ping' );
37
37
} else if (n == 1 ) {
38
- expect (message, equals (" pong" ));
38
+ expect (message, equals (' pong' ));
39
39
webSocket.close ();
40
40
server.close ();
41
41
} else {
42
- fail (" Only expected two messages." );
42
+ fail (' Only expected two messages.' );
43
43
}
44
44
n++ ;
45
45
}).asFuture ();
@@ -48,74 +48,74 @@ void main() {
48
48
}
49
49
});
50
50
51
- test (" negotiates the sub-protocol" , () async {
51
+ test (' negotiates the sub-protocol' , () async {
52
52
var server = await shelf_io.serve (
53
53
webSocketHandler ((webSocket, protocol) {
54
- expect (protocol, equals (" two" ));
54
+ expect (protocol, equals (' two' ));
55
55
webSocket.sink.close ();
56
- }, protocols: [" three" , " two" , "x" ]),
57
- " localhost" ,
56
+ }, protocols: [' three' , ' two' , 'x' ]),
57
+ ' localhost' ,
58
58
0 );
59
59
60
60
try {
61
61
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' ));
64
64
return webSocket.close ();
65
65
} finally {
66
66
await server.close ();
67
67
}
68
68
});
69
69
70
- group (" with a set of allowed origins" , () {
70
+ group (' with a set of allowed origins' , () {
71
71
var server;
72
72
var url;
73
73
setUp (() async {
74
74
server = await shelf_io.serve (
75
75
webSocketHandler ((webSocket) {
76
76
webSocket.sink.close ();
77
- }, allowedOrigins: [" pub.dartlang.org" , " GoOgLe.CoM" ]),
78
- " localhost" ,
77
+ }, allowedOrigins: [' pub.dartlang.org' , ' GoOgLe.CoM' ]),
78
+ ' localhost' ,
79
79
0 );
80
80
url = 'http://localhost:${server .port }/' ;
81
81
});
82
82
83
83
tearDown (() => server.close ());
84
84
85
- test (" allows access with an allowed origin" , () {
85
+ test (' allows access with an allowed origin' , () {
86
86
var headers = _handshakeHeaders;
87
87
headers['Origin' ] = 'pub.dartlang.org' ;
88
88
expect (http.get (url, headers: headers), hasStatus (101 ));
89
89
});
90
90
91
- test (" forbids access with a non-allowed origin" , () {
91
+ test (' forbids access with a non-allowed origin' , () {
92
92
var headers = _handshakeHeaders;
93
93
headers['Origin' ] = 'dartlang.org' ;
94
94
expect (http.get (url, headers: headers), hasStatus (403 ));
95
95
});
96
96
97
- test (" allows access with no origin" , () {
97
+ test (' allows access with no origin' , () {
98
98
expect (http.get (url, headers: _handshakeHeaders), hasStatus (101 ));
99
99
});
100
100
101
- test (" ignores the case of the client origin" , () {
101
+ test (' ignores the case of the client origin' , () {
102
102
var headers = _handshakeHeaders;
103
103
headers['Origin' ] = 'PuB.DaRtLaNg.OrG' ;
104
104
expect (http.get (url, headers: headers), hasStatus (101 ));
105
105
});
106
106
107
- test (" ignores the case of the server origin" , () {
107
+ test (' ignores the case of the server origin' , () {
108
108
var headers = _handshakeHeaders;
109
109
headers['Origin' ] = 'google.com' ;
110
110
expect (http.get (url, headers: headers), hasStatus (101 ));
111
111
});
112
112
});
113
113
114
114
// 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 {
116
116
var server = await shelf_io.serve (webSocketHandler ((webSocket) {
117
117
webSocket.sink.close ();
118
- }), " localhost" , 0 );
118
+ }), ' localhost' , 0 );
119
119
120
120
var url = 'http://localhost:${server .port }/' ;
121
121
var headers = _handshakeHeaders;
@@ -124,54 +124,54 @@ void main() {
124
124
hasStatus (101 ));
125
125
});
126
126
127
- group (" HTTP errors" , () {
127
+ group (' HTTP errors' , () {
128
128
var server;
129
129
var url;
130
130
setUp (() async {
131
131
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 );
134
134
url = 'http://localhost:${server .port }/' ;
135
135
});
136
136
137
137
tearDown (() => server.close ());
138
138
139
- test (" 404s for non-GET requests" , () {
139
+ test (' 404s for non-GET requests' , () {
140
140
expect (http.delete (url, headers: _handshakeHeaders), hasStatus (404 ));
141
141
});
142
142
143
- test (" 404s for non-Upgrade requests" , () {
143
+ test (' 404s for non-Upgrade requests' , () {
144
144
var headers = _handshakeHeaders;
145
145
headers.remove ('Connection' );
146
146
expect (http.get (url, headers: headers), hasStatus (404 ));
147
147
});
148
148
149
- test (" 404s for non-websocket upgrade requests" , () {
149
+ test (' 404s for non-websocket upgrade requests' , () {
150
150
var headers = _handshakeHeaders;
151
151
headers['Upgrade' ] = 'fblthp' ;
152
152
expect (http.get (url, headers: headers), hasStatus (404 ));
153
153
});
154
154
155
- test (" 400s for a missing Sec-WebSocket-Version" , () {
155
+ test (' 400s for a missing Sec-WebSocket-Version' , () {
156
156
var headers = _handshakeHeaders;
157
157
headers.remove ('Sec-WebSocket-Version' );
158
158
expect (http.get (url, headers: headers), hasStatus (400 ));
159
159
});
160
160
161
- test (" 404s for an unknown Sec-WebSocket-Version" , () {
161
+ test (' 404s for an unknown Sec-WebSocket-Version' , () {
162
162
var headers = _handshakeHeaders;
163
163
headers['Sec-WebSocket-Version' ] = '15' ;
164
164
expect (http.get (url, headers: headers), hasStatus (404 ));
165
165
});
166
166
167
- test (" 400s for a missing Sec-WebSocket-Key" , () {
167
+ test (' 400s for a missing Sec-WebSocket-Key' , () {
168
168
var headers = _handshakeHeaders;
169
169
headers.remove ('Sec-WebSocket-Key' );
170
170
expect (http.get (url, headers: headers), hasStatus (400 ));
171
171
});
172
172
});
173
173
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' , () {
175
175
expect (() => webSocketHandler ((_) => null , protocols: ['foo' ]),
176
176
throwsArgumentError);
177
177
});
0 commit comments