Skip to content

Commit

Permalink
WebSockets Next: support endpoints with empty path
Browse files Browse the repository at this point in the history
- fixes #42808
  • Loading branch information
mkouba committed Aug 28, 2024
1 parent d340996 commit b039519
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,9 @@ static String mergePath(String prefix, String path) {
}

static String getPath(String path) {
if (path.isEmpty()) {
return "";
}
StringBuilder sb = new StringBuilder();
Matcher m = PATH_PARAM_PATTERN.matcher(path);
while (m.find()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package io.quarkus.websockets.next.test.client;

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

import java.net.URI;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

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.OnClose;
import io.quarkus.websockets.next.OnTextMessage;
import io.quarkus.websockets.next.WebSocket;
import io.quarkus.websockets.next.WebSocketClient;
import io.quarkus.websockets.next.WebSocketClientConnection;
import io.quarkus.websockets.next.WebSocketConnector;

public class ClientEndpointEmptyPathTest {

@RegisterExtension
public static final QuarkusUnitTest test = new QuarkusUnitTest()
.withApplicationRoot(root -> {
root.addClasses(ServerEndpoint.class, ClientEndpoint.class);
});

@Inject
WebSocketConnector<ClientEndpoint> connector;

@TestHTTPResource("/")
URI uri;

@Test
void testClient() throws InterruptedException {
WebSocketClientConnection connection = connector
.baseUri(uri)
.connectAndAwait();
connection.sendTextAndAwait("Hi!");

assertTrue(ClientEndpoint.MESSAGE_LATCH.await(5, TimeUnit.SECONDS));
assertEquals("Hi!", ClientEndpoint.MESSAGES.get(0));

connection.closeAndAwait();
assertTrue(ClientEndpoint.CLOSED_LATCH.await(5, TimeUnit.SECONDS));
assertTrue(ServerEndpoint.CLOSED_LATCH.await(5, TimeUnit.SECONDS));
}

@WebSocket(path = "")
public static class ServerEndpoint {

static final CountDownLatch CLOSED_LATCH = new CountDownLatch(1);

@OnTextMessage
String echo(String message) {
return message;
}

@OnClose
void close() {
CLOSED_LATCH.countDown();
}

}

@WebSocketClient(path = "")
public static class ClientEndpoint {

static final CountDownLatch MESSAGE_LATCH = new CountDownLatch(1);

static final List<String> MESSAGES = new CopyOnWriteArrayList<>();

static final CountDownLatch CLOSED_LATCH = new CountDownLatch(1);

@OnTextMessage
void onMessage(String message, WebSocketClientConnection connection) {
MESSAGES.add(message);
MESSAGE_LATCH.countDown();
}

@OnClose
void close() {
CLOSED_LATCH.countDown();
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -265,26 +265,26 @@ public Void call() {
}

private String mergePath(String path1, String path2) {
StringBuilder path = new StringBuilder();
StringBuilder ret = new StringBuilder();
if (path1 != null) {
path.append(path1);
ret.append(path1);
}
if (path2 != null) {
if (path1.endsWith("/")) {
if (path2.startsWith("/")) {
path.append(path2.substring(1));
ret.append(path2.substring(1));
} else {
path.append(path2);
ret.append(path2);
}
} else {
if (path2.startsWith("/")) {
path.append(path2);
ret.append(path2);
} else {
path.append(path2.substring(1));
ret.append("/").append(path2);
}
}
}
return path.toString();
return ret.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ Set<String> getPathParamNames(String path) {
}

String replacePathParameters(String path) {
if (path.isEmpty()) {
return "";
}
StringBuilder sb = new StringBuilder();
Matcher m = PATH_PARAM_PATTERN.matcher(path);
while (m.find()) {
Expand Down

0 comments on commit b039519

Please sign in to comment.