From 1e6513136ab94fe8806f739984901b9ac57431f1 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Tue, 24 Aug 2021 18:02:59 -0700 Subject: [PATCH] tests: Set encoding for mock HTTP responses. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes warnings like ‘UserWarning: Trying to detect encoding from a tiny portion of (2) byte(s).’ Signed-off-by: Anders Kaseorg --- zulip_bots/zulip_bots/bots/trello/fixtures/invalid_key.json | 1 + zulip_bots/zulip_bots/request_test_lib.py | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/zulip_bots/zulip_bots/bots/trello/fixtures/invalid_key.json b/zulip_bots/zulip_bots/bots/trello/fixtures/invalid_key.json index ae06ce3..6065a20 100644 --- a/zulip_bots/zulip_bots/bots/trello/fixtures/invalid_key.json +++ b/zulip_bots/zulip_bots/bots/trello/fixtures/invalid_key.json @@ -11,5 +11,6 @@ }, "response": "invalid key", "response-headers": { + "content-type": "text/plain; charset=utf-8" } } diff --git a/zulip_bots/zulip_bots/request_test_lib.py b/zulip_bots/zulip_bots/request_test_lib.py index a66bc67..9bc2ae2 100644 --- a/zulip_bots/zulip_bots/request_test_lib.py +++ b/zulip_bots/zulip_bots/request_test_lib.py @@ -4,6 +4,7 @@ from typing import Any, Dict, List from unittest.mock import patch import requests +from requests.utils import get_encoding_from_headers @contextmanager @@ -25,11 +26,13 @@ def mock_http_conversation(http_data: Dict[str, Any]) -> Any: response headers. """ 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: mock_result._content = http_response.encode() # type: ignore # This modifies a "hidden" attribute. else: mock_result._content = json.dumps(http_response).encode() - mock_result.status_code = http_headers.get("status", 200) return mock_result def assert_called_with_fields(