diff --git a/src/pook/interceptors/http.py b/src/pook/interceptors/http.py index bf6553f..0e8d7ad 100644 --- a/src/pook/interceptors/http.py +++ b/src/pook/interceptors/http.py @@ -1,5 +1,6 @@ +import io import socket -from http.client import _CS_REQ_SENT, HTTPMessage # type: ignore[attr-defined] +from http.client import _CS_REQ_SENT, parse_headers # type: ignore[attr-defined] from http.client import HTTPSConnection from http.client import ( @@ -75,8 +76,12 @@ def _on_request(self, _request, conn, method, url, body=None, headers=None, **kw # urllib requires `code` to be set, rather than `status` mockres.code = res._status mockres.reason = http_reasons.get(res._status) - mockres.headers = HTTPMessage() - mockres.length = len(res._body or b"") + content_length = len(res._body or b"") + headers_fp = io.BytesIO( + f"Content-Length: {content_length}\r\n".encode("iso-8859-1") + ) + mockres.headers = parse_headers(headers_fp) + mockres.length = content_length for hkey, hval in res._headers.itermerged(): mockres.headers.add_header(hkey, hval) diff --git a/tests/unit/interceptors/urllib_test.py b/tests/unit/interceptors/urllib_test.py index f950423..e396a88 100644 --- a/tests/unit/interceptors/urllib_test.py +++ b/tests/unit/interceptors/urllib_test.py @@ -37,8 +37,9 @@ def test_urllib_ssl(): res = urlopen("https://example.com") assert res.read() == b"Hello from pook" - assert res.length == 15 assert res.version == 11 + assert res.length == 15 + assert res.headers.get("Content-Length") == "15" @pytest.mark.pook @@ -47,7 +48,9 @@ def test_urllib_clear(): res = urlopen("http://example.com") assert res.read() == b"Hello from pook" + assert res.version == 11 assert res.length == 15 + assert res.headers.get("Content-Length") == "15" @pytest.mark.pook @@ -56,5 +59,6 @@ def test_without_body_response_length_is_zero(): res = urlopen("http://example.com") assert res.read() == b"" - assert res.length == 0 assert res.version == 11 + assert res.length == 0 + assert res.headers.get("Content-Length") == "0"