Skip to content

Commit 2623693

Browse files
[Java] Fix skip negotiate null ref (#30574)
1 parent 1893fba commit 2623693

File tree

4 files changed

+70
-3
lines changed

4 files changed

+70
-3
lines changed

src/SignalR/build.cmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
@ECHO OFF
22
SET RepoRoot=%~dp0..\..
3-
%RepoRoot%\eng\build.cmd -buildJava -projects %~dp0**\*.*proj %*
3+
%RepoRoot%\eng\build.cmd -nobuildnative -buildJava -projects %~dp0**\*.*proj %*

src/SignalR/clients/java/signalr/core/src/main/java/com/microsoft/signalr/HttpHubConnectionBuilder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ public HttpHubConnectionBuilder withHubProtocol(HubProtocol protocol) {
6969

7070
/**
7171
* Indicates to the {@link HubConnection} that it should skip the negotiate process.
72-
* Note: This option only works with the Websockets transport and the Azure SignalR Service require the negotiate step.
72+
* Note: This option only works with the {@link TransportEnum#WEBSOCKETS} transport selected via {@link #withTransport(TransportEnum) withTransport},
73+
* additionally the Azure SignalR Service requires the negotiate step so this will fail when using the Azure SignalR Service.
7374
*
7475
* @param skipNegotiate Boolean indicating if the {@link HubConnection} should skip the negotiate step.
7576
* @return This instance of the HttpHubConnectionBuilder.

src/SignalR/clients/java/signalr/core/src/main/java/com/microsoft/signalr/HubConnection.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,16 @@ public Completable start() {
264264
Transport transport = customTransport;
265265
if (transport == null) {
266266
Single<String> tokenProvider = negotiateResponse.getAccessToken() != null ? Single.just(negotiateResponse.getAccessToken()) : accessTokenProvider;
267-
switch (negotiateResponse.getChosenTransport()) {
267+
TransportEnum chosenTransport;
268+
if (this.skipNegotiate) {
269+
if (this.transportEnum != TransportEnum.WEBSOCKETS) {
270+
throw new RuntimeException("Negotiation can only be skipped when using the WebSocket transport directly with '.withTransport(TransportEnum.WEBSOCKETS)' on the 'HubConnectionBuilder'.");
271+
}
272+
chosenTransport = this.transportEnum;
273+
} else {
274+
chosenTransport = negotiateResponse.getChosenTransport();
275+
}
276+
switch (chosenTransport) {
268277
case LONG_POLLING:
269278
transport = new LongPollingTransport(localHeaders, httpClient, tokenProvider);
270279
break;

src/SignalR/clients/java/signalr/test/src/main/java/com/microsoft/signalr/HubConnectionTest.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2743,6 +2743,63 @@ public void noConnectionIdWhenSkippingNegotiate() {
27432743
assertNull(hubConnection.getConnectionId());
27442744
}
27452745

2746+
@Test
2747+
public void SkippingNegotiateDoesNotNegotiate() {
2748+
try (TestLogger logger = new TestLogger(WebSocketTransport.class.getName())) {
2749+
AtomicBoolean negotiateCalled = new AtomicBoolean(false);
2750+
TestHttpClient client = new TestHttpClient().on("POST", "http://example.com/negotiate?negotiateVersion=1",
2751+
(req) -> {
2752+
negotiateCalled.set(true);
2753+
return Single.just(new HttpResponse(200, "",
2754+
TestUtils.stringToByteBuffer("{\"connectionId\":\"bVOiRPG8-6YiJ6d7ZcTOVQ\",\""
2755+
+ "availableTransports\":[{\"transport\":\"WebSockets\",\"transferFormats\":[\"Text\",\"Binary\"]}]}")));
2756+
});
2757+
2758+
HubConnection hubConnection = HubConnectionBuilder
2759+
.create("http://example")
2760+
.withTransport(TransportEnum.WEBSOCKETS)
2761+
.shouldSkipNegotiate(true)
2762+
.withHttpClient(client)
2763+
.build();
2764+
2765+
try {
2766+
hubConnection.start().timeout(30, TimeUnit.SECONDS).blockingAwait();
2767+
} catch (Exception e) {
2768+
assertEquals("WebSockets isn't supported in testing currently.", e.getMessage());
2769+
}
2770+
assertEquals(HubConnectionState.DISCONNECTED, hubConnection.getConnectionState());
2771+
assertFalse(negotiateCalled.get());
2772+
2773+
logger.assertLog("Starting Websocket connection.");
2774+
}
2775+
}
2776+
2777+
@Test
2778+
public void ThrowsIfSkipNegotiationSetAndTransportIsNotWebSockets() {
2779+
AtomicBoolean negotiateCalled = new AtomicBoolean(false);
2780+
TestHttpClient client = new TestHttpClient().on("POST", "http://example.com/negotiate?negotiateVersion=1",
2781+
(req) -> {
2782+
negotiateCalled.set(true);
2783+
return Single.just(new HttpResponse(200, "",
2784+
TestUtils.stringToByteBuffer("{\"connectionId\":\"bVOiRPG8-6YiJ6d7ZcTOVQ\",\""
2785+
+ "availableTransports\":[{\"transport\":\"WebSockets\",\"transferFormats\":[\"Text\",\"Binary\"]}]}")));
2786+
});
2787+
2788+
HubConnection hubConnection = HubConnectionBuilder
2789+
.create("http://example")
2790+
.shouldSkipNegotiate(true)
2791+
.withHttpClient(client)
2792+
.build();
2793+
2794+
try {
2795+
hubConnection.start().timeout(30, TimeUnit.SECONDS).blockingAwait();
2796+
} catch (Exception e) {
2797+
assertEquals("Negotiation can only be skipped when using the WebSocket transport directly with '.withTransport(TransportEnum.WEBSOCKETS)' on the 'HubConnectionBuilder'.", e.getMessage());
2798+
}
2799+
assertEquals(HubConnectionState.DISCONNECTED, hubConnection.getConnectionState());
2800+
assertFalse(negotiateCalled.get());
2801+
}
2802+
27462803
@Test
27472804
public void connectionIdIsAvailableAfterStart() {
27482805
TestHttpClient client = new TestHttpClient().on("POST", "http://example.com/negotiate?negotiateVersion=1",

0 commit comments

Comments
 (0)