ngclient: handle timeout on session.get

RequestsFetcher now handles connect/read timeout
errors on session.get() as it does when reading data.

Adding a test which uses unittest.mock to patch the
session.get method instead of simulating the timeout
with a server.

Signed-off-by: Teodora Sechkova <tsechkova@vmware.com>
This commit is contained in:
Teodora Sechkova 2021-09-17 18:07:58 +03:00
parent 6a5b64295c
commit 119693ff40
No known key found for this signature in database
GPG key ID: 65F78F613EA1914E
2 changed files with 17 additions and 1 deletions

View file

@ -13,10 +13,13 @@
import unittest
import tempfile
import math
import urllib3.exceptions
import requests
from tests import utils
from tuf import exceptions, unittest_toolbox
from tuf.ngclient._internal.requests_fetcher import RequestsFetcher
from unittest.mock import patch
logger = logging.getLogger(__name__)
@ -121,6 +124,13 @@ def test_read_timeout(self):
slow_server_process_handler.clean()
# Read/connect session timeout error
@patch.object(requests.Session, 'get', side_effect=urllib3.exceptions.TimeoutError)
def test_session_get_timeout(self, mock_session_get):
with self.assertRaises(exceptions.SlowRetrievalError):
self.fetcher.fetch(self.url)
mock_session_get.assert_called_once()
# Simple bytes download
def test_download_bytes(self):
data = self.fetcher.download_bytes(self.url, self.file_length)

View file

@ -77,7 +77,13 @@ def fetch(self, url: str) -> Iterator[bytes]:
# requests as:
# - connect timeout (max delay before first byte is received)
# - read (gap) timeout (max delay between bytes received)
response = session.get(url, stream=True, timeout=self.socket_timeout)
try:
response = session.get(
url, stream=True, timeout=self.socket_timeout
)
except urllib3.exceptions.TimeoutError as e:
raise exceptions.SlowRetrievalError from e
# Check response status.
try:
response.raise_for_status()