Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.pusher.client.channel.impl;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.annotations.SerializedName;
import com.pusher.client.AuthorizationFailureException;
import com.pusher.client.Authorizer;
Expand Down Expand Up @@ -65,35 +66,10 @@ else if (event.equals(MEMBER_REMOVED_EVENT)) {
}

@Override
@SuppressWarnings("rawtypes")
public String toSubscribeMessage() {

final String authResponse = getAuthResponse();

try {
final Map authResponseMap = GSON.fromJson(authResponse, Map.class);
final String authKey = (String)authResponseMap.get("auth");
final Object channelData = authResponseMap.get("channel_data");

storeMyUserId(channelData);

final Map<Object, Object> jsonObject = new LinkedHashMap<Object, Object>();
jsonObject.put("event", "pusher:subscribe");

final Map<Object, Object> dataMap = new LinkedHashMap<Object, Object>();
dataMap.put("channel", name);
dataMap.put("auth", authKey);
dataMap.put("channel_data", channelData);

jsonObject.put("data", dataMap);

final String json = GSON.toJson(jsonObject);

return json;
}
catch (final Exception e) {
throw new AuthorizationFailureException("Unable to parse response from Authorizer: " + authResponse, e);
}
String msg = super.toSubscribeMessage();
myUserID = extractUserIdFromChannelData(channelData);
return msg;
}

@Override
Expand Down Expand Up @@ -187,9 +163,24 @@ private static PresenceData extractPresenceDataFrom(final String message) {
}

