aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorAldo Cortesi <aldo@corte.si>2017-12-16 09:19:59 +1300
committerAldo Cortesi <aldo@corte.si>2017-12-17 10:11:02 +1300
commit50a94db2cc2f3107f6f94c1e1407cb6840d1da08 (patch)
tree6671394d48bba1671a95a2bd26935ca34bd661f1 /test
parent099aa9cebfc290ffec1e0890a7ac5b315eb56aed (diff)
downloadmitmproxy-50a94db2cc2f3107f6f94c1e1407cb6840d1da08.tar.gz
mitmproxy-50a94db2cc2f3107f6f94c1e1407cb6840d1da08.tar.bz2
mitmproxy-50a94db2cc2f3107f6f94c1e1407cb6840d1da08.zip
commands: Reassess the cuts API
Make the cuts API more transparent. Cut specifications are no longer a centrally resolved core type, and flows are now passed explicitly.
Diffstat (limited to 'test')
-rw-r--r--test/mitmproxy/addons/test_cut.py103
-rw-r--r--test/mitmproxy/test_command.py2
2 files changed, 32 insertions, 73 deletions
diff --git a/test/mitmproxy/addons/test_cut.py b/test/mitmproxy/addons/test_cut.py
index 242c6c2f..bb3e1c2d 100644
--- a/test/mitmproxy/addons/test_cut.py
+++ b/test/mitmproxy/addons/test_cut.py
@@ -56,42 +56,6 @@ def test_extract():
assert "CERTIFICATE" in cut.extract("sc.cert", tf)
-def test_parse_cutspec():
- tests = [
- ("", None, True),
- ("req.method", ("@all", ["req.method"]), False),
- (
- "req.method,req.host",
- ("@all", ["req.method", "req.host"]),
- False
- ),
- (
- "req.method,req.host|~b foo",
- ("~b foo", ["req.method", "req.host"]),
- False
- ),
- (
- "req.method,req.host|~b foo | ~b bar",
- ("~b foo | ~b bar", ["req.method", "req.host"]),
- False
- ),
- (
- "req.method, req.host | ~b foo | ~b bar",
- ("~b foo | ~b bar", ["req.method", "req.host"]),
- False
- ),
- ]
- for cutspec, output, err in tests:
- try:
- assert cut.parse_cutspec(cutspec) == output
- except exceptions.CommandError:
- if not err:
- raise
- else:
- if err:
- raise AssertionError("Expected error.")
-
-
def test_headername():
with pytest.raises(exceptions.CommandError):
cut.headername("header[foo.")
@@ -110,69 +74,64 @@ def test_cut_clip():
v.add([tflow.tflow(resp=True)])
with mock.patch('pyperclip.copy') as pc:
- tctx.command(c.clip, "q.method|@all")
+ tctx.command(c.clip, "@all", "q.method")
assert pc.called
with mock.patch('pyperclip.copy') as pc:
- tctx.command(c.clip, "q.content|@all")
+ tctx.command(c.clip, "@all", "q.content")
assert pc.called
with mock.patch('pyperclip.copy') as pc:
- tctx.command(c.clip, "q.method,q.content|@all")
+ tctx.command(c.clip, "@all", "q.method,q.content")
assert pc.called
-def test_cut_file(tmpdir):
+def test_cut_save(tmpdir):
f = str(tmpdir.join("path"))
v = view.View()
c = cut.Cut()
with taddons.context() as tctx:
tctx.master.addons.add(v, c)
-
v.add([tflow.tflow(resp=True)])
- tctx.command(c.save, "q.method|@all", f)
+ tctx.command(c.save, "@all", "q.method", f)
assert qr(f) == b"GET"
- tctx.command(c.save, "q.content|@all", f)
+ tctx.command(c.save, "@all", "q.content", f)
assert qr(f) == b"content"
- tctx.command(c.save, "q.content|@all", "+" + f)
+ tctx.command(c.save, "@all", "q.content", "+" + f)
assert qr(f) == b"content\ncontent"
v.add([tflow.tflow(resp=True)])
- tctx.command(c.save, "q.method|@all", f)
+ tctx.command(c.save, "@all", "q.method", f)
assert qr(f).splitlines() == [b"GET", b"GET"]
- tctx.command(c.save, "q.method,q.content|@all", f)
+ tctx.command(c.save, "@all", "q.method,q.content", f)
assert qr(f).splitlines() == [b"GET,content", b"GET,content"]
def test_cut():
- v = view.View()
c = cut.Cut()
- with taddons.context() as tctx:
- v.add([tflow.tflow(resp=True)])
- tctx.master.addons.add(v, c)
- assert c.cut("q.method|@all") == [["GET"]]
- assert c.cut("q.scheme|@all") == [["http"]]
- assert c.cut("q.host|@all") == [["address"]]
- assert c.cut("q.port|@all") == [["22"]]
- assert c.cut("q.path|@all") == [["/path"]]
- assert c.cut("q.url|@all") == [["http://address:22/path"]]
- assert c.cut("q.content|@all") == [[b"content"]]
- assert c.cut("q.header[header]|@all") == [["qvalue"]]
- assert c.cut("q.header[unknown]|@all") == [[""]]
-
- assert c.cut("s.status_code|@all") == [["200"]]
- assert c.cut("s.reason|@all") == [["OK"]]
- assert c.cut("s.content|@all") == [[b"message"]]
- assert c.cut("s.header[header-response]|@all") == [["svalue"]]
- assert c.cut("moo") == [[""]]
+ with taddons.context():
+ tflows = [tflow.tflow(resp=True)]
+ assert c.cut(tflows, ["q.method"]) == [["GET"]]
+ assert c.cut(tflows, ["q.scheme"]) == [["http"]]
+ assert c.cut(tflows, ["q.host"]) == [["address"]]
+ assert c.cut(tflows, ["q.port"]) == [["22"]]
+ assert c.cut(tflows, ["q.path"]) == [["/path"]]
+ assert c.cut(tflows, ["q.url"]) == [["http://address:22/path"]]
+ assert c.cut(tflows, ["q.content"]) == [[b"content"]]
+ assert c.cut(tflows, ["q.header[header]"]) == [["qvalue"]]
+ assert c.cut(tflows, ["q.header[unknown]"]) == [[""]]
+
+ assert c.cut(tflows, ["s.status_code"]) == [["200"]]
+ assert c.cut(tflows, ["s.reason"]) == [["OK"]]
+ assert c.cut(tflows, ["s.content"]) == [[b"message"]]
+ assert c.cut(tflows, ["s.header[header-response]"]) == [["svalue"]]
+ assert c.cut(tflows, ["moo"]) == [[""]]
with pytest.raises(exceptions.CommandError):
- assert c.cut("__dict__") == [[""]]
+ assert c.cut(tflows, ["__dict__"]) == [[""]]
- v = view.View()
c = cut.Cut()
- with taddons.context() as tctx:
- tctx.master.addons.add(v, c)
- v.add([tflow.ttcpflow()])
- assert c.cut("q.method|@all") == [[""]]
- assert c.cut("s.status|@all") == [[""]]
+ with taddons.context():
+ tflows = [tflow.ttcpflow()]
+ assert c.cut(tflows, ["q.method"]) == [[""]]
+ assert c.cut(tflows, ["s.status"]) == [[""]]
diff --git a/test/mitmproxy/test_command.py b/test/mitmproxy/test_command.py
index 298b34fb..066cbf15 100644
--- a/test/mitmproxy/test_command.py
+++ b/test/mitmproxy/test_command.py
@@ -155,8 +155,8 @@ def test_typename():
assert command.typename(typing.Sequence[flow.Flow], True) == "[flow]"
assert command.typename(typing.Sequence[flow.Flow], False) == "flowspec"
- assert command.typename(command.Cuts, False) == "cutspec"
assert command.typename(command.Cuts, True) == "[cuts]"
+ assert command.typename(typing.Sequence[command.Cut], False) == "[cut]"
assert command.typename(flow.Flow, False) == "flow"
assert command.typename(typing.Sequence[str], False) == "[str]"