Parse HandshakeResponse (#2656)

This commit is contained in:
Mikael Mengistu 2018-07-25 16:23:25 -07:00 committed by GitHub
parent fd5d27c7cb
commit daed55be36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 226 additions and 77 deletions

View File

@ -1,15 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
import com.google.gson.Gson;
public class DefaultJsonProtocolHandShakeMessage {
private String protocol = "json";
private int version = 1;
private static final String RECORD_SEPARATOR = "\u001e";
public String createHandshakeMessage() {
Gson gson = new Gson();
return gson.toJson(this) + RECORD_SEPARATOR;
}
}

View File

@ -0,0 +1,18 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
import com.google.gson.Gson;
public class HandshakeProtocol {
public static Gson gson = new Gson();
private static final String RECORD_SEPARATOR = "\u001e";
public static String createHandshakeRequestMessage(HandshakeRequestMessage message){
// The handshake request is always in the JSON format
return gson.toJson(message) + RECORD_SEPARATOR;
}
public static HandshakeResponseMessage parseHandshakeResponse(String message){
return gson.fromJson(message, HandshakeResponseMessage.class);
}
}

View File

@ -0,0 +1,12 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
public class HandshakeRequestMessage {
String protocol;
int version;
public HandshakeRequestMessage(String protocol, int version) {
this.protocol = protocol;
this.version = version;
}
}

View File

@ -0,0 +1,14 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
public class HandshakeResponseMessage {
public String error;
public HandshakeResponseMessage() {
this(null);
}
public HandshakeResponseMessage(String error) {
this.error = error;
}
}

View File

@ -15,6 +15,8 @@ public class HubConnection {
private CallbackMap handlers = new CallbackMap();
private HubProtocol protocol;
private Gson gson = new Gson();
private Boolean handshakeReceived = false;
private static final String RECORD_SEPARATOR = "\u001e";
private HubConnectionState connectionState = HubConnectionState.DISCONNECTED;
public HubConnection(String url, Transport transport) {
@ -22,6 +24,22 @@ public class HubConnection {
this.protocol = new JsonHubProtocol();
this.callback = (payload) -> {
if (!handshakeReceived) {
int handshakeLength = payload.indexOf(RECORD_SEPARATOR) + 1;
String handshakeResponseString = payload.substring(0, handshakeLength - 1);
HandshakeResponseMessage handshakeResponse = HandshakeProtocol.parseHandshakeResponse(handshakeResponseString);
if (handshakeResponse.error != null) {
throw new Exception("Error in handshake " + handshakeResponse.error);
}
handshakeReceived = true;
payload = payload.substring(handshakeLength);
// The payload only contained the handshake response so we can return.
if (payload.length() == 0) {
return;
}
}
HubMessage[] messages = protocol.parseMessages(payload);
for (HubMessage message : messages) {
@ -29,7 +47,7 @@ public class HubConnection {
case INVOCATION:
InvocationMessage invocationMessage = (InvocationMessage)message;
if (message != null && handlers.containsKey(invocationMessage.target)) {
ArrayList<Object> args = gson.fromJson((JsonArray)invocationMessage.arguments[0], (new ArrayList<Object>()).getClass());
ArrayList<Object> args = gson.fromJson((JsonArray)invocationMessage.arguments[0], (new ArrayList<>()).getClass());
List<ActionBase> actions = handlers.get(invocationMessage.target);
if (actions != null) {
for (ActionBase action: actions) {
@ -40,10 +58,10 @@ public class HubConnection {
break;
case STREAM_INVOCATION:
case STREAM_ITEM:
throw new UnsupportedOperationException("Streaming is not yet supported");
case CLOSE:
case CANCEL_INVOCATION:
case COMPLETION:
throw new UnsupportedOperationException("The message type " + message.getMessageType() + " is not supported yet.");
case PING:
// We don't need to do anything in the case of a ping message.
// The other message types aren't supported
@ -71,9 +89,11 @@ public class HubConnection {
return connectionState;
}
public void start() throws InterruptedException {
public void start() throws Exception {
transport.setOnReceive(this.callback);
transport.start();
String handshake = HandshakeProtocol.createHandshakeRequestMessage(new HandshakeRequestMessage(protocol.getName(), protocol.getVersion()));
transport.send(handshake);
connectionState = HubConnectionState.CONNECTED;
}

View File

@ -6,6 +6,6 @@ public interface HubProtocol {
int getVersion();
TransferFormat getTransferFormat();
HubMessage[] parseMessages(String message);
String writeMessage(InvocationMessage message);
String writeMessage(HubMessage message);
}

View File

@ -34,10 +34,6 @@ public class JsonHubProtocol implements HubProtocol {
String[] messages = payload.split(RECORD_SEPARATOR);
List<HubMessage> hubMessages = new ArrayList<>();
for (String splitMessage : messages) {
// Empty handshake response "{}". We can ignore it
if (splitMessage.equals("{}")) {
continue;
}
JsonObject jsonMessage = jsonParser.parse(splitMessage).getAsJsonObject();
HubMessageType messageType = HubMessageType.values()[jsonMessage.get("type").getAsInt() -1];
@ -72,7 +68,7 @@ public class JsonHubProtocol implements HubProtocol {
}
@Override
public String writeMessage(InvocationMessage invocationMessage) {
return gson.toJson(invocationMessage) + RECORD_SEPARATOR;
public String writeMessage(HubMessage hubMessage) {
return gson.toJson(hubMessage) + RECORD_SEPARATOR;
}
}

View File

@ -39,7 +39,6 @@ public class WebSocketTransport implements Transport {
public void start() throws InterruptedException {
webSocketClient = createWebSocket();
webSocketClient.connectBlocking();
webSocketClient.send((new DefaultJsonProtocolHandShakeMessage()).createHandshakeMessage());
}
@Override

View File

@ -0,0 +1,31 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
import org.junit.Test;
import static org.junit.Assert.*;
public class HandshakeProtocolTest {
@Test
public void VerifyCreateHandshakerequestMessage() {
HandshakeRequestMessage handshakeRequest = new HandshakeRequestMessage("json", 1);
String result = HandshakeProtocol.createHandshakeRequestMessage(handshakeRequest);
String expectedResult = "{\"protocol\":\"json\",\"version\":1}\u001E";
assertEquals(expectedResult, result);
}
@Test
public void VerifyParseEmptyHandshakeResponseMessage() {
String emptyHandshakeResponse = "{}";
HandshakeResponseMessage hsr = HandshakeProtocol.parseHandshakeResponse(emptyHandshakeResponse);
assertNull(hsr.error);
}
@Test
public void VerifyParseHandshakeResponseMessage() {
String handshakeResponseWithError = "{\"error\": \"Requested protocol \'messagepack\' is not available.\"}";
HandshakeResponseMessage hsr = HandshakeProtocol.parseHandshakeResponse(handshakeResponseWithError);
assertEquals(hsr.error, "Requested protocol 'messagepack' is not available.");
}
}

View File

@ -3,14 +3,17 @@
import org.junit.Test;
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicReference;
import static org.junit.Assert.*;
public class HubConnectionTest {
private static final String RECORD_SEPARATOR = "\u001e";
@Test
public void checkHubConnectionState() throws InterruptedException {
Transport mockTransport = new MockEchoTransport();
public void checkHubConnectionState() throws Exception {
Transport mockTransport = new MockTransport();
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport);
hubConnection.start();
assertEquals(HubConnectionState.CONNECTED, hubConnection.getConnectionState());
@ -22,7 +25,7 @@ public class HubConnectionTest {
@Test
public void RegisteringMultipleHandlersAndBothGetTriggered() throws Exception {
AtomicReference<Double> value = new AtomicReference<>(0.0);
Transport mockTransport = new MockEchoTransport();
MockTransport mockTransport = new MockTransport();
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport);
Action action = () -> value.getAndUpdate((val) -> val + 1);
@ -32,7 +35,14 @@ public class HubConnectionTest {
assertEquals(0.0, value.get(), 0);
hubConnection.start();
hubConnection.send("inc");
String message = mockTransport.getSentMessages()[0];
String expectedHanshakeRequest = "{\"protocol\":\"json\",\"version\":1}" + RECORD_SEPARATOR;
assertEquals(expectedHanshakeRequest, message);
mockTransport.receiveMessage("{}" + RECORD_SEPARATOR);
mockTransport.receiveMessage("{\"type\":1,\"target\":\"inc\",\"arguments\":[]}" + RECORD_SEPARATOR);
// Confirming that our handler was called and that the counter property was incremented.
assertEquals(2, value.get(), 0);
@ -41,7 +51,7 @@ public class HubConnectionTest {
@Test
public void RemoveHandlerByName() throws Exception {
AtomicReference<Double> value = new AtomicReference<>(0.0);
Transport mockTransport = new MockEchoTransport();
MockTransport mockTransport = new MockTransport();
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport);
Action action = () -> value.getAndUpdate((val) -> val + 1);
@ -50,7 +60,13 @@ public class HubConnectionTest {
assertEquals(0.0, value.get(), 0);
hubConnection.start();
hubConnection.send("inc");
String message = mockTransport.getSentMessages()[0];
String expectedHanshakeRequest = "{\"protocol\":\"json\",\"version\":1}" + RECORD_SEPARATOR;
assertEquals(expectedHanshakeRequest, message);
mockTransport.receiveMessage("{}" + RECORD_SEPARATOR);
mockTransport.receiveMessage("{\"type\":1,\"target\":\"inc\",\"arguments\":[]}" + RECORD_SEPARATOR);
// Confirming that our handler was called and that the counter property was incremented.
assertEquals(1, value.get(), 0);
@ -63,7 +79,7 @@ public class HubConnectionTest {
@Test
public void AddAndRemoveHandlerImmediately() throws Exception {
AtomicReference<Double> value = new AtomicReference<>(0.0);
Transport mockTransport = new MockEchoTransport();
MockTransport mockTransport = new MockTransport();
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport);
Action action = () -> value.getAndUpdate((val) -> val + 1);
@ -73,7 +89,13 @@ public class HubConnectionTest {
assertEquals(0.0, value.get(), 0);
hubConnection.start();
hubConnection.send("inc");
String message = mockTransport.getSentMessages()[0];
String expectedHanshakeRequest = "{\"protocol\":\"json\",\"version\":1}" + RECORD_SEPARATOR;
assertEquals(expectedHanshakeRequest, message);
mockTransport.receiveMessage("{}" + RECORD_SEPARATOR);
mockTransport.receiveMessage("{\"type\":1,\"target\":\"inc\",\"arguments\":[]}" + RECORD_SEPARATOR);
// Confirming that the handler was removed.
assertEquals(0, value.get(), 0);
@ -82,7 +104,7 @@ public class HubConnectionTest {
@Test
public void RemovingMultipleHandlersWithOneCallToRemove() throws Exception {
AtomicReference<Double> value = new AtomicReference<>(0.0);
Transport mockTransport = new MockEchoTransport();
MockTransport mockTransport = new MockTransport();
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport);
Action action = () -> value.getAndUpdate((val) -> val + 1);
Action secondAction = () -> value.getAndUpdate((val) -> val + 2);
@ -93,12 +115,19 @@ public class HubConnectionTest {
assertEquals(0.0, value.get(), 0);
hubConnection.start();
hubConnection.send("inc");
String message = mockTransport.getSentMessages()[0];
String expectedHanshakeRequest = "{\"protocol\":\"json\",\"version\":1}" + RECORD_SEPARATOR;
assertEquals(expectedHanshakeRequest, message);
mockTransport.receiveMessage("{}" + RECORD_SEPARATOR);
mockTransport.receiveMessage("{\"type\":1,\"target\":\"inc\",\"arguments\":[]}" + RECORD_SEPARATOR);
assertEquals(3, value.get(), 0);
hubConnection.remove("inc");
hubConnection.send("inc");
mockTransport.receiveMessage("{\"type\":1,\"target\":\"inc\",\"arguments\":[]}" + RECORD_SEPARATOR);
// Confirm that another invocation doesn't change anything because the handlers have been removed.
assertEquals(3, value.get(), 0);
@ -107,7 +136,7 @@ public class HubConnectionTest {
@Test
public void RemoveHandlerWithUnsubscribe() throws Exception {
AtomicReference<Double> value = new AtomicReference<>(0.0);
Transport mockTransport = new MockEchoTransport();
MockTransport mockTransport = new MockTransport();
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport);
Action action = () -> value.getAndUpdate((val) -> val + 1);
@ -116,20 +145,26 @@ public class HubConnectionTest {
assertEquals(0.0, value.get(), 0);
hubConnection.start();
hubConnection.send("inc");
String message = mockTransport.getSentMessages()[0];
String expectedHanshakeRequest = "{\"protocol\":\"json\",\"version\":1}" + RECORD_SEPARATOR;
assertEquals(expectedHanshakeRequest, message);
mockTransport.receiveMessage("{}" + RECORD_SEPARATOR);
mockTransport.receiveMessage("{\"type\":1,\"target\":\"inc\",\"arguments\":[]}" + RECORD_SEPARATOR);
// Confirming that our handler was called and that the counter property was incremented.
assertEquals(1, value.get(), 0);
subscription.unsubscribe();
hubConnection.send("inc");
mockTransport.receiveMessage("{\"type\":1,\"target\":\"inc\",\"arguments\":[]}" + RECORD_SEPARATOR);
assertEquals(1, value.get(), 0);
}
@Test
public void UnsubscribeTwice() throws Exception {
AtomicReference<Double> value = new AtomicReference<>(0.0);
Transport mockTransport = new MockEchoTransport();
MockTransport mockTransport = new MockTransport();
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport);
Action action = () -> value.getAndUpdate((val) -> val + 1);
@ -138,21 +173,27 @@ public class HubConnectionTest {
assertEquals(0.0, value.get(), 0);
hubConnection.start();
hubConnection.send("inc");
String message = mockTransport.getSentMessages()[0];
String expectedHanshakeRequest = "{\"protocol\":\"json\",\"version\":1}" + RECORD_SEPARATOR;
assertEquals(expectedHanshakeRequest, message);
mockTransport.receiveMessage("{}" + RECORD_SEPARATOR);
mockTransport.receiveMessage("{\"type\":1,\"target\":\"inc\",\"arguments\":[]}" + RECORD_SEPARATOR);
// Confirming that our handler was called and that the counter property was incremented.
assertEquals(1, value.get(), 0);
subscription.unsubscribe();
subscription.unsubscribe();
hubConnection.send("inc");
mockTransport.receiveMessage("{\"type\":1,\"target\":\"inc\",\"arguments\":[]}" + RECORD_SEPARATOR);
assertEquals(1, value.get(), 0);
}
@Test
public void RemoveSingleHandlerWithUnsubscribe() throws Exception {
AtomicReference<Double> value = new AtomicReference<>(0.0);
Transport mockTransport = new MockEchoTransport();
MockTransport mockTransport = new MockTransport();
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport);
Action action = () -> value.getAndUpdate((val) -> val + 1);
Action secondAction = () -> value.getAndUpdate((val) -> val + 2);
@ -163,21 +204,26 @@ public class HubConnectionTest {
assertEquals(0.0, value.get(), 0);
hubConnection.start();
hubConnection.send("inc");
String message = mockTransport.getSentMessages()[0];
String expectedHanshakeRequest = "{\"protocol\":\"json\",\"version\":1}" + RECORD_SEPARATOR;
assertEquals(expectedHanshakeRequest, message);
mockTransport.receiveMessage("{}" + RECORD_SEPARATOR);
mockTransport.receiveMessage("{\"type\":1,\"target\":\"inc\",\"arguments\":[]}" + RECORD_SEPARATOR);
// Confirming that our handler was called and that the counter property was incremented.
assertEquals(3, value.get(), 0);
// This removes the first handler so when "inc" is invoked secondAction should still run.
subscription.unsubscribe();
hubConnection.send("inc");
mockTransport.receiveMessage("{\"type\":1,\"target\":\"inc\",\"arguments\":[]}" + RECORD_SEPARATOR);
assertEquals(5, value.get(), 0);
}
@Test
public void AddAndRemoveHandlerImmediatelyWithSubscribe() throws Exception {
AtomicReference<Double> value = new AtomicReference<>(0.0);
Transport mockTransport = new MockEchoTransport();
MockTransport mockTransport = new MockTransport();
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport);
Action action = () -> value.getAndUpdate((val) -> val + 1);
@ -187,8 +233,8 @@ public class HubConnectionTest {
assertEquals(0.0, value.get(), 0);
hubConnection.start();
hubConnection.send("inc");
mockTransport.receiveMessage("{}" + RECORD_SEPARATOR);
mockTransport.receiveMessage("{\"type\":1,\"target\":\"inc\",\"arguments\":[]}" + RECORD_SEPARATOR);
// Confirming that the handler was removed.
assertEquals(0, value.get(), 0);
}
@ -196,7 +242,7 @@ public class HubConnectionTest {
@Test
public void RegisteringMultipleHandlersThatTakeParamsAndBothGetTriggered() throws Exception {
AtomicReference<Double> value = new AtomicReference<>(0.0);
Transport mockTransport = new MockEchoTransport();
MockTransport mockTransport = new MockTransport();
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport);
Action1<Double> action = (number) -> value.getAndUpdate((val) -> val + number);
@ -206,6 +252,8 @@ public class HubConnectionTest {
assertEquals(0, value.get(), 0);
hubConnection.start();
mockTransport.receiveMessage("{}" + RECORD_SEPARATOR);
mockTransport.receiveMessage("{\"type\":1,\"target\":\"add\",\"arguments\":[12]}" + RECORD_SEPARATOR);
hubConnection.send("add", 12);
// Confirming that our handler was called and the correct message was passed in.
@ -216,7 +264,7 @@ public class HubConnectionTest {
@Test
public void SendWithNoParamsTriggersOnHandler() throws Exception {
AtomicReference<Double> value = new AtomicReference<Double>(0.0);
Transport mockTransport = new MockEchoTransport();
MockTransport mockTransport = new MockTransport();
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport);
hubConnection.on("inc", () ->{
@ -225,7 +273,8 @@ public class HubConnectionTest {
});
hubConnection.start();
hubConnection.send("inc");
mockTransport.receiveMessage("{}" + RECORD_SEPARATOR);
mockTransport.receiveMessage("{\"type\":1,\"target\":\"inc\",\"arguments\":[]}" + RECORD_SEPARATOR);
// Confirming that our handler was called and that the counter property was incremented.
assertEquals(1, value.get(), 0);
@ -234,7 +283,7 @@ public class HubConnectionTest {
@Test
public void SendWithParamTriggersOnHandler() throws Exception {
AtomicReference<String> value = new AtomicReference<>();
Transport mockTransport = new MockEchoTransport();
MockTransport mockTransport = new MockTransport();
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport);
hubConnection.on("inc", (param) ->{
@ -243,6 +292,8 @@ public class HubConnectionTest {
}, String.class);
hubConnection.start();
mockTransport.receiveMessage("{}" + RECORD_SEPARATOR);
mockTransport.receiveMessage("{\"type\":1,\"target\":\"inc\",\"arguments\":[\"Hello World\"]}" + RECORD_SEPARATOR);
hubConnection.send("inc", "Hello World");
// Confirming that our handler was called and the correct message was passed in.
@ -254,7 +305,7 @@ public class HubConnectionTest {
AtomicReference<String> value1 = new AtomicReference<>();
AtomicReference<Double> value2 = new AtomicReference<>();
Transport mockTransport = new MockEchoTransport();
MockTransport mockTransport = new MockTransport();
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport);
hubConnection.on("inc", (param1, param2) ->{
@ -266,6 +317,8 @@ public class HubConnectionTest {
}, String.class, Double.class);
hubConnection.start();
mockTransport.receiveMessage("{}" + RECORD_SEPARATOR);
mockTransport.receiveMessage("{\"type\":1,\"target\":\"inc\",\"arguments\":[\"Hello World\", 12]}" + RECORD_SEPARATOR);
hubConnection.send("inc", "Hello World", 12);
// Confirming that our handler was called and the correct message was passed in.
@ -279,7 +332,7 @@ public class HubConnectionTest {
AtomicReference<String> value2 = new AtomicReference<>();
AtomicReference<String> value3 = new AtomicReference<>();
Transport mockTransport = new MockEchoTransport();
MockTransport mockTransport = new MockTransport();
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport);
hubConnection.on("inc", (param1, param2, param3) ->{
@ -293,6 +346,8 @@ public class HubConnectionTest {
}, String.class, String.class, String.class);
hubConnection.start();
mockTransport.receiveMessage("{}" + RECORD_SEPARATOR);
mockTransport.receiveMessage("{\"type\":1,\"target\":\"inc\",\"arguments\":[\"A\", \"B\", \"C\"]}" + RECORD_SEPARATOR);
hubConnection.send("inc", "A", "B", "C");
// Confirming that our handler was called and the correct message was passed in.
@ -308,7 +363,7 @@ public class HubConnectionTest {
AtomicReference<String> value3 = new AtomicReference<>();
AtomicReference<String> value4 = new AtomicReference<>();
Transport mockTransport = new MockEchoTransport();
MockTransport mockTransport = new MockTransport();
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport);
hubConnection.on("inc", (param1, param2, param3, param4) ->{
@ -324,7 +379,8 @@ public class HubConnectionTest {
}, String.class, String.class, String.class, String.class);
hubConnection.start();
hubConnection.send("inc", "A", "B", "C", "D");
mockTransport.receiveMessage("{}" + RECORD_SEPARATOR);
mockTransport.receiveMessage("{\"type\":1,\"target\":\"inc\",\"arguments\":[\"A\", \"B\", \"C\", \"D\"]}" + RECORD_SEPARATOR);
// Confirming that our handler was called and the correct message was passed in.
assertEquals("A", value1.get());
@ -341,7 +397,7 @@ public class HubConnectionTest {
AtomicReference<Boolean> value4 = new AtomicReference<>();
AtomicReference<Double> value5 = new AtomicReference<>();
Transport mockTransport = new MockEchoTransport();
MockTransport mockTransport = new MockTransport();
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport);
hubConnection.on("inc", (param1, param2, param3, param4, param5) ->{
@ -359,7 +415,8 @@ public class HubConnectionTest {
}, String.class, String.class, String.class, Boolean.class, Double.class);
hubConnection.start();
hubConnection.send("inc", "A", "B", "C", true, 12.0);
mockTransport.receiveMessage("{}" + RECORD_SEPARATOR);
mockTransport.receiveMessage("{\"type\":1,\"target\":\"inc\",\"arguments\":[\"A\", \"B\", \"C\",true,12 ]}" + RECORD_SEPARATOR);
// Confirming that our handler was called and the correct message was passed in.
assertEquals("A", value1.get());
@ -369,15 +426,38 @@ public class HubConnectionTest {
assertEquals(12, value5.get(), 0);
}
private class MockEchoTransport implements Transport {
@Test
public void ReceiveHandshakeResponseAndMessage() throws Exception {
AtomicReference<Double> value = new AtomicReference<Double>(0.0);
MockTransport transport = new MockTransport();
HubConnection hubConnection = new HubConnection("http://example.com", transport);
hubConnection.on("inc", () ->{
assertEquals(0.0, value.get(), 0);
value.getAndUpdate((val) -> val + 1);
});
// On start we're going to receive the handshake response and also an invocation in the same payload.
hubConnection.start();
String expectedSentMessage = "{\"protocol\":\"json\",\"version\":1}" + RECORD_SEPARATOR;
assertEquals(expectedSentMessage, transport.getSentMessages()[0]);
transport.receiveMessage("{}" + RECORD_SEPARATOR + "{\"type\":1,\"target\":\"inc\",\"arguments\":[]}" + RECORD_SEPARATOR);
// Confirming that our handler was called and that the counter property was incremented.
assertEquals(1, value.get(), 0);
}
private class MockTransport implements Transport {
private OnReceiveCallBack onReceiveCallBack;
private ArrayList<String> sentMessages = new ArrayList<>();
@Override
public void start() {}
@Override
public void send(String message) throws Exception {
this.onReceive(message);
public void send(String message) {
sentMessages.add(message);
}
@Override
@ -391,6 +471,14 @@ public class HubConnectionTest {
}
@Override
public void stop() {return;}
public void stop() {}
public void receiveMessage(String message) throws Exception {
this.onReceive(message);
}
public String[] getSentMessages(){
return sentMessages.toArray(new String[sentMessages.size()]);
}
}
}

View File

@ -10,7 +10,6 @@ import static org.junit.Assert.*;
public class JsonHubProtocolTest {
private JsonHubProtocol jsonHubProtocol = new JsonHubProtocol();
private static final String RECORD_SEPARATOR = "\u001e";
@Test
public void checkProtocolName() {
@ -84,19 +83,6 @@ public class JsonHubProtocolTest {
HubMessage[] messages = jsonHubProtocol.parseMessages(stringifiedMessage);
}
@Test
public void ParseHandshakeResponsePlusMessage() {
String twoMessages = "{}\u001E{\"type\":1,\"target\":\"test\",\"arguments\":[42]}\u001E";
HubMessage[] messages = jsonHubProtocol.parseMessages(twoMessages);
assertEquals(HubMessageType.INVOCATION, messages[0].getMessageType());
//We ignore the Handshake response for now and we can cast because we know we have in invocation message.
InvocationMessage message = (InvocationMessage) messages[0];
assertEquals("test", message.target);
JsonArray messageResult = (JsonArray) message.arguments[0];
assertEquals(42, messageResult.getAsInt());
}
@Test
public void ParseTwoMessages() {
String twoMessages = "{\"type\":1,\"target\":\"one\",\"arguments\":[42]}\u001E{\"type\":1,\"target\":\"two\",\"arguments\":[43]}\u001E";