Skip to content

Commit 1e65131

Browse files
committed
tests: Set encoding for mock HTTP responses.
Fixes warnings like ‘UserWarning: Trying to detect encoding from a tiny portion of (2) byte(s).’ Signed-off-by: Anders Kaseorg <[email protected]>
1 parent e8bb65b commit 1e65131

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

zulip_bots/zulip_bots/bots/trello/fixtures/invalid_key.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
},
1212
"response": "invalid key",
1313
"response-headers": {
14+
"content-type": "text/plain; charset=utf-8"
1415
}
1516
}

zulip_bots/zulip_bots/request_test_lib.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from unittest.mock import patch
55

66
import requests
7+
from requests.utils import get_encoding_from_headers
78

89

910
@contextmanager
@@ -25,11 +26,13 @@ def get_response(
2526
response headers.
2627
"""
2728
mock_result = requests.Response()
29+
mock_result.status_code = http_headers.pop("status", 200)
30+
mock_result.headers.update(http_headers)
31+
mock_result.encoding = get_encoding_from_headers(mock_result.headers)
2832
if is_raw_response:
2933
mock_result._content = http_response.encode() # type: ignore # This modifies a "hidden" attribute.
3034
else:
3135
mock_result._content = json.dumps(http_response).encode()
32-
mock_result.status_code = http_headers.get("status", 200)
3336
return mock_result
3437

3538
def assert_called_with_fields(

0 commit comments

Comments
 (0)