Skip to content

Commit c2c4125

Browse files
authored
Merge pull request #1019 from ably/ECO-4862/add-clientId-header
feat: include `X-Ably-ClientId` for each request (RSA7e2)
2 parents 6b4996e + 7531c34 commit c2c4125

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

lib/src/main/java/io/ably/lib/http/HttpCore.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import io.ably.lib.types.Param;
2929
import io.ably.lib.types.ProxyOptions;
3030
import io.ably.lib.util.AgentHeaderCreator;
31+
import io.ably.lib.util.Base64Coder;
3132
import io.ably.lib.util.Log;
3233
import io.ably.lib.util.PlatformAgentProvider;
3334

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

215217
/* prepare request body */
216218
byte[] body = null;

lib/src/main/java/io/ably/lib/transport/Defaults.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class Defaults {
2222

2323
/* http headers */
2424
public static final String ABLY_PROTOCOL_VERSION_HEADER = "X-Ably-Version";
25+
public static final String ABLY_CLIENT_ID_HEADER = "X-Ably-ClientId";
2526
public static final String ABLY_AGENT_HEADER = "Ably-Agent";
2627

2728
/* Hosts */

lib/src/test/java/io/ably/lib/test/rest/HttpHeaderTest.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public static void tearDown() {
5050
* should be included in all REST requests to the Ably endpoint
5151
* see {@link io.ably.lib.transport.Defaults#ABLY_AGENT_PARAM}
5252
* <p>
53-
* Spec: RSC7d, G4
53+
* Spec: RSC7d, G4, RSA7e2
5454
* </p>
5555
*/
5656
@Test
@@ -83,12 +83,50 @@ public void header_lib_channel_publish() {
8383
Assert.assertNotNull("Expected headers", headers);
8484
Assert.assertEquals(headers.get("x-ably-version"), "2");
8585
Assert.assertEquals(headers.get("ably-agent"), expectedAblyAgentHeader);
86+
// RSA7e2
87+
Assert.assertNull("Shouldn't include 'x-ably-clientid' if `clientId` is not specified", headers.get("x-ably-clientid"));
8688
} catch (AblyException e) {
8789
e.printStackTrace();
8890
Assert.fail("header_lib_channel_publish: Unexpected exception");
8991
}
9092
}
9193

94+
/**
95+
* The header `X-Ably-ClientId`
96+
* should be included in all REST requests to the Ably endpoint
97+
* if {@link ClientOptions#clientId} is specified
98+
* <p>
99+
* Spec: RSA7e2
100+
* </p>
101+
*/
102+
@Test
103+
public void header_client_id_on_channel_publish() {
104+
try {
105+
/* Init values for local server */
106+
ClientOptions opts = createOptions(testVars.keys[0].keyStr);
107+
opts.environment = null;
108+
opts.tls = false;
109+
opts.port = server.getListeningPort();
110+
opts.restHost = "localhost";
111+
opts.clientId = "test client";
112+
AblyRest ably = new AblyRest(opts);
113+
114+
/* Publish message */
115+
String messageName = "test message";
116+
String messageData = String.valueOf(System.currentTimeMillis());
117+
118+
Channel channel = ably.channels.get("test");
119+
channel.publish(messageName, messageData);
120+
121+
/* Get last headers */
122+
Map<String, String> headers = server.getHeaders();
123+
Assert.assertEquals(headers.get("x-ably-clientid"), /* Base64Coder.encodeString("test client") */ "dGVzdCBjbGllbnQ=");
124+
} catch (AblyException e) {
125+
e.printStackTrace();
126+
Assert.fail("header_client_id_on_channel_publish: Unexpected exception");
127+
}
128+
}
129+
92130
private static class SessionHandlerNanoHTTPD extends NanoHTTPD {
93131
Map<String, String> requestHeaders;
94132

0 commit comments

Comments
 (0)