Esempio n. 1
0
    def test_replay(self):
        o = dump.Options(server_replay=["nonexistent"], replay_kill_extra=True)
        tutils.raises(exceptions.OptionsError, dump.DumpMaster, o,
                      proxy.DummyServer())

        with tutils.tmpdir() as t:
            p = os.path.join(t, "rep")
            self.flowfile(p)

            o = dump.Options(server_replay=[p], replay_kill_extra=True)
            o.verbosity = 0
            o.flow_detail = 0
            m = dump.DumpMaster(o, proxy.DummyServer())

            self.cycle(m, b"content")
            self.cycle(m, b"content")

            o = dump.Options(server_replay=[p], replay_kill_extra=False)
            o.verbosity = 0
            o.flow_detail = 0
            m = dump.DumpMaster(o, proxy.DummyServer())
            self.cycle(m, b"nonexistent")

            o = dump.Options(client_replay=[p], replay_kill_extra=False)
            o.verbosity = 0
            o.flow_detail = 0
            m = dump.DumpMaster(o, proxy.DummyServer())
Esempio n. 2
0
def tscript(cmd, args=""):
    o = options.Options()
    cmd = example_dir.path(cmd) + " " + args
    m = RaiseMaster(o, proxy.DummyServer())
    sc = script.Script(cmd)
    m.addons.add(sc)
    return m, sc
Esempio n. 3
0
 def mkmaster(self, **opts):
     if "verbosity" not in opts:
         opts["verbosity"] = 1
     o = options.Options(**opts)
     m = console.master.ConsoleMaster(o, proxy.DummyServer())
     m.addons.configure_all(o, o.keys())
     return m
Esempio n. 4
0
    def test_stream(self):
        with tutils.tmpdir() as tdir:
            p = os.path.join(tdir, "foo")

            def r():
                r = io.FlowReader(open(p, "rb"))
                return list(r.stream())

            o = options.Options(outfile=(p, "wb"))
            m = master.Master(o, proxy.DummyServer())
            sa = filestreamer.FileStreamer()

            m.addons.add(sa)
            f = tutils.tflow(resp=True)
            m.request(f)
            m.response(f)
            m.addons.remove(sa)

            assert r()[0].response

            m.options.outfile = (p, "ab")

            m.addons.add(sa)
            f = tutils.tflow()
            m.request(f)
            m.addons.remove(sa)
            assert not r()[1].response
def test_nesting():
    o = options.Options()
    m = master.Master(o, proxy.DummyServer(o))
    a = addonmanager.AddonManager(m)

    a.add(
        TAddon(
            "one",
            addons=[
                TAddon("two"),
                TAddon("three", addons=[TAddon("four")])
            ]
        )
    )
    assert len(a.chain) == 1
    assert a.get("one")
    assert a.get("two")
    assert a.get("three")
    assert a.get("four")

    a.trigger("custom")
    assert a.get("one").custom_called
    assert a.get("two").custom_called
    assert a.get("three").custom_called
    assert a.get("four").custom_called

    a.remove(a.get("three"))
    assert not a.get("three")
    assert not a.get("four")
Esempio n. 6
0
 def __init__(self, master = None, options = None):
     options = options or mitmproxy.options.Options()
     self.master = master or RecordingMaster(
         options, proxy.DummyServer(options)
     )
     self.options = self.master.options
     self.wrapped = None
Esempio n. 7
0
 def mkmaster(self, flt, **options):
     if "verbosity" not in options:
         options["verbosity"] = 0
     if "flow_detail" not in options:
         options["flow_detail"] = 0
     o = dump.Options(filtstr=flt, tfile=io.StringIO(), **options)
     return dump.DumpMaster(o, proxy.DummyServer())
Esempio n. 8
0
 def mkmaster(self, flt, **options):
     o = dump.Options(filtstr=flt, verbosity=-1, flow_detail=0, **options)
     m = dump.DumpMaster(o,
                         proxy.DummyServer(),
                         with_termlog=False,
                         with_dumper=False)
     return m
Esempio n. 9
0
    def test_server_playback_full(self):
        s = serverplayback.ServerPlayback()
        o = options.Options(refresh_server_playback=True, keepserving=False)
        m = mastertest.RecordingMaster(o, proxy.DummyServer())
        m.addons.add(s)

        f = tutils.tflow()
        f.response = mitmproxy.test.tutils.tresp(content=f.request.content)
        s.load([f, f])

        tf = tutils.tflow()
        assert not tf.response
        m.request(tf)
        assert tf.response == f.response

        tf = tutils.tflow()
        tf.request.content = b"gibble"
        assert not tf.response
        m.request(tf)
        assert not tf.response

        assert not s.stop
        s.tick()
        assert not s.stop

        tf = tutils.tflow()
        m.request(tutils.tflow())
        assert s.stop
