From b31d8f3898149209097e01bbeefbfb70b5a0c395 Mon Sep 17 00:00:00 2001 From: Michael Calmer Date: Fri, 15 Dec 2017 09:53:10 +0100 Subject: [PATCH] cherrypy read() reads bytes from the wire and write them into contents var adapt tests to reflect reality When CherryPy run with python3 it reads "bytes" from the wire. In case of python2 BytesIO == StringIO, so nothing should change. This change will do the same to make unit tests reflect reality. --- salt/netapi/rest_cherrypy/app.py | 25 +++++++------------------ tests/support/cptestcase.py | 6 ++++-- 2 files changed, 11 insertions(+), 20 deletions(-) diff --git a/salt/netapi/rest_cherrypy/app.py b/salt/netapi/rest_cherrypy/app.py index 4099416a28..67e0bad07a 100644 --- a/salt/netapi/rest_cherrypy/app.py +++ b/salt/netapi/rest_cherrypy/app.py @@ -505,6 +505,7 @@ import salt import salt.auth import salt.utils import salt.utils.event +from salt.ext.six import BytesIO # Import salt-api libs import salt.netapi @@ -830,18 +831,6 @@ def urlencoded_processor(entity): :param entity: raw POST data ''' - if six.PY3: - # https://github.com/cherrypy/cherrypy/pull/1572 - contents = six.StringIO() - entity.fp.read(fp_out=contents) - contents.seek(0) - body_str = contents.read() - body_bytes = salt.utils.to_bytes(body_str) - body_bytes = six.BytesIO(body_bytes) - body_bytes.seek(0) - # Patch fp - entity.fp = body_bytes - del contents # First call out to CherryPy's default processor cherrypy._cpreqbody.process_urlencoded(entity) cherrypy._cpreqbody.process_urlencoded(entity) @@ -860,10 +849,10 @@ def json_processor(entity): body = entity.fp.read() else: # https://github.com/cherrypy/cherrypy/pull/1572 - contents = six.StringIO() + contents = BytesIO() body = entity.fp.read(fp_out=contents) contents.seek(0) - body = contents.read() + body = salt.utils.to_unicode(contents.read()) del contents try: cherrypy.serving.request.unserialized_data = json.loads(body) @@ -884,10 +873,10 @@ def yaml_processor(entity): body = entity.fp.read() else: # https://github.com/cherrypy/cherrypy/pull/1572 - contents = six.StringIO() + contents = BytesIO() body = entity.fp.read(fp_out=contents) contents.seek(0) - body = contents.read() + body = salt.utils.to_unicode(contents.read()) try: cherrypy.serving.request.unserialized_data = yaml.safe_load(body) except ValueError: @@ -910,10 +899,10 @@ def text_processor(entity): body = entity.fp.read() else: # https://github.com/cherrypy/cherrypy/pull/1572 - contents = six.StringIO() + contents = BytesIO() body = entity.fp.read(fp_out=contents) contents.seek(0) - body = contents.read() + body = salt.utils.to_unicode(contents.read()) try: cherrypy.serving.request.unserialized_data = json.loads(body) except ValueError: diff --git a/tests/support/cptestcase.py b/tests/support/cptestcase.py index ea2845f46d..75785b8eb1 100644 --- a/tests/support/cptestcase.py +++ b/tests/support/cptestcase.py @@ -38,9 +38,11 @@ from tests.support.case import TestCase # pylint: disable=import-error import cherrypy # pylint: disable=3rd-party-module-not-gated import salt.ext.six as six -from salt.ext.six.moves import StringIO +from salt.ext.six import BytesIO # pylint: enable=import-error +import salt.utils + # Not strictly speaking mandatory but just makes sense cherrypy.config.update({'environment': "test_suite"}) @@ -92,7 +94,7 @@ class BaseCherryPyTestCase(TestCase): fd = None if body is not None: h['content-length'] = '{0}'.format(len(body)) - fd = StringIO(body) + fd = BytesIO(salt.utils.to_bytes(body)) if headers is not None: h.update(headers) -- 2.15.1