mirror of
https://github.com/logos-storage/nim-ethers.git
synced 2026-01-06 15:43:07 +00:00
Add http error conversion tests for failed connections
This commit is contained in:
parent
af35395ace
commit
be192c880e
@ -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 =
|
||||
|
||||
@ -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.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user