-
-
Notifications
You must be signed in to change notification settings - Fork 858
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add httpx.MockTransport * Add docs on MockTransport * Add pointer to RESPX * Add note on pytest-httpx * Tweak existing docs example to use 'httpx.MockTransport' Co-authored-by: Florimond Manca <[email protected]>
- Loading branch information
1 parent
3bf1863
commit 9c7c2ac
Showing
13 changed files
with
198 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from typing import Callable, List, Optional, Tuple | ||
|
||
import httpcore | ||
|
||
from .._models import Request | ||
|
||
|
||
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 = 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 = 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, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.