From b8ae666c78aef9a6ef0e67179d38980247d0e1a5 Mon Sep 17 00:00:00 2001 From: yonder Date: Wed, 2 Mar 2016 12:19:33 -0500 Subject: Add test for upstream-auth --- test/mitmproxy/test_cmdline.py | 7 +++++++ test/mitmproxy/test_proxy.py | 4 ++++ 2 files changed, 11 insertions(+) (limited to 'test') diff --git a/test/mitmproxy/test_cmdline.py b/test/mitmproxy/test_cmdline.py index 5a70f3e0..1546bb41 100644 --- a/test/mitmproxy/test_cmdline.py +++ b/test/mitmproxy/test_cmdline.py @@ -1,4 +1,5 @@ import argparse +import base64 from mitmproxy import cmdline from . import tutils @@ -53,6 +54,12 @@ def test_parse_server_spec(): "http://") +def test_parse_upstream_auth(): + tutils.raises("Invalid upstream auth specification", cmdline.parse_upstream_auth, "") + assert cmdline.parse_upstream_auth( + "test:test") == "Basic" + " " + base64.b64encode("test:test") + + def test_parse_setheaders(): x = cmdline.parse_setheader("/foo/bar/voing") assert x == ("foo", "bar", "voing") diff --git a/test/mitmproxy/test_proxy.py b/test/mitmproxy/test_proxy.py index 34b75b62..fddb851e 100644 --- a/test/mitmproxy/test_proxy.py +++ b/test/mitmproxy/test_proxy.py @@ -92,6 +92,10 @@ class TestProcessProxyOptions: self.assert_err("expected one argument", "-U") self.assert_err("Invalid server specification", "-U", "upstream") + self.assert_noerr("--upstream-auth", "test:test") + self.assert_err("expected one argument", "--upstream-auth") + self.assert_err("Invalid upstream auth specification", "--upstream-auth", "test") + self.assert_err("not allowed with", "-R", "http://localhost", "-T") def test_socks_auth(self): -- cgit v1.2.3 From 48f1c9afc2e5d02c8de717e2fc9030a7884d493b Mon Sep 17 00:00:00 2001 From: Shadab Zafar Date: Fri, 4 Mar 2016 01:06:09 +0530 Subject: Add a test for json request --- test/mitmproxy/test_flow_export.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'test') diff --git a/test/mitmproxy/test_flow_export.py b/test/mitmproxy/test_flow_export.py index 2dce3fd6..3dc07427 100644 --- a/test/mitmproxy/test_flow_export.py +++ b/test/mitmproxy/test_flow_export.py @@ -1,6 +1,8 @@ +import json from textwrap import dedent import netlib.tutils +from netlib.http import Headers from mitmproxy import flow_export from . import tutils @@ -81,6 +83,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(""" -- cgit v1.2.3 From c982f579dd755adef2456bc2ad1d3fce06cbfd58 Mon Sep 17 00:00:00 2001 From: Shadab Zafar Date: Fri, 4 Mar 2016 02:32:01 +0530 Subject: Add basic test for har_extractor --- test/mitmproxy/test_har_extractor.py | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 test/mitmproxy/test_har_extractor.py (limited to 'test') diff --git a/test/mitmproxy/test_har_extractor.py b/test/mitmproxy/test_har_extractor.py new file mode 100644 index 00000000..da68ac4d --- /dev/null +++ b/test/mitmproxy/test_har_extractor.py @@ -0,0 +1,10 @@ +from . import tutils + +from mitmproxy import script, flow +from examples import har_extractor + + +def test_start(): + fm = flow.FlowMaster(None, flow.State()) + ctx = script.ScriptContext(fm) + tutils.raises(ValueError, har_extractor.start, ctx, []) -- cgit v1.2.3 From 6a4f1ae7c7ea4b5036937312b837184162edaed3 Mon Sep 17 00:00:00 2001 From: Shadab Zafar Date: Fri, 4 Mar 2016 03:10:29 +0530 Subject: Test har_extractor.response --- test/mitmproxy/data/har_extractor.har | 78 +++++++++++++++++++++++++++++++++++ test/mitmproxy/test_har_extractor.py | 27 ++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 test/mitmproxy/data/har_extractor.har (limited to 'test') 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_har_extractor.py b/test/mitmproxy/test_har_extractor.py index da68ac4d..26a092e6 100644 --- a/test/mitmproxy/test_har_extractor.py +++ b/test/mitmproxy/test_har_extractor.py @@ -1,10 +1,37 @@ +import json +import netlib.tutils from . import tutils from mitmproxy import script, flow from examples import har_extractor +trequest = netlib.tutils.treq( + timestamp_start=746203272, + timestamp_end=746203272, +) + +tresponse = netlib.tutils.tresp( + timestamp_start=746203272, + timestamp_end=746203272, +) + + def test_start(): fm = flow.FlowMaster(None, flow.State()) ctx = script.ScriptContext(fm) tutils.raises(ValueError, har_extractor.start, ctx, []) + + +def test_response(): + fm = flow.FlowMaster(None, flow.State()) + ctx = script.ScriptContext(fm) + ctx.HARLog = har_extractor._HARLog([]) + ctx.seen_server = set() + + fl = tutils.tflow(req=trequest, resp=tresponse) + har_extractor.response(ctx, fl) + + with open(tutils.test_data.path("data/har_extractor.har")) as fp: + test_data = json.load(fp) + assert json.loads(ctx.HARLog.json()) == test_data["test_response"] -- cgit v1.2.3 From d17cd5c6eca72fdddafb73028687c8b0152a5fe6 Mon Sep 17 00:00:00 2001 From: Shadab Zafar Date: Fri, 4 Mar 2016 03:13:40 +0530 Subject: Use custom Context object --- test/mitmproxy/test_har_extractor.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'test') diff --git a/test/mitmproxy/test_har_extractor.py b/test/mitmproxy/test_har_extractor.py index 26a092e6..7838f713 100644 --- a/test/mitmproxy/test_har_extractor.py +++ b/test/mitmproxy/test_har_extractor.py @@ -2,10 +2,13 @@ import json import netlib.tutils from . import tutils -from mitmproxy import script, flow from examples import har_extractor +class Context(object): + pass + + trequest = netlib.tutils.treq( timestamp_start=746203272, timestamp_end=746203272, @@ -18,14 +21,11 @@ tresponse = netlib.tutils.tresp( def test_start(): - fm = flow.FlowMaster(None, flow.State()) - ctx = script.ScriptContext(fm) - tutils.raises(ValueError, har_extractor.start, ctx, []) + tutils.raises(ValueError, har_extractor.start, Context(), []) def test_response(): - fm = flow.FlowMaster(None, flow.State()) - ctx = script.ScriptContext(fm) + ctx = Context() ctx.HARLog = har_extractor._HARLog([]) ctx.seen_server = set() -- cgit v1.2.3 From 0e27dfd9c132e1f8768ae33a33ae8a1c55541948 Mon Sep 17 00:00:00 2001 From: yonder Date: Fri, 4 Mar 2016 11:12:03 -0500 Subject: Improve test case --- test/mitmproxy/test_cmdline.py | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'test') diff --git a/test/mitmproxy/test_cmdline.py b/test/mitmproxy/test_cmdline.py index 1546bb41..e75b7baf 100644 --- a/test/mitmproxy/test_cmdline.py +++ b/test/mitmproxy/test_cmdline.py @@ -56,8 +56,12 @@ def test_parse_server_spec(): def test_parse_upstream_auth(): tutils.raises("Invalid upstream auth specification", cmdline.parse_upstream_auth, "") + tutils.raises("Invalid upstream auth specification", cmdline.parse_upstream_auth, ":") + tutils.raises("Invalid upstream auth specification", cmdline.parse_upstream_auth, ":test") assert cmdline.parse_upstream_auth( "test:test") == "Basic" + " " + base64.b64encode("test:test") + assert cmdline.parse_upstream_auth( + "test:") == "Basic" + " " + base64.b64encode("test:") def test_parse_setheaders(): -- cgit v1.2.3