aboutsummaryrefslogtreecommitdiffstats
path: root/test/mitmproxy/addons/test_dumper.py
blob: e49f91bc159fa6b18de58968d0788c36cfbcb870 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import io

from .. import tutils, mastertest

from mitmproxy.addons import dumper
from mitmproxy import exceptions
from mitmproxy.tools import dump
from mitmproxy import http
from mitmproxy import proxy
import netlib.tutils
import mock


class TestDumper(mastertest.MasterTest):
    def test_simple(self):
        d = dumper.Dumper()
        sio = io.StringIO()

        updated = {"tfile", "flow_detail"}
        d.configure(dump.Options(tfile = sio, flow_detail = 0), updated)
        d.response(tutils.tflow())
        assert not sio.getvalue()

        d.configure(dump.Options(tfile = sio, flow_detail = 4), updated)
        d.response(tutils.tflow())
        assert sio.getvalue()

        sio = io.StringIO()
        d.configure(dump.Options(tfile = sio, flow_detail = 4), updated)
        d.response(tutils.tflow(resp=True))
        assert "<<" in sio.getvalue()

        sio = io.StringIO()
        d.configure(dump.Options(tfile = sio, flow_detail = 4), updated)
        d.response(tutils.tflow(err=True))
        assert "<<" in sio.getvalue()

        sio = io.StringIO()
        d.configure(dump.Options(tfile = sio, flow_detail = 4), updated)
        flow = tutils.tflow()
        flow.request = netlib.tutils.treq()
        flow.request.stickycookie = True
        flow.client_conn = mock.MagicMock()
        flow.client_conn.address.host = "foo"
        flow.response = netlib.tutils.tresp(content=None)
        flow.response.is_replay = True
        flow.response.status_code = 300
        d.response(flow)
        assert sio.getvalue()

        sio = io.StringIO()
        d.configure(dump.Options(tfile = sio, flow_detail = 4), updated)
        flow = tutils.tflow(resp=netlib.tutils.tresp(content=b"{"))
        flow.response.headers["content-type"] = "application/json"
        flow.response.status_code = 400
        d.response(flow)
        assert sio.getvalue()

        sio = io.StringIO()
        d.configure(dump.Options(tfile = sio), updated)
        flow = tutils.tflow()
        flow.request.content = None
        flow.response = http.HTTPResponse.wrap(netlib.tutils.tresp())
        flow.response.content = None
        d.response(flow)
        assert "content missing" in sio.getvalue()


class TestContentView(mastertest.MasterTest):
    @mock.patch("mitmproxy.contentviews.ViewAuto.__call__")
    def test_contentview(self, view_auto):
        view_auto.side_effect = exceptions.ContentViewException("")

        sio = io.StringIO()
        o = dump.Options(
            flow_detail=4,
            verbosity=3,
            tfile=sio,
        )
        m = mastertest.RecordingMaster(o, proxy.DummyServer())
        d = dumper.Dumper()
        m.addons.add(d)
        m.response(tutils.tflow())
        assert "Content viewer failed" in m.event_log[0][1]