aboutsummaryrefslogtreecommitdiffstats
path: root/test/mitmproxy/tools/web/test_static_viewer.py
blob: 138c3add17d1f6db68e10577f5b8086c9f238dc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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.tools.web 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)