示例#1
0
def test_cut():
    c = cut.Cut()
    with taddons.context():
        tflows = [tflow.tflow(resp=True)]
        assert c.cut(tflows, ["request.method"]) == [["GET"]]
        assert c.cut(tflows, ["request.scheme"]) == [["http"]]
        assert c.cut(tflows, ["request.host"]) == [["address"]]
        assert c.cut(tflows, ["request.port"]) == [["22"]]
        assert c.cut(tflows, ["request.path"]) == [["/path"]]
        assert c.cut(tflows, ["request.url"]) == [["http://address:22/path"]]
        assert c.cut(tflows, ["request.content"]) == [[b"content"]]
        assert c.cut(tflows, ["request.header[header]"]) == [["qvalue"]]
        assert c.cut(tflows, ["request.header[unknown]"]) == [[""]]

        assert c.cut(tflows, ["response.status_code"]) == [["200"]]
        assert c.cut(tflows, ["response.reason"]) == [["OK"]]
        assert c.cut(tflows, ["response.content"]) == [[b"message"]]
        assert c.cut(tflows, ["response.header[header-response]"]) == [["svalue"]]
        assert c.cut(tflows, ["moo"]) == [[""]]
        with pytest.raises(exceptions.CommandError):
            assert c.cut(tflows, ["__dict__"]) == [[""]]

    with taddons.context():
        tflows = [tflow.tflow(resp=False)]
        assert c.cut(tflows, ["response.reason"]) == [[""]]
        assert c.cut(tflows, ["response.header[key]"]) == [[""]]

    c = cut.Cut()
    with taddons.context():
        tflows = [tflow.ttcpflow()]
        assert c.cut(tflows, ["request.method"]) == [[""]]
        assert c.cut(tflows, ["response.status"]) == [[""]]
示例#2
0
def test_simple():
    v = view.View()
    f = tflow.tflow()
    f.request.timestamp_start = 1
    v.request(f)
    assert list(v) == [f]
    v.request(f)
    assert list(v) == [f]
    assert len(v._store) == 1

    f2 = tflow.tflow()
    f2.request.timestamp_start = 3
    v.request(f2)
    assert list(v) == [f, f2]
    v.request(f2)
    assert list(v) == [f, f2]
    assert len(v._store) == 2

    f3 = tflow.tflow()
    f3.request.timestamp_start = 2
    v.request(f3)
    assert list(v) == [f, f3, f2]
    v.request(f3)
    assert list(v) == [f, f3, f2]
    assert len(v._store) == 3

    v.clear()
    assert len(v) == 0
    assert len(v._store) == 0
示例#3
0
def test_authenticate():
    up = proxyauth.ProxyAuth()
    with taddons.context() as ctx:
        ctx.configure(up, auth_nonanonymous=True)

        f = tflow.tflow()
        assert not f.response
        up.authenticate(f)
        assert f.response.status_code == 407

        f = tflow.tflow()
        f.request.headers["Proxy-Authorization"] = proxyauth.mkauth(
            "test", "test"
        )
        up.authenticate(f)
        assert not f.response
        assert not f.request.headers.get("Proxy-Authorization")

        f = tflow.tflow()
        f.mode = "transparent"
        assert not f.response
        up.authenticate(f)
        assert f.response.status_code == 401

        f = tflow.tflow()
        f.mode = "transparent"
        f.request.headers["Authorization"] = proxyauth.mkauth(
            "test", "test"
        )
        up.authenticate(f)
        assert not f.response
        assert not f.request.headers.get("Authorization")
示例#4
0
    def test_copy(self):
        f = tflow.tflow(resp=True)
        f.get_state()
        f2 = f.copy()
        a = f.get_state()
        b = f2.get_state()
        del a["id"]
        del b["id"]
        assert a == b
        assert not f == f2
        assert f is not f2
        assert f.request.get_state() == f2.request.get_state()
        assert f.request is not f2.request
        assert f.request.headers == f2.request.headers
        assert f.request.headers is not f2.request.headers
        assert f.response.get_state() == f2.response.get_state()
        assert f.response is not f2.response

        f = tflow.tflow(err=True)
        f2 = f.copy()
        assert f is not f2
        assert f.request is not f2.request
        assert f.request.headers == f2.request.headers
        assert f.request.headers is not f2.request.headers
        assert f.error.get_state() == f2.error.get_state()
        assert f.error is not f2.error
