Skip to content

Commit

Permalink
iter in test scripts in python & dep updates (#143)
Browse files Browse the repository at this point in the history
* Support iter in test scripts in python

* chore(dependency): update textx & requests
  • Loading branch information
cedric05 authored Jul 3, 2022
1 parent a428b60 commit 58993d2
Show file tree
Hide file tree
Showing 8 changed files with 331 additions and 164 deletions.
5 changes: 3 additions & 2 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ name = "pypi"
[packages]
jsonschema = "==3.2.0"
jstyleson = "==0.0.2"
requests = "==2.27.1"
requests = "==2.28.1"
requests-pkcs12 = "==1.10"
requests-unixsocket = "==0.2.0"
requests-aws4auth = "==1.1.1"
textX = "==2.3.0"
textX = "==3.0.0"
Js2Py = "==0.71"
requests_ntlm = "==1.1.0"
RestrictedPython = "==5.2"
Expand All @@ -25,6 +25,7 @@ waitress = "==2.1.2"
Flask = "*"
pyperf = "==2.2.0"
pytest-benchmark = "*"
python-magic = "*"

[requires]
python_version = "3.9"
211 changes: 120 additions & 91 deletions Pipfile.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dothttp/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.0.41a4'
__version__ = '0.0.41a5'
6 changes: 6 additions & 0 deletions dothttp/js3py.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@
from js2py.base import JsObjectWrapper
from js2py.internals.simplex import JsException
from requests import Response

from RestrictedPython import compile_restricted, safe_globals
from RestrictedPython.PrintCollector import PrintCollector
from RestrictedPython.Guards import guarded_iter_unpack_sequence
from RestrictedPython.Eval import default_guarded_getiter

from operator import getitem
from faker import Faker

Expand Down Expand Up @@ -48,6 +52,8 @@ def write_guard(x):
'datetime': datetime,
'_write_': write_guard,
"_getitem_": getitem,
"_getiter_" : default_guarded_getiter,
"_iter_unpack_sequence_": guarded_iter_unpack_sequence,
'csv': csv,
'uuid': uuid,
'base64': base64,
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
jsonschema==3.2.0
jstyleson==0.0.2
requests==2.27.1
textx==2.3.0
requests==2.28.1
textx==3.0.0
js2py==0.71
requests_pkcs12==1.10
requests-unixsocket==0.2.0
Expand Down
11 changes: 11 additions & 0 deletions test/core/requests/script.http
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,14 @@ client.test("check status", function(){
})

%}


@name("python iter")
GET http://httpbin.org/robots.txt
> {%

def test_iter():
for i in range(10):
log('something')

%} python
254 changes: 187 additions & 67 deletions test/core/test_script_execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,112 @@ class ScriptExecutionIntegrationTest(TestBase):
def test_python(self):
req, result = self.load_comp("python_class")
self.assertEqual(ScriptType.PYTHON, req.httpdef.test_script_lang)
self.assertEqual({"stdout": "working\n", "error": "", "properties": {}, "tests": [
{"name": "test_hai (test_script.SampleTest)", "success": True,"result": None,"error": None}], "compiled": True}, result)
self.assertEqual(
{
"stdout":
"working\n",
"error":
"",
"properties": {},
"tests": [{
"name": "test_hai (test_script.SampleTest)",
"success": True,
"result": None,
"error": None
}],
"compiled":
True
}, result)

def test_python_iter(self):
req, result = self.load_comp("python iter")
self.assertEqual(ScriptType.PYTHON, req.httpdef.test_script_lang)
self.assertEqual(
{
"stdout":
"""something
something
something
something
something
something
something
something
something
something
""",
"error":
"",
"properties": {},
"tests": [{
"name": "test_iter",
"success": True,
"result": None,
"error": None
}],
"compiled":
True
}, result)

def test_python_func(self):
req, result = self.load_comp("python_function")
self.assertEqual(ScriptType.PYTHON, req.httpdef.test_script_lang)
self.assertEqual({"stdout": "working\n", "error": "", "properties": {}, "tests": [
{"name": "test_hai", "success": True, "result": None,"error": None}], "compiled": True}, result)
self.assertEqual(
{
"stdout":
"working\n",
"error":
"",
"properties": {},
"tests": [{
"name": "test_hai",
"success": True,
"result": None,
"error": None
}],
"compiled":
True
}, result)

def test_javascript(self):
req, result = self.load_comp("javascript")
self.assertEqual(ScriptType.JAVA_SCRIPT, req.httpdef.test_script_lang)
self.assertEqual({"stdout": "", "error": "", "properties": {}, "tests": [
{"name": "check status", "success": True, "result": None,"error": None}], "compiled": True}, result)
self.assertEqual(
{
"stdout":
"",
"error":
"",
"properties": {},
"tests": [{
"name": "check status",
"success": True,
"result": None,
"error": None
}],
"compiled":
True
}, result)

def test_javascript_default(self):
req, result = self.load_comp("default_javascript")
self.assertEqual(ScriptType.JAVA_SCRIPT, req.httpdef.test_script_lang)
self.assertEqual({"stdout": "", "error": "", "properties": {}, "tests": [
{"name": "check status", "success": True, "result": None,"error": None}], "compiled": True}, result)
self.assertEqual(
{
"stdout":
"",
"error":
"",
"properties": {},
"tests": [{
"name": "check status",
"success": True,
"result": None,
"error": None
}],
"compiled":
True
}, result)

