diff --git a/dothttp/js3py.py b/dothttp/js3py.py index fbaf77e..9ccb741 100644 --- a/dothttp/js3py.py +++ b/dothttp/js3py.py @@ -15,6 +15,7 @@ from requests import Response from RestrictedPython import compile_restricted, safe_globals from RestrictedPython.PrintCollector import PrintCollector +from operator import getitem from faker import Faker from dothttp.exceptions import PreRequestScriptException @@ -31,6 +32,9 @@ def write_guard(x): else: raise Exception("not allowed") +# def read_guard(x, attr): +# return getattr(x, attr) + allowed_global = { '_print_': PrintCollector, @@ -42,6 +46,7 @@ def write_guard(x): 'unittest': unittest, 'datetime': datetime, '_write_': write_guard, + "_getitem_": getitem, 'csv': csv, 'uuid': uuid, 'base64': base64, @@ -58,8 +63,8 @@ def write_guard(x): @dataclass class TestResult: name: str - result: str = field(init=False) - error: typing.Optional[str] = field(init=False) + result: typing.Union[None, str] = None + error: typing.Union[None, str] = None success: bool = True diff --git a/dothttp/request_base.py b/dothttp/request_base.py index 934794a..d563022 100644 --- a/dothttp/request_base.py +++ b/dothttp/request_base.py @@ -14,7 +14,8 @@ from requests_pkcs12 import Pkcs12Adapter from textx import metamodel_from_file -from dothttp import APPLICATION_JSON, MIME_TYPE_JSON, UNIX_SOCKET_SCHEME, TEXT_PLAIN, CONTENT_TYPE +from .js3py import ScriptResult +from . import APPLICATION_JSON, MIME_TYPE_JSON, UNIX_SOCKET_SCHEME, TEXT_PLAIN, CONTENT_TYPE from . import eprint, Config, HttpDefBase, js3py, AWS4Auth from .curl_utils import to_curl from .dsl_jsonparser import json_or_array_to_json @@ -396,21 +397,29 @@ def run(self): self.print_req_info(resp, '<') self.write_to_output(resp) request_logger.debug(f'request executed completely') - script_result = script_execution.execute_test_script(resp=resp).as_json() + script_result = script_execution.execute_test_script(resp=resp) self.print_script_result(script_result) return resp - def print_script_result(self, script_result): + def print_script_result(self, script_result: ScriptResult): print("\n------------") - if script_result['stdout']: + if script_result.stdout: print("\n##STDOUT:") - print(script_result['stdout']) - if script_result['error']: + print(script_result.stdout) + if script_result.error: print("\n##ERROR:") - print(script_result['error']) - if len(script_result['properties']) != 0: + print(script_result.error) + if len(script_result.properties) != 0: print("\n##PROPERTIES:") - pprint(script_result['properties']) + pprint(script_result.properties) + if len(script_result.tests) > 0: + print("\n##TESTS:\n") + for test in script_result.tests: + print(f"Test: {test.name} {'success' if test.success else 'failure!!'}") + if test.result: + print(f"result={test.result}") + if test.error: + print(f"{test.error}") print("\n------------") # TODO print tests output individually request_logger.debug(f'script execution result {script_result}') diff --git a/examples/misc/script.http b/examples/misc/script.http index 851d522..c9403ad 100644 --- a/examples/misc/script.http +++ b/examples/misc/script.http @@ -2,10 +2,20 @@ GET http://httpbin.org/get > {% +def pre_hai(): + client.request.headers.setdefault("preheader", "prevalue") + def test_hai(): if client.response.status_code == 200: log('working') else: log('not working') +class SampleTest(unittest.TestCase): + def test_is_mime_not_json(self): + self.assertTrue(client.response.headers.get("content-type").startswith("application/xml"), "should not be json") + + def test_pre_request_preheader(self): + body = client.response.json() + self.assertEqual("prevalue", body['headers']['Preheader']) %} python