NC-1378: Adding error handling to http requests on websocket endpoint (#213)

Lucas Saldanha 6 years ago committed by GitHub
parent 7f78c6e62a
commit 8354acd071
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/websocket/WebSocketService.java
  2. 77
      ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/websocket/WebSocketServiceTest.java

@ -22,6 +22,7 @@ import io.vertx.core.Handler;
import io.vertx.core.Vertx; import io.vertx.core.Vertx;
import io.vertx.core.http.HttpServer; import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpServerOptions; import io.vertx.core.http.HttpServerOptions;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.http.ServerWebSocket; import io.vertx.core.http.ServerWebSocket;
import io.vertx.core.net.SocketAddress; import io.vertx.core.net.SocketAddress;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
@ -62,6 +63,7 @@ public class WebSocketService {
.setPort(configuration.getPort()) .setPort(configuration.getPort())
.setWebsocketSubProtocols("undefined")) .setWebsocketSubProtocols("undefined"))
.websocketHandler(websocketHandler()) .websocketHandler(websocketHandler())
.requestHandler(httpHandler())
.listen(startHandler(resultFuture)); .listen(startHandler(resultFuture));
return resultFuture; return resultFuture;
@ -103,6 +105,11 @@ public class WebSocketService {
}; };
} }
private Handler<HttpServerRequest> httpHandler() {
return http ->
http.response().setStatusCode(400).end("Websocket endpoint can't handle HTTP requests");
}
private Handler<AsyncResult<HttpServer>> startHandler(final CompletableFuture<?> resultFuture) { private Handler<AsyncResult<HttpServer>> startHandler(final CompletableFuture<?> resultFuture) {
return res -> { return res -> {
if (res.succeeded()) { if (res.succeeded()) {

@ -25,6 +25,8 @@ import java.util.Map;
import io.vertx.core.Vertx; import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer; import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpClient;
import io.vertx.core.http.HttpClientOptions;
import io.vertx.core.http.HttpServerOptions; import io.vertx.core.http.HttpServerOptions;
import io.vertx.core.http.WebSocketBase; import io.vertx.core.http.WebSocketBase;
import io.vertx.ext.unit.Async; import io.vertx.ext.unit.Async;
@ -44,6 +46,7 @@ public class WebSocketServiceTest {
private WebSocketConfiguration websocketConfiguration; private WebSocketConfiguration websocketConfiguration;
private WebSocketRequestHandler webSocketRequestHandlerSpy; private WebSocketRequestHandler webSocketRequestHandlerSpy;
private WebSocketService websocketService; private WebSocketService websocketService;
private HttpClient httpClient;
@Before @Before
public void before() { public void before() {
@ -61,6 +64,13 @@ public class WebSocketServiceTest {
websocketService.start().join(); websocketService.start().join();
websocketConfiguration.setPort(websocketService.socketAddress().getPort()); websocketConfiguration.setPort(websocketService.socketAddress().getPort());
HttpClientOptions httpClientOptions =
new HttpClientOptions()
.setDefaultHost(websocketConfiguration.getHost())
.setDefaultPort(websocketConfiguration.getPort());
httpClient = vertx.createHttpClient(httpClientOptions);
} }
@After @After
@ -76,21 +86,17 @@ public class WebSocketServiceTest {
final String request = "{\"id\": 1, \"method\": \"eth_subscribe\", \"params\": [\"syncing\"]}"; final String request = "{\"id\": 1, \"method\": \"eth_subscribe\", \"params\": [\"syncing\"]}";
final String expectedResponse = "{\"jsonrpc\":\"2.0\",\"id\":1,\"result\":\"0x1\"}"; final String expectedResponse = "{\"jsonrpc\":\"2.0\",\"id\":1,\"result\":\"0x1\"}";
vertx httpClient.websocket(
.createHttpClient() "/",
.websocket( webSocket -> {
websocketConfiguration.getPort(), webSocket.write(Buffer.buffer(request));
websocketConfiguration.getHost(),
"/",
webSocket -> {
webSocket.write(Buffer.buffer(request));
webSocket.handler( webSocket.handler(
buffer -> { buffer -> {
context.assertEquals(expectedResponse, buffer.toString()); context.assertEquals(expectedResponse, buffer.toString());
async.complete(); async.complete();
}); });
}); });
async.awaitSuccess(VERTX_AWAIT_TIMEOUT_MILLIS); async.awaitSuccess(VERTX_AWAIT_TIMEOUT_MILLIS);
} }
@ -107,15 +113,7 @@ public class WebSocketServiceTest {
context.assertNotNull(m.body()); context.assertNotNull(m.body());
async.complete(); async.complete();
}) })
.completionHandler( .completionHandler(v -> httpClient.websocket("/", WebSocketBase::close));
v ->
vertx
.createHttpClient()
.websocket(
websocketConfiguration.getPort(),
websocketConfiguration.getHost(),
"/",
WebSocketBase::close));
async.awaitSuccess(VERTX_AWAIT_TIMEOUT_MILLIS); async.awaitSuccess(VERTX_AWAIT_TIMEOUT_MILLIS);
} }
@ -127,16 +125,35 @@ public class WebSocketServiceTest {
final byte[] bigMessage = new byte[HttpServerOptions.DEFAULT_MAX_WEBSOCKET_MESSAGE_SIZE + 1]; final byte[] bigMessage = new byte[HttpServerOptions.DEFAULT_MAX_WEBSOCKET_MESSAGE_SIZE + 1];
Arrays.fill(bigMessage, (byte) 1); Arrays.fill(bigMessage, (byte) 1);
vertx httpClient.websocket(
.createHttpClient() "/",
.websocket( webSocket -> {
webSocket.write(Buffer.buffer(bigMessage));
webSocket.closeHandler(v -> async.complete());
});
async.awaitSuccess(VERTX_AWAIT_TIMEOUT_MILLIS);
}
@Test
public void websocketServiceMustReturnErrorOnHttpRequest(final TestContext context) {
final Async async = context.async();
httpClient
.post(
websocketConfiguration.getPort(), websocketConfiguration.getPort(),
websocketConfiguration.getHost(), websocketConfiguration.getHost(),
"/", "/",
webSocket -> { response ->
webSocket.write(Buffer.buffer(bigMessage)); response.bodyHandler(
webSocket.closeHandler(v -> async.complete()); b -> {
}); context
.assertEquals(400, response.statusCode())
.assertEquals(
"Websocket endpoint can't handle HTTP requests", b.toString());
async.complete();
}))
.end();
async.awaitSuccess(VERTX_AWAIT_TIMEOUT_MILLIS); async.awaitSuccess(VERTX_AWAIT_TIMEOUT_MILLIS);
} }

Loading…
Cancel
Save