@SuppressWarnings("rawtypes")
private void storeMyUserId(final Object channelData) {
final Map channelDataMap = GSON.fromJson((String)channelData, Map.class);
myUserID = String.valueOf(channelDataMap.get("user_id"));
private String extractUserIdFromChannelData(final String channelData) {
final Map channelDataMap;
try {
channelDataMap = GSON.fromJson((String)channelData, Map.class);
} catch (final JsonSyntaxException e) {
throw new AuthorizationFailureException("Invalid response from Authorizer: unable to parse channel_data object: " + channelData, e);
}
Object maybeUserId;
try {
maybeUserId = channelDataMap.get("user_id");
} catch (final NullPointerException e) {
throw new AuthorizationFailureException("Invalid response from Authorizer: no user_id key in channel_data object: " + channelData);
}
if (maybeUserId == null) {
throw new AuthorizationFailureException("Invalid response from Authorizer: no user_id key in channel_data object: " + channelData);
}
// user_id can be a string or an integer in the Channels websocket protocol
return String.valueOf(maybeUserId);
}

private class MemberData {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public class PrivateChannelImpl extends ChannelImpl implements PrivateChannel {
private final InternalConnection connection;
private final Authorizer authorizer;

protected String channelData;

public PrivateChannelImpl(final InternalConnection connection, final String channelName,
final Authorizer authorizer, final Factory factory) {
super(channelName, factory);
Expand Down Expand Up @@ -90,13 +92,17 @@ public String toSubscribeMessage() {
try {
final Map authResponseMap = GSON.fromJson(authResponse, Map.class);
final String authKey = (String)authResponseMap.get("auth");
channelData = (String)authResponseMap.get("channel_data");

final Map<Object, Object> jsonObject = new LinkedHashMap<Object, Object>();
jsonObject.put("event", "pusher:subscribe");

final Map<Object, Object> dataMap = new LinkedHashMap<Object, Object>();
dataMap.put("channel", name);
dataMap.put("auth", authKey);
if (channelData != null) {
dataMap.put("channel_data", channelData);
}

jsonObject.put("data", dataMap);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
@RunWith(MockitoJUnitRunner.class)
public class PresenceChannelImplTest extends PrivateChannelImplTest {

private static final String AUTH_RESPONSE = "\"auth\":\"a87fe72c6f36272aa4b1:f9db294eae7\",\"channel_data\":\"{\\\"user_id\\\":\\\"51169fc47abac\\\",\\\"user_info\\\":{\\\"name\\\":\\\"Phil Leggetter\\\",\\\"twitter_id\\\":\\\"@leggetter\\\"}}\"";
private static final String AUTH_RESPONSE = "\"auth\":\"a87fe72c6f36272aa4b1:f9db294eae7\",\"channel_data\":\"{\\\"user_id\\\":\\\"5116a4519575b\\\",\\\"user_info\\\":{\\\"name\\\":\\\"Phil Leggetter\\\",\\\"twitter_id\\\":\\\"@leggetter\\\"}}\"";
private static final String AUTH_RESPONSE_NUMERIC_ID = "\"auth\":\"a87fe72c6f36272aa4b1:f9db294eae7\",\"channel_data\":\"{\\\"user_id\\\":51169,\\\"user_info\\\":{\\\"name\\\":\\\"Phil Leggetter\\\",\\\"twitter_id\\\":\\\"@leggetter\\\"}}\"";
private static final String USER_ID = "5116a4519575b";

Expand Down Expand Up @@ -60,6 +60,14 @@ public void testReturnsCorrectSubscribeMessageWhenNumericId() {
+ AUTH_RESPONSE_NUMERIC_ID + "}}", message);
}

@Test
public void testStoresCorrectUser() {
channel.toSubscribeMessage();
channel.onMessage("pusher_internal:subscription_succeeded",
"{\"event\":\"pusher_internal:subscription_succeeded\",\"data\":\"{\\\"presence\\\":{\\\"count\\\":1,\\\"ids\\\":[\\\"5116a4519575b\\\"],\\\"hash\\\":{\\\"5116a4519575b\\\":{\\\"name\\\":\\\"Phil Leggetter\\\",\\\"twitter_id\\\":\\\"@leggetter\\\"}}}}\",\"channel\":\"presence-myChannel\"}");
assertEquals(USER_ID, ((PresenceChannelImpl)channel).getMe().getId());
}

@Override
@Test
public void testIsSubscribedMethod(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
@RunWith(MockitoJUnitRunner.class)
public class PrivateChannelImplTest extends ChannelImplTest {

private static final String AUTH_TOKEN = "\"auth\":\"a87fe72c6f36272aa4b1:41dce43734b18bb\"";
private static final String AUTH_RESPONSE = "\"auth\":\"a87fe72c6f36272aa4b1:41dce43734b18bb\"";
private static final String AUTH_RESPONSE_WITH_CHANNEL_DATA = "\"auth\":\"a87fe72c6f36272aa4b1:41dce43734b18bb\",\"channel_data\":\"{\\\"user_id\\\":\\\"51169fc47abac\\\"}\"";

@Mock
protected InternalConnection mockConnection;
Expand All @@ -32,7 +33,7 @@ public class PrivateChannelImplTest extends ChannelImplTest {
@Before
public void setUp() {
super.setUp();
when(mockAuthorizer.authorize(eq(getChannelName()), anyString())).thenReturn("{" + AUTH_TOKEN + "}");
when(mockAuthorizer.authorize(eq(getChannelName()), anyString())).thenReturn("{" + AUTH_RESPONSE + "}");
}

@Test
Expand Down Expand Up @@ -72,7 +73,16 @@ public void testPrivateChannelName() {
@Test
@Override
public void testReturnsCorrectSubscribeMessage() {
assertEquals("{\"event\":\"pusher:subscribe\",\"data\":{\"channel\":\"" + getChannelName() + "\"," + AUTH_TOKEN
assertEquals("{\"event\":\"pusher:subscribe\",\"data\":{\"channel\":\"" + getChannelName() + "\"," + AUTH_RESPONSE
+ "}}", channel.toSubscribeMessage());
}

@Test
public void testReturnsCorrectSubscribeMessageWithChannelData() {
when(mockAuthorizer.authorize(eq(getChannelName()), anyString())).thenReturn(
"{" + AUTH_RESPONSE_WITH_CHANNEL_DATA + "}");

assertEquals("{\"event\":\"pusher:subscribe\",\"data\":{\"channel\":\"" + getChannelName() + "\"," + AUTH_RESPONSE_WITH_CHANNEL_DATA
+ "}}", channel.toSubscribeMessage());
}

Expand Down