utils.py
This file
Path
tests/utils.py
82 lines · 2.4 KiB · Python
Last changed by
Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb
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 = httpx.Request(Requests from transport API (#1293)Tom Christiebefore adoptione1f7791
lines 40–41why, one step back40 method=method,41 url=url,Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb
lines 42–43why, one step back42 headers=headers,43 stream=stream,Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d
lines 44–47why, one step back44 )45 request.read()46 response = self.handler(request)47 return (Requests from transport API (#1293)Tom Christiebefore adoptione1f7791
line 48why, one step back48 (response.http_version or "HTTP/1.1").encode("ascii"),Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d
lines 49–51why, one step back49 response.status_code,50 response.reason_phrase.encode("ascii"),51 response.headers.raw,Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb
line 52why, one step back52 response.stream,Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d
lines 53–57why, one step back53 )545556class AsyncMockTransport(httpcore.AsyncHTTPTransport):57 def __init__(self, handler: Callable) -> None:Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb
line 58why, one step back58 self.handler = handlerRefactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d
lines 59–67why, one step back5960 async def request(61 self,62 method: bytes,63 url: Tuple[bytes, bytes, Optional[int], bytes],64 headers: List[Tuple[bytes, bytes]] = None,65 stream: httpcore.AsyncByteStream = None,66 timeout: Mapping[str, Optional[float]] = None,67 ) -> Tuple[bytes, int, bytes, List[Tuple[bytes, bytes]], httpcore.AsyncByteStream]:Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb
lines 68–72why, one step back68 request = httpx.Request(69 method=method,70 url=url,71 headers=headers,72 stream=stream,Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d
line 73why, one step back73 )Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb
lines 74–75why, one step back74 await request.aread()75 response = self.handler(request)Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d
line 76why, one step back76 return (Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb
lines 77–81why, one step back77 (response.http_version or "HTTP/1.1").encode("ascii"),78 response.status_code,79 response.reason_phrase.encode("ascii"),80 response.headers.raw,81 response.stream,Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d
line 82why, one step back82 )