[Java] Check for 200 status code on negotiate (#3050)
This commit is contained in:
parent
1ccb889f9c
commit
3f6c5e3435
|
|
@ -137,6 +137,9 @@ public class HubConnection {
|
||||||
request.setHeaders(this.headers);
|
request.setHeaders(this.headers);
|
||||||
|
|
||||||
return httpClient.post(Negotiate.resolveNegotiateUrl(url), request).thenCompose((response) -> {
|
return httpClient.post(Negotiate.resolveNegotiateUrl(url), request).thenCompose((response) -> {
|
||||||
|
if (response.getStatusCode() != 200) {
|
||||||
|
throw new RuntimeException(String.format("Unexpected status code returned from negotiate: %d %s.", response.getStatusCode(), response.getStatusText()));
|
||||||
|
}
|
||||||
NegotiateResponse negotiateResponse;
|
NegotiateResponse negotiateResponse;
|
||||||
try {
|
try {
|
||||||
negotiateResponse = new NegotiateResponse(response.getContent());
|
negotiateResponse = new NegotiateResponse(response.getContent());
|
||||||
|
|
|
||||||
|
|
@ -1064,4 +1064,21 @@ class HubConnectionTest {
|
||||||
hubConnection.stop().get(1000, TimeUnit.MILLISECONDS);
|
hubConnection.stop().get(1000, TimeUnit.MILLISECONDS);
|
||||||
assertEquals(HubConnectionState.DISCONNECTED, hubConnection.getConnectionState());
|
assertEquals(HubConnectionState.DISCONNECTED, hubConnection.getConnectionState());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void non200FromNegotiateThrowsError() {
|
||||||
|
TestHttpClient client = new TestHttpClient()
|
||||||
|
.on("POST", "http://example.com/negotiate",
|
||||||
|
(req) -> CompletableFuture
|
||||||
|
.completedFuture(new HttpResponse(500, "Internal server error", "")));
|
||||||
|
|
||||||
|
MockTransport transport = new MockTransport();
|
||||||
|
HttpConnectionOptions options = new HttpConnectionOptions();
|
||||||
|
options.setTransport(transport);
|
||||||
|
HubConnection hubConnection = new HubConnectionBuilder().withUrl("http://example.com", options)
|
||||||
|
.configureHttpClient(client).build();
|
||||||
|
|
||||||
|
ExecutionException exception = assertThrows(ExecutionException.class, () -> hubConnection.start().get(1000, TimeUnit.MILLISECONDS));
|
||||||
|
assertEquals("Unexpected status code returned from negotiate: 500 Internal server error.", exception.getCause().getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue