|
13 | 13 | import unittest
|
14 | 14 | import tempfile
|
15 | 15 | import math
|
| 16 | +import urllib3.exceptions |
| 17 | +import requests |
16 | 18 |
|
17 | 19 | from tests import utils
|
18 | 20 | from tuf import exceptions, unittest_toolbox
|
19 | 21 | from tuf.ngclient._internal.requests_fetcher import RequestsFetcher
|
| 22 | +from unittest.mock import Mock, patch |
20 | 23 |
|
21 | 24 | logger = logging.getLogger(__name__)
|
22 | 25 |
|
@@ -107,19 +110,24 @@ def test_http_error(self):
|
107 | 110 | self.fetcher.fetch(self.url)
|
108 | 111 | self.assertEqual(cm.exception.status_code, 404)
|
109 | 112 |
|
110 |
| - # Read timeout error |
111 |
| - def test_read_timeout(self): |
112 |
| - # Reduce the read socket timeout to speed up the test |
113 |
| - # while keeping the connect timeout |
114 |
| - default_socket_timeout = self.fetcher.socket_timeout |
115 |
| - self.fetcher.socket_timeout = (default_socket_timeout, 0.1) |
116 |
| - # Launch a new "slow retrieval" server sending one byte each 40s |
117 |
| - slow_server_process_handler = utils.TestServerProcess(log=logger, server='slow_retrieval_server.py') |
118 |
| - self.url = f"http://{utils.TEST_HOST_ADDRESS}:{str(slow_server_process_handler.port)}/{self.rel_target_filepath}" |
| 113 | + # Response read timeout error |
| 114 | + @patch.object(requests.Session, 'get') |
| 115 | + def test_response_read_timeout(self, mock_session_get): |
| 116 | + mock_response = Mock() |
| 117 | + attr = {'raw.read.side_effect': urllib3.exceptions.ReadTimeoutError(None, None, "Read timed out.")} |
| 118 | + mock_response.configure_mock(**attr) |
| 119 | + mock_session_get.return_value = mock_response |
| 120 | + |
119 | 121 | with self.assertRaises(exceptions.SlowRetrievalError):
|
120 | 122 | next(self.fetcher.fetch(self.url))
|
| 123 | + mock_response.raw.read.assert_called_once() |
121 | 124 |
|
122 |
| - slow_server_process_handler.clean() |
| 125 | + # Read/connect session timeout error |
| 126 | + @patch.object(requests.Session, 'get', side_effect=urllib3.exceptions.TimeoutError) |
| 127 | + def test_session_get_timeout(self, mock_session_get): |
| 128 | + with self.assertRaises(exceptions.SlowRetrievalError): |
| 129 | + self.fetcher.fetch(self.url) |
| 130 | + mock_session_get.assert_called_once() |
123 | 131 |
|
124 | 132 | # Simple bytes download
|
125 | 133 | def test_download_bytes(self):
|
|
0 commit comments