Esempio n. 10
0
def test_loadchain():
    o = options.Options()
    m = master.Master(o, proxy.DummyServer(o))
    a = addonmanager.AddonManager(m)

    a.add(AChain("one", None))
    assert a.get("one")
    a.clear()

    a.add(AChain("one", AChain("two", None)))
    assert not a.get("one")
    assert a.get("two")
    a.clear()

    a.add(AChain("one", AChain("two", AChain("three", None))))
    assert not a.get("one")
    assert not a.get("two")
    assert a.get("three")
    a.clear()

    a.add(AChain("one", AChain("two", AChain("three", AChain("four", None)))))
    assert not a.get("one")
    assert not a.get("two")
    assert not a.get("three")
    assert a.get("four")
    a.clear()
Esempio n. 11
0
 def test_server_simple(self):
     m = master.Master(None)
     m.server = proxy.DummyServer()
     m.start()
     m.shutdown()
     m.start()
     m.shutdown()
Esempio n. 12
0
def test_simple():
    o = options.Options()
    m = master.Master(o, proxy.DummyServer(o))
    a = addonmanager.AddonManager(m)
    with pytest.raises(exceptions.AddonError):
        a.invoke_addon(TAddon("one"), "done")

    assert len(a) == 0
    a.add(TAddon("one"))
    assert a.get("one")
    assert not a.get("two")
    assert len(a) == 1
    a.clear()
    assert len(a) == 0
    assert not a.chain

    a.add(TAddon("one"))
    a.trigger("done")
    with pytest.raises(exceptions.AddonError):
        a.trigger("tick")

    a.remove(a.get("one"))
    assert not a.get("one")

    ta = TAddon("one")
    a.add(ta)
    a.trigger("custom")
    assert ta.custom_called
Esempio n. 13
0
 def test_setheader(self):
     o = dump.Options(setheaders=[(".*", "one", "two")],
                      tfile=io.StringIO())
     o.verbosity = 0
     o.flow_detail = 0
     m = dump.DumpMaster(o, proxy.DummyServer())
     f = self.cycle(m, b"content")
     assert f.request.headers["one"] == "two"
Esempio n. 14
0
 def test_concurrent_err(self):
     m = mastertest.RecordingMaster(options.Options(), proxy.DummyServer())
     sc = script.Script(
         tutils.test_data.path(
             "data/addonscripts/concurrent_decorator_err.py"))
     with m.handlecontext():
         sc.start()
     assert "decorator not supported" in m.event_log[0][1]
Esempio n. 15
0
 def test_addon(self):
     o = options.Options()
     m = master.Master(o, proxy.DummyServer())
     sc = script.Script(tutils.test_data.path("data/addonscripts/addon.py"))
     m.addons.add(sc)
     assert sc.ns.event_log == [
         'scriptstart', 'addonstart', 'addonconfigure'
     ]
Esempio n. 16
0
async def test_configure():
    ts = termstatus.TermStatus()
    with taddons.context() as ctx:
        ctx.master.server = proxy.DummyServer()
        ctx.configure(ts, server=False)
        ts.running()
        ctx.configure(ts, server=True)
        ts.running()
        await ctx.master.await_log("server listening")
Esempio n. 17
0
def test_simple():
    o = options.Options()
    m = master.Master(o, proxy.DummyServer(o))
    a = addonmanager.AddonManager(m)
    a.add(TAddon("one"))
    assert a.get("one")
    assert not a.get("two")
    a.clear()
    assert not a.chain
Esempio n. 18
0
def test_lifecycle():
    o = options.Options()
    m = master.Master(o, proxy.DummyServer(o))
    a = addonmanager.AddonManager(m)
    a.add(TAddon("one"))

    f = tflow.tflow()
    a.handle_lifecycle("request", f)

    a.configure_all(o, o.keys())
Esempio n. 19
0
 def test_replacements(self):
     o = dump.Options(
         replacements=[(".*", "content", "foo")],
         tfile=io.StringIO(),
     )
     o.verbosity = 0
     o.flow_detail = 0
     m = dump.DumpMaster(o, proxy.DummyServer())
     f = self.cycle(m, b"content")
     assert f.request.content == b"foo"
Esempio n. 20
0
def test_configure():
    ts = termstatus.TermStatus()
    with taddons.context() as ctx:
        ctx.master.server = proxy.DummyServer()
        ctx.configure(ts, server=False)
        ts.running()
        assert not ctx.master.logs
        ctx.configure(ts, server=True)
        ts.running()
        assert ctx.master.logs
