Add http error conversion tests for failed connections

This commit is contained in:
Eric 2025-05-28 16:18:10 +10:00
parent af35395ace
commit be192c880e
No known key found for this signature in database
2 changed files with 29 additions and 1 deletions

View File

@ -65,7 +65,7 @@ proc init*(_: type MockHttpServer, address: TransportAddress): MockHttpServer =
server = MockHttpServer(server: res.get(), rpcResponses: newTable[string, RpcResponse]())
return server
template registerRpcMethod*(server: MockHttpServer, `method`: string, rpcResponse: untyped): untyped =
template registerRpcResponse*(server: MockHttpServer, `method`: string, rpcResponse: untyped): untyped =
server.rpcResponses[`method`] = rpcResponse
proc address*(server: MockHttpServer): TransportAddress =

View File

@ -138,3 +138,31 @@ suite "Network errors":
testCustomResponse("429", Http429, "Too many requests", RequestLimitError)
testCustomResponse("408", Http408, "Request timed out", RequestTimeoutError)
testCustomResponse("non-429", Http500, "Server error", JsonRpcProviderError)
test "raises RpcNetworkError when reading response headers times out":
privateAccess(JsonRpcProvider)
privateAccess(RpcHttpClient)
let responseTimeout = proc(request: HttpRequestRef): Future[HttpResponseRef] {.async: (raises: [CancelledError]).} =
try:
await sleepAsync(5.minutes)
return await request.respond(Http200, "OK")
except HttpWriteError as exc:
return defaultResponse(exc)
let rpcClient = await provider.client
let client: RpcHttpClient = (RpcHttpClient)(rpcClient)
client.httpSession = HttpSessionRef.new(headersTimeout = 1.millis)
mockServer.registerRpcResponse("eth_accounts", responseTimeout)
expect RpcNetworkError:
discard await provider.send("eth_accounts")
test "raises RpcNetworkError when connection is closed":
await mockServer.stop()
expect RpcNetworkError:
discard await provider.send("eth_accounts")
# We don't need to recreate each and every possible exception condition (eg
# "Connection timed out"), as they are all wrapped up in RpcPostError and
# converted. The tests above cover this conversion.