mitmproxy/test/test_export_flow.py

80 lines
2.5 KiB
Python
Raw Normal View History

2016-02-08 15:58:10 +00:00
import netlib.tutils
from libmproxy import flow_export
from . import tutils
req_get = netlib.tutils.treq(
method='GET',
content=None,
)
req_post = netlib.tutils.treq(
method='POST',
headers=None,
)
req_patch = netlib.tutils.treq(
method='PATCH',
path=b"/path?query=param",
)
2016-02-08 15:58:10 +00:00
def test_curl_command():
2016-02-08 15:58:10 +00:00
flow = tutils.tflow(req=req_get)
result = """curl -H 'header:qvalue' 'http://address/path'"""
assert flow_export.curl_command(flow) == result
2016-02-08 15:58:10 +00:00
flow = tutils.tflow(req=req_post)
result = """curl -X POST 'http://address/path' --data-binary 'content'"""
assert flow_export.curl_command(flow) == result
flow = tutils.tflow(req=req_patch)
result = """curl -H 'header:qvalue' -X PATCH 'http://address/path?query=param' --data-binary 'content'"""
assert flow_export.curl_command(flow) == result
2016-02-08 16:26:43 +00:00
def test_python_code():
flow = tutils.tflow(req=req_get)
result = ("""import requests\n\n"""
"""url = 'http://address/path'\n\n"""
"""headers = {\n"""
""" 'header': 'qvalue',\n"""
"""}\n\n"""
"""response = requests.request(\n"""
""" method='GET',\n"""
""" url=url,\n"""
""" headers=headers,\n"""
""")\n\n"""
"""print(response.text)""")
assert flow_export.python_code(flow) == result
flow = tutils.tflow(req=req_post)
result = ("""import requests\n\n"""
"""url = 'http://address/path'\n\n"""
"""data = '''content'''\n\n"""
"""response = requests.request(\n"""
""" method='POST',\n"""
""" url=url,\n"""
""" data=data,\n)\n\n"""
"""print(response.text)""")
assert flow_export.python_code(flow) == result
flow = tutils.tflow(req=req_patch)
result = ("""import requests\n\n"""
"""url = 'http://address/path'\n\n"""
"""headers = {\n"""
""" 'header': 'qvalue',\n"""
"""}\n\n"""
"""params = {\n"""
""" 'query': 'param',\n"""
"""}\n\n"""
"""data = '''content'''\n\n"""
"""response = requests.request(\n"""
""" method='PATCH',\n"""
""" url=url,\n"""
""" headers=headers,\n"""
""" params=params,\n"""
""" data=data,\n"""
""")\n\n"""
"""print(response.text)""")
assert flow_export.python_code(flow) == result