Skip to content

Commit

Permalink
Fix json-rpc HTTP tests [hyperledger#535]
Browse files Browse the repository at this point in the history
The `o.h.b.e.a.j.JsonRpcHttpServiceTest#exceptionallyHandleJsonSingleRequest` and `o.h.b.e.a.j.JsonRpcHttpServiceTest#exceptionallyHandleJsonBatchRequest` tests were throwing ClassCastException in `o.h.b.e.a.j.JsonRpcHttpService#validateMethodAvailability` which wasn't ever catched, returning status 500 by default, but that wasn't the use case aimed to test. Another test running an exceptional method is `o.h.b.t.a.p.EnclaveErrorAcceptanceTest#whenEnclaveIsDisconnectedGetReceiptReturnsInternalError` which validates an "Internal Error" proper json-rpc response. I changed the first two tests to be consistent with the later one.

Signed-off-by: Diego López León <[email protected]>
Signed-off-by: Diego López León <[email protected]>
  • Loading branch information
diega committed Apr 22, 2022
1 parent 23f5e2e commit 3fdcd76
Showing 1 changed file with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1413,13 +1413,17 @@ public void exceptionallyHandleJsonSingleRequest() throws Exception {
when(jsonRpcMethod.getName()).thenReturn("foo");
when(jsonRpcMethod.response(any())).thenThrow(new RuntimeException("test exception"));

doReturn(Optional.of(jsonRpcMethod)).when(rpcMethods).get("foo");
doReturn(jsonRpcMethod).when(rpcMethods).get("foo");

final RequestBody body =
RequestBody.create(JSON, "{\"jsonrpc\":\"2.0\",\"id\":\"666\",\"method\":\"foo\"}");

try (final Response resp = client.newCall(buildPostRequest(body)).execute()) {
assertThat(resp.code()).isEqualTo(500);
assertThat(resp.code()).isEqualTo(200);
final JsonObject json = new JsonObject(resp.body().string());
final JsonRpcError expectedError = JsonRpcError.INTERNAL_ERROR;
testHelper.assertValidJsonRpcError(
json, "666", expectedError.getCode(), expectedError.getMessage());
}
}

Expand All @@ -1428,7 +1432,7 @@ public void exceptionallyHandleJsonBatchRequest() throws Exception {
final JsonRpcMethod jsonRpcMethod = mock(JsonRpcMethod.class);
when(jsonRpcMethod.getName()).thenReturn("foo");
when(jsonRpcMethod.response(any())).thenThrow(new RuntimeException("test exception"));
doReturn(Optional.of(jsonRpcMethod)).when(rpcMethods).get("foo");
doReturn(jsonRpcMethod).when(rpcMethods).get("foo");

final RequestBody body =
RequestBody.create(
Expand All @@ -1438,7 +1442,13 @@ public void exceptionallyHandleJsonBatchRequest() throws Exception {
+ "{\"jsonrpc\":\"2.0\",\"id\":\"222\",\"method\":\"net_version\"}]");

try (final Response resp = client.newCall(buildPostRequest(body)).execute()) {
assertThat(resp.code()).isEqualTo(400);
assertThat(resp.code()).isEqualTo(200);
final JsonArray array = new JsonArray(resp.body().string());
testHelper.assertValidJsonRpcResult(array.getJsonObject(0), "000");
final JsonRpcError expectedError = JsonRpcError.INTERNAL_ERROR;
testHelper.assertValidJsonRpcError(
array.getJsonObject(1), "111", expectedError.getCode(), expectedError.getMessage());
testHelper.assertValidJsonRpcResult(array.getJsonObject(2), "222");
}
}

Expand Down

0 comments on commit 3fdcd76

Please sign in to comment.