From b0155446abe03e7c28b314234e36ea55a0e80db5 Mon Sep 17 00:00:00 2001 From: Mikael Mengistu Date: Thu, 6 Sep 2018 11:00:05 -0700 Subject: [PATCH 1/3] Use HubException for error in HandShakeResponse (#2914) --- .../aspnet/signalr/HubConnection.java | 2 +- .../src/test/java/HubConnectionTest.java | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/clients/java/signalr/src/main/java/com/microsoft/aspnet/signalr/HubConnection.java b/clients/java/signalr/src/main/java/com/microsoft/aspnet/signalr/HubConnection.java index 34c9619fff..6bea115c1a 100644 --- a/clients/java/signalr/src/main/java/com/microsoft/aspnet/signalr/HubConnection.java +++ b/clients/java/signalr/src/main/java/com/microsoft/aspnet/signalr/HubConnection.java @@ -50,7 +50,7 @@ public class HubConnection { if (handshakeResponse.error != null) { String errorMessage = "Error in handshake " + handshakeResponse.error; logger.log(LogLevel.Error, errorMessage); - throw new Exception(errorMessage); + throw new HubException(errorMessage); } handshakeReceived = true; diff --git a/clients/java/signalr/src/test/java/HubConnectionTest.java b/clients/java/signalr/src/test/java/HubConnectionTest.java index d3780afa76..c45fd8e552 100644 --- a/clients/java/signalr/src/test/java/HubConnectionTest.java +++ b/clients/java/signalr/src/test/java/HubConnectionTest.java @@ -14,6 +14,9 @@ import static org.junit.Assert.*; public class HubConnectionTest { private static final String RECORD_SEPARATOR = "\u001e"; + @Rule + public ExpectedException exceptionRule = ExpectedException.none(); + @Test public void checkHubConnectionState() throws Exception { Transport mockTransport = new MockTransport(); @@ -40,6 +43,18 @@ public class HubConnectionTest { assertEquals(HubConnectionState.DISCONNECTED, hubConnection.getConnectionState()); } + @Test + public void HubConnectionReceiveHandshakeResponseWithError() throws Exception { + exceptionRule.expect(HubException.class); + exceptionRule.expectMessage("Requested protocol 'messagepack' is not available."); + + MockTransport mockTransport = new MockTransport(); + HubConnection hubConnection = new HubConnection("http://example.com", mockTransport); + + hubConnection.start(); + mockTransport.receiveMessage("{\"error\": \"Requested protocol 'messagepack' is not available.\"}" + RECORD_SEPARATOR); + } + @Test public void RegisteringMultipleHandlersAndBothGetTriggered() throws Exception { AtomicReference value = new AtomicReference<>(0.0); @@ -673,9 +688,6 @@ public class HubConnectionTest { assertEquals(HubConnectionState.DISCONNECTED, hubConnection.getConnectionState()); } - @Rule - public ExpectedException exceptionRule = ExpectedException.none(); - @Test public void CannotSendBeforeStart() throws Exception { exceptionRule.expect(HubException.class); From 43cbb9edda3dced4793705f09d4082102b1f9d66 Mon Sep 17 00:00:00 2001 From: Mikael Mengistu Date: Thu, 6 Sep 2018 11:01:12 -0700 Subject: [PATCH 2/3] Unify exceptions for unsupported message types (#2908) --- .../aspnet/signalr/JsonHubProtocol.java | 8 +------ .../src/test/java/JsonHubProtocolTest.java | 21 +++++++++++++++++-- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/clients/java/signalr/src/main/java/com/microsoft/aspnet/signalr/JsonHubProtocol.java b/clients/java/signalr/src/main/java/com/microsoft/aspnet/signalr/JsonHubProtocol.java index 3703afb8d8..a861b3814f 100644 --- a/clients/java/signalr/src/main/java/com/microsoft/aspnet/signalr/JsonHubProtocol.java +++ b/clients/java/signalr/src/main/java/com/microsoft/aspnet/signalr/JsonHubProtocol.java @@ -47,16 +47,10 @@ public class JsonHubProtocol implements HubProtocol { hubMessages.add(new InvocationMessage(target, new Object[] {args})); break; case STREAM_ITEM: - throw new UnsupportedOperationException("Support for streaming is not yet available"); case COMPLETION: - //Don't care yet - break; case STREAM_INVOCATION: - //Don't care yet; - throw new UnsupportedOperationException("Support for streaming is not yet available"); case CANCEL_INVOCATION: - // Not tracking invocations yet - break; + throw new UnsupportedOperationException(String.format("The message type %s is not supported yet.", messageType)); case PING: //Ping hubMessages.add(new PingMessage()); diff --git a/clients/java/signalr/src/test/java/JsonHubProtocolTest.java b/clients/java/signalr/src/test/java/JsonHubProtocolTest.java index faa866700d..9fb72622bd 100644 --- a/clients/java/signalr/src/test/java/JsonHubProtocolTest.java +++ b/clients/java/signalr/src/test/java/JsonHubProtocolTest.java @@ -103,7 +103,7 @@ public class JsonHubProtocolTest { @Test public void ParseSingleUnsupportedStreamItemMessage() { exceptionRule.expect(UnsupportedOperationException.class); - exceptionRule.expectMessage("Support for streaming is not yet available"); + exceptionRule.expectMessage("The message type STREAM_ITEM is not supported yet."); String stringifiedMessage = "{\"type\":2,\"Id\":1,\"Item\":42}\u001E"; HubMessage[] messages = jsonHubProtocol.parseMessages(stringifiedMessage); } @@ -111,8 +111,25 @@ public class JsonHubProtocolTest { @Test public void ParseSingleUnsupportedStreamInvocationMessage() { exceptionRule.expect(UnsupportedOperationException.class); - exceptionRule.expectMessage("Support for streaming is not yet available"); + exceptionRule.expectMessage("The message type STREAM_INVOCATION is not supported yet."); String stringifiedMessage = "{\"type\":4,\"Id\":1,\"target\":\"test\",\"arguments\":[42]}\u001E"; + + HubMessage[] messages = jsonHubProtocol.parseMessages(stringifiedMessage); + } + + @Test + public void ParseSingleUnsupportedCancelInvocationMessage() { + exceptionRule.expect(UnsupportedOperationException.class); + exceptionRule.expectMessage("The message type CANCEL_INVOCATION is not supported yet."); + String stringifiedMessage = "{\"type\":5,\"invocationId\":123}\u001E"; + HubMessage[] messages = jsonHubProtocol.parseMessages(stringifiedMessage); + } + + @Test + public void ParseSingleUnsupportedCompletionMessage() { + exceptionRule.expect(UnsupportedOperationException.class); + exceptionRule.expectMessage("The message type COMPLETION is not supported yet."); + String stringifiedMessage = "{\"type\":3,\"invocationId\":123}\u001E"; HubMessage[] messages = jsonHubProtocol.parseMessages(stringifiedMessage); } From f18c1d71599a1e1d598362b13cde05aef0e4821d Mon Sep 17 00:00:00 2001 From: Mikael Mengistu Date: Thu, 6 Sep 2018 11:49:46 -0700 Subject: [PATCH 3/3] Should skip negotiate in tests that use fake urls (#2921) --- clients/java/signalr/src/test/java/HubConnectionTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clients/java/signalr/src/test/java/HubConnectionTest.java b/clients/java/signalr/src/test/java/HubConnectionTest.java index c45fd8e552..1359bbfee5 100644 --- a/clients/java/signalr/src/test/java/HubConnectionTest.java +++ b/clients/java/signalr/src/test/java/HubConnectionTest.java @@ -49,10 +49,10 @@ public class HubConnectionTest { exceptionRule.expectMessage("Requested protocol 'messagepack' is not available."); MockTransport mockTransport = new MockTransport(); - HubConnection hubConnection = new HubConnection("http://example.com", mockTransport); + HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, true); hubConnection.start(); - mockTransport.receiveMessage("{\"error\": \"Requested protocol 'messagepack' is not available.\"}" + RECORD_SEPARATOR); + mockTransport.receiveMessage("{\"error\":\"Requested protocol 'messagepack' is not available.\"}" + RECORD_SEPARATOR); } @Test