From 1c2f8a608e4657f5b7eaeca97164aec221dcb341 Mon Sep 17 00:00:00 2001 From: Shadab Zafar Date: Tue, 9 Feb 2016 21:26:05 +0530 Subject: Rename test_export_flow.py to test_flow_export.py --- test/test_flow_export.py | 105 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 test/test_flow_export.py (limited to 'test/test_flow_export.py') diff --git a/test/test_flow_export.py b/test/test_flow_export.py new file mode 100644 index 00000000..7bd7a442 --- /dev/null +++ b/test/test_flow_export.py @@ -0,0 +1,105 @@ +import netlib.tutils +from libmproxy import flow_export +from . import tutils + +req_get = netlib.tutils.treq( + method='GET', + content='', +) + +req_post = netlib.tutils.treq( + method='POST', + headers=None, +) + +req_patch = netlib.tutils.treq( + method='PATCH', + path=b"/path?query=param", +) + + +def test_curl_command(): + flow = tutils.tflow(req=req_get) + result = """curl -H 'header:qvalue' -H 'content-length:7' 'http://address/path'""" + assert flow_export.curl_command(flow) == result + + 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' -H 'content-length:7' -X PATCH 'http://address/path?query=param' --data-binary 'content'""" + assert flow_export.curl_command(flow) == result + + +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""" + """ 'content-length': '7',\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""" + """ 'content-length': '7',\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 + + +def test_raw_request(): + flow = tutils.tflow(req=req_get) + result = ("""GET /path HTTP/1.1\r\n""" + """header: qvalue\r\n""" + """content-length: 7\r\n""" + """host: address:22\r\n\r\n""" + """""") + assert flow_export.raw_request(flow) == result + + flow = tutils.tflow(req=req_post) + result = ("""POST /path HTTP/1.1\r\n""" + """host: address:22\r\n\r\n""" + """content""") + assert flow_export.raw_request(flow) == result + + flow = tutils.tflow(req=req_patch) + result = ("""PATCH /path?query=param HTTP/1.1\r\n""" + """header: qvalue\r\n""" + """content-length: 7\r\n""" + """host: address:22\r\n\r\n""" + """content""") + assert flow_export.raw_request(flow) == result -- cgit v1.2.3 From 0576f579ed1e97f150b37c1511d0e7b9b43a329c Mon Sep 17 00:00:00 2001 From: Shadab Zafar Date: Tue, 9 Feb 2016 21:52:22 +0530 Subject: Use textwrap.dedent to improve readability of tests --- test/test_flow_export.py | 133 +++++++++++++++++++++++++++++------------------ 1 file changed, 82 insertions(+), 51 deletions(-) (limited to 'test/test_flow_export.py') diff --git a/test/test_flow_export.py b/test/test_flow_export.py index 7bd7a442..cee21311 100644 --- a/test/test_flow_export.py +++ b/test/test_flow_export.py @@ -1,3 +1,5 @@ +from textwrap import dedent + import netlib.tutils from libmproxy import flow_export from . import tutils @@ -34,72 +36,101 @@ def test_curl_command(): 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""" - """ 'content-length': '7',\n""" - """}\n\n""" - """response = requests.request(\n""" - """ method='GET',\n""" - """ url=url,\n""" - """ headers=headers,\n""" - """)\n\n""" - """print(response.text)""") + result = dedent(""" + import requests + + url = 'http://address/path' + + headers = { + 'header': 'qvalue', + 'content-length': '7', + } + + response = requests.request( + method='GET', + url=url, + headers=headers, + ) + + print(response.text) + """).strip() 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)""") + result = dedent(""" + import requests + + url = 'http://address/path' + + data = '''content''' + + response = requests.request( + method='POST', + url=url, + data=data, + ) + + print(response.text) + """).strip() 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""" - """ 'content-length': '7',\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)""") + result = dedent(""" + import requests + + url = 'http://address/path' + + headers = { + 'header': 'qvalue', + 'content-length': '7', + } + + params = { + 'query': 'param', + } + + data = '''content''' + + response = requests.request( + method='PATCH', + url=url, + headers=headers, + params=params, + data=data, + ) + + print(response.text) + """).strip() assert flow_export.python_code(flow) == result def test_raw_request(): flow = tutils.tflow(req=req_get) - result = ("""GET /path HTTP/1.1\r\n""" - """header: qvalue\r\n""" - """content-length: 7\r\n""" - """host: address:22\r\n\r\n""" - """""") + result = dedent(""" + GET /path HTTP/1.1\r + header: qvalue\r + content-length: 7\r + host: address:22\r + \r + """).strip(" ").lstrip() assert flow_export.raw_request(flow) == result flow = tutils.tflow(req=req_post) - result = ("""POST /path HTTP/1.1\r\n""" - """host: address:22\r\n\r\n""" - """content""") + result = dedent(""" + POST /path HTTP/1.1\r + host: address:22\r + \r + content + """).strip() assert flow_export.raw_request(flow) == result flow = tutils.tflow(req=req_patch) - result = ("""PATCH /path?query=param HTTP/1.1\r\n""" - """header: qvalue\r\n""" - """content-length: 7\r\n""" - """host: address:22\r\n\r\n""" - """content""") + result = dedent(""" + PATCH /path?query=param HTTP/1.1\r + header: qvalue\r + content-length: 7\r + host: address:22\r + \r + content + """).strip() assert flow_export.raw_request(flow) == result -- cgit v1.2.3 From 5f044d03b7c9d95be910f2eb643b6c8a9036d8de Mon Sep 17 00:00:00 2001 From: Shadab Zafar Date: Tue, 9 Feb 2016 21:58:29 +0530 Subject: Use classes to test a command, move code to separate methods under classes --- test/test_flow_export.py | 240 +++++++++++++++++++++++++---------------------- 1 file changed, 126 insertions(+), 114 deletions(-) (limited to 'test/test_flow_export.py') diff --git a/test/test_flow_export.py b/test/test_flow_export.py index cee21311..e5e9c0a3 100644 --- a/test/test_flow_export.py +++ b/test/test_flow_export.py @@ -20,117 +20,129 @@ req_patch = netlib.tutils.treq( ) -def test_curl_command(): - flow = tutils.tflow(req=req_get) - result = """curl -H 'header:qvalue' -H 'content-length:7' 'http://address/path'""" - assert flow_export.curl_command(flow) == result - - 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' -H 'content-length:7' -X PATCH 'http://address/path?query=param' --data-binary 'content'""" - assert flow_export.curl_command(flow) == result - - -def test_python_code(): - flow = tutils.tflow(req=req_get) - result = dedent(""" - import requests - - url = 'http://address/path' - - headers = { - 'header': 'qvalue', - 'content-length': '7', - } - - response = requests.request( - method='GET', - url=url, - headers=headers, - ) - - print(response.text) - """).strip() - assert flow_export.python_code(flow) == result - - flow = tutils.tflow(req=req_post) - result = dedent(""" - import requests - - url = 'http://address/path' - - data = '''content''' - - response = requests.request( - method='POST', - url=url, - data=data, - ) - - print(response.text) - """).strip() - assert flow_export.python_code(flow) == result - - flow = tutils.tflow(req=req_patch) - result = dedent(""" - import requests - - url = 'http://address/path' - - headers = { - 'header': 'qvalue', - 'content-length': '7', - } - - params = { - 'query': 'param', - } - - data = '''content''' - - response = requests.request( - method='PATCH', - url=url, - headers=headers, - params=params, - data=data, - ) - - print(response.text) - """).strip() - assert flow_export.python_code(flow) == result - - -def test_raw_request(): - flow = tutils.tflow(req=req_get) - result = dedent(""" - GET /path HTTP/1.1\r - header: qvalue\r - content-length: 7\r - host: address:22\r - \r - """).strip(" ").lstrip() - assert flow_export.raw_request(flow) == result - - flow = tutils.tflow(req=req_post) - result = dedent(""" - POST /path HTTP/1.1\r - host: address:22\r - \r - content - """).strip() - assert flow_export.raw_request(flow) == result - - flow = tutils.tflow(req=req_patch) - result = dedent(""" - PATCH /path?query=param HTTP/1.1\r - header: qvalue\r - content-length: 7\r - host: address:22\r - \r - content - """).strip() - assert flow_export.raw_request(flow) == result +class TestExportCurlCommand(): + + def test_get(self): + flow = tutils.tflow(req=req_get) + result = """curl -H 'header:qvalue' -H 'content-length:7' 'http://address/path'""" + assert flow_export.curl_command(flow) == result + + def test_post(self): + flow = tutils.tflow(req=req_post) + result = """curl -X POST 'http://address/path' --data-binary 'content'""" + assert flow_export.curl_command(flow) == result + + def test_patch(self): + flow = tutils.tflow(req=req_patch) + result = """curl -H 'header:qvalue' -H 'content-length:7' -X PATCH 'http://address/path?query=param' --data-binary 'content'""" + assert flow_export.curl_command(flow) == result + + +class TestExportPythonCode(): + + def test_get(self): + flow = tutils.tflow(req=req_get) + result = dedent(""" + import requests + + url = 'http://address/path' + + headers = { + 'header': 'qvalue', + 'content-length': '7', + } + + response = requests.request( + method='GET', + url=url, + headers=headers, + ) + + print(response.text) + """).strip() + assert flow_export.python_code(flow) == result + + def test_post(self): + flow = tutils.tflow(req=req_post) + result = dedent(""" + import requests + + url = 'http://address/path' + + data = '''content''' + + response = requests.request( + method='POST', + url=url, + data=data, + ) + + print(response.text) + """).strip() + assert flow_export.python_code(flow) == result + + def test_patch(self): + flow = tutils.tflow(req=req_patch) + result = dedent(""" + import requests + + url = 'http://address/path' + + headers = { + 'header': 'qvalue', + 'content-length': '7', + } + + params = { + 'query': 'param', + } + + data = '''content''' + + response = requests.request( + method='PATCH', + url=url, + headers=headers, + params=params, + data=data, + ) + + print(response.text) + """).strip() + assert flow_export.python_code(flow) == result + + +def TestRawRequest(): + + def test_get(self): + flow = tutils.tflow(req=req_get) + result = dedent(""" + GET /path HTTP/1.1\r + header: qvalue\r + content-length: 7\r + host: address:22\r + \r + """).strip(" ").lstrip() + assert flow_export.raw_request(flow) == result + + def test_post(self): + flow = tutils.tflow(req=req_post) + result = dedent(""" + POST /path HTTP/1.1\r + host: address:22\r + \r + content + """).strip() + assert flow_export.raw_request(flow) == result + + def test_patch(self): + flow = tutils.tflow(req=req_patch) + result = dedent(""" + PATCH /path?query=param HTTP/1.1\r + header: qvalue\r + content-length: 7\r + host: address:22\r + \r + content + """).strip() + assert flow_export.raw_request(flow) == result -- cgit v1.2.3