|
45 | 45 | import java.util.concurrent.atomic.AtomicBoolean;
|
46 | 46 | import java.util.concurrent.atomic.AtomicInteger;
|
47 | 47 | import java.util.concurrent.atomic.AtomicReference;
|
48 |
| -import java.util.function.BiConsumer; |
49 |
| -import java.util.function.Consumer; |
50 |
| -import java.util.function.Function; |
51 |
| -import java.util.function.LongConsumer; |
| 48 | +import java.util.function.*; |
52 | 49 | import java.util.stream.Collectors;
|
53 | 50 | import java.util.stream.IntStream;
|
54 | 51 | import java.util.stream.Stream;
|
@@ -5310,4 +5307,61 @@ public void testMissingHostHeader() throws Exception {
|
5310 | 5307 | }));
|
5311 | 5308 | await();
|
5312 | 5309 | }
|
| 5310 | + |
| 5311 | + @Test |
| 5312 | + public void testCanUpgradeToWebSocket() throws Exception { |
| 5313 | + UnaryOperator<RequestOptions> config = ro -> ro.setMethod(HttpMethod.GET) |
| 5314 | + .putHeader(HttpHeaders.CONNECTION, HttpHeaders.UPGRADE) |
| 5315 | + .putHeader(HttpHeaders.UPGRADE, HttpHeaders.WEBSOCKET); |
| 5316 | + doTestCanUpgradeToWebSocket(config, true); |
| 5317 | + } |
| 5318 | + |
| 5319 | + @Test |
| 5320 | + public void testCanUpgradeToWebSocketFirefox() throws Exception { |
| 5321 | + UnaryOperator<RequestOptions> config = ro -> ro.setMethod(HttpMethod.GET) |
| 5322 | + .putHeader(HttpHeaders.CONNECTION, HttpHeaders.KEEP_ALIVE + ", " + HttpHeaders.UPGRADE) |
| 5323 | + .putHeader(HttpHeaders.UPGRADE, HttpHeaders.WEBSOCKET); |
| 5324 | + doTestCanUpgradeToWebSocket(config, true); |
| 5325 | + } |
| 5326 | + |
| 5327 | + @Test |
| 5328 | + public void testCannotUpgradePostRequestToWebSocket() throws Exception { |
| 5329 | + UnaryOperator<RequestOptions> config = ro -> ro.setMethod(HttpMethod.POST) |
| 5330 | + .putHeader(HttpHeaders.CONNECTION, HttpHeaders.UPGRADE) |
| 5331 | + .putHeader(HttpHeaders.UPGRADE, HttpHeaders.WEBSOCKET); |
| 5332 | + doTestCanUpgradeToWebSocket(config, false); |
| 5333 | + } |
| 5334 | + |
| 5335 | + @Test |
| 5336 | + public void testCannotUpgradeToWebSocketIfConnectionHeaderIsMissing() throws Exception { |
| 5337 | + UnaryOperator<RequestOptions> config = ro -> ro.setMethod(HttpMethod.GET) |
| 5338 | + .putHeader(HttpHeaders.UPGRADE, HttpHeaders.WEBSOCKET); |
| 5339 | + doTestCanUpgradeToWebSocket(config, false); |
| 5340 | + } |
| 5341 | + |
| 5342 | + @Test |
| 5343 | + public void testCannotUpgradeToWebSocketIfUpgradeDoesNotContainWebsocket() throws Exception { |
| 5344 | + UnaryOperator<RequestOptions> config = ro -> ro.setMethod(HttpMethod.GET) |
| 5345 | + .putHeader(HttpHeaders.CONNECTION, HttpHeaders.UPGRADE) |
| 5346 | + .putHeader(HttpHeaders.UPGRADE, "foo"); |
| 5347 | + doTestCanUpgradeToWebSocket(config, false); |
| 5348 | + } |
| 5349 | + |
| 5350 | + private void doTestCanUpgradeToWebSocket(UnaryOperator<RequestOptions> config, boolean shouldSucceed) throws Exception { |
| 5351 | + server.requestHandler(req -> { |
| 5352 | + HttpServerResponse serverResponse = req.response(); |
| 5353 | + int statusCode = HttpUtils.canUpgradeToWebSocket(req) ? 200 : 500; |
| 5354 | + serverResponse.setStatusCode(statusCode); |
| 5355 | + serverResponse.end(); |
| 5356 | + }); |
| 5357 | + startServer(testAddress); |
| 5358 | + client.request(config.apply(new RequestOptions(requestOptions))).onComplete(onSuccess(req -> { |
| 5359 | + req.response().onComplete(onSuccess(resp -> { |
| 5360 | + assertEquals(shouldSucceed ? 200 : 500, resp.statusCode()); |
| 5361 | + testComplete(); |
| 5362 | + })); |
| 5363 | + req.send(); |
| 5364 | + })); |
| 5365 | + await(); |
| 5366 | + } |
5313 | 5367 | }
|
0 commit comments