use of org.ethereum.rpc.Web3 in project rskj by rsksmart.
the class Web3HttpServerTest method smokeTest.
private void smokeTest(String contentType, String host, InetAddress rpcAddress, List<String> rpcHost, boolean excludeJsonRequestId) throws Exception {
Web3 web3Mock = Mockito.mock(Web3.class);
String mockResult = "output";
Mockito.when(web3Mock.web3_sha3(Mockito.anyString())).thenReturn(mockResult);
CorsConfiguration mockCorsConfiguration = Mockito.mock(CorsConfiguration.class);
Mockito.when(mockCorsConfiguration.hasHeader()).thenReturn(true);
Mockito.when(mockCorsConfiguration.getHeader()).thenReturn("*");
// new ServerSocket(0).getLocalPort();
int randomPort = 9999;
List<ModuleDescription> filteredModules = Collections.singletonList(new ModuleDescription("web3", "1.0", true, Collections.emptyList(), Collections.emptyList()));
JsonRpcWeb3FilterHandler filterHandler = new JsonRpcWeb3FilterHandler("*", rpcAddress, rpcHost);
JsonRpcWeb3ServerHandler serverHandler = new JsonRpcWeb3ServerHandler(web3Mock, filteredModules);
Web3HttpServer server = new Web3HttpServer(InetAddress.getLoopbackAddress(), randomPort, 0, Boolean.TRUE, mockCorsConfiguration, filterHandler, serverHandler);
server.start();
try {
Response response = sendJsonRpcMessage(randomPort, contentType, host, excludeJsonRequestId);
String strResponse = response.body().string();
JsonNode jsonRpcResponse = OBJECT_MAPPER.readTree(strResponse);
assertThat(response.code(), is(HttpResponseStatus.OK.code()));
if (excludeJsonRequestId) {
assertEquals(jsonRpcResponse.get("error").get("code").asLong(), -32700L);
assertEquals(jsonRpcResponse.get("error").get("message").asText(), "missing request id");
} else {
assertThat(jsonRpcResponse.at("/result").asText(), is(mockResult));
}
} finally {
server.stop();
}
}
use of org.ethereum.rpc.Web3 in project rskj by rsksmart.
the class Web3HttpServerTest method smokeTest.
private void smokeTest(String contentType, String host, InetAddress rpcAddress, List<String> rpcHost) throws Exception {
Web3 web3Mock = Mockito.mock(Web3.class);
String mockResult = "output";
Mockito.when(web3Mock.web3_sha3(Mockito.anyString())).thenReturn(mockResult);
CorsConfiguration mockCorsConfiguration = Mockito.mock(CorsConfiguration.class);
Mockito.when(mockCorsConfiguration.hasHeader()).thenReturn(true);
Mockito.when(mockCorsConfiguration.getHeader()).thenReturn("*");
// new ServerSocket(0).getLocalPort();
int randomPort = 9999;
List<ModuleDescription> filteredModules = Collections.singletonList(new ModuleDescription("web3", "1.0", true, Collections.emptyList(), Collections.emptyList()));
JsonRpcWeb3FilterHandler filterHandler = new JsonRpcWeb3FilterHandler("*", rpcAddress, rpcHost);
JsonRpcWeb3ServerHandler serverHandler = new JsonRpcWeb3ServerHandler(web3Mock, filteredModules);
Web3HttpServer server = new Web3HttpServer(InetAddress.getLoopbackAddress(), randomPort, 0, Boolean.TRUE, mockCorsConfiguration, filterHandler, serverHandler);
server.start();
try {
Response response = sendJsonRpcMessage(randomPort, contentType, host);
JsonNode jsonRpcResponse = OBJECT_MAPPER.readTree(response.body().string());
assertThat(response.code(), is(HttpResponseStatus.OK.code()));
assertThat(jsonRpcResponse.at("/result").asText(), is(mockResult));
} finally {
server.stop();
}
}
use of org.ethereum.rpc.Web3 in project rskj by rsksmart.
the class Web3WebSocketServerTest method smokeTest.
private void smokeTest(byte[] msg) throws Exception {
Web3 web3Mock = mock(Web3.class);
String mockResult = "output";
when(web3Mock.web3_sha3(anyString())).thenReturn(mockResult);
int randomPort = 9998;
TestSystemProperties testSystemProperties = new TestSystemProperties();
List<ModuleDescription> filteredModules = Collections.singletonList(new ModuleDescription("web3", "1.0", true, Collections.emptyList(), Collections.emptyList()));
RskWebSocketJsonRpcHandler handler = new RskWebSocketJsonRpcHandler(null);
JsonRpcWeb3ServerHandler serverHandler = new JsonRpcWeb3ServerHandler(web3Mock, filteredModules);
int serverWriteTimeoutSeconds = testSystemProperties.rpcWebSocketServerWriteTimeoutSeconds();
int maxFrameSize = testSystemProperties.rpcWebSocketMaxFrameSize();
int maxAggregatedFrameSize = testSystemProperties.rpcWebSocketMaxAggregatedFrameSize();
assertEquals(DEFAULT_WRITE_TIMEOUT_SECONDS, serverWriteTimeoutSeconds);
assertEquals(DEFAULT_MAX_FRAME_SIZE, maxFrameSize);
assertEquals(DEFAULT_MAX_AGGREGATED_FRAME_SIZE, maxAggregatedFrameSize);
Web3WebSocketServer websocketServer = new Web3WebSocketServer(InetAddress.getLoopbackAddress(), randomPort, handler, serverHandler, serverWriteTimeoutSeconds, maxFrameSize, maxAggregatedFrameSize);
websocketServer.start();
OkHttpClient wsClient = new OkHttpClient();
Request wsRequest = new Request.Builder().url("ws://localhost:" + randomPort + "/websocket").build();
WebSocketCall wsCall = WebSocketCall.create(wsClient, wsRequest);
CountDownLatch wsAsyncResultLatch = new CountDownLatch(1);
CountDownLatch wsAsyncCloseLatch = new CountDownLatch(1);
AtomicReference<Exception> failureReference = new AtomicReference<>();
wsCall.enqueue(new WebSocketListener() {
private WebSocket webSocket;
@Override
public void onOpen(WebSocket webSocket, Response response) {
wsExecutor.submit(() -> {
RequestBody body = RequestBody.create(WebSocket.TEXT, msg);
try {
this.webSocket = webSocket;
this.webSocket.sendMessage(body);
this.webSocket.close(1000, null);
} catch (IOException e) {
failureReference.set(e);
}
});
}
@Override
public void onFailure(IOException e, Response response) {
failureReference.set(e);
}
@Override
public void onMessage(ResponseBody message) throws IOException {
JsonNode jsonRpcResponse = OBJECT_MAPPER.readTree(message.bytes());
assertThat(jsonRpcResponse.at("/result").asText(), is(mockResult));
message.close();
wsAsyncResultLatch.countDown();
}
@Override
public void onPong(Buffer payload) {
}
@Override
public void onClose(int code, String reason) {
wsAsyncCloseLatch.countDown();
}
});
if (!wsAsyncResultLatch.await(10, TimeUnit.SECONDS)) {
fail("Result timed out");
}
if (!wsAsyncCloseLatch.await(10, TimeUnit.SECONDS)) {
fail("Close timed out");
}
websocketServer.stop();
Exception failure = failureReference.get();
if (failure != null) {
failure.printStackTrace();
fail(failure.getMessage());
}
}
Aggregations