Skip to content

Commit

Permalink
Fix #1328: custom timeout argument does not work (#1330)
Browse files Browse the repository at this point in the history
* Fix #1328: custom timeout argument does not work

* Remove unused import
  • Loading branch information
lorien authored and jsmnbom committed Jan 30, 2019
1 parent c03160c commit 3e8d715
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
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)

0 comments on commit 3e8d715

Please sign in to comment.