Merge "tests: FakeSwift._responses is always a list of tuples"

This commit is contained in:
Zuul 2023-11-14 22:27:16 +00:00 committed by Gerrit Code Review
commit a52e18e005
3 changed files with 11 additions and 23 deletions

View File

@ -151,17 +151,12 @@ class FakeSwift(object):
def _find_response(self, method, path):
path = normalize_path(path)
resp_or_resps = self._responses[(method, path)]
if isinstance(resp_or_resps, list):
resps = resp_or_resps
if len(resps) > 1:
resp = resps.pop(0)
else:
# we'll return the last registered response forever
resp = resps[0]
resps = self._responses[(method, path)]
if len(resps) == 1:
# we'll return the last registered response forever
return resps[0]
else:
resp = resp_or_resps
return resp
return resps.pop(0)
def _select_response(self, env):
# in some cases we can borrow different registered response
@ -360,20 +355,13 @@ class FakeSwift(object):
def register(self, method, path, response_class, headers, body=b''):
path = normalize_path(path)
# many historical tests assume this is "private" attribute is a simple
# map of tuple => tuple that they can go grubbing around in
self._responses[(method, path)] = (response_class, headers, body)
self._responses[(method, path)] = [(response_class, headers, body)]
def register_next_response(self, method, path,
response_class, headers, body=b''):
path = normalize_path(path)
resp_key = (method, path)
resp_key = (method, normalize_path(path))
next_resp = (response_class, headers, body)
# setdefault is weird; I hope this makes sense
maybe_resp = self._responses.setdefault(resp_key, [])
if isinstance(maybe_resp, tuple):
self._responses[resp_key] = [maybe_resp]
self._responses[resp_key].append(next_resp)
self._responses.setdefault(resp_key, []).append(next_resp)
class FakeAppThatExcepts(object):

View File

@ -73,7 +73,7 @@ class FakeSwift(BaseFakeSwift):
index = len(list(filter(None, split_path(path, 0, 4, True)[1:]))) - 1
resource = resource_map[index]
if (method, path) in self._responses:
old_headers = self._responses[(method, path)][1]
old_headers = self._responses[(method, path)][0][1]
headers = headers.copy()
for key, value in old_headers.items():
if is_sys_meta(resource, key) and key not in headers:
@ -91,7 +91,7 @@ class FakeSwift(BaseFakeSwift):
# register_unconditionally() keeps nothing.
if body is not None and not isinstance(body, bytes):
body = body.encode('utf8')
self._responses[(method, path)] = (response_class, headers, body)
self._responses[(method, path)] = [(response_class, headers, body)]
def clear_calls(self):
del self._calls[:]

View File

@ -28,7 +28,7 @@ class S3ApiHelperTestCase(unittest.TestCase):
self.path = '/v1/AUTH_test/bucket'
def _check_headers(self, swift, method, path, headers):
_, response_headers, _ = swift._responses[(method, path)]
_, response_headers, _ = swift._responses[(method, path)][0]
self.assertEqual(headers, response_headers)
def test_fake_swift_sysmeta(self):