示例#5
0
def test_simple():
    r = intercept.Intercept()
    with taddons.context(options=Options()) as tctx:
        assert not r.filt
        tctx.configure(r, intercept="~q")
        assert r.filt
        with pytest.raises(exceptions.OptionsError):
            tctx.configure(r, intercept="~~")
        tctx.configure(r, intercept=None)
        assert not r.filt

        tctx.configure(r, intercept="~s")

        f = tflow.tflow(resp=True)
        tctx.cycle(r, f)
        assert f.intercepted

        f = tflow.tflow(resp=False)
        tctx.cycle(r, f)
        assert not f.intercepted

        f = tflow.tflow(resp=True)
        f.reply._state = "handled"
        r.response(f)
        assert f.intercepted
示例#6
0
def test_handlers():
    up = proxyauth.ProxyAuth()
    with taddons.context() as ctx:
        ctx.configure(up, auth_nonanonymous=True, mode="regular")

        f = tflow.tflow()
        assert not f.response
        up.requestheaders(f)
        assert f.response.status_code == 407

        f = tflow.tflow()
        f.request.method = "CONNECT"
        assert not f.response
        up.http_connect(f)
        assert f.response.status_code == 407

        f = tflow.tflow()
        f.request.method = "CONNECT"
        f.request.headers["Proxy-Authorization"] = proxyauth.mkauth(
            "test", "test"
        )
        up.http_connect(f)
        assert not f.response

        f2 = tflow.tflow(client_conn=f.client_conn)
        up.requestheaders(f2)
        assert not f2.response
    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 = tflow.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 = tflow.tflow()
            m.request(f)
            m.addons.remove(sa)
            assert not r()[1].response
示例#8
0
def test_load():
    s = serverplayback.ServerPlayback()
    with taddons.context(s) as tctx:
        tctx.configure(s)

        r = tflow.tflow(resp=True)
        r.request.headers["key"] = "one"

        r2 = tflow.tflow(resp=True)
        r2.request.headers["key"] = "two"

        s.load_flows([r, r2])

        assert s.count() == 2

        n = s.next_flow(r)
        assert n.request.headers["key"] == "one"
        assert s.count() == 1

        n = s.next_flow(r)
        assert n.request.headers["key"] == "two"
        assert not s.flowmap
        assert s.count() == 0

        assert not s.next_flow(r)
def test_simple():
    sa = streambodies.StreamBodies()
    with taddons.context() as tctx:
        with pytest.raises(exceptions.OptionsError):
            tctx.configure(sa, stream_large_bodies = "invalid")
        tctx.configure(sa, stream_large_bodies = "10")

        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
示例#10
0
def test_ignore_content():
    s = serverplayback.ServerPlayback()
    with taddons.context(s) as tctx:
        tctx.configure(s, server_replay_ignore_content=False)

        r = tflow.tflow(resp=True)
        r2 = tflow.tflow(resp=True)

        r.request.content = b"foo"
        r2.request.content = b"foo"
        assert s._hash(r) == s._hash(r2)
        r2.request.content = b"bar"
        assert not s._hash(r) == s._hash(r2)

        tctx.configure(s, server_replay_ignore_content=True)
        r = tflow.tflow(resp=True)
        r2 = tflow.tflow(resp=True)
        r.request.content = b"foo"
        r2.request.content = b"foo"
        assert s._hash(r) == s._hash(r2)
        r2.request.content = b"bar"
        assert s._hash(r) == s._hash(r2)
        r2.request.content = b""
        assert s._hash(r) == s._hash(r2)
        r2.request.content = None
        assert s._hash(r) == s._hash(r2)
