From 743e8648af59c6e7cd3f3ae223c0c66cfc01c59a Mon Sep 17 00:00:00 2001 From: Austin Drummond Date: Wed, 5 Mar 2025 01:18:29 -0500 Subject: [PATCH] added the ability to specify a path --- .../java/com/pusher/client/PusherOptions.java | 20 ++++++++++++++++++- .../com/pusher/client/PusherOptionsTest.java | 13 ++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/pusher/client/PusherOptions.java b/src/main/java/com/pusher/client/PusherOptions.java index 07b520f6..ca79cd4d 100644 --- a/src/main/java/com/pusher/client/PusherOptions.java +++ b/src/main/java/com/pusher/client/PusherOptions.java @@ -34,6 +34,7 @@ public class PusherOptions { private String host = "ws.pusherapp.com"; private int wsPort = WS_PORT; private int wssPort = WSS_PORT; + private String path = ""; private boolean useTLS = true; private long activityTimeout = DEFAULT_ACTIVITY_TIMEOUT; private long pongTimeout = DEFAULT_PONG_TIMEOUT; @@ -183,10 +184,26 @@ public PusherOptions setWssPort(final int wssPort) { return this; } + /** + * The path to which connections will be made. + *

+ * Note that if you wish to connect to a standard Pusher cluster, the + * convenience method setCluster will set the host and ports correctly from + * a single argument. + * + * @param path The path + * @return this, for chaining + */ + public PusherOptions setPath(final String path) { + this.path = path; + return this; + } + public PusherOptions setCluster(final String cluster) { host = "ws-" + cluster + "." + PUSHER_DOMAIN; wsPort = WS_PORT; wssPort = WSS_PORT; + path = ""; return this; } @@ -272,10 +289,11 @@ public long getPongTimeout() { */ public String buildUrl(final String apiKey) { return String.format( - "%s://%s:%s/app/%s%s", + "%s://%s:%s%s/app/%s%s", useTLS ? WSS_SCHEME : WS_SCHEME, host, useTLS ? wssPort : wsPort, + path, apiKey, URI_SUFFIX ); diff --git a/src/test/java/com/pusher/client/PusherOptionsTest.java b/src/test/java/com/pusher/client/PusherOptionsTest.java index a84c9af6..d571ecb5 100644 --- a/src/test/java/com/pusher/client/PusherOptionsTest.java +++ b/src/test/java/com/pusher/client/PusherOptionsTest.java @@ -186,4 +186,17 @@ public void testSetProxy() { public void testGetProxyReturnDefaultProxy() { assertEquals(pusherOptions.getProxy(), Proxy.NO_PROXY); } + + @Test + public void testCustomHostPortAnPathURLIsCorrect() { + pusherOptions.setHost("subdomain.example.com").setWsPort(8080).setWssPort(8181).setPath("/custom/path"); + assertEquals( + pusherOptions.buildUrl(API_KEY), + "wss://subdomain.example.com:8181/custom/path/app/" + + API_KEY + + "?client=java-client&protocol=5&version=" + + PusherOptions.LIB_VERSION + ); + } + }