test_headers: use fixture to hook request headers

Signed-off-by: Casey Bodley <cbodley@redhat.com>
(cherry picked from commit aecd282a11)
This commit is contained in:
Casey Bodley 2024-03-10 10:39:26 -04:00
parent a559d86236
commit 7a52579573

View file

@ -36,11 +36,9 @@ from . import (
) )
_orig_conn = {}
_orig_authorize = None _orig_authorize = None
_custom_headers = {} _custom_headers = {}
_remove_headers = [] _remove_headers = []
boto_type = None
# HeaderS3Connection and _our_authorize are necessary to be able to arbitrarily # HeaderS3Connection and _our_authorize are necessary to be able to arbitrarily
@ -84,15 +82,15 @@ def _our_authorize(self, connection, **kwargs):
_update_headers(self.headers) _update_headers(self.headers)
def setup(): @pytest.fixture
global boto_type def hook_headers(setup_teardown):
boto_type = None
_orig_conn = {}
# we determine what we need to replace by the existence of particular # we determine what we need to replace by the existence of particular
# attributes. boto 2.0rc1 as fill_in_auth for S3Connection, while boto 2.0 # attributes. boto 2.0rc1 as fill_in_auth for S3Connection, while boto 2.0
# has authorize for HTTPRequest. # has authorize for HTTPRequest.
if hasattr(S3Connection, 'fill_in_auth'): if hasattr(S3Connection, 'fill_in_auth'):
global _orig_conn
boto_type = 'S3Connection' boto_type = 'S3Connection'
for conn in s3: for conn in s3:
_orig_conn[conn] = s3[conn] _orig_conn[conn] = s3[conn]
@ -116,19 +114,14 @@ def setup():
else: else:
raise RuntimeError raise RuntimeError
yield
def teardown():
global boto_type
# replace original functionality depending on the boto version # replace original functionality depending on the boto version
if boto_type is 'S3Connection': if boto_type is 'S3Connection':
global _orig_conn
for conn in s3: for conn in s3:
s3[conn] = _orig_conn[conn] s3[conn] = _orig_conn[conn]
_orig_conn = {} _orig_conn = {}
elif boto_type is 'HTTPRequest': elif boto_type is 'HTTPRequest':
global _orig_authorize
boto.connection.HTTPRequest.authorize = _orig_authorize boto.connection.HTTPRequest.authorize = _orig_authorize
_orig_authorize = None _orig_authorize = None
else: else:
@ -143,7 +136,7 @@ def _clear_custom_headers():
_remove_headers = [] _remove_headers = []
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def clear_custom_headers(setup_teardown): def clear_custom_headers(setup_teardown, hook_headers):
yield yield
_clear_custom_headers() # clear headers before teardown() _clear_custom_headers() # clear headers before teardown()