def load_comp(self, target):
req = self.get_req_comp(file_name, target=target)
Expand All @@ -57,14 +143,22 @@ def test_status_code(self):
self.assertEquals(200 , client.response.status_code)
""")

self.assertEqual({'compiled': True,
'error': '',
'properties': {},
'stdout': '',
'tests': [{'name': 'test_status_code (test_script.SampleTestCase)',
'success': True,
'result': None,
'error': None}]}, resp.as_json())
self.assertEqual(
{
'compiled':
True,
'error':
'',
'properties': {},
'stdout':
'',
'tests': [{
'name': 'test_status_code (test_script.SampleTestCase)',
'success': True,
'result': None,
'error': None
}]
}, resp.as_json())

def get_script_exe(self, script):
resp = Response()
Expand All @@ -88,8 +182,10 @@ def pre_request():
script_exe = ScriptExecutionPython(httpdef, props)
script_exe.pre_request_script()
self.assertEquals({"ram": "ranga"}, httpdef.headers)
self.assertEquals({"new": "value", "ram": "raju"},
script_exe.client.properties)
self.assertEquals({
"new": "value",
"ram": "raju"
}, script_exe.client.properties)

def test_math_n_headers(self):
resp = self.get_script_exe("""
Expand All @@ -110,66 +206,90 @@ def test_hash(self):
hashobj.hexdigest())
""")
self.assertEqual({'compiled': True,
'error': '',
'properties': {},
'stdout': '',
'tests': [
{
'name': 'test_date (test_script.SampleTestCase)',
'success': True,
'result': None,
'error': None
},
{
'name': 'test_hash (test_script.SampleTestCase)',
'success': True,
'result': None,
'error': None
},
{
'name': 'test_headers (test_script.SampleTestCase)',
'success': True,
'result': None,
'error': None
},
{
'name': 'test_math (test_script.SampleTestCase)',
'success': True,
'result': None,
'error': None
}
]
}, resp.as_json())
self.assertEqual(
{
'compiled':
True,
'error':
'',
'properties': {},
'stdout':
'',
'tests': [{
'name': 'test_date (test_script.SampleTestCase)',
'success': True,
'result': None,
'error': None
}, {
'name': 'test_hash (test_script.SampleTestCase)',
'success': True,
'result': None,
'error': None
}, {
'name': 'test_headers (test_script.SampleTestCase)',
'success': True,
'result': None,
'error': None
}, {
'name': 'test_math (test_script.SampleTestCase)',
'success': True,
'result': None,
'error': None
}]
}, resp.as_json())

def test_assertion_failure(self):
resp = self.get_script_exe("""
class SampleTestCase(unittest.TestCase):
def test_status_code(self):
self.assertEquals(401 , client.response.status_code)
""")
self.assertEqual({'stdout': '',
'error': '',
'properties': {},
'tests': [{'name': 'test_status_code (test_script.SampleTestCase)',
'success': False,
'result': None,
'error': 'Traceback (most recent call last):\n File "test_script.py", line 4, in test_status_code\nAssertionError: 401 != 200\n'}],
'compiled': True}, resp.as_json())
self.assertEqual(
{
'stdout':
'',
'error':
'',
'properties': {},
'tests': [{
'name':
'test_status_code (test_script.SampleTestCase)',
'success':
False,
'result':
None,
'error':
'Traceback (most recent call last):\n File "test_script.py", line 4, in test_status_code\nAssertionError: 401 != 200\n'
}],
'compiled':
True
}, resp.as_json())

def test_exception(self):
resp = self.get_script_exe("""
class SampleTestCase(unittest.TestCase):
def test_raise(self):
raise Exception()
""")
self.assertEqual({'compiled': True,
'error': '',
'properties': {},
'stdout': '',
'tests': [{'error': 'Traceback (most recent call last):\n'
' File "test_script.py", line 4, in test_raise\n'
'Exception\n',
'name': 'test_raise (test_script.SampleTestCase)',
'result': None,
'success': False}]}, resp.as_json())
self.assertEqual(
{
'compiled':
True,
'error':
'',
'properties': {},
'stdout':
'',
'tests': [{
'error':
'Traceback (most recent call last):\n'
' File "test_script.py", line 4, in test_raise\n'
'Exception\n',
'name':
'test_raise (test_script.SampleTestCase)',
'result':
None,
'success':
False
}]
}, resp.as_json())
2 changes: 1 addition & 1 deletion test/extensions/test_http2postman.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def test_negative(self):
second = self.error_scenario("names_fail.http")
self.assertTrue(second.get("error"))
self.assertTrue(second.get("error_message").startswith(
"unable to parse because of parsing issues None:2:2: error: Expected '(' or STRING or '\w+' at position"))
"unable to parse because of parsing issues None:2:2: Expected '(' or STRING or '\\w+' => 'http.dev ?*'"))

def test_extend(self):
self.execute_n_get("auth_extend.postman_collection.json", os.path.join(requests_dir, "auth_extend.http"))
Expand Down

0 comments on commit 58993d2

Please sign in to comment.