11# Copyright 2021, New York University and the TUF contributors
22# SPDX-License-Identifier: MIT OR Apache-2.0
33
4- """Unit test for RequestsFetcher ."""
4+ """Unit test for Urllib3Fetcher ."""
55
66import io
77import logging
1313from typing import ClassVar
1414from unittest .mock import Mock , patch
1515
16- import requests
16+ import urllib3
1717
1818from tests import utils
1919from tuf .api import exceptions
20- from tuf .ngclient import RequestsFetcher
20+ from tuf .ngclient import Urllib3Fetcher
2121
2222logger = logging .getLogger (__name__ )
2323
2424
2525class TestFetcher (unittest .TestCase ):
26- """Test RequestsFetcher class."""
26+ """Test Urllib3Fetcher class."""
2727
2828 server_process_handler : ClassVar [utils .TestServerProcess ]
2929
@@ -57,7 +57,7 @@ def tearDownClass(cls) -> None:
5757
5858 def setUp (self ) -> None :
5959 # Instantiate a concrete instance of FetcherInterface
60- self .fetcher = RequestsFetcher ()
60+ self .fetcher = Urllib3Fetcher ()
6161
6262 # Simple fetch.
6363 def test_fetch (self ) -> None :
@@ -94,7 +94,7 @@ def test_fetch_in_chunks(self) -> None:
9494 # Incorrect URL parsing
9595 def test_url_parsing (self ) -> None :
9696 with self .assertRaises (exceptions .DownloadError ):
97- self .fetcher .fetch ("missing-scheme-and-hostname-in-url " )
97+ self .fetcher .fetch ("http://invalid/ " )
9898
9999 # File not found error
100100 def test_http_error (self ) -> None :
@@ -104,26 +104,33 @@ def test_http_error(self) -> None:
104104 self .assertEqual (cm .exception .status_code , 404 )
105105
106106 # Response read timeout error
107- @patch .object (requests . Session , "get " )
107+ @patch .object (urllib3 . PoolManager , "request " )
108108 def test_response_read_timeout (self , mock_session_get : Mock ) -> None :
109109 mock_response = Mock ()
110+ mock_response .status = 200
110111 attr = {
111- "iter_content.side_effect" : requests .exceptions .ConnectionError (
112- "Simulated timeout"
112+ "stream.side_effect" : urllib3 .exceptions .MaxRetryError (
113+ urllib3 .connectionpool .ConnectionPool ("localhost" ),
114+ "" ,
115+ urllib3 .exceptions .TimeoutError (),
113116 )
114117 }
115118 mock_response .configure_mock (** attr )
116119 mock_session_get .return_value = mock_response
117120
118121 with self .assertRaises (exceptions .SlowRetrievalError ):
119122 next (self .fetcher .fetch (self .url ))
120- mock_response .iter_content .assert_called_once ()
123+ mock_response .stream .assert_called_once ()
121124
122125 # Read/connect session timeout error
123126 @patch .object (
124- requests .Session ,
125- "get" ,
126- side_effect = requests .exceptions .Timeout ("Simulated timeout" ),
127+ urllib3 .PoolManager ,
128+ "request" ,
129+ side_effect = urllib3 .exceptions .MaxRetryError (
130+ urllib3 .connectionpool .ConnectionPool ("localhost" ),
131+ "" ,
132+ urllib3 .exceptions .TimeoutError (),
133+ ),
127134 )
128135 def test_session_get_timeout (self , mock_session_get : Mock ) -> None :
129136 with self .assertRaises (exceptions .SlowRetrievalError ):
0 commit comments