def default(self, data):
# We expect data to contain a single key which is the XML root.
- root_key = data.keys()[0]
+ root_key = list(data.keys())[0]
doc = minidom.Document()
node = self._to_xml_node(doc, self.metadata, root_key, data[root_key])
response.headers[hdr] = value
response.headers['Content-Type'] = content_type
if self.obj is not None:
- response.body = serializer.serialize(self.obj)
+ body = serializer.serialize(self.obj)
+ if isinstance(body, six.text_type):
+ body = body.encode('utf-8')
+ response.body = body
return response
raise exception.MalformedRequestBody(reason=msg)
# Return the action and the decoded body...
- return decoded.keys()[0]
+ return list(decoded.keys())[0]
def action_peek_xml(body):
class RequestTest(test.TestCase):
def test_content_type_missing(self):
request = wsgi.Request.blank('/tests/123', method='POST')
- request.body = "<body />"
+ request.body = b"<body />"
self.assertIsNone(request.get_content_type())
def test_content_type_unsupported(self):
request = wsgi.Request.blank('/tests/123', method='POST')
request.headers["Content-Type"] = "text/html"
- request.body = "asdf<br />"
+ request.body = b"asdf<br />"
self.assertRaises(exception.InvalidContentType,
request.get_content_type)
class XMLDictSerializerTest(test.TestCase):
def test_xml(self):
input_dict = dict(servers=dict(a=(2, 3)))
- expected_xml = '<serversxmlns="asdf"><a>(2,3)</a></servers>'
+ expected_xml = b'<serversxmlns="asdf"><a>(2,3)</a></servers>'
serializer = wsgi.XMLDictSerializer(xmlns="asdf")
result = serializer.serialize(input_dict)
- result = result.replace('\n', '').replace(' ', '')
+ result = result.replace(b'\n', b'').replace(b' ', b'')
self.assertEqual(expected_xml, result)
req = webob.Request.blank('/tests')
app = fakes.TestRouter(Controller())
response = req.get_response(app)
- self.assertEqual('off', response.body)
+ self.assertEqual(b'off', response.body)
self.assertEqual(200, response.status_int)
def test_resource_not_authorized(self):
request = wsgi.Request.blank('/', method='POST')
request.headers['Content-Type'] = 'application/none'
- request.body = 'foo'
+ request.body = b'foo'
content_type, body = resource.get_body(request)
self.assertIsNone(content_type)
resource = wsgi.Resource(controller)
request = wsgi.Request.blank('/', method='POST')
- request.body = 'foo'
+ request.body = b'foo'
content_type, body = resource.get_body(request)
self.assertIsNone(content_type)
request = wsgi.Request.blank('/', method='POST')
request.headers['Content-Type'] = 'application/json'
- request.body = ''
+ request.body = b''
content_type, body = resource.get_body(request)
self.assertIsNone(content_type)
request = wsgi.Request.blank('/', method='POST')
request.headers['Content-Type'] = 'application/json'
- request.body = 'foo'
+ request.body = b'foo'
content_type, body = resource.get_body(request)
self.assertEqual('application/json', content_type)
- self.assertEqual('foo', body)
+ self.assertEqual(b'foo', body)
def test_deserialize_badtype(self):
class Controller(object):
self.assertEqual('header1', response.headers['X-header1'])
self.assertEqual('header2', response.headers['X-header2'])
self.assertEqual(202, response.status_int)
- self.assertEqual(mtype, response.body)
+ self.assertEqual(mtype, response.body.decode('utf-8'))
class ValidBodyTest(test.TestCase):
def __call__(self, environ, start_response):
start_response("200", [("X-Test", "checking")])
- return ['Test result']
+ return [b'Test result']
- with mock.patch('sys.stdout', new=six.StringIO()):
+ with mock.patch('sys.stdout', new=six.StringIO()) as mock_stdout:
+ mock_stdout.buffer = six.BytesIO()
application = wsgi.Debug(Application())
result = webob.Request.blank('/').get_response(application)
- self.assertEqual("Test result", result.body)
+ self.assertEqual(b"Test result", result.body)
def test_router(self):
def __call__(self, environ, start_response):
start_response("200", [])
- return ['Router result']
+ return [b'Router result']
class Router(wsgi.Router):
"""Test router."""
super(Router, self).__init__(mapper)
result = webob.Request.blank('/test').get_response(Router())
- self.assertEqual("Router result", result.body)
+ self.assertEqual(b"Router result", result.body)
result = webob.Request.blank('/bad').get_response(Router())
- self.assertNotEqual("Router result", result.body)
+ self.assertNotEqual(b"Router result", result.body)