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
3 changed files with 11 additions and 23 deletions

View File

@@ -151,17 +151,12 @@ class FakeSwift(object):
def _find_response(self, method, path): def _find_response(self, method, path):
path = normalize_path(path) path = normalize_path(path)
resp_or_resps = self._responses[(method, path)] resps = self._responses[(method, path)]
if isinstance(resp_or_resps, list): if len(resps) == 1:
resps = resp_or_resps
if len(resps) > 1:
resp = resps.pop(0)
else:
# we'll return the last registered response forever # we'll return the last registered response forever
resp = resps[0] return resps[0]
else: else:
resp = resp_or_resps return resps.pop(0)
return resp
def _select_response(self, env): def _select_response(self, env):
# in some cases we can borrow different registered response # 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''): def register(self, method, path, response_class, headers, body=b''):
path = normalize_path(path) path = normalize_path(path)
# many historical tests assume this is "private" attribute is a simple self._responses[(method, path)] = [(response_class, headers, body)]
# map of tuple => tuple that they can go grubbing around in
self._responses[(method, path)] = (response_class, headers, body)
def register_next_response(self, method, path, def register_next_response(self, method, path,
response_class, headers, body=b''): response_class, headers, body=b''):
path = normalize_path(path) resp_key = (method, normalize_path(path))
resp_key = (method, path)
next_resp = (response_class, headers, body) next_resp = (response_class, headers, body)
# setdefault is weird; I hope this makes sense self._responses.setdefault(resp_key, []).append(next_resp)
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)
class FakeAppThatExcepts(object): 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 index = len(list(filter(None, split_path(path, 0, 4, True)[1:]))) - 1
resource = resource_map[index] resource = resource_map[index]
if (method, path) in self._responses: if (method, path) in self._responses:
old_headers = self._responses[(method, path)][1] old_headers = self._responses[(method, path)][0][1]
headers = headers.copy() headers = headers.copy()
for key, value in old_headers.items(): for key, value in old_headers.items():
if is_sys_meta(resource, key) and key not in headers: if is_sys_meta(resource, key) and key not in headers:
@@ -91,7 +91,7 @@ class FakeSwift(BaseFakeSwift):
# register_unconditionally() keeps nothing. # register_unconditionally() keeps nothing.
if body is not None and not isinstance(body, bytes): if body is not None and not isinstance(body, bytes):
body = body.encode('utf8') body = body.encode('utf8')
self._responses[(method, path)] = (response_class, headers, body) self._responses[(method, path)] = [(response_class, headers, body)]
def clear_calls(self): def clear_calls(self):
del self._calls[:] del self._calls[:]

View File

@@ -28,7 +28,7 @@ class S3ApiHelperTestCase(unittest.TestCase):
self.path = '/v1/AUTH_test/bucket' self.path = '/v1/AUTH_test/bucket'
def _check_headers(self, swift, method, path, headers): 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) self.assertEqual(headers, response_headers)
def test_fake_swift_sysmeta(self): def test_fake_swift_sysmeta(self):