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() #1401

Merged
merged 7 commits into from
Jan 6, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add docs on MockTransport
  • Loading branch information
tomchristie committed Nov 25, 2020
commit 4a4f8557dd1eebbc76b74bbf16b839664d16c152
22 changes: 22 additions & 0 deletions docs/advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -1059,6 +1059,28 @@ Which we can use in the same way:
{"text": "Hello, world!"}
```

### Mock transports

During testing it can often be useful to be able to mock out a transport,
and return pre-determined responses, rather than making actual network requests.

The `httpx.MockTransport` class accepts a handler function, which can be used
to map requests onto pre-determined responses:

```python
def handler(request):
return httpx.Response(200, json={"text": "Hello, world!"})


# Switch to a mock transport, if the TESTING environment variable is set.
if os.environ['TESTING'].upper() == "TRUE":
transport = httpx.MockTransport(handler)
else:
transport = httpx.HTTPTransport()

client = httpx.Client(transport=transport)
```

### Mounting transports

You can also mount transports against given schemes or domains, to control
Expand Down