swob: Stop auto-encoding unicode bodies

Instead, require that callers provide an encoding.

Related-Change: I31408f525ba9836f634a35581d4aee6fa2c9428f
Change-Id: I3e5ed9e4401eea76c375bb43ad4afc58b1d8006a
This commit is contained in:
Tim Burke
2018-06-26 15:56:56 -07:00
parent bea74a3260
commit d03fc9bc54
9 changed files with 99 additions and 72 deletions

View File

@@ -29,7 +29,7 @@ class FakeApp(object):
def __call__(self, env, start_response):
self.req = Request(env)
return Response(request=self.req, body='FAKE APP',
return Response(request=self.req, body=b'FAKE APP',
headers=self.headers)(env, start_response)
@@ -224,7 +224,7 @@ class TestGatekeeper(unittest.TestCase):
class SelfishApp(FakeApp):
def __call__(self, env, start_response):
self.req = Request(env)
resp = Response(request=self.req, body='FAKE APP',
resp = Response(request=self.req, body=b'FAKE APP',
headers=self.headers)
# like webob, middlewares in the pipeline may rewrite
# location header from relative to absolute

View File

@@ -25,7 +25,7 @@ from swift.common.middleware import healthcheck
class FakeApp(object):
def __call__(self, env, start_response):
req = Request(env)
return Response(request=req, body='FAKE APP')(
return Response(request=req, body=b'FAKE APP')(
env, start_response)

View File

@@ -1342,7 +1342,15 @@ class TestAccountAcls(unittest.TestCase):
resp = req.get_response(test_auth)
self.assertEqual(resp.status_int, 400)
self.assertTrue(resp.body.startswith(
errmsg % "Key 'other-auth-system' not recognized"), resp.body)
errmsg % 'Key "other-auth-system" not recognized'), resp.body)
# and do something sane with crazy data
update = {'x-account-access-control': u'{"\u1234": []}'.encode('utf8')}
req = self._make_request(target, headers=dict(good_headers, **update))
resp = req.get_response(test_auth)
self.assertEqual(resp.status_int, 400)
self.assertTrue(resp.body.startswith(
errmsg % 'Key "\\u1234" not recognized'), resp.body)
# acls with good keys but bad values also get a 400
update = {'x-account-access-control': bad_value_acl}
@@ -1350,7 +1358,7 @@ class TestAccountAcls(unittest.TestCase):
resp = req.get_response(test_auth)
self.assertEqual(resp.status_int, 400)
self.assertTrue(resp.body.startswith(
errmsg % "Value for key 'admin' must be a list"), resp.body)
errmsg % 'Value for key "admin" must be a list'), resp.body)
# acls with non-string-types in list also get a 400
update = {'x-account-access-control': bad_list_types}
@@ -1358,7 +1366,7 @@ class TestAccountAcls(unittest.TestCase):
resp = req.get_response(test_auth)
self.assertEqual(resp.status_int, 400)
self.assertTrue(resp.body.startswith(
errmsg % "Elements of 'read-only' list must be strings"),
errmsg % 'Elements of "read-only" list must be strings'),
resp.body)
# acls with wrong json structure also get a 400

View File

@@ -832,7 +832,7 @@ class TestRequest(unittest.TestCase):
@swift.common.swob.wsgify
def _wsgi_func(req):
used_req.append(req)
return swift.common.swob.Response('200 OK')
return swift.common.swob.Response(b'200 OK')
req = swift.common.swob.Request.blank('/hi/there')
resp = req.get_response(_wsgi_func)
@@ -1117,13 +1117,15 @@ class TestResponse(unittest.TestCase):
def test_empty_body(self):
resp = self._get_response()
resp.body = ''
resp.body = b''
self.assertEqual(resp.body, b'')
def test_unicode_body(self):
resp = self._get_response()
resp.body = u'\N{SNOWMAN}'
self.assertEqual(resp.body, u'\N{SNOWMAN}'.encode('utf-8'))
with self.assertRaises(TypeError) as catcher:
resp.body = u'\N{SNOWMAN}'
self.assertEqual(str(catcher.exception),
'WSGI responses must be bytes')
def test_call_reifies_request_if_necessary(self):
"""
@@ -1388,7 +1390,7 @@ class TestResponse(unittest.TestCase):
'/', headers={'Range': 'bytes=1-3'})
resp = swift.common.swob.Response(
body='1234567890', request=req,
body=b'1234567890', request=req,
conditional_response=True)
body = b''.join(resp({}, start_response))
self.assertEqual(body, b'234')
@@ -1408,7 +1410,7 @@ class TestResponse(unittest.TestCase):
self.assertEqual(resp.content_range, 'bytes */10')
resp = swift.common.swob.Response(
body='1234567890', request=req,
body=b'1234567890', request=req,
conditional_response=True)
body = b''.join(resp({}, start_response))
self.assertIn(b'The Range requested is not available', body)
@@ -1426,7 +1428,7 @@ class TestResponse(unittest.TestCase):
self.assertNotIn('Content-Range', resp.headers)
resp = swift.common.swob.Response(
body='1234567890', request=req,
body=b'1234567890', request=req,
conditional_response=True)
body = b''.join(resp({}, start_response))
self.assertEqual(body, b'1234567890')
@@ -1575,7 +1577,7 @@ class TestResponse(unittest.TestCase):
# body, headers with content_length and app_iter exist
resp = swift.common.swob.Response(
body='ok', headers={'Content-Length': '5'}, app_iter=iter([]))
body=b'ok', headers={'Content-Length': '5'}, app_iter=iter([]))
self.assertEqual(resp.content_length, 5)
self.assertEqual(resp.body, b'')