Skip to content
Myr

utils.py

This file

Path

tests/utils.py

111 lines · 3.2 KiB · Python

Last changed by

Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f

History of this file

Take it

The walk stopped after 200 commits: lines older than 918c55d point at it.

Update api.py (#745)Jakob Jul Elbenbefore adoption918c55d

lines 1–3why, one step back
1import contextlib
2import logging3import os

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

line 4why, one step back
4from typing import Callable, List, Mapping, Optional, Tuple

Update api.py (#745)Jakob Jul Elbenbefore adoption918c55d

line 5why, one step back
5

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

lines 6–8why, one step back
6import httpcore
78import httpx

Switch to private module names (#785)Florimond Mancabefore adoption82dc6f3

line 9why, one step back
9from httpx import _utils

Update api.py (#745)Jakob Jul Elbenbefore adoption918c55d

lines 10–16why, one step back
10
1112@contextlib.contextmanager13def override_log_level(log_level: str):14 os.environ["HTTPX_LOG_LEVEL"] = log_level1516 # Force a reload on the logging handlers

Switch to private module names (#785)Florimond Mancabefore adoption82dc6f3

lines 17–18why, one step back
17 _utils._LOGGER_INITIALIZED = False
18 _utils.get_logger("httpx")

Update api.py (#745)Jakob Jul Elbenbefore adoption918c55d

lines 19–24why, one step back
19
20 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–45why, one step back
25
2627class 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 raw_scheme, raw_host, port, raw_path = url40 scheme = raw_scheme.decode("ascii")41 host = raw_host.decode("ascii")42 port_str = "" if port is None else f":{port}"43 path = raw_path.decode("ascii")4445 request_headers = httpx.Headers(headers)

Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f

line 46why, one step back
46 content = (

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

lines 47–59why, one step back
47 (item for item in stream)
48 if stream49 and (50 "Content-Length" in request_headers51 or "Transfer-Encoding" in request_headers52 )53 else None54 )5556 request = httpx.Request(57 method=method.decode("ascii"),58 url=f"{scheme}://{host}{port_str}{path}",59 headers=request_headers,

Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f

line 60why, one step back
60 content=content,

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

lines 61–111why, one step back
61 )
62 request.read()63 response = self.handler(request)64 return (65 response.http_version.encode("ascii")66 if response.http_version67 else b"HTTP/1.1",68 response.status_code,69 response.reason_phrase.encode("ascii"),70 response.headers.raw,71 response._raw_stream,72 )737475class AsyncMockTransport(httpcore.AsyncHTTPTransport):76 def __init__(self, handler: Callable) -> None:77 self.impl = MockTransport(handler)7879 async def request(80 self,81 method: bytes,82 url: Tuple[bytes, bytes, Optional[int], bytes],83 headers: List[Tuple[bytes, bytes]] = None,84 stream: httpcore.AsyncByteStream = None,85 timeout: Mapping[str, Optional[float]] = None,86 ) -> Tuple[bytes, int, bytes, List[Tuple[bytes, bytes]], httpcore.AsyncByteStream]:87 content = (88 httpcore.PlainByteStream(b"".join([part async for part in stream]))89 if stream90 else httpcore.PlainByteStream(b"")91 )9293 (94 http_version,95 status_code,96 reason_phrase,97 headers,98 response_stream,99 ) = self.impl.request(100 method, url, headers=headers, stream=content, timeout=timeout101 )102103 content = httpcore.PlainByteStream(b"".join([part for part in response_stream]))104105 return (106 http_version,107 status_code,108 reason_phrase,109 headers,110 content,111 )