diff --git a/tests/test_opensearch.py b/tests/test_opensearch.py index fc45f19ec..1d3049bb6 100644 --- a/tests/test_opensearch.py +++ b/tests/test_opensearch.py @@ -11,10 +11,9 @@ import mock import pytest from pyramid import testing -from pyramid.testing import DummyRequest from pywps.inout.inputs import LiteralInput -from tests.utils import setup_mongodb_processstore +from tests.utils import MockedRequest, setup_mongodb_processstore from weaver.processes import opensearch from weaver.processes.constants import OpenSearchField from weaver.processes.opensearch import make_param_id @@ -61,7 +60,7 @@ def load_json_test_file(filename): def make_request(**kw): - request = DummyRequest(**kw) + request = MockedRequest(**kw) if request.registry.settings is None: request.registry.settings = {} request.registry.settings["weaver.url"] = "localhost" diff --git a/tests/utils.py b/tests/utils.py index de8ae151b..ca0d6077a 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -34,6 +34,7 @@ from pyramid.config import Configurator from pyramid.httpexceptions import HTTPException, HTTPNotFound, HTTPUnprocessableEntity from pyramid.registry import Registry +from pyramid.testing import DummyRequest from requests import Response from webtest import TestApp, TestResponse @@ -45,6 +46,7 @@ from weaver.formats import ContentType from weaver.store.mongodb import MongodbJobStore, MongodbProcessStore, MongodbServiceStore from weaver.utils import ( + bytes2str, fetch_file, get_header, get_path_kvp, @@ -64,7 +66,14 @@ from owslib.wps import Process as ProcessOWSWPS from pywps.app import Process as ProcessPyWPS - from weaver.typedefs import AnyHeadersContainer, AnyRequestMethod, AnyRequestType, AnyResponseType, SettingsType + from weaver.typedefs import ( + JSON, + AnyHeadersContainer, + AnyRequestMethod, + AnyRequestType, + AnyResponseType, + SettingsType + ) # pylint: disable=C0103,invalid-name,E1101,no-member MockPatch = mock._patch # noqa @@ -385,12 +394,23 @@ def run_command(command, trim=True, expect_error=False, entrypoint=None): return out_lines +class MockedRequest(DummyRequest): + """ + Patch missing properties that are expected from :mod:`pyramid` requests. + """ + json = {} # type: JSON + + @property + def text(self): + return bytes2str(self.body) if self.body else json.dumps(self.json, ensure_ascii=False) + + class MockedResponse(TestResponse): """ - Replaces the ``json`` property by the expected callable from all real response implementations. + Replaces the ``json`` property by the expected callable from responses using :mod:`requests` implementation. """ def json(self): # pylint: disable=W0236,invalid-overridden-method - return self.json_body or json.loads(self.body.decode("UTF-8")) + return self.json_body or json.loads(bytes2str(self.body)) def mocked_file_response(path, url): diff --git a/weaver/utils.py b/weaver/utils.py index c5945f4f8..33cede80d 100644 --- a/weaver/utils.py +++ b/weaver/utils.py @@ -883,7 +883,7 @@ def str2bytes(string): raise TypeError(f"Cannot convert item to bytes: {type(string)!r}") if isinstance(string, bytes): return string - return string.encode() + return string.encode("UTF-8") def bytes2str(string): @@ -895,7 +895,7 @@ def bytes2str(string): raise TypeError(f"Cannot convert item to unicode: {type(string)!r}") if not isinstance(string, bytes): return string - return string.decode() + return string.decode("UTF-8") def islambda(func):