Skip to content

Commit

Permalink
fix(example): add couple of examples for pre request and test
Browse files Browse the repository at this point in the history
  • Loading branch information
cedric05 committed May 23, 2022
1 parent 46da4c9 commit 2d01127
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
9 changes: 7 additions & 2 deletions dothttp/js3py.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -42,6 +46,7 @@ def write_guard(x):
'unittest': unittest,
'datetime': datetime,
'_write_': write_guard,
"_getitem_": getitem,
'csv': csv,
'uuid': uuid,
'base64': base64,
Expand All @@ -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


Expand Down
27 changes: 18 additions & 9 deletions dothttp/request_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}')
Expand Down
10 changes: 10 additions & 0 deletions examples/misc/script.http
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 2d01127

Please sign in to comment.