diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/mitmproxy/script/test_concurrent.py | 4 | ||||
-rw-r--r-- | test/mitmproxy/script/test_reloader.py | 2 | ||||
-rw-r--r-- | test/mitmproxy/script/test_script.py | 28 | ||||
-rw-r--r-- | test/mitmproxy/test_examples.py | 49 | ||||
-rw-r--r-- | test/mitmproxy/test_server.py | 3 |
5 files changed, 38 insertions, 48 deletions
diff --git a/test/mitmproxy/script/test_concurrent.py b/test/mitmproxy/script/test_concurrent.py index 62541f3f..57eeca19 100644 --- a/test/mitmproxy/script/test_concurrent.py +++ b/test/mitmproxy/script/test_concurrent.py @@ -11,7 +11,7 @@ class Thing: @tutils.skip_appveyor def test_concurrent(): - with Script(tutils.test_data.path("data/scripts/concurrent_decorator.py"), None) as s: + with Script(tutils.test_data.path("data/scripts/concurrent_decorator.py")) as s: f1, f2 = Thing(), Thing() s.run("request", f1) s.run("request", f2) @@ -23,6 +23,6 @@ def test_concurrent(): def test_concurrent_err(): - s = Script(tutils.test_data.path("data/scripts/concurrent_decorator_err.py"), None) + s = Script(tutils.test_data.path("data/scripts/concurrent_decorator_err.py")) with tutils.raises("Concurrent decorator not supported for 'start' method"): s.load() diff --git a/test/mitmproxy/script/test_reloader.py b/test/mitmproxy/script/test_reloader.py index 0345f6ed..e33903b9 100644 --- a/test/mitmproxy/script/test_reloader.py +++ b/test/mitmproxy/script/test_reloader.py @@ -10,7 +10,7 @@ def test_simple(): pass script = mock.Mock() - script.filename = "foo.py" + script.path = "foo.py" e = Event() diff --git a/test/mitmproxy/script/test_script.py b/test/mitmproxy/script/test_script.py index fe98fab5..48fe65c9 100644 --- a/test/mitmproxy/script/test_script.py +++ b/test/mitmproxy/script/test_script.py @@ -21,21 +21,21 @@ class TestParseCommand: def test_parse_args(self): with tutils.chdir(tutils.test_data.dirname): - assert Script.parse_command("data/scripts/a.py") == ["data/scripts/a.py"] - assert Script.parse_command("data/scripts/a.py foo bar") == ["data/scripts/a.py", "foo", "bar"] - assert Script.parse_command("data/scripts/a.py 'foo bar'") == ["data/scripts/a.py", "foo bar"] + assert Script.parse_command("data/scripts/a.py") == ("data/scripts/a.py", []) + assert Script.parse_command("data/scripts/a.py foo bar") == ("data/scripts/a.py", ["foo", "bar"]) + assert Script.parse_command("data/scripts/a.py 'foo bar'") == ("data/scripts/a.py", ["foo bar"]) @tutils.skip_not_windows def test_parse_windows(self): with tutils.chdir(tutils.test_data.dirname): - assert Script.parse_command("data\\scripts\\a.py") == ["data\\scripts\\a.py"] - assert Script.parse_command("data\\scripts\\a.py 'foo \\ bar'") == ["data\\scripts\\a.py", 'foo \\ bar'] + assert Script.parse_command("data\\scripts\\a.py") == ("data\\scripts\\a.py", []) + assert Script.parse_command("data\\scripts\\a.py 'foo \\ bar'") == ("data\\scripts\\a.py", ['foo \\ bar']) def test_simple(): with tutils.chdir(tutils.test_data.path("data/scripts")): - s = Script("a.py --var 42", None) - assert s.filename == "a.py" + s = Script("a.py --var 42") + assert s.path == "a.py" assert s.ns is None s.load() @@ -50,34 +50,34 @@ def test_simple(): with tutils.raises(ScriptException): s.run("here") - with Script("a.py --var 42", None) as s: + with Script("a.py --var 42") as s: s.run("here") def test_script_exception(): with tutils.chdir(tutils.test_data.path("data/scripts")): - s = Script("syntaxerr.py", None) + s = Script("syntaxerr.py") with tutils.raises(ScriptException): s.load() - s = Script("starterr.py", None) + s = Script("starterr.py") with tutils.raises(ScriptException): s.load() - s = Script("a.py", None) + s = Script("a.py") s.load() with tutils.raises(ScriptException): s.load() - s = Script("a.py", None) + s = Script("a.py") with tutils.raises(ScriptException): s.run("here") with tutils.raises(ScriptException): - with Script("reqerr.py", None) as s: + with Script("reqerr.py") as s: s.run("request", None) - s = Script("unloaderr.py", None) + s = Script("unloaderr.py") s.load() with tutils.raises(ScriptException): s.unload() diff --git a/test/mitmproxy/test_examples.py b/test/mitmproxy/test_examples.py index f30973e7..3b5ff2a2 100644 --- a/test/mitmproxy/test_examples.py +++ b/test/mitmproxy/test_examples.py @@ -1,47 +1,31 @@ import glob import json +import mock import os import sys from contextlib import contextmanager from mitmproxy import script -from mitmproxy.proxy import config import netlib.utils from netlib import tutils as netutils from netlib.http import Headers -from . import tservers, tutils +from . import tutils example_dir = netlib.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() - with script.Script(command, ctx) as s: + with script.Script(command) as s: yield s -def test_load_scripts(): +@mock.patch("mitmproxy.master") +@mock.patch("mitmproxy.log") +def test_load_scripts(log, master): scripts = glob.glob("%s/*.py" % example_dir) - tmaster = tservers.TestMaster(config.ProxyConfig()) - for f in scripts: if "har_extractor" in f: continue @@ -54,7 +38,7 @@ def test_load_scripts(): if "modify_response_body" in f: f += " foo bar" # two arguments required - s = script.Script(f, script.ScriptContext(tmaster)) + s = script.Script(f) try: s.load() except Exception as v: @@ -71,17 +55,21 @@ def test_add_header(): assert flow.response.headers["newheader"] == "foo" -def test_custom_contentviews(): - with example("custom_contentviews.py") as ex: - pig = ex.ctx.contentview +@mock.patch("mitmproxy.contentviews.remove") +@mock.patch("mitmproxy.contentviews.add") +def test_custom_contentviews(add, remove): + with example("custom_contentviews.py"): + assert add.called + pig = add.call_args[0][0] _, fmt = pig(b"<html>test!</html>") assert any(b'esttay!' in val[0][1] for val in fmt) assert not pig(b"gobbledygook") + assert remove.called def test_iframe_injector(): with tutils.raises(script.ScriptException): - with example("iframe_injector.py") as ex: + with example("iframe_injector.py"): pass flow = tutils.tflow(resp=netutils.tresp(content=b"<html>mitmproxy</html>")) @@ -121,7 +109,7 @@ def test_modify_response_body(): flow = tutils.tflow(resp=netutils.tresp(content=b"I <3 mitmproxy")) with example("modify_response_body.py mitmproxy rocks") as ex: - assert ex.ctx.old == b"mitmproxy" and ex.ctx.new == b"rocks" + assert ex.ns["state"]["old"] == b"mitmproxy" and ex.ns["state"]["new"] == b"rocks" ex.run("response", flow) assert flow.response.content == b"I <3 rocks" @@ -133,7 +121,8 @@ def test_redirect_requests(): assert flow.request.host == "mitmproxy.org" -def test_har_extractor(): +@mock.patch("mitmproxy.log") +def test_har_extractor(log): if sys.version_info >= (3, 0): with tutils.raises("does not work on Python 3"): with example("har_extractor.py -"): @@ -159,4 +148,4 @@ def test_har_extractor(): 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"] + assert json.loads(ex.ns["context"].HARLog.json()) == test_data["test_response"] diff --git a/test/mitmproxy/test_server.py b/test/mitmproxy/test_server.py index 0ab7624e..9dd8b79c 100644 --- a/test/mitmproxy/test_server.py +++ b/test/mitmproxy/test_server.py @@ -1,6 +1,7 @@ import os import socket import time +import types from OpenSSL import SSL from netlib.exceptions import HttpReadDisconnect, HttpException from netlib.tcp import Address @@ -945,7 +946,7 @@ class TestProxyChainingSSLReconnect(tservers.HTTPUpstreamProxyTest): f.reply.kill() return _func(f) - setattr(master, attr, handler) + setattr(master, attr, types.MethodType(handler, master)) kill_requests( self.chain[1].tmaster, |