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 <anders@zulip.com>
This commit is contained in:
parent
e8bb65b188
commit
1e6513136a
|
@ -11,5 +11,6 @@
|
||||||
},
|
},
|
||||||
"response": "invalid key",
|
"response": "invalid key",
|
||||||
"response-headers": {
|
"response-headers": {
|
||||||
|
"content-type": "text/plain; charset=utf-8"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ from typing import Any, Dict, List
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
from requests.utils import get_encoding_from_headers
|
||||||
|
|
||||||
|
|
||||||
@contextmanager
|
@contextmanager
|
||||||
|
@ -25,11 +26,13 @@ def mock_http_conversation(http_data: Dict[str, Any]) -> Any:
|
||||||
response headers.
|
response headers.
|
||||||
"""
|
"""
|
||||||
mock_result = requests.Response()
|
mock_result = requests.Response()
|
||||||
|
mock_result.status_code = http_headers.pop("status", 200)
|
||||||
|
mock_result.headers.update(http_headers)
|
||||||
|
mock_result.encoding = get_encoding_from_headers(mock_result.headers)
|
||||||
if is_raw_response:
|
if is_raw_response:
|
||||||
mock_result._content = http_response.encode() # type: ignore # This modifies a "hidden" attribute.
|
mock_result._content = http_response.encode() # type: ignore # This modifies a "hidden" attribute.
|
||||||
else:
|
else:
|
||||||
mock_result._content = json.dumps(http_response).encode()
|
mock_result._content = json.dumps(http_response).encode()
|
||||||
mock_result.status_code = http_headers.get("status", 200)
|
|
||||||
return mock_result
|
return mock_result
|
||||||
|
|
||||||
def assert_called_with_fields(
|
def assert_called_with_fields(
|
||||||
|
|
Loading…
Reference in a new issue