Skip to content

Commit 4598663

Browse files
xrmxsarayourfriend
andauthored
interceptors/http: set length on response (#174)
* interceptors/http: set length on response In case there is no body handle it as read() is doing, that is returning an empty body with length 0. * Reword test name * Fix linting error --------- Co-authored-by: Sara <git@sarayourfriend.pictures>
1 parent a15f6fb commit 4598663

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/pook/interceptors/http.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ def _on_request(self, _request, conn, method, url, body=None, headers=None, **kw
7676
mockres.code = res._status
7777
mockres.reason = http_reasons.get(res._status)
7878
mockres.headers = HTTPMessage()
79+
mockres.length = len(res._body or b"")
7980

8081
for hkey, hval in res._headers.itermerged():
8182
mockres.headers.add_header(hkey, hval)

tests/unit/interceptors/urllib_test.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def test_urllib_ssl():
3737
res = urlopen("https://example.com")
3838

3939
assert res.read() == b"Hello from pook"
40+
assert res.length == 15
4041
assert res.version == 11
4142

4243

@@ -46,4 +47,14 @@ def test_urllib_clear():
4647
res = urlopen("http://example.com")
4748

4849
assert res.read() == b"Hello from pook"
50+
assert res.length == 15
51+
52+
53+
@pytest.mark.pook
54+
def test_without_body_response_length_is_zero():
55+
pook.get("http://example.com").reply(200)
56+
res = urlopen("http://example.com")
57+
58+
assert res.read() == b""
59+
assert res.length == 0
4960
assert res.version == 11

0 commit comments

Comments
 (0)