[Java] Check for 200 status code on negotiate (#3050)

This commit is contained in:
BrennanConroy 2018-10-03 15:07:56 -07:00 committed by GitHub
parent 1ccb889f9c
commit 3f6c5e3435
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 0 deletions

View File

@ -137,6 +137,9 @@ public class HubConnection {
request.setHeaders(this.headers);
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;
try {
negotiateResponse = new NegotiateResponse(response.getContent());

View File

@ -1064,4 +1064,21 @@ class HubConnectionTest {
hubConnection.stop().get(1000, TimeUnit.MILLISECONDS);
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());
}
}