Update HubConnection url in Java client (#11010)
This commit is contained in:
parent
7e92e4c280
commit
eeaba9252f
|
|
@ -29,7 +29,7 @@ public class HubConnection {
|
||||||
private static final List<Class<?>> emptyArray = new ArrayList<>();
|
private static final List<Class<?>> emptyArray = new ArrayList<>();
|
||||||
private static final int MAX_NEGOTIATE_ATTEMPTS = 100;
|
private static final int MAX_NEGOTIATE_ATTEMPTS = 100;
|
||||||
|
|
||||||
private final String baseUrl;
|
private String baseUrl;
|
||||||
private Transport transport;
|
private Transport transport;
|
||||||
private OnReceiveCallBack callback;
|
private OnReceiveCallBack callback;
|
||||||
private final CallbackMap handlers = new CallbackMap();
|
private final CallbackMap handlers = new CallbackMap();
|
||||||
|
|
@ -288,6 +288,27 @@ public class HubConnection {
|
||||||
return hubConnectionState;
|
return hubConnectionState;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// For testing only
|
||||||
|
String getBaseUrl() {
|
||||||
|
return this.baseUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets a new url for the HubConnection.
|
||||||
|
* @param url The url to connect to.
|
||||||
|
*/
|
||||||
|
public void setBaseUrl(String url) {
|
||||||
|
if (url == null || url.isEmpty()) {
|
||||||
|
throw new IllegalArgumentException("The HubConnection url must be a valid url.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hubConnectionState != HubConnectionState.DISCONNECTED) {
|
||||||
|
throw new IllegalStateException("The HubConnection must be in the disconnected state to change the url.");
|
||||||
|
}
|
||||||
|
|
||||||
|
this.baseUrl = url;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Starts a connection to the server.
|
* Starts a connection to the server.
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -106,6 +106,82 @@ class HubConnectionTest {
|
||||||
assertEquals(HubConnectionState.DISCONNECTED, hubConnection.getConnectionState());
|
assertEquals(HubConnectionState.DISCONNECTED, hubConnection.getConnectionState());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void hubConnectionUrlCanBeChanged() {
|
||||||
|
MockTransport mockTransport = new MockTransport();
|
||||||
|
HubConnection hubConnection = TestUtils.createHubConnection("http://example.com", mockTransport);
|
||||||
|
|
||||||
|
hubConnection.start().timeout(1, TimeUnit.SECONDS).blockingAwait();
|
||||||
|
|
||||||
|
assertEquals(HubConnectionState.CONNECTED, hubConnection.getConnectionState());
|
||||||
|
assertEquals("http://example.com", hubConnection.getBaseUrl());
|
||||||
|
|
||||||
|
hubConnection.stop().timeout(1, TimeUnit.SECONDS).blockingAwait();
|
||||||
|
|
||||||
|
assertEquals(HubConnectionState.DISCONNECTED, hubConnection.getConnectionState());
|
||||||
|
|
||||||
|
hubConnection.setBaseUrl("http://newurl.com");
|
||||||
|
hubConnection.start().timeout(1, TimeUnit.SECONDS).blockingAwait();
|
||||||
|
|
||||||
|
assertEquals("http://newurl.com", hubConnection.getBaseUrl());
|
||||||
|
|
||||||
|
hubConnection.stop().timeout(1, TimeUnit.SECONDS).blockingAwait();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void canUpdateUrlInOnClosed() {
|
||||||
|
MockTransport mockTransport = new MockTransport();
|
||||||
|
HubConnection hubConnection = TestUtils.createHubConnection("http://example.com", mockTransport);
|
||||||
|
|
||||||
|
hubConnection.start().timeout(1, TimeUnit.SECONDS).blockingAwait();
|
||||||
|
|
||||||
|
assertEquals(HubConnectionState.CONNECTED, hubConnection.getConnectionState());
|
||||||
|
assertEquals("http://example.com", hubConnection.getBaseUrl());
|
||||||
|
|
||||||
|
hubConnection.onClosed((error) -> {
|
||||||
|
hubConnection.setBaseUrl("http://newurl.com");
|
||||||
|
});
|
||||||
|
|
||||||
|
hubConnection.stop().timeout(1, TimeUnit.SECONDS).blockingAwait();
|
||||||
|
|
||||||
|
assertEquals(HubConnectionState.DISCONNECTED, hubConnection.getConnectionState());
|
||||||
|
|
||||||
|
hubConnection.start().timeout(1, TimeUnit.SECONDS).blockingAwait();
|
||||||
|
|
||||||
|
assertEquals("http://newurl.com", hubConnection.getBaseUrl());
|
||||||
|
|
||||||
|
hubConnection.stop().timeout(1, TimeUnit.SECONDS).blockingAwait();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void changingUrlWhenConnectedThrows() {
|
||||||
|
MockTransport mockTransport = new MockTransport();
|
||||||
|
HubConnection hubConnection = TestUtils.createHubConnection("http://example.com", mockTransport);
|
||||||
|
|
||||||
|
hubConnection.start().timeout(1, TimeUnit.SECONDS).blockingAwait();
|
||||||
|
|
||||||
|
assertEquals(HubConnectionState.CONNECTED, hubConnection.getConnectionState());
|
||||||
|
assertEquals("http://example.com", hubConnection.getBaseUrl());
|
||||||
|
|
||||||
|
|
||||||
|
Throwable exception = assertThrows(IllegalStateException.class, () -> hubConnection.setBaseUrl("http://newurl.com"));
|
||||||
|
assertEquals("The HubConnection must be in the disconnected state to change the url.",exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void settingNewUrlToNullThrows() {
|
||||||
|
MockTransport mockTransport = new MockTransport();
|
||||||
|
HubConnection hubConnection = TestUtils.createHubConnection("http://example.com", mockTransport);
|
||||||
|
|
||||||
|
hubConnection.start().timeout(1, TimeUnit.SECONDS).blockingAwait();
|
||||||
|
|
||||||
|
assertEquals(HubConnectionState.CONNECTED, hubConnection.getConnectionState());
|
||||||
|
assertEquals("http://example.com", hubConnection.getBaseUrl());
|
||||||
|
|
||||||
|
Throwable exception = assertThrows(IllegalArgumentException.class, () -> hubConnection.setBaseUrl(null));
|
||||||
|
assertEquals("The HubConnection url must be a valid url.",exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void invalidHandShakeResponse() {
|
public void invalidHandShakeResponse() {
|
||||||
MockTransport mockTransport = new MockTransport(false);
|
MockTransport mockTransport = new MockTransport(false);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue