Don't use 'requests.PreparedRequest'

We're using 'requests.PreparedRequest' in one place in the tests.
However, the requests docs have a warning that one shouldn't do this:

  Instances are generated from a Request object, and should not be
  instantiated manually; doing so may produce undesirable effects.

It seems we're now seeing just such an effect, as requests has started
attempting to do proxy-related things resulting in the following error:

    Traceback (most recent call last):
      File "/usr/lib64/python3.6/unittest/mock.py", line 1183, in patched
    return func(*args, **keywargs)
      File ".../oslo.vmware/oslo_vmware/tests/test_service.py", line 518, in test_send_with_local_file_url
    resp = transport.session.send(request)
      File ".../oslo.vmware/.tox/py36/lib/python3.6/site-packages/requests/sessions.py", line 636, in send
    kwargs.setdefault('proxies', self.rebuild_proxies(request, self.proxies))
      File ".../oslo.vmware/.tox/py36/lib/python3.6/site-packages/requests/sessions.py", line 301, in rebuild_proxies
    if 'Proxy-Authorization' in headers:
    TypeError: argument of type 'NoneType' is not iterable

Do what we should have done from the beginning and use
'requests.Request.prepare' instead.

[1] https://docs.python-requests.org/en/master/api/#requests.PreparedRequest

Change-Id: I072c64904298cf83cb113e402d85fb62626f39f7
Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
This commit is contained in:
Stephen Finucane 2021-09-21 15:41:58 +01:00
parent a2e76d648a
commit e991194cbd
1 changed files with 2 additions and 4 deletions

View File

@ -493,8 +493,7 @@ class RequestsTransportTest(base.TestCase):
transport = service.RequestsTransport()
url = 'file:///foo'
request = requests.PreparedRequest()
request.url = url
request = requests.Request('GET', url).prepare()
data = b"Hello World"
get_size_mock.return_value = len(data)
@ -502,7 +501,6 @@ class RequestsTransportTest(base.TestCase):
def read_mock():
return data
builtin_open = 'builtins.open'
open_mock = mock.MagicMock(name='file_handle',
spec=open)
file_spec = list(set(dir(io.TextIOWrapper)).union(
@ -514,7 +512,7 @@ class RequestsTransportTest(base.TestCase):
file_handle.read.side_effect = read_mock
open_mock.return_value = file_handle
with mock.patch(builtin_open, open_mock, create=True):
with mock.patch('builtins.open', open_mock, create=True):
resp = transport.session.send(request)
self.assertEqual(data, resp.content)