-
Notifications
You must be signed in to change notification settings - Fork 564
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds support for users to control the outcome of a WebSocket upgrade …
…request (#8594) * Adds support for users to control the outcome of a WebSocket upgrade request. If the user handler returns a non-101 code, the protocol upgrade fails and a response is written back based on the data returned by the handler, including the error code, headers and the reason for the failure. See issue 7953. Some new tests. * Removes all possible upgrade headers that don't belong in a failed response. Some other minor cleanup. Signed-off-by: Santiago Pericas-Geertsen <[email protected]> --------- Signed-off-by: Santiago Pericas-Geertsen <[email protected]>
- Loading branch information
Showing
5 changed files
with
421 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
...ver/websocket/src/test/java/io/helidon/webserver/websocket/test/HandshakeFailureTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Copyright (c) 2022, 2024 Oracle and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.helidon.webserver.websocket.test; | ||
|
||
import java.net.URI; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import io.helidon.common.http.Http; | ||
|
||
import jakarta.websocket.DeploymentException; | ||
import jakarta.websocket.HandshakeResponse; | ||
import jakarta.websocket.server.HandshakeRequest; | ||
import jakarta.websocket.server.ServerEndpointConfig; | ||
import org.glassfish.tyrus.client.auth.AuthenticationException; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.instanceOf; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
class HandshakeFailureTest extends TyrusSupportBaseTest { | ||
|
||
@BeforeAll | ||
static void startServer() throws Exception { | ||
webServer(true, EchoEndpoint.class); | ||
} | ||
|
||
/** | ||
* Should fail because user is not Helidon. See server handshake at | ||
* {@link EchoEndpoint.ServerConfigurator#modifyHandshake(ServerEndpointConfig, HandshakeRequest, HandshakeResponse)}. | ||
*/ | ||
@Test | ||
void testEchoSingleUpgradeFail() { | ||
URI uri = URI.create("ws://localhost:" + webServer().port() + "/tyrus/echo?user=Unknown"); | ||
EchoClient echoClient = new EchoClient(uri); | ||
try { | ||
echoClient.echo("One"); | ||
} catch (Exception e) { | ||
assertThat(e, instanceOf(DeploymentException.class)); | ||
assertThat(e.getCause(), instanceOf(AuthenticationException.class)); | ||
AuthenticationException ae = (AuthenticationException) e.getCause(); | ||
assertThat(ae.getHttpStatusCode(), is(401)); | ||
assertThat(ae.getMessage(), is("Authentication failed.")); | ||
return; | ||
} | ||
fail("Exception not thrown"); | ||
} | ||
|
||
/** | ||
* Should fail because user is not Helidon. See server handshake at | ||
* {@link EchoEndpoint.ServerConfigurator#modifyHandshake(ServerEndpointConfig, HandshakeRequest, HandshakeResponse)}. | ||
*/ | ||
@Test | ||
void testEchoSingleUpgradeFailRaw() throws Exception { | ||
String response = SocketHttpClient.sendAndReceive("/tyrus/echo?user=Unknown", | ||
Http.Method.GET, | ||
List.of("Connection:Upgrade", | ||
"Upgrade:websocket", | ||
"Sec-WebSocket-Key:0SBbaRkS/idPrmvImDNHBA==", | ||
"Sec-WebSocket-Version:13"), | ||
webServer()); | ||
|
||
assertThat(SocketHttpClient.statusFromResponse(response), | ||
is(Http.Status.UNAUTHORIZED_401)); | ||
assertThat(SocketHttpClient.entityFromResponse(response, false), | ||
is("Failed to authenticate\n")); | ||
Map<String, String> headers = SocketHttpClient.headersFromResponse(response); | ||
assertThat(headers.get("Endpoint"), is("EchoEndpoint")); | ||
assertFalse(headers.containsKey("Connection") || headers.containsKey("connection")); | ||
assertFalse(headers.containsKey("Upgrade") || headers.containsKey("upgrade")); | ||
} | ||
} |
Oops, something went wrong.