diff --git a/telegram/utils/request.py b/telegram/utils/request.py index db948fb30..ca3a7731e 100644 --- a/telegram/utils/request.py +++ b/telegram/utils/request.py @@ -323,7 +323,8 @@ class Request(object): else: result = self._request_wrapper('POST', url, body=json.dumps(data).encode('utf-8'), - headers={'Content-Type': 'application/json'}) + headers={'Content-Type': 'application/json'}, + **urlopen_kwargs) return self._parse(result) diff --git a/tests/test_bot.py b/tests/test_bot.py index fe3b38736..a84d52abf 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -618,16 +618,27 @@ class TestBot(object): # test_sticker module. def test_timeout_propagation(self, monkeypatch, bot, chat_id): + + from telegram.vendor.ptb_urllib3.urllib3.util.timeout import Timeout + class OkException(Exception): pass - timeout = 500 + TIMEOUT = 500 - def post(*args, **kwargs): - if kwargs.get('timeout') == 500: + def request_wrapper(*args, **kwargs): + obj = kwargs.get('timeout') + if isinstance(obj, Timeout) and obj._read == TIMEOUT: raise OkException - monkeypatch.setattr('telegram.utils.request.Request.post', post) + return b'{"ok": true, "result": []}' + monkeypatch.setattr('telegram.utils.request.Request._request_wrapper', request_wrapper) + + # Test file uploading with pytest.raises(OkException): - bot.send_photo(chat_id, open('tests/data/telegram.jpg', 'rb'), timeout=timeout) + bot.send_photo(chat_id, open('tests/data/telegram.jpg', 'rb'), timeout=TIMEOUT) + + # Test JSON submition + with pytest.raises(OkException): + bot.get_chat_administrators(chat_id, timeout=TIMEOUT)