aboutsummaryrefslogtreecommitdiffstats
path: root/test/test_dump.py
blob: 0d0a621999f112951be80b40804bf91f01dc1ee8 (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
import os
from cStringIO import StringIO
import libpry
from libmproxy import dump, flow
import utils


class uDumpMaster(libpry.AutoTree):
    def _dummy_cycle(self, m):
        req = utils.treq()
        cc = req.client_conn
        resp = utils.tresp(req)
        m.handle_clientconnection(cc)
        m.handle_request(req)
        m.handle_response(resp)

    def test_options(self):
        o = dump.Options(verbosity = 2)
        assert o.verbosity == 2
        libpry.raises(AttributeError, dump.Options, nonexistent = 2)

    def test_filter(self):
            cs = StringIO()
            o = dump.Options(
                verbosity = 1
            )
            m = dump.DumpMaster(None, o, "~u foo", outfile=cs)
            self._dummy_cycle(m)
            assert not "GET" in cs.getvalue()

    def test_basic(self):
        for i in (1, 2, 3):
            cs = StringIO()
            o = dump.Options(
                verbosity = i
            )
            m = dump.DumpMaster(None, o, "~s", outfile=cs)
            self._dummy_cycle(m)
            assert "GET" in cs.getvalue()

    def test_write(self):
        d = self.tmpdir()
        p = os.path.join(d, "a")
        o = dump.Options(
            wfile = p,
            verbosity = 0
        )
        cs = StringIO()
        m = dump.DumpMaster(None, o, None, outfile=cs)
        self._dummy_cycle(m)
        del m
        assert len(list(flow.FlowReader(open(p)).stream())) == 1

    def test_write_err(self):
        o = dump.Options(
            wfile = "nonexistentdir/foo",
            verbosity = 0
        )
        cs = StringIO()
        libpry.raises(dump.DumpError, dump.DumpMaster, None, o, None)






tests = [
    uDumpMaster()
]