Skip to content

Commit

Permalink
Merge pull request #40992 from mkouba/issue-40981
Browse files Browse the repository at this point in the history
WebSockets Next client: encode path param values automatically
  • Loading branch information
gastaldi authored Jun 6, 2024
2 parents 72b8122 + 76af842 commit d7e022b
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@ public class ClientEndpointTest {
void testClient() throws InterruptedException {
WebSocketClientConnection connection = connector
.baseUri(uri)
.pathParam("name", "Lu")
// The value will be encoded automatically
.pathParam("name", "Lu=")
.connectAndAwait();
assertEquals("Lu", connection.pathParam("name"));
assertEquals("Lu=", connection.pathParam("name"));
connection.sendTextAndAwait("Hi!");

assertTrue(ClientEndpoint.MESSAGE_LATCH.await(5, TimeUnit.SECONDS));
assertEquals("Lu:Hello Lu!", ClientEndpoint.MESSAGES.get(0));
assertEquals("Lu:Hi!", ClientEndpoint.MESSAGES.get(1));
assertEquals("Lu=:Hello Lu=!", ClientEndpoint.MESSAGES.get(0));
assertEquals("Lu=:Hi!", ClientEndpoint.MESSAGES.get(1));

connection.closeAndAwait();
assertTrue(ClientEndpoint.CLOSED_LATCH.await(5, TimeUnit.SECONDS));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.quarkus.websockets.next;

import java.net.URI;
import java.net.URLEncoder;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

Expand Down Expand Up @@ -51,6 +52,9 @@ static BasicWebSocketConnector create() {

/**
* Set the path param.
* <p>
* The value is encoded using {@link URLEncoder#encode(String, java.nio.charset.Charset)} before it's used to build the
* target URI.
*
* @param name
* @param value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public interface WebSocketClientConnection extends Sender, BlockingSender {
/**
*
* @param name
* @return the actual value of the path parameter or {@code null}
* @return the value of the path parameter or {@code null}
* @see WebSocketClient#path()
*/
String pathParam(String name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public interface WebSocketConnection extends Sender, BlockingSender {
/**
*
* @param name
* @return the actual value of the path parameter or {@code null}
* @return the decoded value of the path parameter or {@code null}
* @see WebSocket#path()
*/
String pathParam(String name);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.quarkus.websockets.next;

import java.net.URI;
import java.net.URLEncoder;

import io.smallrye.common.annotation.CheckReturnValue;
import io.smallrye.common.annotation.Experimental;
Expand Down Expand Up @@ -28,6 +29,9 @@ public interface WebSocketConnector<CLIENT> {

/**
* Set the path param.
* <p>
* The value is encoded using {@link URLEncoder#encode(String, java.nio.charset.Charset)} before it's used to build the
* target URI.
*
* @param name
* @param value
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.quarkus.websockets.next.runtime;

import java.net.URI;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -121,7 +123,7 @@ String replacePathParameters(String path) {
if (val == null) {
throw new WebSocketClientException("Unable to obtain the path param for: " + paramName);
}
m.appendReplacement(sb, val);
m.appendReplacement(sb, URLEncoder.encode(val, StandardCharsets.UTF_8));
}
m.appendTail(sb);
return path.startsWith("/") ? sb.toString() : "/" + sb.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public Uni<WebSocketClientConnection> connect() {
.setPort(serverEndpointUri.getPort());
StringBuilder uri = new StringBuilder();
if (serverEndpointUri.getPath() != null) {
uri.append(serverEndpointUri.getPath());
uri.append(serverEndpointUri.getRawPath());
}
if (serverEndpointUri.getQuery() != null) {
uri.append("?").append(serverEndpointUri.getQuery());
Expand Down

0 comments on commit d7e022b

Please sign in to comment.