aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorMaximilian Hils <git@maximilianhils.com>2017-08-21 15:08:25 +0200
committerGitHub <noreply@github.com>2017-08-21 15:08:25 +0200
commit7fcc945b4fa8d02edaec76349898c5cbdb678339 (patch)
tree5a1b5dd80db449eb0037e0367356e49123dc7f3d /test
parent9d3759728ae2437645d610fd5284144cc9c0a3f9 (diff)
parent2baa2c4049ab6b2801b3836b28b3e9df815a0075 (diff)
downloadmitmproxy-7fcc945b4fa8d02edaec76349898c5cbdb678339.tar.gz
mitmproxy-7fcc945b4fa8d02edaec76349898c5cbdb678339.tar.bz2
mitmproxy-7fcc945b4fa8d02edaec76349898c5cbdb678339.zip
Merge pull request #2510 from MatthewShao/static-viewer
[WIP][web]Static viewer converter for mitmweb
Diffstat (limited to 'test')
-rw-r--r--test/mitmproxy/addons/test_static_viewer.py63
-rw-r--r--test/mitmproxy/tools/web/test_app.py10
2 files changed, 68 insertions, 5 deletions
diff --git a/test/mitmproxy/addons/test_static_viewer.py b/test/mitmproxy/addons/test_static_viewer.py
new file mode 100644
index 00000000..bb2b6777
--- /dev/null
+++ b/test/mitmproxy/addons/test_static_viewer.py
@@ -0,0 +1,63 @@
+import json
+from unittest import mock
+
+from mitmproxy.test import taddons
+from mitmproxy.test import tflow
+
+from mitmproxy import flowfilter
+from mitmproxy.tools.web.app import flow_to_json
+
+from mitmproxy.addons import static_viewer
+from mitmproxy.addons import save
+
+
+def test_save_static(tmpdir):
+ tmpdir.mkdir('static')
+ static_viewer.save_static(tmpdir)
+ assert len(tmpdir.listdir()) == 2
+ assert tmpdir.join('index.html').check(file=1)
+ assert tmpdir.join('static/static.js').read() == 'MITMWEB_STATIC = true;'
+
+
+def test_save_filter_help(tmpdir):
+ static_viewer.save_filter_help(tmpdir)
+ f = tmpdir.join('/filter-help.json')
+ assert f.check(file=1)
+ assert f.read() == json.dumps(dict(commands=flowfilter.help))
+
+
+def test_save_flows(tmpdir):
+ flows = [tflow.tflow(req=True, resp=None), tflow.tflow(req=True, resp=True)]
+ static_viewer.save_flows(tmpdir, flows)
+ assert tmpdir.join('flows.json').check(file=1)
+ assert tmpdir.join('flows.json').read() == json.dumps([flow_to_json(f) for f in flows])
+
+
+@mock.patch('mitmproxy.ctx.log')
+def test_save_flows_content(ctx, tmpdir):
+ flows = [tflow.tflow(req=True, resp=None), tflow.tflow(req=True, resp=True)]
+ with mock.patch('time.time', mock.Mock(side_effect=[1, 2, 2] * 4)):
+ static_viewer.save_flows_content(tmpdir, flows)
+ flows_path = tmpdir.join('flows')
+ assert len(flows_path.listdir()) == len(flows)
+ for p in flows_path.listdir():
+ assert p.join('request').check(dir=1)
+ assert p.join('response').check(dir=1)
+ assert p.join('request/_content').check(file=1)
+ assert p.join('request/content').check(dir=1)
+ assert p.join('response/_content').check(file=1)
+ assert p.join('response/content').check(dir=1)
+ assert p.join('request/content/Auto.json').check(file=1)
+ assert p.join('response/content/Auto.json').check(file=1)
+
+
+def test_static_viewer(tmpdir):
+ s = static_viewer.StaticViewer()
+ sa = save.Save()
+ with taddons.context() as tctx:
+ sa.save([tflow.tflow(resp=True)], str(tmpdir.join('foo')))
+ tctx.master.addons.add(s)
+ tctx.configure(s, web_static_viewer=str(tmpdir), rfile=str(tmpdir.join('foo')))
+ assert tmpdir.join('index.html').check(file=1)
+ assert tmpdir.join('static').check(dir=1)
+ assert tmpdir.join('flows').check(dir=1)
diff --git a/test/mitmproxy/tools/web/test_app.py b/test/mitmproxy/tools/web/test_app.py
index 091ef5e8..aaf949a8 100644
--- a/test/mitmproxy/tools/web/test_app.py
+++ b/test/mitmproxy/tools/web/test_app.py
@@ -186,7 +186,7 @@ class TestApp(tornado.testing.AsyncHTTPTestCase):
f.response.headers["Content-Encoding"] = "ran\x00dom"
f.response.headers["Content-Disposition"] = 'inline; filename="filename.jpg"'
- r = self.fetch("/flows/42/response/content")
+ r = self.fetch("/flows/42/response/_content")
assert r.body == b"message"
assert r.headers["Content-Encoding"] == "random"
assert r.headers["Content-Disposition"] == 'attachment; filename="filename.jpg"'
@@ -194,17 +194,17 @@ class TestApp(tornado.testing.AsyncHTTPTestCase):
del f.response.headers["Content-Disposition"]
f.request.path = "/foo/bar.jpg"
assert self.fetch(
- "/flows/42/response/content"
+ "/flows/42/response/_content"
).headers["Content-Disposition"] == 'attachment; filename=bar.jpg'
f.response.content = b""
- assert self.fetch("/flows/42/response/content").code == 400
+ assert self.fetch("/flows/42/response/_content").code == 400
f.revert()
def test_update_flow_content(self):
assert self.fetch(
- "/flows/42/request/content",
+ "/flows/42/request/_content",
method="POST",
body="new"
).code == 200
@@ -222,7 +222,7 @@ class TestApp(tornado.testing.AsyncHTTPTestCase):
b'--somefancyboundary--\r\n'
)
assert self.fetch(
- "/flows/42/request/content",
+ "/flows/42/request/_content",
method="POST",
headers={"Content-Type": 'multipart/form-data; boundary="somefancyboundary"'},
body=body