Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for mocked client requests. #41

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Support for mocked client requests. #41

wants to merge 1 commit into from

Conversation

ioquatix
Copy link
Member

@ioquatix ioquatix commented Jan 6, 2020

This provides some basic building blocks for mocking up Async::HTTP::Client in the same process.

#35

@ioquatix
Copy link
Member Author

ioquatix commented Jan 6, 2020

@ayanko @bblimke here is a rough POC showing how to use an endpoint and an internal HTTP connection/server (using HTTP2 by default) to easily intercept requests. It's inspired by @ayanko's webmock implementation, but I wanted to try and standardise some of it.

The next step is to try and figure out what Async::HTTP::Mock::Client should look like. Either, 1/ something which lets you stub in client side request/response with delegation to the server connection if required, or 2/ just doing everything on the server, proxying requests as needed.

Personally, while 2/ is simpler, the chance of it breaking things is higher, because we'd need to correctly proxy WebSockets, HTTPS, etc, it's basically a man in the middle. But because of that, it's a centralised place for the logic to go.

It's all in the same process. @ayanko see #35 for more discussion if you are interested.

@ioquatix
Copy link
Member Author

ioquatix commented Jan 6, 2020

The other part of this, is the ability to replace Async::HTTP::Client, which I'm happy to leave in WebMock since it's kind of a hack and maybe shouldn't be officially sanctioned (but unofficially allowed as per normal Ruby behaviour).

It would be nice if it was possible to only make these changes when working within the spec, but this would require injecting Async::HTTP::Client everywhere which is probably impossible in general. The only way I could see that working is some kind of feature within Ruby like refinements, which basically lets you override globals/constants on a per-block basis.

That being said, it would be nice if we split the WebMock support for Async into two parts - those that are fairly pure and require injection, and those which just do the constant replacement. That way, engineer's writing tests can decide for themselves what approach works best for them, and the former approach, I'd be happy to completely support within Async::HTTP::Mock.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.2%) to 91.217% when pulling 92be93d on mock into 118f0ea on master.

@ioquatix
Copy link
Member Author

ioquatix commented Jan 9, 2020

I realised a slightly better implementation might be:

endpoint = Async::HTTP::Endpoint.parse("http://localhost", Async::IO::Endpoint.unix("server.ipc"))

Using a unix pipe avoids all the in-process queues and behaves more like a standard I/O.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants