From 10767e482e7f773754b2974e1862b8bb7d120036 Mon Sep 17 00:00:00 2001 From: Tim Burke Date: Tue, 11 May 2021 15:45:08 -0700 Subject: [PATCH] staticweb: Allow empty listings at the root of a container This is useful for testing that staticweb metadata was properly set before putting data into the container. Change-Id: I433bad1cb3f52ad60a81dcbf3209681f7068ed55 --- swift/common/middleware/staticweb.py | 2 +- test/unit/common/middleware/test_staticweb.py | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/swift/common/middleware/staticweb.py b/swift/common/middleware/staticweb.py index 7e25e61675..5e9d928566 100644 --- a/swift/common/middleware/staticweb.py +++ b/swift/common/middleware/staticweb.py @@ -281,7 +281,7 @@ class _StaticWebContext(WSGIContext): body = b''.join(resp) if body: listing = json.loads(body) - if not listing: + if prefix and not listing: resp = HTTPNotFound()(env, self._start_response) return self._error_response(resp, env, start_response) headers = {'Content-Type': 'text/html; charset=UTF-8'} diff --git a/test/unit/common/middleware/test_staticweb.py b/test/unit/common/middleware/test_staticweb.py index b72b34bf29..02547f4d4f 100644 --- a/test/unit/common/middleware/test_staticweb.py +++ b/test/unit/common/middleware/test_staticweb.py @@ -61,6 +61,7 @@ meta_map = { 'web-error': 'error.html'}}, 'c13': {'meta': {'web-listings': 'f', 'web-listings-css': 'listing.css'}}, + 'c14': {'meta': {'web-listings': 't'}}, } @@ -271,6 +272,8 @@ class FakeApp(object): elif env['PATH_INFO'] == '/v1/a/c12/200error.html': return Response(status='200 Ok', body='error file')(env, start_response) + elif env['PATH_INFO'] == '/v1/a/c14': + return self.listing(env, start_response) else: raise Exception('Unknown path %r' % env['PATH_INFO']) @@ -344,6 +347,13 @@ class FakeApp(object): "last_modified":"2011-03-24T04:27:52.709100"}, {"subdir":"\u2603/\u2603/"}] '''.strip() + elif env['PATH_INFO'] == '/v1/a/c14' and env['QUERY_STRING'] == \ + 'delimiter=/': + headers.update({'X-Container-Object-Count': '0', + 'X-Container-Bytes-Used': '0', + 'X-Container-Read': '.r:*', + 'Content-Type': 'application/json; charset=utf-8'}) + body = '[]' elif 'prefix=' in env['QUERY_STRING']: return Response(status='204 No Content')(env, start_response) else: @@ -777,6 +787,16 @@ class TestStaticWeb(unittest.TestCase): self.assertEqual(resp.status_int, 200) self.assertIn(b'index file', resp.body) + def test_container13empty(self): + resp = Request.blank( + '/v1/a/c14/').get_response(self.test_staticweb) + self.assertEqual(resp.status_int, 200) + self.assertIn(b'Listing of /v1/a/c14/', resp.body) + self.assertIn(b'', resp.body) + self.assertNotIn(b'