diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/mitmproxy/data/har_extractor.har | 78 | ||||
-rw-r--r-- | test/mitmproxy/test_examples.py | 119 | ||||
-rw-r--r-- | test/mitmproxy/test_flow_export.py | 30 | ||||
-rw-r--r-- | test/mitmproxy/tutils.py | 1 |
4 files changed, 226 insertions, 2 deletions
diff --git a/test/mitmproxy/data/har_extractor.har b/test/mitmproxy/data/har_extractor.har new file mode 100644 index 00000000..2f5099b3 --- /dev/null +++ b/test/mitmproxy/data/har_extractor.har @@ -0,0 +1,78 @@ +{ + "test_response": { + "log": { + "__page_count__": 1, + "version": "1.2", + "creator": { + "comment": "", + "version": "0.1", + "name": "MITMPROXY HARExtractor" + }, + "pages": [ + { + "startedDateTime": "1993-08-24T14:41:12", + "id": "autopage_1", + "title": "http://address:22/path" + } + ], + "entries": [ + { + "pageref": "autopage_1", + "startedDateTime": "1993-08-24T14:41:12", + "cache": {}, + "request": { + "cookies": [], + "url": "http://address:22/path", + "queryString": [], + "headers": [ + { + "name": "header", + "value": "qvalue" + }, + { + "name": "content-length", + "value": "7" + } + ], + "headersSize": 35, + "httpVersion": "HTTP/1.1", + "method": "GET", + "bodySize": 7 + }, + "timings": { + "receive": 0, + "ssl": 1000, + "connect": 1000, + "send": 0, + "wait": 0 + }, + "time": 2000, + "response": { + "status": 200, + "cookies": [], + "statusText": "OK", + "content": { + "mimeType": "", + "compression": 0, + "size": 7 + }, + "headers": [ + { + "name": "content-length", + "value": "7" + }, + { + "name": "header-response", + "value": "svalue" + } + ], + "headersSize": 44, + "redirectURL": "", + "httpVersion": "HTTP/1.1", + "bodySize": 7 + } + } + ] + } + } +}
\ No newline at end of file diff --git a/test/mitmproxy/test_examples.py b/test/mitmproxy/test_examples.py index 163ace17..803776ac 100644 --- a/test/mitmproxy/test_examples.py +++ b/test/mitmproxy/test_examples.py @@ -1,11 +1,42 @@ import glob +import json +import os +from contextlib import contextmanager + from mitmproxy import utils, script from mitmproxy.proxy import config -from . import tservers +from netlib import tutils as netutils +from netlib.http import Headers +from . import tservers, tutils + +example_dir = utils.Data(__name__).path("../../examples") + + +class DummyContext(object): + """Emulate script.ScriptContext() functionality.""" + + contentview = None + + def log(self, *args, **kwargs): + pass + + def add_contentview(self, view_obj): + self.contentview = view_obj + + def remove_contentview(self, view_obj): + self.contentview = None + + +@contextmanager +def example(command): + command = os.path.join(example_dir, command) + ctx = DummyContext() + s = script.Script(command, ctx) + yield s + s.unload() def test_load_scripts(): - example_dir = utils.Data(__name__).path("../../examples") scripts = glob.glob("%s/*.py" % example_dir) tmaster = tservers.TestMaster(config.ProxyConfig()) @@ -28,3 +59,87 @@ def test_load_scripts(): raise else: s.unload() + + +def test_add_header(): + flow = tutils.tflow(resp=netutils.tresp()) + with example("add_header.py") as ex: + ex.run("response", flow) + assert flow.response.headers["newheader"] == "foo" + + +def test_custom_contentviews(): + with example("custom_contentviews.py") as ex: + pig = ex.ctx.contentview + _, fmt = pig("<html>test!</html>") + assert any('esttay!' in val[0][1] for val in fmt) + assert not pig("gobbledygook") + + +def test_iframe_injector(): + with tutils.raises(script.ScriptException): + with example("iframe_injector.py") as ex: + pass + + flow = tutils.tflow(resp=netutils.tresp(content="<html>mitmproxy</html>")) + with example("iframe_injector.py http://example.org/evil_iframe") as ex: + ex.run("response", flow) + content = flow.response.content + assert 'iframe' in content and 'evil_iframe' in content + + +def test_modify_form(): + form_header = Headers(content_type="application/x-www-form-urlencoded") + flow = tutils.tflow(req=netutils.treq(headers=form_header)) + with example("modify_form.py") as ex: + ex.run("request", flow) + assert flow.request.urlencoded_form["mitmproxy"] == ["rocks"] + + +def test_modify_querystring(): + flow = tutils.tflow(req=netutils.treq(path="/search?q=term")) + with example("modify_querystring.py") as ex: + ex.run("request", flow) + assert flow.request.query["mitmproxy"] == ["rocks"] + + +def test_modify_response_body(): + with tutils.raises(script.ScriptException): + with example("modify_response_body.py") as ex: + pass + + flow = tutils.tflow(resp=netutils.tresp(content="I <3 mitmproxy")) + with example("modify_response_body.py mitmproxy rocks") as ex: + assert ex.ctx.old == "mitmproxy" and ex.ctx.new == "rocks" + ex.run("response", flow) + assert flow.response.content == "I <3 rocks" + + +def test_redirect_requests(): + flow = tutils.tflow(req=netutils.treq(host="example.org")) + with example("redirect_requests.py") as ex: + ex.run("request", flow) + assert flow.request.host == "mitmproxy.org" + + +def test_har_extractor(): + with tutils.raises(script.ScriptException): + with example("har_extractor.py") as ex: + pass + + times = dict( + timestamp_start=746203272, + timestamp_end=746203272, + ) + + flow = tutils.tflow( + req=netutils.treq(**times), + resp=netutils.tresp(**times) + ) + + with example("har_extractor.py -") as ex: + ex.run("response", flow) + + with open(tutils.test_data.path("data/har_extractor.har")) as fp: + test_data = json.load(fp) + assert json.loads(ex.ctx.HARLog.json()) == test_data["test_response"] diff --git a/test/mitmproxy/test_flow_export.py b/test/mitmproxy/test_flow_export.py index 2dce3fd6..62161d5d 100644 --- a/test/mitmproxy/test_flow_export.py +++ b/test/mitmproxy/test_flow_export.py @@ -1,6 +1,7 @@ from textwrap import dedent import netlib.tutils +from netlib.http import Headers from mitmproxy import flow_export from . import tutils @@ -81,6 +82,35 @@ class TestExportPythonCode(): """).strip() assert flow_export.python_code(flow) == result + def test_post_json(self): + req_post.content = '{"name": "example", "email": "example@example.com"}' + req_post.headers = Headers(content_type="application/json") + flow = tutils.tflow(req=req_post) + result = dedent(""" + import requests + + url = 'http://address/path' + + headers = { + 'content-type': 'application/json', + } + + json = { + "name": "example", + "email": "example@example.com" + } + + response = requests.request( + method='POST', + url=url, + headers=headers, + json=json, + ) + + print(response.text) + """).strip() + assert flow_export.python_code(flow) == result + def test_patch(self): flow = tutils.tflow(req=req_patch) result = dedent(""" diff --git a/test/mitmproxy/tutils.py b/test/mitmproxy/tutils.py index edcdf3e2..0d65df71 100644 --- a/test/mitmproxy/tutils.py +++ b/test/mitmproxy/tutils.py @@ -93,6 +93,7 @@ def tserver_conn(): c = ServerConnection.from_state(dict( address=dict(address=("address", 22), use_ipv6=True), source_address=dict(address=("address", 22), use_ipv6=True), + peer_address=None, cert=None, timestamp_start=1, timestamp_tcp_setup=2, |