Esempio n. 21
0
 def test_simple(self):
     o = options.Options(scripts=[])
     m = master.Master(o, proxy.DummyServer())
     sc = script.ScriptLoader()
     m.addons.add(sc)
     assert len(m.addons) == 1
     o.update(
         scripts=[tutils.test_data.path("data/addonscripts/recorder.py")])
     assert len(m.addons) == 2
     o.update(scripts=[])
     assert len(m.addons) == 1
Esempio n. 22
0
    def test_duplicate_flow(self):
        s = state.State()
        fm = master.Master(None, proxy.DummyServer())
        fm.addons.add(s)
        f = tflow.tflow(resp=True)
        fm.load_flow(f)
        assert s.flow_count() == 1

        f2 = s.duplicate_flow(f)
        assert f2.response
        assert s.flow_count() == 2
        assert s.index(f2) == 1
Esempio n. 23
0
    def test_call(self):
        o = options.Options()
        m = master.Master(o, proxy.DummyServer(o))
        cm = command.CommandManager(m)

        a = TAddon()
        c = command.Command(cm, "cmd.path", a.cmd1)
        assert c.call(["foo"]) == "ret foo"
        assert c.signature_help() == "cmd.path str -> str"

        c = command.Command(cm, "cmd.two", a.cmd2)
        with pytest.raises(exceptions.CommandError):
            c.call(["foo"])
Esempio n. 24
0
 def test_concurrent(self):
     m = master.Master(options.Options(), proxy.DummyServer())
     sc = script.Script(
         tutils.test_data.path("data/addonscripts/concurrent_decorator.py"))
     m.addons.add(sc)
     f1, f2 = tutils.tflow(), tutils.tflow()
     m.request(f1)
     m.request(f2)
     start = time.time()
     while time.time() - start < 5:
         if f1.reply.state == f2.reply.state == "committed":
             return
     raise ValueError("Script never acked")
Esempio n. 25
0
 def test_exception(self):
     o = options.Options()
     m = mastertest.RecordingMaster(o, proxy.DummyServer())
     sc = script.Script(tutils.test_data.path("data/addonscripts/error.py"))
     m.addons.add(sc)
     f = tflow.tflow(resp=True)
     m.request(f)
     assert m.event_log[0][0] == "error"
     assert len(m.event_log[0][1].splitlines()) == 6
     assert re.search('addonscripts/error.py", line \d+, in request',
                      m.event_log[0][1])
     assert re.search('addonscripts/error.py", line \d+, in mkerr',
                      m.event_log[0][1])
     assert m.event_log[0][1].endswith("ValueError: Error!\n")
Esempio n. 26
0
 def get_app(self):
     o = options.Options()
     m = webmaster.WebMaster(o, proxy.DummyServer())
     f = tflow.tflow(resp=True)
     f.id = "42"
     m.view.add(f)
     m.view.add(tflow.tflow(err=True))
     m.add_log("test log", "info")
     self.master = m
     self.view = m.view
     self.events = m.events
     webapp = app.Application(m, None)
     webapp.settings["xsrf_cookies"] = False
     return webapp
Esempio n. 27
0
def test_simple():
    o = options.Options()
    m = master.Master(o, proxy.DummyServer(o))
    a = addonmanager.AddonManager(m)
    a.add(TAddon("one"))
    assert a.get("one")
    assert not a.get("two")
    a.clear()
    assert not a.chain

    a.add(TAddon("one"))
    a("noop")
    with pytest.raises(exceptions.AddonError):
        a("noop_member")
Esempio n. 28
0
    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(tflow.tflow())
        assert "Content viewer failed" in m.event_log[0][1]
Esempio n. 29
0
    def test_simple(self):
        o = options.Options(anticomp = True)
        m = master.Master(o, proxy.DummyServer())
        sa = anticomp.AntiComp()
        m.addons.add(sa)

        f = tutils.tflow(resp=True)
        m.request(f)

        f = tutils.tflow(resp=True)

        f.request.headers["Accept-Encoding"] = "foobar"
        m.request(f)
        assert f.request.headers["Accept-Encoding"] == "identity"
Esempio n. 30
0
    def test_simple(self):
        o = options.Options(anticache = True)
        m = master.Master(o, proxy.DummyServer())
        sa = anticache.AntiCache()
        m.addons.add(sa)

        f = tflow.tflow(resp=True)
        m.request(f)

        f = tflow.tflow(resp=True)
        f.request.headers["if-modified-since"] = "test"
        f.request.headers["if-none-match"] = "test"
        m.request(f)
        assert "if-modified-since" not in f.request.headers
        assert "if-none-match" not in f.request.headers