aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/mitmproxy/addons/test_replace.py69
-rw-r--r--test/mitmproxy/addons/test_script.py46
-rw-r--r--test/mitmproxy/addons/test_termstatus.py12
-rw-r--r--test/mitmproxy/data/addonscripts/addon.py4
-rw-r--r--test/mitmproxy/data/addonscripts/concurrent_decorator_class.py2
-rw-r--r--test/mitmproxy/data/addonscripts/concurrent_decorator_err.py2
-rw-r--r--test/mitmproxy/data/addonscripts/recorder.py2
-rw-r--r--test/mitmproxy/net/http/test_message.py6
-rw-r--r--test/mitmproxy/proxy/test_server.py3
-rw-r--r--test/mitmproxy/script/test_concurrent.py6
-rw-r--r--test/mitmproxy/test_addonmanager.py8
-rw-r--r--test/mitmproxy/test_certs.py1
-rw-r--r--test/mitmproxy/test_connections.py25
-rw-r--r--test/mitmproxy/test_optmanager.py50
-rw-r--r--test/mitmproxy/tools/console/test_master.py4
-rw-r--r--test/mitmproxy/tservers.py2
-rw-r--r--test/mitmproxy/types/test_multidict.py14
-rw-r--r--test/mitmproxy/types/test_serializable.py19
18 files changed, 176 insertions, 99 deletions
diff --git a/test/mitmproxy/addons/test_replace.py b/test/mitmproxy/addons/test_replace.py
index 2311641a..7d590b35 100644
--- a/test/mitmproxy/addons/test_replace.py
+++ b/test/mitmproxy/addons/test_replace.py
@@ -1,6 +1,5 @@
import pytest
-from .. import tservers
from mitmproxy.addons import replace
from mitmproxy.test import taddons
from mitmproxy.test import tflow
@@ -32,7 +31,7 @@ class TestReplace:
with taddons.context() as tctx:
tctx.configure(
r,
- replacements = [
+ replacements=[
"/~q/foo/bar",
"/~s/foo/bar",
]
@@ -47,53 +46,57 @@ class TestReplace:
r.response(f)
assert f.response.content == b"bar"
-
-class TestUpstreamProxy(tservers.HTTPUpstreamProxyTest):
- ssl = False
-
def test_order(self):
- sa = replace.Replace()
- self.proxy.tmaster.addons.add(sa)
-
- self.proxy.tmaster.options.replacements = [
- "/~q/foo/bar",
- "/~q/bar/baz",
- "/~q/foo/oh noes!",
- "/~s/baz/ORLY"
- ]
- p = self.pathoc()
- with p.connect():
- req = p.request("get:'%s/p/418:b\"foo\"'" % self.server.urlbase)
- assert req.content == b"ORLY"
- assert req.status_code == 418
+ r = replace.Replace()
+ with taddons.context() as tctx:
+ tctx.configure(
+ r,
+ replacements=[
+ "/foo/bar",
+ "/bar/baz",
+ "/foo/oh noes!",
+ "/bar/oh noes!",
+ ]
+ )
+ f = tflow.tflow()
+ f.request.content = b"foo"
+ r.request(f)
+ assert f.request.content == b"baz"
class TestReplaceFile:
def test_simple(self, tmpdir):
- r = replace.ReplaceFile()
- rp = tmpdir.join("replacement")
- rp.write("bar")
+ r = replace.Replace()
with taddons.context() as tctx:
+ tmpfile = tmpdir.join("replacement")
+ tmpfile.write("bar")
tctx.configure(
r,
- replacement_files = [
- "/~q/foo/" + str(rp),
- "/~s/foo/" + str(rp),
- "/~b nonexistent/nonexistent/nonexistent",
- ]
+ replacements=["/~q/foo/@" + str(tmpfile)]
)
f = tflow.tflow()
f.request.content = b"foo"
r.request(f)
assert f.request.content == b"bar"
- f = tflow.tflow(resp=True)
- f.response.content = b"foo"
- r.response(f)
- assert f.response.content == b"bar"
+ def test_nonexistent(self, tmpdir):
+ r = replace.Replace()
+ with taddons.context() as tctx:
+ with pytest.raises(Exception, match="Invalid file path"):
+ tctx.configure(
+ r,
+ replacements=["/~q/foo/@nonexistent"]
+ )
+ tmpfile = tmpdir.join("replacement")
+ tmpfile.write("bar")
+ tctx.configure(
+ r,
+ replacements=["/~q/foo/@" + str(tmpfile)]
+ )
+ tmpfile.remove()
f = tflow.tflow()
- f.request.content = b"nonexistent"
+ f.request.content = b"foo"
assert not tctx.master.event_log
r.request(f)
assert tctx.master.event_log
diff --git a/test/mitmproxy/addons/test_script.py b/test/mitmproxy/addons/test_script.py
index d79ed4ef..4c1b2e43 100644
--- a/test/mitmproxy/addons/test_script.py
+++ b/test/mitmproxy/addons/test_script.py
@@ -116,9 +116,7 @@ class TestScript:
)
)
sc.load_script()
- assert sc.ns.call_log == [
- ("solo", "start", (), {}),
- ]
+ assert sc.ns.call_log[0][0:2] == ("solo", "start")
sc.ns.call_log = []
f = tflow.tflow(resp=True)
@@ -146,7 +144,7 @@ class TestScript:
sc = script.Script(
tutils.test_data.path("mitmproxy/data/addonscripts/error.py")
)
- sc.start()
+ sc.start(tctx.options)
f = tflow.tflow(resp=True)
sc.request(f)
assert tctx.master.event_log[0][0] == "error"
@@ -162,7 +160,7 @@ class TestScript:
"mitmproxy/data/addonscripts/addon.py"
)
)
- sc.start()
+ sc.start(tctx.options)
tctx.configure(sc)
assert sc.ns.event_log == [
'scriptstart', 'addonstart', 'addonconfigure'
@@ -225,24 +223,31 @@ class TestScriptLoader:
assert len(m.addons) == 1
def test_dupes(self):
- o = options.Options(scripts=["one", "one"])
- m = master.Master(o, proxy.DummyServer())
sc = script.ScriptLoader()
- with pytest.raises(exceptions.OptionsError):
- m.addons.add(o, sc)
+ with taddons.context() as tctx:
+ tctx.master.addons.add(sc)
+ with pytest.raises(exceptions.OptionsError):
+ tctx.configure(
+ sc,
+ scripts = ["one", "one"]
+ )
def test_nonexistent(self):
- o = options.Options(scripts=["nonexistent"])
- m = master.Master(o, proxy.DummyServer())
sc = script.ScriptLoader()
- with pytest.raises(exceptions.OptionsError):
- m.addons.add(o, sc)
+ with taddons.context() as tctx:
+ tctx.master.addons.add(sc)
+ with pytest.raises(exceptions.OptionsError):
+ tctx.configure(
+ sc,
+ scripts = ["nonexistent"]
+ )
def test_order(self):
rec = tutils.test_data.path("mitmproxy/data/addonscripts/recorder.py")
sc = script.ScriptLoader()
with taddons.context() as tctx:
tctx.master.addons.add(sc)
+ sc.running()
tctx.configure(
sc,
scripts = [
@@ -253,9 +258,17 @@ class TestScriptLoader:
)
debug = [(i[0], i[1]) for i in tctx.master.event_log if i[0] == "debug"]
assert debug == [
- ('debug', 'a start'), ('debug', 'a configure'),
- ('debug', 'b start'), ('debug', 'b configure'),
- ('debug', 'c start'), ('debug', 'c configure')
+ ('debug', 'a start'),
+ ('debug', 'a configure'),
+ ('debug', 'a running'),
+
+ ('debug', 'b start'),
+ ('debug', 'b configure'),
+ ('debug', 'b running'),
+
+ ('debug', 'c start'),
+ ('debug', 'c configure'),
+ ('debug', 'c running'),
]
tctx.master.event_log = []
tctx.configure(
@@ -284,4 +297,5 @@ class TestScriptLoader:
('debug', 'b done'),
('debug', 'x start'),
('debug', 'x configure'),
+ ('debug', 'x running'),
]
diff --git a/test/mitmproxy/addons/test_termstatus.py b/test/mitmproxy/addons/test_termstatus.py
new file mode 100644
index 00000000..01c14814
--- /dev/null
+++ b/test/mitmproxy/addons/test_termstatus.py
@@ -0,0 +1,12 @@
+from mitmproxy.addons import termstatus
+from mitmproxy.test import taddons
+
+
+def test_configure():
+ ts = termstatus.TermStatus()
+ with taddons.context() as ctx:
+ ts.running()
+ assert not ctx.master.event_log
+ ctx.configure(ts, server=True)
+ ts.running()
+ assert ctx.master.event_log
diff --git a/test/mitmproxy/data/addonscripts/addon.py b/test/mitmproxy/data/addonscripts/addon.py
index 84173cb6..f34f41cb 100644
--- a/test/mitmproxy/data/addonscripts/addon.py
+++ b/test/mitmproxy/data/addonscripts/addon.py
@@ -6,7 +6,7 @@ class Addon:
def event_log(self):
return event_log
- def start(self):
+ def start(self, opts):
event_log.append("addonstart")
def configure(self, options, updated):
@@ -17,6 +17,6 @@ def configure(options, updated):
event_log.append("addonconfigure")
-def start():
+def start(opts):
event_log.append("scriptstart")
return Addon()
diff --git a/test/mitmproxy/data/addonscripts/concurrent_decorator_class.py b/test/mitmproxy/data/addonscripts/concurrent_decorator_class.py
index bd047c99..10ba24cd 100644
--- a/test/mitmproxy/data/addonscripts/concurrent_decorator_class.py
+++ b/test/mitmproxy/data/addonscripts/concurrent_decorator_class.py
@@ -9,5 +9,5 @@ class ConcurrentClass:
time.sleep(0.1)
-def start():
+def start(opts):
return ConcurrentClass()
diff --git a/test/mitmproxy/data/addonscripts/concurrent_decorator_err.py b/test/mitmproxy/data/addonscripts/concurrent_decorator_err.py
index 756869c8..7bc28182 100644
--- a/test/mitmproxy/data/addonscripts/concurrent_decorator_err.py
+++ b/test/mitmproxy/data/addonscripts/concurrent_decorator_err.py
@@ -2,5 +2,5 @@ from mitmproxy.script import concurrent
@concurrent
-def start():
+def start(opts):
pass
diff --git a/test/mitmproxy/data/addonscripts/recorder.py b/test/mitmproxy/data/addonscripts/recorder.py
index 6b9b6ea8..aff524a8 100644
--- a/test/mitmproxy/data/addonscripts/recorder.py
+++ b/test/mitmproxy/data/addonscripts/recorder.py
@@ -22,5 +22,5 @@ class CallLogger:
raise AttributeError
-def start():
+def start(opts):
return CallLogger(*sys.argv[1:])
diff --git a/test/mitmproxy/net/http/test_message.py b/test/mitmproxy/net/http/test_message.py
index 034bd600..b75bc7c2 100644
--- a/test/mitmproxy/net/http/test_message.py
+++ b/test/mitmproxy/net/http/test_message.py
@@ -38,14 +38,12 @@ def _test_decoded_attr(message, attr):
class TestMessageData:
- def test_eq_ne(self):
+ def test_eq(self):
data = tutils.tresp(timestamp_start=42, timestamp_end=42).data
same = tutils.tresp(timestamp_start=42, timestamp_end=42).data
assert data == same
- assert not data != same
other = tutils.tresp(content=b"foo").data
- assert not data == other
assert data != other
assert data != 0
@@ -61,10 +59,8 @@ class TestMessage:
resp = tutils.tresp(timestamp_start=42, timestamp_end=42)
same = tutils.tresp(timestamp_start=42, timestamp_end=42)
assert resp == same
- assert not resp != same
other = tutils.tresp(timestamp_start=0, timestamp_end=0)
- assert not resp == other
assert resp != other
assert resp != 0
diff --git a/test/mitmproxy/proxy/test_server.py b/test/mitmproxy/proxy/test_server.py
index aa45761a..16efe415 100644
--- a/test/mitmproxy/proxy/test_server.py
+++ b/test/mitmproxy/proxy/test_server.py
@@ -302,6 +302,9 @@ class TestHTTP(tservers.HTTPProxyTest, CommonMixin):
class TestHTTPAuth(tservers.HTTPProxyTest):
def test_auth(self):
self.master.addons.add(proxyauth.ProxyAuth())
+ self.master.addons.configure_all(
+ self.master.options, self.master.options.keys()
+ )
self.master.options.proxyauth = "test:test"
assert self.pathod("202").status_code == 407
p = self.pathoc()
diff --git a/test/mitmproxy/script/test_concurrent.py b/test/mitmproxy/script/test_concurrent.py
index e81c023d..a9b6f0c4 100644
--- a/test/mitmproxy/script/test_concurrent.py
+++ b/test/mitmproxy/script/test_concurrent.py
@@ -24,7 +24,7 @@ class TestConcurrent(tservers.MasterTest):
"mitmproxy/data/addonscripts/concurrent_decorator.py"
)
)
- sc.start()
+ sc.start(tctx.options)
f1, f2 = tflow.tflow(), tflow.tflow()
tctx.cycle(sc, f1)
@@ -42,7 +42,7 @@ class TestConcurrent(tservers.MasterTest):
"mitmproxy/data/addonscripts/concurrent_decorator_err.py"
)
)
- sc.start()
+ sc.start(tctx.options)
assert "decorator not supported" in tctx.master.event_log[0][1]
def test_concurrent_class(self):
@@ -52,7 +52,7 @@ class TestConcurrent(tservers.MasterTest):
"mitmproxy/data/addonscripts/concurrent_decorator_class.py"
)
)
- sc.start()
+ sc.start(tctx.options)
f1, f2 = tflow.tflow(), tflow.tflow()
tctx.cycle(sc, f1)
diff --git a/test/mitmproxy/test_addonmanager.py b/test/mitmproxy/test_addonmanager.py
index 17402e26..3e5f71c6 100644
--- a/test/mitmproxy/test_addonmanager.py
+++ b/test/mitmproxy/test_addonmanager.py
@@ -10,12 +10,12 @@ from mitmproxy import proxy
class TAddon:
def __init__(self, name):
self.name = name
- self.noop_member = True
+ self.tick = True
def __repr__(self):
return "Addon(%s)" % self.name
- def noop(self):
+ def done(self):
pass
@@ -30,6 +30,6 @@ def test_simple():
assert not a.chain
a.add(TAddon("one"))
- a("noop")
+ a("done")
with pytest.raises(exceptions.AddonError):
- a("noop_member")
+ a("tick")
diff --git a/test/mitmproxy/test_certs.py b/test/mitmproxy/test_certs.py
index 2d12c370..88c49561 100644
--- a/test/mitmproxy/test_certs.py
+++ b/test/mitmproxy/test_certs.py
@@ -160,7 +160,6 @@ class TestSSLCert:
assert c2.to_pem()
assert c2.has_expired is not None
- assert not c1 == c2
assert c1 != c2
def test_err_broken_sans(self):
diff --git a/test/mitmproxy/test_connections.py b/test/mitmproxy/test_connections.py
index 0083f57c..67a6552f 100644
--- a/test/mitmproxy/test_connections.py
+++ b/test/mitmproxy/test_connections.py
@@ -66,8 +66,18 @@ class TestClientConnection:
assert c.timestamp_start == 42
c3 = c.copy()
+ assert c3.get_state() != c.get_state()
+ c.id = c3.id = "foo"
assert c3.get_state() == c.get_state()
+ def test_eq(self):
+ c = tflow.tclient_conn()
+ c2 = c.copy()
+ assert c == c
+ assert c != c2
+ assert c != 42
+ assert hash(c) != hash(c2)
+
class TestServerConnection:
@@ -147,6 +157,21 @@ class TestServerConnection:
with pytest.raises(ValueError, matches='sni must be str, not '):
c.establish_ssl(None, b'foobar')
+ def test_state(self):
+ c = tflow.tserver_conn()
+ c2 = c.copy()
+ assert c2.get_state() != c.get_state()
+ c.id = c2.id = "foo"
+ assert c2.get_state() == c.get_state()
+
+ def test_eq(self):
+ c = tflow.tserver_conn()
+ c2 = c.copy()
+ assert c == c
+ assert c != c2
+ assert c != 42
+ assert hash(c) != hash(c2)
+
class TestClientConnectionTLS:
diff --git a/test/mitmproxy/test_optmanager.py b/test/mitmproxy/test_optmanager.py
index 0ecfc4e5..df392829 100644
--- a/test/mitmproxy/test_optmanager.py
+++ b/test/mitmproxy/test_optmanager.py
@@ -83,10 +83,11 @@ def test_options():
with pytest.raises(TypeError):
TO(nonexistent = "value")
- with pytest.raises(Exception, match="No such option"):
+ with pytest.raises(Exception, match="Unknown options"):
o.nonexistent = "value"
- with pytest.raises(Exception, match="No such option"):
+ with pytest.raises(Exception, match="Unknown options"):
o.update(nonexistent = "value")
+ assert o.update_known(nonexistent = "value") == {"nonexistent": "value"}
rec = []
@@ -199,61 +200,63 @@ def test_simple():
def test_serialize():
o = TD2()
o.three = "set"
- assert "dfour" in o.serialize(None, defaults=True)
+ assert "dfour" in optmanager.serialize(o, None, defaults=True)
- data = o.serialize(None)
+ data = optmanager.serialize(o, None)
assert "dfour" not in data
o2 = TD2()
- o2.load(data)
+ optmanager.load(o2, data)
assert o2 == o
t = """
unknown: foo
"""
- data = o.serialize(t)
+ data = optmanager.serialize(o, t)
o2 = TD2()
- o2.load(data)
+ optmanager.load(o2, data)
assert o2 == o
t = "invalid: foo\ninvalid"
with pytest.raises(Exception, match="Config error"):
- o2.load(t)
+ optmanager.load(o2, t)
t = "invalid"
with pytest.raises(Exception, match="Config error"):
- o2.load(t)
+ optmanager.load(o2, t)
t = ""
- o2.load(t)
-
- with pytest.raises(exceptions.OptionsError, matches='No such option: foobar'):
- o2.load("foobar: '123'")
+ optmanager.load(o2, t)
+ assert optmanager.load(o2, "foobar: '123'") == {"foobar": "123"}
def test_serialize_defaults():
o = options.Options()
- assert o.serialize(None, defaults=True)
+ assert optmanager.serialize(o, None, defaults=True)
def test_saving(tmpdir):
o = TD2()
o.three = "set"
dst = str(tmpdir.join("conf"))
- o.save(dst, defaults=True)
+ optmanager.save(o, dst, defaults=True)
o2 = TD2()
- o2.load_paths(dst)
+ optmanager.load_paths(o2, dst)
o2.three = "foo"
- o2.save(dst, defaults=True)
+ optmanager.save(o2, dst, defaults=True)
- o.load_paths(dst)
+ optmanager.load_paths(o, dst)
assert o.three == "foo"
with open(dst, 'a') as f:
f.write("foobar: '123'")
- with pytest.raises(exceptions.OptionsError, matches=''):
- o.load_paths(dst)
+ assert optmanager.load_paths(o, dst) == {"foobar": "123"}
+
+ with open(dst, 'a') as f:
+ f.write("'''")
+ with pytest.raises(exceptions.OptionsError):
+ optmanager.load_paths(o, dst)
def test_merge():
@@ -280,9 +283,9 @@ def test_option():
assert o2 != o
-def test_dump():
+def test_dump_defaults():
o = options.Options()
- assert optmanager.dump(o)
+ assert optmanager.dump_defaults(o)
class TTypes(optmanager.OptManager):
@@ -346,3 +349,6 @@ def test_set():
assert opts.seqstr == ["foo", "bar"]
opts.set("seqstr")
assert opts.seqstr == []
+
+ with pytest.raises(exceptions.OptionsError):
+ opts.set("nonexistent=wobble")
diff --git a/test/mitmproxy/tools/console/test_master.py b/test/mitmproxy/tools/console/test_master.py
index 0bf3734b..6c716ad1 100644
--- a/test/mitmproxy/tools/console/test_master.py
+++ b/test/mitmproxy/tools/console/test_master.py
@@ -28,7 +28,9 @@ class TestMaster(tservers.MasterTest):
if "verbosity" not in opts:
opts["verbosity"] = 1
o = options.Options(**opts)
- return console.master.ConsoleMaster(o, proxy.DummyServer())
+ m = console.master.ConsoleMaster(o, proxy.DummyServer())
+ m.addons.configure_all(o, o.keys())
+ return m
def test_basic(self):
m = self.mkmaster()
diff --git a/test/mitmproxy/tservers.py b/test/mitmproxy/tservers.py
index a8aaa358..c47411ee 100644
--- a/test/mitmproxy/tservers.py
+++ b/test/mitmproxy/tservers.py
@@ -79,6 +79,8 @@ class TestMaster(master.Master):
self.state = TestState()
self.addons.add(self.state)
self.addons.add(*addons)
+ self.addons.configure_all(self.options, self.options.keys())
+ self.addons.invoke_all_with_context("running")
def clear_log(self):
self.tlog = []
diff --git a/test/mitmproxy/types/test_multidict.py b/test/mitmproxy/types/test_multidict.py
index 9b13c5cd..c76cd753 100644
--- a/test/mitmproxy/types/test_multidict.py
+++ b/test/mitmproxy/types/test_multidict.py
@@ -93,11 +93,6 @@ class TestMultiDict:
md1.fields = md1.fields[1:] + md1.fields[:1]
assert not (md1 == md2)
- def test_ne(self):
- assert not TMultiDict() != TMultiDict()
- assert TMultiDict() != self._multi()
- assert TMultiDict() != 42
-
def test_hash(self):
"""
If a class defines mutable objects and implements an __eq__() method,
@@ -205,3 +200,12 @@ class TestMultiDictView:
tv["c"] = "b"
assert p.vals == (("a", "b"), ("c", "b"))
assert tv["a"] == "b"
+
+ def test_copy(self):
+ p = TParent()
+ tv = multidict.MultiDictView(p.getter, p.setter)
+ c = tv.copy()
+ assert isinstance(c, multidict.MultiDict)
+ assert tv.items() == c.items()
+ c["foo"] = "bar"
+ assert tv.items() != c.items()
diff --git a/test/mitmproxy/types/test_serializable.py b/test/mitmproxy/types/test_serializable.py
index dd4a3778..390d17e1 100644
--- a/test/mitmproxy/types/test_serializable.py
+++ b/test/mitmproxy/types/test_serializable.py
@@ -1,3 +1,5 @@
+import copy
+
from mitmproxy.types import serializable
@@ -6,17 +8,17 @@ class SerializableDummy(serializable.Serializable):
self.i = i
def get_state(self):
- return self.i
+ return copy.copy(self.i)
def set_state(self, i):
self.i = i
- def from_state(self, state):
- return type(self)(state)
+ @classmethod
+ def from_state(cls, state):
+ return cls(state)
class TestSerializable:
-
def test_copy(self):
a = SerializableDummy(42)
assert a.i == 42
@@ -26,3 +28,12 @@ class TestSerializable:
a.set_state(1)
assert a.i == 1
assert b.i == 42
+
+ def test_copy_id(self):
+ a = SerializableDummy({
+ "id": "foo",
+ "foo": 42
+ })
+ b = a.copy()
+ assert a.get_state()["id"] != b.get_state()["id"]
+ assert a.get_state()["foo"] == b.get_state()["foo"]