Skip to content

Commit

Permalink
Merge pull request #1019 from ably/ECO-4862/add-clientId-header
Browse files Browse the repository at this point in the history
feat: include `X-Ably-ClientId` for each request (RSA7e2)
  • Loading branch information
ttypic authored Jul 18, 2024
2 parents 6b4996e + 7531c34 commit c2c4125
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/src/main/java/io/ably/lib/http/HttpCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import io.ably.lib.types.Param;
import io.ably.lib.types.ProxyOptions;
import io.ably.lib.util.AgentHeaderCreator;
import io.ably.lib.util.Base64Coder;
import io.ably.lib.util.Log;
import io.ably.lib.util.PlatformAgentProvider;

Expand Down Expand Up @@ -211,6 +212,7 @@ <T> T httpExecute(HttpURLConnection conn, String method, Param[] headers, Reques
/* pass required headers */
conn.setRequestProperty(Defaults.ABLY_PROTOCOL_VERSION_HEADER, Defaults.ABLY_PROTOCOL_VERSION); // RSC7a
conn.setRequestProperty(Defaults.ABLY_AGENT_HEADER, AgentHeaderCreator.create(options.agents, platformAgentProvider));
if (options.clientId != null) conn.setRequestProperty(Defaults.ABLY_CLIENT_ID_HEADER, Base64Coder.encodeString(options.clientId));

/* prepare request body */
byte[] body = null;
Expand Down
1 change: 1 addition & 0 deletions lib/src/main/java/io/ably/lib/transport/Defaults.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class Defaults {

/* http headers */
public static final String ABLY_PROTOCOL_VERSION_HEADER = "X-Ably-Version";
public static final String ABLY_CLIENT_ID_HEADER = "X-Ably-ClientId";
public static final String ABLY_AGENT_HEADER = "Ably-Agent";

/* Hosts */
Expand Down
40 changes: 39 additions & 1 deletion lib/src/test/java/io/ably/lib/test/rest/HttpHeaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static void tearDown() {
* should be included in all REST requests to the Ably endpoint
* see {@link io.ably.lib.transport.Defaults#ABLY_AGENT_PARAM}
* <p>
* Spec: RSC7d, G4
* Spec: RSC7d, G4, RSA7e2
* </p>
*/
@Test
Expand Down Expand Up @@ -83,12 +83,50 @@ public void header_lib_channel_publish() {
Assert.assertNotNull("Expected headers", headers);
Assert.assertEquals(headers.get("x-ably-version"), "2");
Assert.assertEquals(headers.get("ably-agent"), expectedAblyAgentHeader);
// RSA7e2
Assert.assertNull("Shouldn't include 'x-ably-clientid' if `clientId` is not specified", headers.get("x-ably-clientid"));
} catch (AblyException e) {
e.printStackTrace();
Assert.fail("header_lib_channel_publish: Unexpected exception");
}
}

/**
* The header `X-Ably-ClientId`
* should be included in all REST requests to the Ably endpoint
* if {@link ClientOptions#clientId} is specified
* <p>
* Spec: RSA7e2
* </p>
*/
@Test
public void header_client_id_on_channel_publish() {
try {
/* Init values for local server */
ClientOptions opts = createOptions(testVars.keys[0].keyStr);
opts.environment = null;
opts.tls = false;
opts.port = server.getListeningPort();
opts.restHost = "localhost";
opts.clientId = "test client";
AblyRest ably = new AblyRest(opts);

/* Publish message */
String messageName = "test message";
String messageData = String.valueOf(System.currentTimeMillis());

Channel channel = ably.channels.get("test");
channel.publish(messageName, messageData);

/* Get last headers */
Map<String, String> headers = server.getHeaders();
Assert.assertEquals(headers.get("x-ably-clientid"), /* Base64Coder.encodeString("test client") */ "dGVzdCBjbGllbnQ=");
} catch (AblyException e) {
e.printStackTrace();
Assert.fail("header_client_id_on_channel_publish: Unexpected exception");
}
}

private static class SessionHandlerNanoHTTPD extends NanoHTTPD {
Map<String, String> requestHeaders;

Expand Down

0 comments on commit c2c4125

Please sign in to comment.