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

Add httpx.MockTransport() #1303

Closed
tomchristie opened this issue Sep 21, 2020 · 1 comment · Fixed by #1401
Closed

Add httpx.MockTransport() #1303

tomchristie opened this issue Sep 21, 2020 · 1 comment · Fixed by #1401
Labels
enhancement New feature or request
Milestone

Comments

@tomchristie
Copy link
Member

tomchristie commented Sep 21, 2020

Based on

httpx/tests/utils.py

Lines 27 to 75 in 8ceb34f

class MockTransport(httpcore.SyncHTTPTransport, httpcore.AsyncHTTPTransport):
def __init__(self, handler: Callable) -> None:
self.handler = handler
def request(
self,
method: bytes,
url: Tuple[bytes, bytes, Optional[int], bytes],
headers: List[Tuple[bytes, bytes]] = None,
stream: httpcore.SyncByteStream = None,
ext: dict = None,
) -> Tuple[int, List[Tuple[bytes, bytes]], httpcore.SyncByteStream, dict]:
request = httpx.Request(
method=method,
url=url,
headers=headers,
stream=stream,
)
request.read()
response = self.handler(request)
return (
response.status_code,
response.headers.raw,
response.stream,
response.ext,
)
async def arequest(
self,
method: bytes,
url: Tuple[bytes, bytes, Optional[int], bytes],
headers: List[Tuple[bytes, bytes]] = None,
stream: httpcore.AsyncByteStream = None,
ext: dict = None,
) -> Tuple[int, List[Tuple[bytes, bytes]], httpcore.AsyncByteStream, dict]:
request = httpx.Request(
method=method,
url=url,
headers=headers,
stream=stream,
)
await request.aread()
response = self.handler(request)
return (
response.status_code,
response.headers.raw,
response.stream,
response.ext,
)

Usage:

def handler(request: httpx.Request) -> httpx.Response:
    return httpx.Response(200, json={"hello": "world"})


client = httpx.Client(transport=httpx.MockTransport(handler))

Since #1297 and #1293 we're almost at the point where we can have a really nice simple httpx.MockTransport(handler=...) implementation, which I think would be worthwhile, given how useful we find it in our own tests, and how it'd help round out the usefulness of the Transport API to our users (and provide a point of demonstration around being able to use Request/Response models in that context.)

The one outstanding bit of work that we still need really is the .request+.arequest refactoring mentioned in #1274

Also worth mentioning that the following all fit together as as a nice related set of enhancements...

So it's possible that we ought to aim to release them together, just to bring some extra coherence to eg. "Here's the mount API we've just added, and here's the sort of stuff you can do with it"

@tomchristie tomchristie added the enhancement New feature or request label Sep 21, 2020
@tomchristie tomchristie added this to the v1.0 milestone Sep 21, 2020
@tomchristie tomchristie mentioned this issue Sep 22, 2020
4 tasks
@lundberg
Copy link
Contributor

I like your thoughts @tomchristie !

If I read this correctly, the current RESPX MockTransport could get rid of mock patching httpcore transports, and instead be refactored to a pattern > response handler.

Though, I still think mock.patch functionality would be of interest in RESPX to be able to mock httpcore individually, so that future libs using httpcore still can use RESPX for mocking.

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

Successfully merging a pull request may close this issue.

2 participants