"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "test/unit/common/middleware/test_listing_formats.py" between
swift-2.19.1.tar.gz and swift-2.21.0.tar.gz

About: OpenStack swift is software for creating redundant, scalable object storage using clusters of commodity servers to store terabytes or even petabytes of accessible data (now supporting storage policies).
The "Stein" series (latest release).

test_listing_formats.py  (swift-2.19.1):test_listing_formats.py  (swift-2.21.0)
skipping to change at line 31 skipping to change at line 31
from test.unit.common.middleware.helpers import FakeSwift from test.unit.common.middleware.helpers import FakeSwift
class TestListingFormats(unittest.TestCase): class TestListingFormats(unittest.TestCase):
def setUp(self): def setUp(self):
self.fake_swift = FakeSwift() self.fake_swift = FakeSwift()
self.app = listing_formats.ListingFilter(self.fake_swift) self.app = listing_formats.ListingFilter(self.fake_swift)
self.fake_account_listing = json.dumps([ self.fake_account_listing = json.dumps([
{'name': 'bar', 'bytes': 0, 'count': 0, {'name': 'bar', 'bytes': 0, 'count': 0,
'last_modified': '1970-01-01T00:00:00.000000'}, 'last_modified': '1970-01-01T00:00:00.000000'},
{'subdir': 'foo_'}, {'subdir': 'foo_'},
]) ]).encode('ascii')
self.fake_container_listing = json.dumps([ self.fake_container_listing = json.dumps([
{'name': 'bar', 'hash': 'etag', 'bytes': 0, {'name': 'bar', 'hash': 'etag', 'bytes': 0,
'content_type': 'text/plain', 'content_type': 'text/plain',
'last_modified': '1970-01-01T00:00:00.000000'}, 'last_modified': '1970-01-01T00:00:00.000000'},
{'subdir': 'foo/'}, {'subdir': 'foo/'},
]) ]).encode('ascii')
def test_valid_account(self): def test_valid_account(self):
self.fake_swift.register('GET', '/v1/a', HTTPOk, { self.fake_swift.register('GET', '/v1/a', HTTPOk, {
'Content-Length': str(len(self.fake_account_listing)), 'Content-Length': str(len(self.fake_account_listing)),
'Content-Type': 'application/json'}, self.fake_account_listing) 'Content-Type': 'application/json'}, self.fake_account_listing)
req = Request.blank('/v1/a') req = Request.blank('/v1/a')
resp = req.get_response(self.app) resp = req.get_response(self.app)
self.assertEqual(resp.body, 'bar\nfoo_\n') self.assertEqual(resp.body, b'bar\nfoo_\n')
self.assertEqual(resp.headers['Content-Type'], self.assertEqual(resp.headers['Content-Type'],
'text/plain; charset=utf-8') 'text/plain; charset=utf-8')
self.assertEqual(self.fake_swift.calls[-1], ( self.assertEqual(self.fake_swift.calls[-1], (
'GET', '/v1/a?format=json')) 'GET', '/v1/a?format=json'))
req = Request.blank('/v1/a?format=txt') req = Request.blank('/v1/a?format=txt')
resp = req.get_response(self.app) resp = req.get_response(self.app)
self.assertEqual(resp.body, 'bar\nfoo_\n') self.assertEqual(resp.body, b'bar\nfoo_\n')
self.assertEqual(resp.headers['Content-Type'], self.assertEqual(resp.headers['Content-Type'],
'text/plain; charset=utf-8') 'text/plain; charset=utf-8')
self.assertEqual(self.fake_swift.calls[-1], ( self.assertEqual(self.fake_swift.calls[-1], (
'GET', '/v1/a?format=json')) 'GET', '/v1/a?format=json'))
req = Request.blank('/v1/a?format=json') req = Request.blank('/v1/a?format=json')
resp = req.get_response(self.app) resp = req.get_response(self.app)
self.assertEqual(resp.body, self.fake_account_listing) self.assertEqual(resp.body, self.fake_account_listing)
self.assertEqual(resp.headers['Content-Type'], self.assertEqual(resp.headers['Content-Type'],
'application/json; charset=utf-8') 'application/json; charset=utf-8')
self.assertEqual(self.fake_swift.calls[-1], ( self.assertEqual(self.fake_swift.calls[-1], (
'GET', '/v1/a?format=json')) 'GET', '/v1/a?format=json'))
req = Request.blank('/v1/a?format=xml') req = Request.blank('/v1/a?format=xml')
resp = req.get_response(self.app) resp = req.get_response(self.app)
self.assertEqual(resp.body.split('\n'), [ self.assertEqual(resp.body.split(b'\n'), [
'<?xml version="1.0" encoding="UTF-8"?>', b'<?xml version="1.0" encoding="UTF-8"?>',
'<account name="a">', b'<account name="a">',
'<container><name>bar</name><count>0</count><bytes>0</bytes>' b'<container><name>bar</name><count>0</count><bytes>0</bytes>'
'<last_modified>1970-01-01T00:00:00.000000</last_modified>' b'<last_modified>1970-01-01T00:00:00.000000</last_modified>'
'</container>', b'</container>',
'<subdir name="foo_" />', b'<subdir name="foo_" />',
'</account>', b'</account>',
]) ])
self.assertEqual(resp.headers['Content-Type'], self.assertEqual(resp.headers['Content-Type'],
'application/xml; charset=utf-8') 'application/xml; charset=utf-8')
self.assertEqual(self.fake_swift.calls[-1], ( self.assertEqual(self.fake_swift.calls[-1], (
'GET', '/v1/a?format=json')) 'GET', '/v1/a?format=json'))
def test_valid_container(self): def test_valid_container(self):
self.fake_swift.register('GET', '/v1/a/c', HTTPOk, { self.fake_swift.register('GET', '/v1/a/c', HTTPOk, {
'Content-Length': str(len(self.fake_container_listing)), 'Content-Length': str(len(self.fake_container_listing)),
'Content-Type': 'application/json'}, self.fake_container_listing) 'Content-Type': 'application/json'}, self.fake_container_listing)
req = Request.blank('/v1/a/c') req = Request.blank('/v1/a/c')
resp = req.get_response(self.app) resp = req.get_response(self.app)
self.assertEqual(resp.body, 'bar\nfoo/\n') self.assertEqual(resp.body, b'bar\nfoo/\n')
self.assertEqual(resp.headers['Content-Type'], self.assertEqual(resp.headers['Content-Type'],
'text/plain; charset=utf-8') 'text/plain; charset=utf-8')
self.assertEqual(self.fake_swift.calls[-1], ( self.assertEqual(self.fake_swift.calls[-1], (
'GET', '/v1/a/c?format=json')) 'GET', '/v1/a/c?format=json'))
req = Request.blank('/v1/a/c?format=txt') req = Request.blank('/v1/a/c?format=txt')
resp = req.get_response(self.app) resp = req.get_response(self.app)
self.assertEqual(resp.body, 'bar\nfoo/\n') self.assertEqual(resp.body, b'bar\nfoo/\n')
self.assertEqual(resp.headers['Content-Type'], self.assertEqual(resp.headers['Content-Type'],
'text/plain; charset=utf-8') 'text/plain; charset=utf-8')
self.assertEqual(self.fake_swift.calls[-1], ( self.assertEqual(self.fake_swift.calls[-1], (
'GET', '/v1/a/c?format=json')) 'GET', '/v1/a/c?format=json'))
req = Request.blank('/v1/a/c?format=json') req = Request.blank('/v1/a/c?format=json')
resp = req.get_response(self.app) resp = req.get_response(self.app)
self.assertEqual(resp.body, self.fake_container_listing) self.assertEqual(resp.body, self.fake_container_listing)
self.assertEqual(resp.headers['Content-Type'], self.assertEqual(resp.headers['Content-Type'],
'application/json; charset=utf-8') 'application/json; charset=utf-8')
self.assertEqual(self.fake_swift.calls[-1], ( self.assertEqual(self.fake_swift.calls[-1], (
'GET', '/v1/a/c?format=json')) 'GET', '/v1/a/c?format=json'))
req = Request.blank('/v1/a/c?format=xml') req = Request.blank('/v1/a/c?format=xml')
resp = req.get_response(self.app) resp = req.get_response(self.app)
self.assertEqual( self.assertEqual(
resp.body, resp.body,
'<?xml version="1.0" encoding="UTF-8"?>\n' b'<?xml version="1.0" encoding="UTF-8"?>\n'
'<container name="c">' b'<container name="c">'
'<object><name>bar</name><hash>etag</hash><bytes>0</bytes>' b'<object><name>bar</name><hash>etag</hash><bytes>0</bytes>'
'<content_type>text/plain</content_type>' b'<content_type>text/plain</content_type>'
'<last_modified>1970-01-01T00:00:00.000000</last_modified>' b'<last_modified>1970-01-01T00:00:00.000000</last_modified>'
'</object>' b'</object>'
'<subdir name="foo/"><name>foo/</name></subdir>' b'<subdir name="foo/"><name>foo/</name></subdir>'
'</container>' b'</container>'
) )
self.assertEqual(resp.headers['Content-Type'], self.assertEqual(resp.headers['Content-Type'],
'application/xml; charset=utf-8') 'application/xml; charset=utf-8')
self.assertEqual(self.fake_swift.calls[-1], ( self.assertEqual(self.fake_swift.calls[-1], (
'GET', '/v1/a/c?format=json')) 'GET', '/v1/a/c?format=json'))
def test_blank_account(self): def test_blank_account(self):
self.fake_swift.register('GET', '/v1/a', HTTPOk, { self.fake_swift.register('GET', '/v1/a', HTTPOk, {
'Content-Length': '2', 'Content-Type': 'application/json'}, '[]') 'Content-Length': '2', 'Content-Type': 'application/json'}, b'[]')
req = Request.blank('/v1/a') req = Request.blank('/v1/a')
resp = req.get_response(self.app) resp = req.get_response(self.app)
self.assertEqual(resp.status, '204 No Content') self.assertEqual(resp.status, '204 No Content')
self.assertEqual(resp.body, '') self.assertEqual(resp.body, b'')
self.assertEqual(resp.headers['Content-Type'], self.assertEqual(resp.headers['Content-Type'],
'text/plain; charset=utf-8') 'text/plain; charset=utf-8')
self.assertEqual(self.fake_swift.calls[-1], ( self.assertEqual(self.fake_swift.calls[-1], (
'GET', '/v1/a?format=json')) 'GET', '/v1/a?format=json'))
req = Request.blank('/v1/a?format=txt') req = Request.blank('/v1/a?format=txt')
resp = req.get_response(self.app) resp = req.get_response(self.app)
self.assertEqual(resp.status, '204 No Content') self.assertEqual(resp.status, '204 No Content')
self.assertEqual(resp.body, '') self.assertEqual(resp.body, b'')
self.assertEqual(resp.headers['Content-Type'], self.assertEqual(resp.headers['Content-Type'],
'text/plain; charset=utf-8') 'text/plain; charset=utf-8')
self.assertEqual(self.fake_swift.calls[-1], ( self.assertEqual(self.fake_swift.calls[-1], (
'GET', '/v1/a?format=json')) 'GET', '/v1/a?format=json'))
req = Request.blank('/v1/a?format=json') req = Request.blank('/v1/a?format=json')
resp = req.get_response(self.app) resp = req.get_response(self.app)
self.assertEqual(resp.status, '200 OK') self.assertEqual(resp.status, '200 OK')
self.assertEqual(resp.body, '[]') self.assertEqual(resp.body, b'[]')
self.assertEqual(resp.headers['Content-Type'], self.assertEqual(resp.headers['Content-Type'],
'application/json; charset=utf-8') 'application/json; charset=utf-8')
self.assertEqual(self.fake_swift.calls[-1], ( self.assertEqual(self.fake_swift.calls[-1], (
'GET', '/v1/a?format=json')) 'GET', '/v1/a?format=json'))
req = Request.blank('/v1/a?format=xml') req = Request.blank('/v1/a?format=xml')
resp = req.get_response(self.app) resp = req.get_response(self.app)
self.assertEqual(resp.status, '200 OK') self.assertEqual(resp.status, '200 OK')
self.assertEqual(resp.body.split('\n'), [ self.assertEqual(resp.body.split(b'\n'), [
'<?xml version="1.0" encoding="UTF-8"?>', b'<?xml version="1.0" encoding="UTF-8"?>',
'<account name="a">', b'<account name="a">',
'</account>', b'</account>',
]) ])
self.assertEqual(resp.headers['Content-Type'], self.assertEqual(resp.headers['Content-Type'],
'application/xml; charset=utf-8') 'application/xml; charset=utf-8')
self.assertEqual(self.fake_swift.calls[-1], ( self.assertEqual(self.fake_swift.calls[-1], (
'GET', '/v1/a?format=json')) 'GET', '/v1/a?format=json'))
def test_blank_container(self): def test_blank_container(self):
self.fake_swift.register('GET', '/v1/a/c', HTTPOk, { self.fake_swift.register('GET', '/v1/a/c', HTTPOk, {
'Content-Length': '2', 'Content-Type': 'application/json'}, '[]') 'Content-Length': '2', 'Content-Type': 'application/json'}, b'[]')
req = Request.blank('/v1/a/c') req = Request.blank('/v1/a/c')
resp = req.get_response(self.app) resp = req.get_response(self.app)
self.assertEqual(resp.status, '204 No Content') self.assertEqual(resp.status, '204 No Content')
self.assertEqual(resp.body, '') self.assertEqual(resp.body, b'')
self.assertEqual(resp.headers['Content-Type'], self.assertEqual(resp.headers['Content-Type'],
'text/plain; charset=utf-8') 'text/plain; charset=utf-8')
self.assertEqual(self.fake_swift.calls[-1], ( self.assertEqual(self.fake_swift.calls[-1], (
'GET', '/v1/a/c?format=json')) 'GET', '/v1/a/c?format=json'))
req = Request.blank('/v1/a/c?format=txt') req = Request.blank('/v1/a/c?format=txt')
resp = req.get_response(self.app) resp = req.get_response(self.app)
self.assertEqual(resp.status, '204 No Content') self.assertEqual(resp.status, '204 No Content')
self.assertEqual(resp.body, '') self.assertEqual(resp.body, b'')
self.assertEqual(resp.headers['Content-Type'], self.assertEqual(resp.headers['Content-Type'],
'text/plain; charset=utf-8') 'text/plain; charset=utf-8')
self.assertEqual(self.fake_swift.calls[-1], ( self.assertEqual(self.fake_swift.calls[-1], (
'GET', '/v1/a/c?format=json')) 'GET', '/v1/a/c?format=json'))
req = Request.blank('/v1/a/c?format=json') req = Request.blank('/v1/a/c?format=json')
resp = req.get_response(self.app) resp = req.get_response(self.app)
self.assertEqual(resp.status, '200 OK') self.assertEqual(resp.status, '200 OK')
self.assertEqual(resp.body, '[]') self.assertEqual(resp.body, b'[]')
self.assertEqual(resp.headers['Content-Type'], self.assertEqual(resp.headers['Content-Type'],
'application/json; charset=utf-8') 'application/json; charset=utf-8')
self.assertEqual(self.fake_swift.calls[-1], ( self.assertEqual(self.fake_swift.calls[-1], (
'GET', '/v1/a/c?format=json')) 'GET', '/v1/a/c?format=json'))
req = Request.blank('/v1/a/c?format=xml') req = Request.blank('/v1/a/c?format=xml')
resp = req.get_response(self.app) resp = req.get_response(self.app)
self.assertEqual(resp.status, '200 OK') self.assertEqual(resp.status, '200 OK')
self.assertEqual(resp.body.split('\n'), [ self.assertEqual(resp.body.split(b'\n'), [
'<?xml version="1.0" encoding="UTF-8"?>', b'<?xml version="1.0" encoding="UTF-8"?>',
'<container name="c" />', b'<container name="c" />',
]) ])
self.assertEqual(resp.headers['Content-Type'], self.assertEqual(resp.headers['Content-Type'],
'application/xml; charset=utf-8') 'application/xml; charset=utf-8')
self.assertEqual(self.fake_swift.calls[-1], ( self.assertEqual(self.fake_swift.calls[-1], (
'GET', '/v1/a/c?format=json')) 'GET', '/v1/a/c?format=json'))
def test_pass_through(self): def test_pass_through(self):
def do_test(path): def do_test(path):
self.fake_swift.register( self.fake_swift.register(
'GET', path, HTTPOk, { 'GET', path, HTTPOk, {
skipping to change at line 238 skipping to change at line 238
self.assertEqual(resp.headers['Content-Type'], 'application/json') self.assertEqual(resp.headers['Content-Type'], 'application/json')
self.assertEqual(self.fake_swift.calls[-1], ( self.assertEqual(self.fake_swift.calls[-1], (
'GET', path + '?format=xml')) # query param is unchanged 'GET', path + '?format=xml')) # query param is unchanged
do_test('/') do_test('/')
do_test('/v1') do_test('/v1')
do_test('/auth/v1.0') do_test('/auth/v1.0')
do_test('/v1/a/c/o') do_test('/v1/a/c/o')
def test_static_web_not_json(self): def test_static_web_not_json(self):
body = 'doesnt matter' body = b'doesnt matter'
self.fake_swift.register( self.fake_swift.register(
'GET', '/v1/staticweb/not-json', HTTPOk, 'GET', '/v1/staticweb/not-json', HTTPOk,
{'Content-Length': str(len(body)), {'Content-Length': str(len(body)),
'Content-Type': 'text/plain'}, 'Content-Type': 'text/plain'},
body) body)
resp = Request.blank('/v1/staticweb/not-json').get_response(self.app) resp = Request.blank('/v1/staticweb/not-json').get_response(self.app)
self.assertEqual(resp.body, body) self.assertEqual(resp.body, body)
self.assertEqual(resp.headers['Content-Type'], 'text/plain') self.assertEqual(resp.headers['Content-Type'], 'text/plain')
# We *did* try, though # We *did* try, though
self.assertEqual(self.fake_swift.calls[-1], ( self.assertEqual(self.fake_swift.calls[-1], (
'GET', '/v1/staticweb/not-json?format=json')) 'GET', '/v1/staticweb/not-json?format=json'))
# TODO: add a similar test that has *no* content-type # TODO: add a similar test that has *no* content-type
# FakeSwift seems to make this hard to do # FakeSwift seems to make this hard to do
def test_static_web_not_really_json(self): def test_static_web_not_really_json(self):
body = 'raises ValueError' body = b'raises ValueError'
self.fake_swift.register( self.fake_swift.register(
'GET', '/v1/staticweb/not-json', HTTPOk, 'GET', '/v1/staticweb/not-json', HTTPOk,
{'Content-Length': str(len(body)), {'Content-Length': str(len(body)),
'Content-Type': 'application/json'}, 'Content-Type': 'application/json'},
body) body)
resp = Request.blank('/v1/staticweb/not-json').get_response(self.app) resp = Request.blank('/v1/staticweb/not-json').get_response(self.app)
self.assertEqual(resp.body, body) self.assertEqual(resp.body, body)
self.assertEqual(resp.headers['Content-Type'], 'application/json') self.assertEqual(resp.headers['Content-Type'], 'application/json')
self.assertEqual(self.fake_swift.calls[-1], ( self.assertEqual(self.fake_swift.calls[-1], (
'GET', '/v1/staticweb/not-json?format=json')) 'GET', '/v1/staticweb/not-json?format=json'))
def test_static_web_pretend_to_be_giant_json(self): def test_static_web_pretend_to_be_giant_json(self):
body = json.dumps([ body = json.dumps([
{'name': 'bar', 'hash': 'etag', 'bytes': 0, {'name': 'bar', 'hash': 'etag', 'bytes': 0,
'content_type': 'text/plain', 'content_type': 'text/plain',
'last_modified': '1970-01-01T00:00:00.000000'}, 'last_modified': '1970-01-01T00:00:00.000000'},
{'subdir': 'foo/'}, {'subdir': 'foo/'},
] * 160000) ] * 160000).encode('ascii')
self.assertGreater( # sanity self.assertGreater( # sanity
len(body), listing_formats.MAX_CONTAINER_LISTING_CONTENT_LENGTH) len(body), listing_formats.MAX_CONTAINER_LISTING_CONTENT_LENGTH)
self.fake_swift.register( self.fake_swift.register(
'GET', '/v1/staticweb/long-json', HTTPOk, 'GET', '/v1/staticweb/long-json', HTTPOk,
{'Content-Type': 'application/json'}, {'Content-Type': 'application/json'},
body) body)
resp = Request.blank('/v1/staticweb/long-json').get_response(self.app) resp = Request.blank('/v1/staticweb/long-json').get_response(self.app)
self.assertEqual(resp.headers['Content-Type'], 'application/json') self.assertEqual(resp.headers['Content-Type'], 'application/json')
self.assertEqual(resp.body, body) self.assertEqual(resp.body, body)
self.assertEqual(self.fake_swift.calls[-1], ( self.assertEqual(self.fake_swift.calls[-1], (
'GET', '/v1/staticweb/long-json?format=json')) 'GET', '/v1/staticweb/long-json?format=json'))
# TODO: add a similar test for chunked transfers # TODO: add a similar test for chunked transfers
# (staticweb referencing a DLO that doesn't fit in a single listing?) # (staticweb referencing a DLO that doesn't fit in a single listing?)
def test_static_web_bad_json(self): def test_static_web_bad_json(self):
def do_test(body_obj): def do_test(body_obj):
body = json.dumps(body_obj) body = json.dumps(body_obj).encode('ascii')
self.fake_swift.register( self.fake_swift.register(
'GET', '/v1/staticweb/bad-json', HTTPOk, 'GET', '/v1/staticweb/bad-json', HTTPOk,
{'Content-Length': str(len(body)), {'Content-Length': str(len(body)),
'Content-Type': 'application/json'}, 'Content-Type': 'application/json'},
body) body)
def do_sub_test(path): def do_sub_test(path):
resp = Request.blank(path).get_response(self.app) resp = Request.blank(path).get_response(self.app)
self.assertEqual(resp.body, body) self.assertEqual(resp.body, body)
# NB: no charset is added; we pass through whatever we got # NB: no charset is added; we pass through whatever we got
skipping to change at line 324 skipping to change at line 324
do_test({}) do_test({})
do_test({'non-empty': 'hash'}) do_test({'non-empty': 'hash'})
do_test(None) do_test(None)
do_test(0) do_test(0)
do_test('some string') do_test('some string')
do_test([None]) do_test([None])
do_test([0]) do_test([0])
do_test(['some string']) do_test(['some string'])
def test_static_web_bad_but_not_terrible_json(self): def test_static_web_bad_but_not_terrible_json(self):
body = json.dumps([{'no name': 'nor subdir'}]) body = json.dumps([{'no name': 'nor subdir'}]).encode('ascii')
self.fake_swift.register( self.fake_swift.register(
'GET', '/v1/staticweb/bad-json', HTTPOk, 'GET', '/v1/staticweb/bad-json', HTTPOk,
{'Content-Length': str(len(body)), {'Content-Length': str(len(body)),
'Content-Type': 'application/json'}, 'Content-Type': 'application/json'},
body) body)
def do_test(path, expect_charset=False): def do_test(path, expect_charset=False):
resp = Request.blank(path).get_response(self.app) resp = Request.blank(path).get_response(self.app)
self.assertEqual(resp.body, body) self.assertEqual(resp.body, body)
if expect_charset: if expect_charset:
 End of changes. 23 change blocks. 
42 lines changed or deleted 42 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)