Hyper: Patterns for testing hyper client usage?

Created on 16 Feb 2018  Â·  3Comments  Â·  Source: hyperium/hyper

I'm implementing a program that is using Hyper for its HTTP client. I'm still pretty new to Rust, especially writing tests in Rust, so please bear with me.

I want to be able to test the production of requests and the consumption of responses within my program. To this end, I _think_ I want some kind of a testing HttpClient that enables me to intercept the request that would be sent and mock a response given "any" request.

It seems like tests/client.rs is actually standing up a little HTTP server for each test. While certainly a way to accomplish it, this seems… excessive. This may be a limitation of Hyper's HttpClient at the moment. If so, I'm willing to explore what it will take to make my dreams into reality.

Ultimately, I'm looking for something like mocking in Ruby or Scala, where I can say something like this for testing a parser:

val client = new HttpClient
val resp = createExpectedResponse()
when(client.execute(Any)) thenReturn(resp)
val output = parser.parse(client.execute(createRequest()))
output should be(expectedParsedValue)

My desired state at close of this issue is an article in the Hyper guide recommending best practices for testing when Hyper is involved.

Most helpful comment

My intended outcome for this was the inclusion of recommendations in the
documentation. That there aren't good examples means there may be work
for _someone_ to do, even if that someone is me-a-few-months-from-now.

All 3 comments

Since Client is generic over a connector, which can return any type of AsyncRead + AsyncWrite, it is definitely possible to make tests using some mocked thing.

hyper's tests/client.rs specifically use TCP as more of a promise that the client works correctly on TCP, instead of a bug existing in a mocked transport.

I'm going to close this as it's a question about patterns, rather than a bug in hyper.

My intended outcome for this was the inclusion of recommendations in the
documentation. That there aren't good examples means there may be work
for _someone_ to do, even if that someone is me-a-few-months-from-now.

Was this page helpful?
0 / 5 - 0 ratings