py34: heat.tests.api (1)

* convert to bytes for response.body
* assert against dicts of params instead of string url params
* use py3 compatible stubout from mox3 than the default stubout module
* NoneType and int aren't comparable, so use int(NoneType or 0)
* use list() to use pop() over return value of filter()

partial blueprint heat-python34-support

Change-Id: Iadab94ff7446a3f5a01e45d5408f3219ef518c6a
This commit is contained in:
Sirushti Murugesan 2015-08-28 14:43:37 +05:30
parent 4dba62a7bb
commit 1710ceead5
7 changed files with 33 additions and 26 deletions

View File

@ -598,7 +598,7 @@ class StackSerializer(serializers.JSONResponseSerializer):
self._populate_response_header(response,
result['stack']['links'][0]['href'],
201)
response.body = self.to_json(result)
response.body = six.b(self.to_json(result))
return response

View File

@ -761,7 +761,7 @@ def is_json_content_type(request):
if not content_type or content_type.startswith('text/plain'):
content_type = 'application/json'
if (content_type in ('JSON', 'application/json')
and request.body.startswith('{')):
and request.body.startswith(b'{')):
return True
return False
@ -773,7 +773,8 @@ class JSONRequestDeserializer(object):
:param request: Webob.Request object
"""
if request.content_length > 0 and is_json_content_type(request):
if (int(request.content_length or 0) > 0 and
is_json_content_type(request)):
return True
return False

View File

@ -2211,5 +2211,5 @@ class StackSerializerTest(common.HeatTestCase):
response = webob.Response()
response = self.serializer.create(response, result)
self.assertEqual(201, response.status_int)
self.assertEqual('location', response.headers['Location'])
self.assertEqual(b'location', response.headers['Location'])
self.assertEqual('application/json', response.headers['Content-Type'])

View File

@ -39,7 +39,8 @@ class TestViewsCommon(common.HeatTestCase):
links = views_common.get_collection_links(self.request, self.items)
expected_params = {'marker': ['id2'], 'limit': ['2']}
next_link = filter(lambda link: link['rel'] == 'next', links).pop()
next_link = list(filter(
lambda link: link['rel'] == 'next', links)).pop()
self.assertEqual('next', next_link['rel'])
url_path, url_params = next_link['href'].split('?', 1)
self.assertEqual(url_path, self.request.path_url)
@ -63,9 +64,9 @@ class TestViewsCommon(common.HeatTestCase):
self.setUpGetCollectionLinks()
self.request.params = {'limit': '2', 'marker': 'some_marker'}
links = views_common.get_collection_links(self.request, self.items)
expected_params = {'marker': ['id2'], 'limit': ['2']}
next_link = filter(lambda link: link['rel'] == 'next', links).pop()
next_link = list(filter(
lambda link: link['rel'] == 'next', links)).pop()
self.assertEqual('next', next_link['rel'])
url_path, url_params = next_link['href'].split('?', 1)
self.assertEqual(url_path, self.request.path_url)
@ -76,7 +77,8 @@ class TestViewsCommon(common.HeatTestCase):
self.request.params = {'limit': '2', 'foo': 'bar'}
links = views_common.get_collection_links(self.request, self.items)
next_link = filter(lambda link: link['rel'] == 'next', links).pop()
next_link = list(
filter(lambda link: link['rel'] == 'next', links)).pop()
url = next_link['href']
query_string = urlparse.urlparse(url).query
params = {}

View File

@ -14,6 +14,7 @@
from oslo_config import cfg
from oslo_log import log
from oslo_messaging._drivers import common as rpc_common
import six
import webob.exc
from heat.common import wsgi
@ -95,7 +96,7 @@ class ControllerTest(object):
req = wsgi.Request(environ)
req.context = utils.dummy_context('api_test_user', self.tenant)
self.context = req.context
req.body = data
req.body = six.b(data)
return req
def _post(self, path, data, content_type='application/json'):

View File

@ -20,9 +20,9 @@ import json
import mock
import six
import socket
import stubout
import webob
from mox import stubout
from oslo_config import cfg
from heat.api.aws import exception as aws_exception
@ -190,7 +190,7 @@ class ResourceTest(common.HeatTestCase):
actions = {'action': 'delete', 'id': 12, 'body': 'data'}
env = {'wsgiorg.routing_args': [None, actions]}
request = wsgi.Request.blank('/tests/123', environ=env)
request.body = '{"foo" : "value"}'
request.body = b'{"foo" : "value"}'
resource = wsgi.Resource(Controller(),
wsgi.JSONRequestDeserializer(),
None)
@ -210,7 +210,7 @@ class ResourceTest(common.HeatTestCase):
actions = {'action': 'delete', 'id': 12, 'body': 'data'}
env = {'wsgiorg.routing_args': [None, actions]}
request = wsgi.Request.blank('/tests/123', environ=env)
request.body = '{"foo" : "value"}'
request.body = b'{"foo" : "value"}'
message_es = "No Encontrado"
translated_ex = webob.exc.HTTPBadRequest(message_es)
@ -257,7 +257,7 @@ class ResourceExceptionHandlingTest(common.HeatTestCase):
actions = {'action': 'raise_exception', 'body': 'data'}
env = {'wsgiorg.routing_args': [None, actions]}
request = wsgi.Request.blank('/tests/123', environ=env)
request.body = '{"foo" : "value"}'
request.body = b'{"foo" : "value"}'
resource = wsgi.Resource(Controller(self.exception),
wsgi.JSONRequestDeserializer(),
None)
@ -271,7 +271,7 @@ class JSONRequestDeserializerTest(common.HeatTestCase):
def test_has_body_no_content_length(self):
request = wsgi.Request.blank('/')
request.method = 'POST'
request.body = 'asdf'
request.body = b'asdf'
request.headers.pop('Content-Length')
request.headers['Content-Type'] = 'application/json'
self.assertFalse(wsgi.JSONRequestDeserializer().has_body(request))
@ -279,7 +279,7 @@ class JSONRequestDeserializerTest(common.HeatTestCase):
def test_has_body_zero_content_length(self):
request = wsgi.Request.blank('/')
request.method = 'POST'
request.body = 'asdf'
request.body = b'asdf'
request.headers['Content-Length'] = 0
request.headers['Content-Type'] = 'application/json'
self.assertFalse(wsgi.JSONRequestDeserializer().has_body(request))
@ -287,14 +287,14 @@ class JSONRequestDeserializerTest(common.HeatTestCase):
def test_has_body_has_content_length_no_content_type(self):
request = wsgi.Request.blank('/')
request.method = 'POST'
request.body = '{"key": "value"}'
request.body = b'{"key": "value"}'
self.assertIn('Content-Length', request.headers)
self.assertTrue(wsgi.JSONRequestDeserializer().has_body(request))
def test_has_body_has_content_length_plain_content_type(self):
request = wsgi.Request.blank('/')
request.method = 'POST'
request.body = '{"key": "value"}'
request.body = b'{"key": "value"}'
self.assertIn('Content-Length', request.headers)
request.headers['Content-Type'] = 'text/plain'
self.assertTrue(wsgi.JSONRequestDeserializer().has_body(request))
@ -302,7 +302,7 @@ class JSONRequestDeserializerTest(common.HeatTestCase):
def test_has_body_has_content_type_malformed(self):
request = wsgi.Request.blank('/')
request.method = 'POST'
request.body = 'asdf'
request.body = b'asdf'
self.assertIn('Content-Length', request.headers)
request.headers['Content-Type'] = 'application/json'
self.assertFalse(wsgi.JSONRequestDeserializer().has_body(request))
@ -310,7 +310,7 @@ class JSONRequestDeserializerTest(common.HeatTestCase):
def test_has_body_has_content_type(self):
request = wsgi.Request.blank('/')
request.method = 'POST'
request.body = '{"key": "value"}'
request.body = b'{"key": "value"}'
self.assertIn('Content-Length', request.headers)
request.headers['Content-Type'] = 'application/json'
self.assertTrue(wsgi.JSONRequestDeserializer().has_body(request))
@ -318,7 +318,7 @@ class JSONRequestDeserializerTest(common.HeatTestCase):
def test_has_body_has_wrong_content_type(self):
request = wsgi.Request.blank('/')
request.method = 'POST'
request.body = '{"key": "value"}'
request.body = b'{"key": "value"}'
self.assertIn('Content-Length', request.headers)
request.headers['Content-Type'] = 'application/xml'
self.assertFalse(wsgi.JSONRequestDeserializer().has_body(request))
@ -326,14 +326,14 @@ class JSONRequestDeserializerTest(common.HeatTestCase):
def test_has_body_has_aws_content_type_only(self):
request = wsgi.Request.blank('/?ContentType=JSON')
request.method = 'GET'
request.body = '{"key": "value"}'
request.body = b'{"key": "value"}'
self.assertIn('Content-Length', request.headers)
self.assertTrue(wsgi.JSONRequestDeserializer().has_body(request))
def test_has_body_respect_aws_content_type(self):
request = wsgi.Request.blank('/?ContentType=JSON')
request.method = 'GET'
request.body = '{"key": "value"}'
request.body = b'{"key": "value"}'
self.assertIn('Content-Length', request.headers)
request.headers['Content-Type'] = 'application/xml'
self.assertTrue(wsgi.JSONRequestDeserializer().has_body(request))
@ -341,7 +341,7 @@ class JSONRequestDeserializerTest(common.HeatTestCase):
def test_has_body_content_type_with_get(self):
request = wsgi.Request.blank('/')
request.method = 'GET'
request.body = '{"key": "value"}'
request.body = b'{"key": "value"}'
self.assertIn('Content-Length', request.headers)
self.assertTrue(wsgi.JSONRequestDeserializer().has_body(request))
@ -369,7 +369,7 @@ class JSONRequestDeserializerTest(common.HeatTestCase):
def test_default_with_body(self):
request = wsgi.Request.blank('/')
request.method = 'POST'
request.body = '{"key": "value"}'
request.body = b'{"key": "value"}'
actual = wsgi.JSONRequestDeserializer().default(request)
expected = {"body": {"key": "value"}}
self.assertEqual(expected, actual)
@ -377,7 +377,7 @@ class JSONRequestDeserializerTest(common.HeatTestCase):
def test_default_with_get_with_body(self):
request = wsgi.Request.blank('/')
request.method = 'GET'
request.body = '{"key": "value"}'
request.body = b'{"key": "value"}'
actual = wsgi.JSONRequestDeserializer().default(request)
expected = {"body": {"key": "value"}}
self.assertEqual(expected, actual)
@ -385,7 +385,7 @@ class JSONRequestDeserializerTest(common.HeatTestCase):
def test_default_with_get_with_body_with_aws(self):
request = wsgi.Request.blank('/?ContentType=JSON')
request.method = 'GET'
request.body = '{"key": "value"}'
request.body = b'{"key": "value"}'
actual = wsgi.JSONRequestDeserializer().default(request)
expected = {"body": {"key": "value"}}
self.assertEqual(expected, actual)

View File

@ -2,7 +2,10 @@ heat.tests.api.aws.test_api_aws
heat.tests.api.middleware.test_ssl_middleware
heat.tests.api.middleware.test_version_negotiation_middleware
heat.tests.api.openstack_v1.test_stacks_view
heat.tests.api.openstack_v1.test_stacks
heat.tests.api.openstack_v1.test_util
heat.tests.api.openstack_v1.test_views_common
heat.tests.api.test_wsgi
heat.tests.autoscaling
heat.tests.autoscaling.test_heat_scaling_group
heat.tests.autoscaling.test_heat_scaling_policy