Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #1328: custom timeout argument does not work #1330

Merged
merged 2 commits into from
Jan 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion telegram/utils/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,8 @@ def post(self, url, data, timeout=None):
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)

Expand Down
21 changes: 16 additions & 5 deletions tests/test_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,16 +618,27 @@ def test_error_pin_unpin_message(self, bot, message):
# 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)

# Test JSON submition
with pytest.raises(OkException):
bot.send_photo(chat_id, open('tests/data/telegram.jpg', 'rb'), timeout=timeout)
bot.get_chat_administrators(chat_id, timeout=TIMEOUT)