aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/mitmproxy/data/scripts/a.py4
-rw-r--r--test/mitmproxy/data/scripts/all.py29
-rw-r--r--test/mitmproxy/data/scripts/concurrent_decorator.py2
-rw-r--r--test/mitmproxy/data/scripts/concurrent_decorator_err.py2
-rw-r--r--test/mitmproxy/data/scripts/duplicate_flow.py8
-rw-r--r--test/mitmproxy/data/scripts/reqerr.py4
-rw-r--r--test/mitmproxy/data/scripts/starterr.py2
-rw-r--r--test/mitmproxy/data/scripts/stream_modify.py2
-rw-r--r--test/mitmproxy/data/scripts/tcp_stream_modify.py2
-rw-r--r--test/mitmproxy/data/scripts/unloaderr.py2
-rw-r--r--test/mitmproxy/script/test_concurrent.py4
-rw-r--r--test/mitmproxy/script/test_reloader.py2
-rw-r--r--test/mitmproxy/script/test_script.py28
-rw-r--r--test/mitmproxy/test_examples.py49
-rw-r--r--test/mitmproxy/test_server.py3
15 files changed, 68 insertions, 75 deletions
diff --git a/test/mitmproxy/data/scripts/a.py b/test/mitmproxy/data/scripts/a.py
index 33dbaa64..ab0dbf96 100644
--- a/test/mitmproxy/data/scripts/a.py
+++ b/test/mitmproxy/data/scripts/a.py
@@ -5,12 +5,12 @@ from a_helper import parser
var = 0
-def start(ctx):
+def start():
global var
var = parser.parse_args(sys.argv[1:]).var
-def here(ctx):
+def here():
global var
var += 1
return var
diff --git a/test/mitmproxy/data/scripts/all.py b/test/mitmproxy/data/scripts/all.py
index dad2aade..bf8e93ec 100644
--- a/test/mitmproxy/data/scripts/all.py
+++ b/test/mitmproxy/data/scripts/all.py
@@ -1,36 +1,37 @@
+import mitmproxy
log = []
-def clientconnect(ctx, cc):
- ctx.log("XCLIENTCONNECT")
+def clientconnect(cc):
+ mitmproxy.ctx.log("XCLIENTCONNECT")
log.append("clientconnect")
-def serverconnect(ctx, cc):
- ctx.log("XSERVERCONNECT")
+def serverconnect(cc):
+ mitmproxy.ctx.log("XSERVERCONNECT")
log.append("serverconnect")
-def request(ctx, f):
- ctx.log("XREQUEST")
+def request(f):
+ mitmproxy.ctx.log("XREQUEST")
log.append("request")
-def response(ctx, f):
- ctx.log("XRESPONSE")
+def response(f):
+ mitmproxy.ctx.log("XRESPONSE")
log.append("response")
-def responseheaders(ctx, f):
- ctx.log("XRESPONSEHEADERS")
+def responseheaders(f):
+ mitmproxy.ctx.log("XRESPONSEHEADERS")
log.append("responseheaders")
-def clientdisconnect(ctx, cc):
- ctx.log("XCLIENTDISCONNECT")
+def clientdisconnect(cc):
+ mitmproxy.ctx.log("XCLIENTDISCONNECT")
log.append("clientdisconnect")
-def error(ctx, cc):
- ctx.log("XERROR")
+def error(cc):
+ mitmproxy.ctx.log("XERROR")
log.append("error")
diff --git a/test/mitmproxy/data/scripts/concurrent_decorator.py b/test/mitmproxy/data/scripts/concurrent_decorator.py
index e017f605..162c00f4 100644
--- a/test/mitmproxy/data/scripts/concurrent_decorator.py
+++ b/test/mitmproxy/data/scripts/concurrent_decorator.py
@@ -3,5 +3,5 @@ from mitmproxy.script import concurrent
@concurrent
-def request(context, flow):
+def request(flow):
time.sleep(0.1)
diff --git a/test/mitmproxy/data/scripts/concurrent_decorator_err.py b/test/mitmproxy/data/scripts/concurrent_decorator_err.py
index 349e5dd6..756869c8 100644
--- a/test/mitmproxy/data/scripts/concurrent_decorator_err.py
+++ b/test/mitmproxy/data/scripts/concurrent_decorator_err.py
@@ -2,5 +2,5 @@ from mitmproxy.script import concurrent
@concurrent
-def start(context):
+def start():
pass
diff --git a/test/mitmproxy/data/scripts/duplicate_flow.py b/test/mitmproxy/data/scripts/duplicate_flow.py
index e13af786..565b1845 100644
--- a/test/mitmproxy/data/scripts/duplicate_flow.py
+++ b/test/mitmproxy/data/scripts/duplicate_flow.py
@@ -1,4 +1,6 @@
+import mitmproxy
-def request(ctx, f):
- f = ctx.duplicate_flow(f)
- ctx.replay_request(f)
+
+def request(f):
+ f = mitmproxy.ctx.master.duplicate_flow(f)
+ mitmproxy.ctx.master.replay_request(f, block=True, run_scripthooks=False)
diff --git a/test/mitmproxy/data/scripts/reqerr.py b/test/mitmproxy/data/scripts/reqerr.py
index e7c503a8..7b419361 100644
--- a/test/mitmproxy/data/scripts/reqerr.py
+++ b/test/mitmproxy/data/scripts/reqerr.py
@@ -1,2 +1,2 @@
-def request(ctx, r):
- raise ValueError
+def request(r):
+ raise ValueError()
diff --git a/test/mitmproxy/data/scripts/starterr.py b/test/mitmproxy/data/scripts/starterr.py
index 82d773bd..28ba2ff1 100644
--- a/test/mitmproxy/data/scripts/starterr.py
+++ b/test/mitmproxy/data/scripts/starterr.py
@@ -1,3 +1,3 @@
-def start(ctx):
+def start():
raise ValueError()
diff --git a/test/mitmproxy/data/scripts/stream_modify.py b/test/mitmproxy/data/scripts/stream_modify.py
index 8221b0dd..4fbf45c2 100644
--- a/test/mitmproxy/data/scripts/stream_modify.py
+++ b/test/mitmproxy/data/scripts/stream_modify.py
@@ -3,5 +3,5 @@ def modify(chunks):
yield chunk.replace(b"foo", b"bar")
-def responseheaders(context, flow):
+def responseheaders(flow):
flow.response.stream = modify
diff --git a/test/mitmproxy/data/scripts/tcp_stream_modify.py b/test/mitmproxy/data/scripts/tcp_stream_modify.py
index 0965beba..2281e6e6 100644
--- a/test/mitmproxy/data/scripts/tcp_stream_modify.py
+++ b/test/mitmproxy/data/scripts/tcp_stream_modify.py
@@ -1,4 +1,4 @@
-def tcp_message(ctx, flow):
+def tcp_message(flow):
message = flow.messages[-1]
if not message.from_client:
message.content = message.content.replace(b"foo", b"bar")
diff --git a/test/mitmproxy/data/scripts/unloaderr.py b/test/mitmproxy/data/scripts/unloaderr.py
index fba02734..6a48ab43 100644
--- a/test/mitmproxy/data/scripts/unloaderr.py
+++ b/test/mitmproxy/data/scripts/unloaderr.py
@@ -1,2 +1,2 @@
-def done(ctx):
+def done():
raise RuntimeError()
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..bdadcd11 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.ctx.master")
+@mock.patch("mitmproxy.ctx.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.ctx.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,