How to use mitmproxy - 10 common examples

To help you get started, we’ve selected a few mitmproxy examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mitmproxy / mitmproxy / test / mitmproxy / net / http / test_request.py View on Github external
def test_init(self):
        with pytest.raises(UnicodeEncodeError):
            treq(method="fööbär")
        with pytest.raises(UnicodeEncodeError):
            treq(scheme="fööbär")
        assert treq(host="fööbär").host == "fööbär"
        with pytest.raises(UnicodeEncodeError):
            treq(path="/fööbär")
        with pytest.raises(UnicodeEncodeError):
            treq(http_version="föö/bä.r")
        with pytest.raises(ValueError):
            treq(headers="foobar")
        with pytest.raises(ValueError):
            treq(content="foobar")

        assert isinstance(treq(headers=()).headers, Headers)
github mitmproxy / mitmproxy / test / mitmproxy / net / test_tcp.py View on Github external
def test_reader_incomplete_error(self):
        s = BytesIO(b"foobar")
        s = tcp.Reader(s)
        with pytest.raises(exceptions.TcpReadIncomplete):
            s.safe_read(10)
github mitmproxy / mitmproxy / test / mitmproxy / addons / test_serverplayback.py View on Github external
def test_load_file(tmpdir):
    s = serverplayback.ServerPlayback()
    with taddons.context(s):
        fpath = str(tmpdir.join("flows"))
        tdump(fpath, [tflow.tflow(resp=True)])
        s.load_file(fpath)
        assert s.flowmap
        with pytest.raises(exceptions.CommandError):
            s.load_file("/nonexistent")
github mitmproxy / mitmproxy / test / mitmproxy / addons / test_upstream_auth.py View on Github external
def test_configure():
    up = upstream_auth.UpstreamAuth()
    with taddons.context() as tctx:
        tctx.configure(up, upstream_auth="test:test")
        assert up.auth == b"Basic" + b" " + base64.b64encode(b"test:test")

        tctx.configure(up, upstream_auth="test:")
        assert up.auth == b"Basic" + b" " + base64.b64encode(b"test:")

        tctx.configure(up, upstream_auth=None)
        assert not up.auth

        with pytest.raises(exceptions.OptionsError):
            tctx.configure(up, upstream_auth="")
        with pytest.raises(exceptions.OptionsError):
            tctx.configure(up, upstream_auth=":")
        with pytest.raises(exceptions.OptionsError):
            tctx.configure(up, upstream_auth=":test")
github mitmproxy / mitmproxy / test / mitmproxy / addons / test_serverplayback.py View on Github external
with taddons.context(s) as tctx:
        tctx.configure(
            s,
            server_replay_refresh=True,
        )

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

        tf = tflow.tflow()
        assert not tf.response
        s.request(tf)
        assert tf.response == f.response

        tf = tflow.tflow()
        tf.request.content = b"gibble"
        assert not tf.response
        s.request(tf)
        assert not tf.response
github mitmproxy / mitmproxy / test / mitmproxy / addons / test_save.py View on Github external
def test_save_command(tmpdir):
    sa = save.Save()
    with taddons.context() as tctx:
        p = str(tmpdir.join("foo"))
        sa.save([tflow.tflow(resp=True)], p)
        assert len(rd(p)) == 1
        sa.save([tflow.tflow(resp=True)], p)
        assert len(rd(p)) == 1
        sa.save([tflow.tflow(resp=True)], "+" + p)
        assert len(rd(p)) == 2

        with pytest.raises(exceptions.CommandError):
            sa.save([tflow.tflow(resp=True)], str(tmpdir))

        v = view.View()
        tctx.master.addons.add(v)
        tctx.master.addons.add(sa)
        tctx.master.commands.execute("save.file @shown %s" % p)
github mitmproxy / mitmproxy / test / examples / test_har_dump.py View on Github external
def flow(self, resp_content=b'message'):
        times = dict(
            timestamp_start=746203272,
            timestamp_end=746203272,
        )

        # Create a dummy flow for testing
        return tflow.tflow(
            req=tutils.treq(method=b'GET', **times),
            resp=tutils.tresp(content=resp_content, **times)
        )
github mitmproxy / mitmproxy / test / mitmproxy / addons / test_streambodies.py View on Github external
f = tflow.tflow()
        f.request.content = b""
        f.request.headers["Content-Length"] = "1024"
        assert not f.request.stream
        sa.requestheaders(f)
        assert f.request.stream

        f = tflow.tflow(resp=True)
        f.response.content = b""
        f.response.headers["Content-Length"] = "1024"
        assert not f.response.stream
        sa.responseheaders(f)
        assert f.response.stream

        f = tflow.tflow(resp=True)
        f.response.headers["content-length"] = "invalid"
        tctx.cycle(sa, f)

        tctx.configure(sa, stream_websockets = True)
        f = tflow.twebsocketflow()
        assert not f.stream
        sa.websocket_start(f)
        assert f.stream
github mitmproxy / mitmproxy / test / mitmproxy / addons / test_readstdin.py View on Github external
def gen_data(corrupt=False):
    tf = io.BytesIO()
    w = mitmproxy.io.FlowWriter(tf)
    for i in range(3):
        f = tflow.tflow(resp=True)
        w.add(f)
    for i in range(3):
        f = tflow.tflow(err=True)
        w.add(f)
    f = tflow.ttcpflow()
    w.add(f)
    f = tflow.ttcpflow(err=True)
    w.add(f)
    if corrupt:
        tf.write(b"flibble")
    tf.seek(0)
    return tf
github mitmproxy / mitmproxy / test / mitmproxy / tools / web / test_app.py View on Github external
def get_app(self):
        o = options.Options(http2=False)
        m = webmaster.WebMaster(o, with_termlog=False)
        f = tflow.tflow(resp=True)
        f.id = "42"
        m.view.add([f])
        m.view.add([tflow.tflow(err=True)])
        m.log.info("test log")
        self.master = m
        self.view = m.view
        self.events = m.events
        webapp = app.Application(m, None)
        webapp.settings["xsrf_cookies"] = False
        return webapp