Skip to content

Commit

Permalink
Merge pull request #630 from ably/add-connection-key
Browse files Browse the repository at this point in the history
Add connectionKey attribute missing from the Message object
  • Loading branch information
QuintinWillison authored Nov 16, 2020
2 parents 7fc5dd3 + cc1df58 commit 0e4da0f
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 14 deletions.
3 changes: 1 addition & 2 deletions lib/src/main/java/io/ably/lib/types/BaseMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ public class BaseMessage implements Cloneable {
private static final String DATA = "data";

/**
* Generate a String summary of this BaseMessage
* @return string
* Generate a String summary of this BaseMessage.
*/
public void getDetails(StringBuilder builder) {
if(clientId != null)
Expand Down
46 changes: 35 additions & 11 deletions lib/src/main/java/io/ably/lib/types/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,23 @@
public class Message extends BaseMessage {

/**
* The event name, if available
* The event name, if available.
*/
public String name;

/**
* Extras, if available
* Extras, if available.
*/
public MessageExtras extras;

/**
* Key needed only in case one client is publishing this message on behalf of another client.
*/
public String connectionKey;

private static final String NAME = "name";
private static final String EXTRAS = "extras";
private static final String CONNECTION_KEY = "connectionKey";

/**
* Default constructor
Expand All @@ -44,29 +50,44 @@ public Message() {
}

/**
* Construct a message from event name and data
* @param name
* @param data
* Construct a message from event name and data.
*
* @param name the event name
* @param data the message payload
*/
public Message(String name, Object data) {
this(name, data, null, null);
}


/**
* Construct a message from name, data, and client id.
*
* @param name the event name
* @param data the message payload
* @param clientId the client identifier
*/
public Message(String name, Object data, String clientId) {
this(name, data, clientId, null);
}

/**
* Construct a message from name, data, and extras.
*
* @param name the event name
* @param data the message payload
* @param extras extra information to be sent with this message
*/
public Message(String name, Object data, MessageExtras extras) {
this(name, data, null, extras);
}

/**
* Generic constructor
* @param name
* @param data
* @param clientId
* @param extras
* Construct a message from name, data, client id, and extras.
*
* @param name the event name
* @param data the message payload
* @param clientId the client identifier
* @param extras extra information to be sent with this message
*/
public Message(String name, Object data, String clientId, MessageExtras extras) {
this.name = name;
Expand Down Expand Up @@ -273,6 +294,9 @@ public JsonElement serialize(Message message, Type typeOfMessage, JsonSerializat
if (message.extras != null) {
json.add(EXTRAS, Serialisation.gson.toJsonTree(message.extras));
}
if (message.connectionKey != null) {
json.addProperty(CONNECTION_KEY, message.connectionKey);
}
return json;
}

Expand Down
26 changes: 25 additions & 1 deletion lib/src/test/java/io/ably/lib/test/rest/RestRequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ public void onError(ErrorInfo reason) {
/**
* Publish a message using the request() API
* Spec: RSC19a, RSC19b
*
*
*/
@Test
public void request_post() {
Expand Down Expand Up @@ -717,4 +717,28 @@ public void onError(ErrorInfo reason) {
}
}

/**
* Trying to publish a message on behalf of another client with invalid connection key.
* Specs: TM2h
*/
@Test
public void request_post_with_invalid_connection_key() throws AblyException {
// Given
DebugOptions opts = new DebugOptions(testVars.keys[0].keyStr);
fillInOptions(opts);
opts.httpListener = new RawHttpTracker();
AblyRest ably = new AblyRest(opts);
Message message = new Message("Test event", "Test data (invalid key)");
message.connectionKey = "invalid";
HttpUtils.JsonRequestBody requestBody = new HttpUtils.JsonRequestBody(message);

// When
HttpPaginatedResponse publishResponse = ably.request(HttpConstants.Methods.POST, channelMessagesPath, null, requestBody, null);

// Then
assertFalse("Verify failure is indicated", publishResponse.success);
assertNotNull("Verify error is indicated", publishResponse.errorMessage);
assertEquals("Verify statusCode is present", publishResponse.statusCode, 400);
assertEquals("Verify errorCode is present", publishResponse.errorCode, 40006);
}
}
49 changes: 49 additions & 0 deletions lib/src/test/java/io/ably/lib/types/MessageTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.ably.lib.types;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import io.ably.lib.types.Message.Serializer;
import org.junit.Test;

public class MessageTest {

private final Serializer serializer = new Serializer();

@Test
public void serialize_message() {
// Given
Message message = new Message("test-name", "test-data");
message.clientId = "test-client-id";
message.connectionKey = "test-key";

// When
JsonElement serializedElement = serializer.serialize(message, null, null);

// Then
JsonObject serializedObject = serializedElement.getAsJsonObject();
assertEquals("test-client-id", serializedObject.get("clientId").getAsString());
assertEquals("test-key", serializedObject.get("connectionKey").getAsString());
assertEquals("test-data", serializedObject.get("data").getAsString());
assertEquals("test-name", serializedObject.get("name").getAsString());
}

@Test
public void serialize_message_with_name_and_data() {
// Given
Message message = new Message("test-name", "test-data");

// When
JsonElement serializedElement = serializer.serialize(message, null, null);

// Then
JsonObject serializedObject = serializedElement.getAsJsonObject();
assertNull(serializedObject.get("clientId"));
assertNull(serializedObject.get("connectionKey"));
assertNull(serializedObject.get("extras"));
assertEquals("test-data", serializedObject.get("data").getAsString());
assertEquals("test-name", serializedObject.get("name").getAsString());
}
}

0 comments on commit 0e4da0f

Please sign in to comment.