Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebSockets Next client: encode path param values automatically #40992

Merged
merged 1 commit into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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