示例#11
0
def test_ignore_payload_params():
    def urlencode_setter(r, **kwargs):
        r.request.content = urllib.parse.urlencode(kwargs).encode()

    r = tflow.tflow(resp=True)
    r.request.headers["Content-Type"] = "application/x-www-form-urlencoded"
    r2 = tflow.tflow(resp=True)
    r2.request.headers["Content-Type"] = "application/x-www-form-urlencoded"
    thash(r, r2, urlencode_setter)

    boundary = 'somefancyboundary'

    def multipart_setter(r, **kwargs):
        b = "--{0}\n".format(boundary)
        parts = []
        for k, v in kwargs.items():
            parts.append(
                "Content-Disposition: form-data; name=\"%s\"\n\n"
                "%s\n" % (k, v)
            )
        c = b + b.join(parts) + b
        r.request.content = c.encode()
        r.request.headers["content-type"] = 'multipart/form-data; boundary=' +\
            boundary

    r = tflow.tflow(resp=True)
    r2 = tflow.tflow(resp=True)
    thash(r, r2, multipart_setter)
示例#12
0
    def test_replay(self):
        opts = options.Options()
        fm = master.Master(opts)
        f = tflow.tflow(resp=True)
        f.request.content = None
        with pytest.raises(ReplayException, match="missing"):
            fm.replay_request(f)

        f.request = None
        with pytest.raises(ReplayException, match="request"):
            fm.replay_request(f)

        f.intercepted = True
        with pytest.raises(ReplayException, match="intercepted"):
            fm.replay_request(f)

        f.live = True
        with pytest.raises(ReplayException, match="live"):
            fm.replay_request(f)

        req = tutils.treq(headers=net_http.Headers(((b":authority", b"foo"), (b"header", b"qvalue"), (b"content-length", b"7"))))
        f = tflow.tflow(req=req)
        f.request.http_version = "HTTP/2.0"
        with mock.patch('mitmproxy.proxy.protocol.http_replay.RequestReplayThread.run'):
            rt = fm.replay_request(f)
            assert rt.f.request.http_version == "HTTP/1.1"
            assert ":authority" not in rt.f.request.headers
示例#13
0
    def test_simple(self):
        r = replace.ReplaceFile()
        with tutils.tmpdir() as td:
            rp = os.path.join(td, "replacement")
            with open(rp, "w") as f:
                f.write("bar")
            with taddons.context() as tctx:
                tctx.configure(
                    r,
                    replacement_files = [
                        ("~q", "foo", rp),
                        ("~s", "foo", rp),
                        ("~b nonexistent", "nonexistent", "nonexistent"),
                    ]
                )
                f = tflow.tflow()
                f.request.content = b"foo"
                r.request(f)
                assert f.request.content == b"bar"

                f = tflow.tflow(resp=True)
                f.response.content = b"foo"
                r.response(f)
                assert f.response.content == b"bar"

                f = tflow.tflow()
                f.request.content = b"nonexistent"
                assert not tctx.master.event_log
                r.request(f)
                assert tctx.master.event_log
示例#14
0
def test_movement():
    v = view.View()
    with taddons.context():
        v.go(0)
        v.add([
            tflow.tflow(),
            tflow.tflow(),
            tflow.tflow(),
            tflow.tflow(),
            tflow.tflow(),
        ])
        assert v.focus.index == 0
        v.go(-1)
        assert v.focus.index == 4
        v.go(0)
        assert v.focus.index == 0
        v.go(1)
        assert v.focus.index == 1
        v.go(999)
        assert v.focus.index == 4
        v.go(-999)
        assert v.focus.index == 0

        v.focus_next()
        assert v.focus.index == 1
        v.focus_prev()
        assert v.focus.index == 0
    def test_ignore_content(self):
        s = serverplayback.ServerPlayback()
        s.configure(options.Options(server_replay_ignore_content=False), [])

        r = tflow.tflow(resp=True)
        r2 = tflow.tflow(resp=True)

        r.request.content = b"foo"
        r2.request.content = b"foo"
        assert s._hash(r) == s._hash(r2)
        r2.request.content = b"bar"
        assert not s._hash(r) == s._hash(r2)

        s.configure(options.Options(server_replay_ignore_content=True), [])
        r = tflow.tflow(resp=True)
        r2 = tflow.tflow(resp=True)
        r.request.content = b"foo"
        r2.request.content = b"foo"
        assert s._hash(r) == s._hash(r2)
        r2.request.content = b"bar"
        assert s._hash(r) == s._hash(r2)
        r2.request.content = b""
        assert s._hash(r) == s._hash(r2)
        r2.request.content = None
        assert s._hash(r) == s._hash(r2)
