Skip to content

Commit 119693f

Browse files
committed
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 <[email protected]>
1 parent 6a5b642 commit 119693f

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

tests/test_fetcher_ng.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@
1313
import unittest
1414
import tempfile
1515
import math
16+
import urllib3.exceptions
17+
import requests
1618

1719
from tests import utils
1820
from tuf import exceptions, unittest_toolbox
1921
from tuf.ngclient._internal.requests_fetcher import RequestsFetcher
22+
from unittest.mock import patch
2023

2124
logger = logging.getLogger(__name__)
2225

@@ -121,6 +124,13 @@ def test_read_timeout(self):
121124

122125
slow_server_process_handler.clean()
123126

127+
# Read/connect session timeout error
128+
@patch.object(requests.Session, 'get', side_effect=urllib3.exceptions.TimeoutError)
129+
def test_session_get_timeout(self, mock_session_get):
130+
with self.assertRaises(exceptions.SlowRetrievalError):
131+
self.fetcher.fetch(self.url)
132+
mock_session_get.assert_called_once()
133+
124134
# Simple bytes download
125135
def test_download_bytes(self):
126136
data = self.fetcher.download_bytes(self.url, self.file_length)

tuf/ngclient/_internal/requests_fetcher.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,13 @@ def fetch(self, url: str) -> Iterator[bytes]:
7777
# requests as:
7878
# - connect timeout (max delay before first byte is received)
7979
# - read (gap) timeout (max delay between bytes received)
80-
response = session.get(url, stream=True, timeout=self.socket_timeout)
80+
try:
81+
response = session.get(
82+
url, stream=True, timeout=self.socket_timeout
83+
)
84+
except urllib3.exceptions.TimeoutError as e:
85+
raise exceptions.SlowRetrievalError from e
86+
8187
# Check response status.
8288
try:
8389
response.raise_for_status()

0 commit comments

Comments
 (0)