Skip to content

Commit

Permalink
WebSockets Next: honor the quarkus.http.root-path correctly
Browse files Browse the repository at this point in the history
- i.e. do not use HttpRootPathBuildItem together with
RouteBuildItem.builder()
  • Loading branch information
mkouba committed Jul 23, 2024
1 parent ffafe44 commit 4bee31a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
2 changes: 2 additions & 0 deletions docs/src/main/asciidoc/websockets-next-reference.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ public class ChatWebSocket {
Thus, client can connect to this web socket endpoint using `ws://localhost:8080/chat/your-name`.
If TLS is used, the URL is `wss://localhost:8443/chat/your-name`.

NOTE: The endpoint path is relative to the xref:http-reference.adoc#context-path[root context] configured by the `quarkus.http.root-path` (which is `/` by default). For example, if you add `quarkus.http.root-path=/api` to your `application.properties` then a client can connect to this endpoint using `http://localhost:8080/api/chat/the-name`.

Check warning on line 111 in docs/src/main/asciidoc/websockets-next-reference.adoc

View workflow job for this annotation

GitHub Actions / Linting with Vale

[vale] reported by reviewdog 🐶 [Quarkus.Spelling] Use correct American English spelling. Did you really mean 'th'? Raw Output: {"message": "[Quarkus.Spelling] Use correct American English spelling. Did you really mean 'th'?", "location": {"path": "docs/src/main/asciidoc/websockets-next-reference.adoc", "range": {"start": {"line": 111, "column": 68}}}, "severity": "WARNING"}

[[client-endpoints]]
=== Client endpoints

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@
import io.quarkus.security.spi.ClassSecurityCheckStorageBuildItem;
import io.quarkus.security.spi.SecurityTransformerUtils;
import io.quarkus.security.spi.runtime.SecurityCheck;
import io.quarkus.vertx.http.deployment.HttpRootPathBuildItem;
import io.quarkus.vertx.http.deployment.RouteBuildItem;
import io.quarkus.vertx.http.runtime.HandlerType;
import io.quarkus.vertx.http.runtime.HttpBuildTimeConfig;
Expand Down Expand Up @@ -445,18 +444,17 @@ public String apply(String name) {
@Consume(SyntheticBeansRuntimeInitBuildItem.class) // SecurityHttpUpgradeCheck is runtime init due to runtime config
@Record(RUNTIME_INIT)
@BuildStep
public void registerRoutes(WebSocketServerRecorder recorder, HttpRootPathBuildItem httpRootPath,
List<GeneratedEndpointBuildItem> generatedEndpoints, HttpBuildTimeConfig httpConfig, Capabilities capabilities,
public void registerRoutes(WebSocketServerRecorder recorder, List<GeneratedEndpointBuildItem> generatedEndpoints,
HttpBuildTimeConfig httpConfig, Capabilities capabilities,
BuildProducer<RouteBuildItem> routes) {
for (GeneratedEndpointBuildItem endpoint : generatedEndpoints.stream().filter(GeneratedEndpointBuildItem::isServer)
.toList()) {
RouteBuildItem.Builder builder = RouteBuildItem.builder();
String relativePath = httpRootPath.relativePath(endpoint.path);
if (capabilities.isPresent(Capability.SECURITY) && !httpConfig.auth.proactive) {
// Add a special handler so that it's possible to capture the SecurityIdentity before the HTTP upgrade
builder.routeFunction(relativePath, recorder.initializeSecurityHandler());
builder.routeFunction(endpoint.path, recorder.initializeSecurityHandler());
} else {
builder.route(relativePath);
builder.route(endpoint.path);
}
builder
.displayOnNotFoundPage("WebSocket Endpoint")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package io.quarkus.websockets.next.test.rootpath;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.net.URI;

import jakarta.inject.Inject;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;
import io.quarkus.test.common.http.TestHTTPResource;
import io.quarkus.websockets.next.OnTextMessage;
import io.quarkus.websockets.next.WebSocket;
import io.quarkus.websockets.next.test.utils.WSClient;
import io.vertx.core.Vertx;

public class CustomRootPathTest {

@RegisterExtension
public static final QuarkusUnitTest test = new QuarkusUnitTest()
.withApplicationRoot(root -> {
root.addClasses(Echo.class, WSClient.class);
}).overrideConfigKey("quarkus.http.root-path", "/api");

@Inject
Vertx vertx;

@TestHTTPResource("echo")
URI testUri;

@Test
void testEndpoint() {
assertTrue(testUri.toString().contains("/api"));
try (WSClient client = WSClient.create(vertx).connect(testUri)) {
assertEquals("monty", client.sendAndAwaitReply("monty").toString());
}
}

@WebSocket(path = "/echo")
public static class Echo {

@OnTextMessage
String process(String message) throws InterruptedException {
return message;
}

}

}

0 comments on commit 4bee31a

Please sign in to comment.