示例#16
0
def test_echo_request_line():
    sio = io.StringIO()
    d = dumper.Dumper(sio)
    with taddons.context(options=options.Options()) as ctx:
        ctx.configure(d, flow_detail=3, showhost=True)
        f = tflow.tflow(client_conn=None, server_conn=True, resp=True)
        f.request.is_replay = True
        d._echo_request_line(f)
        assert "[replay]" in sio.getvalue()
        sio.truncate(0)

        f = tflow.tflow(client_conn=None, server_conn=True, resp=True)
        f.request.is_replay = False
        d._echo_request_line(f)
        assert "[replay]" not in sio.getvalue()
        sio.truncate(0)

        f = tflow.tflow(client_conn=None, server_conn=True, resp=True)
        f.request.http_version = "nonstandard"
        d._echo_request_line(f)
        assert "nonstandard" in sio.getvalue()
        sio.truncate(0)

        ctx.configure(d, flow_detail=0, showhost=True)
        f = tflow.tflow(client_conn=None, server_conn=True, resp=True)
        terminalWidth = max(shutil.get_terminal_size()[0] - 25, 50)
        f.request.url = "http://address:22/" + ("x" * terminalWidth) + "textToBeTruncated"
        d._echo_request_line(f)
        assert "textToBeTruncated" not in sio.getvalue()
        sio.truncate(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 = tflow.tflow()
        f.response = mitmproxy.test.tutils.tresp(content=f.request.content)
        s.load([f, f])

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

        tf = tflow.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 = tflow.tflow()
        m.request(tflow.tflow())
        assert s.stop
示例#18
0
    def test_handlers(self):
        up = proxyauth.ProxyAuth()
        with taddons.context(up) as ctx:
            ctx.configure(up, proxyauth="any", mode="regular")

            f = tflow.tflow()
            assert not f.response
            up.requestheaders(f)
            assert f.response.status_code == 407

            f = tflow.tflow()
            f.request.method = "CONNECT"
            assert not f.response
            up.http_connect(f)
            assert f.response.status_code == 407

            f = tflow.tflow()
            f.request.method = "CONNECT"
            f.request.headers["Proxy-Authorization"] = proxyauth.mkauth(
                "test", "test"
            )
            up.http_connect(f)
            assert not f.response

            f2 = tflow.tflow(client_conn=f.client_conn)
            up.requestheaders(f2)
            assert not f2.response
            assert f2.metadata["proxyauth"] == ('test', 'test')
    def test_ignore_payload_params(self):
        s = serverplayback.ServerPlayback()
        s.configure(
            options.Options(
                server_replay_ignore_payload_params=["param1", "param2"]
            ),
            []
        )

        r = tflow.tflow(resp=True)
        r.request.headers["Content-Type"] = "application/x-www-form-urlencoded"
        r.request.content = b"paramx=x&param1=1"
        r2 = tflow.tflow(resp=True)
        r2.request.headers["Content-Type"] = "application/x-www-form-urlencoded"
        r2.request.content = b"paramx=x&param1=1"
        # same parameters
        assert s._hash(r) == s._hash(r2)
        # ignored parameters !=
        r2.request.content = b"paramx=x&param1=2"
        assert s._hash(r) == s._hash(r2)
        # missing parameter
        r2.request.content = b"paramx=x"
        assert s._hash(r) == s._hash(r2)
        # ignorable parameter added
        r2.request.content = b"paramx=x&param1=2"
        assert s._hash(r) == s._hash(r2)
        # not ignorable parameter changed
        r2.request.content = b"paramx=y&param1=1"
        assert not s._hash(r) == s._hash(r2)
        # not ignorable parameter missing
        r2.request.content = b"param1=1"
        assert not s._hash(r) == s._hash(r2)
示例#20
0
    def test_authenticate(self):
        up = proxyauth.ProxyAuth()
        with taddons.context(up, loadcore=False) as ctx:
            ctx.configure(up, proxyauth="any", mode="regular")

            f = tflow.tflow()
            assert not f.response
            up.authenticate(f)
            assert f.response.status_code == 407

            f = tflow.tflow()
            f.request.headers["Proxy-Authorization"] = proxyauth.mkauth(
                "test", "test"
            )
            up.authenticate(f)
            assert not f.response
            assert not f.request.headers.get("Proxy-Authorization")

            f = tflow.tflow()
            ctx.configure(up, mode="reverse")
            assert not f.response
            up.authenticate(f)
            assert f.response.status_code == 401

            f = tflow.tflow()
            f.request.headers["Authorization"] = proxyauth.mkauth(
                "test", "test"
            )
            up.authenticate(f)
            assert not f.response
            assert not f.request.headers.get("Authorization")
def test_server_playback_full():
    s = serverplayback.ServerPlayback()
    with taddons.context() as tctx:
        tctx.configure(
            s,
            refresh_server_playback = 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

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

        tf = tflow.tflow()
        s.request(tflow.tflow())
        assert s.stop
示例#22
0
def test_remove():
    v = view.View()
    with taddons.context():
        f = [tflow.tflow(), tflow.tflow()]
        v.add(f)
        assert len(v) == 2
        v.remove(f)
        assert len(v) == 0
示例#23
0
    def test_match(self):
        f = tflow.tflow(resp=True)
        assert not flowfilter.match("~b test", f)
        assert flowfilter.match(None, f)
        assert not flowfilter.match("~b test", f)

        f = tflow.tflow(err=True)
        assert flowfilter.match("~e", f)

        tutils.raises(ValueError, flowfilter.match, "~", f)
示例#24
0
    def test_match(self):
        f = tflow.tflow(resp=True)
        assert not flowfilter.match("~b test", f)
        assert flowfilter.match(None, f)
        assert not flowfilter.match("~b test", f)

        f = tflow.tflow(err=True)
        assert flowfilter.match("~e", f)

        with pytest.raises(ValueError):
            flowfilter.match("~", f)
示例#25
0
def test_simple():
    r = stickyauth.StickyAuth()
    with taddons.context():
        f = tflow.tflow(resp=True)
        f.request.headers["authorization"] = "foo"
        r.request(f)

        assert "address" in r.hosts

        f = tflow.tflow(resp=True)
        r.request(f)
        assert f.request.headers["authorization"] == "foo"
示例#26
0
 def test_intercept(self):
     """regression test for https://github.com/mitmproxy/mitmproxy/issues/1605"""
     m = self.mkmaster(intercept="~b bar")
     f = tflow.tflow(req=tutils.treq(content=b"foo"))
     m.addons.handle_lifecycle("request", f)
     assert not m.view[0].intercepted
     f = tflow.tflow(req=tutils.treq(content=b"bar"))
     m.addons.handle_lifecycle("request", f)
     assert m.view[1].intercepted
     f = tflow.tflow(resp=tutils.tresp(content=b"bar"))
     m.addons.handle_lifecycle("request", f)
     assert m.view[2].intercepted
示例#27
0
    def test_simple(self):
        sa = anticomp.AntiComp()
        with taddons.context() as tctx:
            f = tflow.tflow(resp=True)
            sa.request(f)

            tctx.configure(sa, anticomp=True)
            f = tflow.tflow(resp=True)

            f.request.headers["Accept-Encoding"] = "foobar"
            sa.request(f)
            assert f.request.headers["Accept-Encoding"] == "identity"
示例#28
0
def test_duplicate():
    v = view.View()
    with taddons.context():
        f = [
            tflow.tflow(),
            tflow.tflow(),
        ]
        v.add(f)
        assert len(v) == 2
        v.duplicate(f)
        assert len(v) == 4
        assert v.focus.index == 2
示例#29
0
 def test_intercept(self):
     """regression test for https://github.com/mitmproxy/mitmproxy/issues/1605"""
     m = self.mkmaster(intercept="~b bar")
     f = tflow.tflow(req=mitmproxy.test.tutils.treq(content=b"foo"))
     m.request(f)
     assert not m.view[0].intercepted
     f = tflow.tflow(req=mitmproxy.test.tutils.treq(content=b"bar"))
     m.request(f)
     assert m.view[1].intercepted
     f = tflow.tflow(resp=mitmproxy.test.tutils.tresp(content=b"bar"))
     m.request(f)
     assert m.view[2].intercepted
    def test_ignore_host(self):
        sp = serverplayback.ServerPlayback()
        sp.configure(options.Options(server_replay_ignore_host=True), [])

        r = tflow.tflow(resp=True)
        r2 = tflow.tflow(resp=True)

        r.request.host = "address"
        r2.request.host = "address"
        assert sp._hash(r) == sp._hash(r2)
        r2.request.host = "wrong_address"
        assert sp._hash(r) == sp._hash(r2)
示例#31
0
 async def test_script_run_nonexistent(self):
     sc = script.ScriptLoader()
     with taddons.context(sc) as tctx:
         sc.script_run([tflow.tflow(resp=True)], "/")
         assert await tctx.master.await_log("/: No such script")
示例#32
0
def get_response():
    return tflow.tflow(
        resp=tutils.tresp(status_code=404, content=b"Test Response Body"))
示例#33
0
 def test_base_class(self, tmpdir):
     tmpfile = tmpdir.join("tmpfile")
     index_writer = UrlIndexWriter(tmpfile)
     index_writer.load()
     index_writer.add_url(tflow.tflow())
     index_writer.save()
示例#34
0
 def test_filer_true(self):
     f = tflow.tflow(resp=tutils.tresp())
     assert filter_404(f)
示例#35
0
 def flowfile(self, path):
     f = open(path, "wb")
     fw = io.FlowWriter(f)
     t = tflow.tflow(resp=True)
     fw.add(t)
     f.close()
示例#36
0
 def req(self):
     return tflow.tflow()
示例#37
0
 def resolve(self, spec: str) -> typing.Sequence[flow.Flow]:
     n = int(spec)
     return [tflow.tflow(resp=True)] * n
示例#38
0
def get_request():
    return tflow.tflow(req=tutils.treq(
        method=b'GET', content=b'', path=b"/path?a=foo&a=bar&b=baz"))
示例#39
0
 def test_add_header(self):
     m, _ = tscript("simple/add_header.py")
     f = tflow.tflow(resp=tutils.tresp())
     m.response(f)
     assert f.response.headers["newheader"] == "foo"
示例#40
0
 def test_arguments(self):
     m, sc = tscript("simple/script_arguments.py", "mitmproxy rocks")
     f = tflow.tflow(resp=tutils.tresp(content=b"I <3 mitmproxy"))
     m.response(f)
     assert f.response.content == b"I <3 rocks"
示例#41
0
 def test_redirect_requests(self):
     m, sc = tscript("simple/redirect_requests.py")
     f = tflow.tflow(req=tutils.treq(host="example.org"))
     m.request(f)
     assert f.request.host == "mitmproxy.org"
示例#42
0
 def flowfile(self, path):
     with open(path, "wb") as f:
         fw = io.FlowWriter(f)
         t = tflow.tflow(resp=True)
         fw.add(t)
示例#43
0
 def test_send_reply_from_proxy(self):
     m, sc = tscript("simple/send_reply_from_proxy.py")
     f = tflow.tflow(req=tutils.treq(host="example.com", port=80))
     m.request(f)
     assert f.response.content == b"Hello World"
示例#44
0
 def _response(self, sc, cookie, host):
     f = tflow.tflow(req=ntutils.treq(host=host, port=80), resp=True)
     f.response.headers["Set-Cookie"] = cookie
     sc.response(f)
     return f
示例#45
0
def test_simple():
    sio = io.StringIO()
    d = dumper.Dumper(sio)
    with taddons.context(options=options.Options()) as ctx:
        ctx.configure(d, flow_detail=0)
        d.response(tflow.tflow(resp=True))
        assert not sio.getvalue()
        sio.truncate(0)

        ctx.configure(d, flow_detail=1)
        d.response(tflow.tflow(resp=True))
        assert sio.getvalue()
        sio.truncate(0)

        ctx.configure(d, flow_detail=1)
        d.error(tflow.tflow(err=True))
        assert sio.getvalue()
        sio.truncate(0)

        ctx.configure(d, flow_detail=4)
        d.response(tflow.tflow(resp=True))
        assert sio.getvalue()
        sio.truncate(0)

        ctx.configure(d, flow_detail=4)
        d.response(tflow.tflow(resp=True))
        assert "<<" in sio.getvalue()
        sio.truncate(0)

        ctx.configure(d, flow_detail=4)
        d.response(tflow.tflow(err=True))
        assert "<<" in sio.getvalue()
        sio.truncate(0)

        ctx.configure(d, flow_detail=4)
        flow = tflow.tflow()
        flow.request = tutils.treq()
        flow.request.stickycookie = True
        flow.client_conn = mock.MagicMock()
        flow.client_conn.address[0] = "foo"
        flow.response = tutils.tresp(content=None)
        flow.response.is_replay = True
        flow.response.status_code = 300
        d.response(flow)
        assert sio.getvalue()
        sio.truncate(0)

        ctx.configure(d, flow_detail=4)
        flow = tflow.tflow(resp=tutils.tresp(content=b"{"))
        flow.response.headers["content-type"] = "application/json"
        flow.response.status_code = 400
        d.response(flow)
        assert sio.getvalue()
        sio.truncate(0)

        ctx.configure(d, flow_detail=4)
        flow = tflow.tflow()
        flow.request.content = None
        flow.response = http.HTTPResponse.wrap(tutils.tresp())
        flow.response.content = None
        d.response(flow)
        assert "content missing" in sio.getvalue()
        sio.truncate(0)
示例#46
0
 def test_escape_single_quotes_in_body(self):
     request = tflow.tflow(
         req=tutils.treq(method=b'POST', headers=(), content=b"'&#"))
     command = export.curl_command(request)
     assert shlex.split(command)[-2] == '-d'
     assert shlex.split(command)[-1] == "'&#"
示例#47
0
 def err(self):
     return tflow.tflow(err=True)
示例#48
0
def patch_request():
    return tflow.tflow(req=tutils.treq(
        method=b'PATCH', content=b'content', path=b"/path?query=param"))
示例#49
0
 def resp(self):
     return tflow.tflow(resp=True)
示例#50
0
def post_request():
    return tflow.tflow(
        req=tutils.treq(method=b'POST', headers=(), content=bytes(range(256))))
示例#51
0
 def test_script_run_nonexistent(self):
     sc = script.ScriptLoader()
     with taddons.context(sc):
         with pytest.raises(exceptions.CommandError):
             sc.script_run([tflow.tflow(resp=True)], "/")
示例#52
0
def test_format_flow():
    f = tflow.tflow(resp=True)
    assert common.format_flow(f, True)
    assert common.format_flow(f, True, hostheader=True)
    assert common.format_flow(f, True, extended=True)
def test_save_flows(tmpdir):
    flows = [tflow.tflow(resp=False), tflow.tflow(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])
示例#54
0
def get_flow():
    return tflow.tflow(req=tutils.treq(method=b'GET',
                                       content=b'',
                                       path=b"/path?a=foo&a=bar&b=baz"),
                       resp=tutils.tresp(status_code=404,
                                         content=b"Test Response Body"))
示例#55
0
 def test_repr(self):
     f = tflow.tflow(resp=True, err=True)
     assert repr(f)
示例#56
0
 def test_filter_false(self):
     f = tflow.tflow(resp=tutils.tresp())
     f.response.status_code = 404
     assert not filter_404(f)
示例#57
0
 def test_add_header(self):
     m, _ = tscript("simple/add_header.py")
     f = tflow.tflow(resp=tutils.tresp())
     m.addons.handle_lifecycle("response", f)
     assert f.response.headers["newheader"] == "foo"
示例#58
0
def tft(*, method="get", start=0):
    f = tflow.tflow()
    f.request.method = method
    f.request.timestamp_start = start
    return f
示例#59
0
 def test_arguments(self):
     m, sc = tscript("simple/script_arguments.py", "mitmproxy rocks")
     f = tflow.tflow(resp=tutils.tresp(content=b"I <3 mitmproxy"))
     m.addons.handle_lifecycle("response", f)
     assert f.response.content == b"I <3 rocks"
示例#60
0
 def test_simple(self):
     f = tflow.tflow(resp=True)
     resp = f.response
     resp2 = resp.copy()
     assert resp2.get_state() == resp.get_state()