utils.py
This file
Path
tests/utils.py
103 lines · 2.9 KiB · Python
Last changed by
Requests from transport API (#1293)Tom Christiebefore adoptione1f7791
History of this fileTake it
The walk stopped after 200 commits: lines older than 19bf211 point at it.
Test Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 1–3why, one step back1import contextlib2import logging3import osRefactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d
line 4why, one step back4from typing import Callable, List, Mapping, Optional, TupleTest Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
line 5why, one step back5Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d
lines 6–8why, one step back6import httpcore78import httpxTest Suite on GitHub Actions (#907)Tom Christiebefore adoption19bf211
lines 9–24why, one step back9from httpx import _utils101112@contextlib.contextmanager13def override_log_level(log_level: str):14 os.environ["HTTPX_LOG_LEVEL"] = log_level1516 # Force a reload on the logging handlers17 _utils._LOGGER_INITIALIZED = False18 _utils.get_logger("httpx")1920 try:21 yield22 finally:23 # Reset the logger so we don't have verbose output in all unit tests24 logging.getLogger("httpx").handlers = []Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d
lines 25–39why, one step back252627class MockTransport(httpcore.SyncHTTPTransport):28 def __init__(self, handler: Callable) -> None:29 self.handler = handler3031 def request(32 self,33 method: bytes,34 url: Tuple[bytes, bytes, Optional[int], bytes],35 headers: List[Tuple[bytes, bytes]] = None,36 stream: httpcore.SyncByteStream = None,37 timeout: Mapping[str, Optional[float]] = None,38 ) -> Tuple[bytes, int, bytes, List[Tuple[bytes, bytes]], httpcore.SyncByteStream]:39 request_headers = httpx.Headers(headers)Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f
line 40why, one step back40 content = (Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d
lines 41–50why, one step back41 (item for item in stream)42 if stream43 and (44 "Content-Length" in request_headers45 or "Transfer-Encoding" in request_headers46 )47 else None48 )4950 request = httpx.Request(Requests from transport API (#1293)Tom Christiebefore adoptione1f7791
lines 51–52why, one step back51 method=method,52 url=url,Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d
line 53why, one step back53 headers=request_headers,Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f
line 54why, one step back54 content=content,Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d
lines 55–58why, one step back55 )56 request.read()57 response = self.handler(request)58 return (Requests from transport API (#1293)Tom Christiebefore adoptione1f7791
line 59why, one step back59 (response.http_version or "HTTP/1.1").encode("ascii"),Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d
lines 60–103why, one step back60 response.status_code,61 response.reason_phrase.encode("ascii"),62 response.headers.raw,63 response._raw_stream,64 )656667class AsyncMockTransport(httpcore.AsyncHTTPTransport):68 def __init__(self, handler: Callable) -> None:69 self.impl = MockTransport(handler)7071 async def request(72 self,73 method: bytes,74 url: Tuple[bytes, bytes, Optional[int], bytes],75 headers: List[Tuple[bytes, bytes]] = None,76 stream: httpcore.AsyncByteStream = None,77 timeout: Mapping[str, Optional[float]] = None,78 ) -> Tuple[bytes, int, bytes, List[Tuple[bytes, bytes]], httpcore.AsyncByteStream]:79 content = (80 httpcore.PlainByteStream(b"".join([part async for part in stream]))81 if stream82 else httpcore.PlainByteStream(b"")83 )8485 (86 http_version,87 status_code,88 reason_phrase,89 headers,90 response_stream,91 ) = self.impl.request(92 method, url, headers=headers, stream=content, timeout=timeout93 )9495 content = httpcore.PlainByteStream(b"".join([part for part in response_stream]))9697 return (98 http_version,99 status_code,100 reason_phrase,101 headers,102 content,103 )