diff --git a/lib/src/main/java/io/ably/lib/http/HttpCore.java b/lib/src/main/java/io/ably/lib/http/HttpCore.java index 6410849ca..e224262c6 100644 --- a/lib/src/main/java/io/ably/lib/http/HttpCore.java +++ b/lib/src/main/java/io/ably/lib/http/HttpCore.java @@ -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; @@ -211,6 +212,7 @@ 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; diff --git a/lib/src/main/java/io/ably/lib/transport/Defaults.java b/lib/src/main/java/io/ably/lib/transport/Defaults.java index 7725afbc2..3b33a7719 100644 --- a/lib/src/main/java/io/ably/lib/transport/Defaults.java +++ b/lib/src/main/java/io/ably/lib/transport/Defaults.java @@ -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 */ diff --git a/lib/src/test/java/io/ably/lib/test/rest/HttpHeaderTest.java b/lib/src/test/java/io/ably/lib/test/rest/HttpHeaderTest.java index eab5cc724..55e47003b 100644 --- a/lib/src/test/java/io/ably/lib/test/rest/HttpHeaderTest.java +++ b/lib/src/test/java/io/ably/lib/test/rest/HttpHeaderTest.java @@ -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} *

- * Spec: RSC7d, G4 + * Spec: RSC7d, G4, RSA7e2 *

*/ @Test @@ -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 + *

+ * Spec: RSA7e2 + *

+ */ + @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 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 requestHeaders;