Ejemplo n.º 1
0
    def test_flow(self):
        """
            normal flow:

                connect -> request -> response
        """
        c = flow.State()
        f = tutils.tflow()
        c.add_flow(f)
        assert f
        assert c.flow_count() == 1
        assert c.active_flow_count() == 1

        newf = tutils.tflow()
        assert c.add_flow(newf)
        assert c.active_flow_count() == 2

        f.response = HTTPResponse.wrap(netlib.tutils.tresp())
        assert c.update_flow(f)
        assert c.flow_count() == 2
        assert c.active_flow_count() == 1

        _ = HTTPResponse.wrap(netlib.tutils.tresp())
        assert not c.update_flow(None)
        assert c.active_flow_count() == 1

        newf.response = HTTPResponse.wrap(netlib.tutils.tresp())
        assert c.update_flow(newf)
        assert c.active_flow_count() == 0
Ejemplo n.º 2
0
    def test_decodeencode(self):
        r = HTTPResponse.wrap(netlib.tutils.tresp())
        r.headers["content-encoding"] = "identity"
        r.content = "falafel"
        assert r.decode()
        assert "content-encoding" not in r.headers
        assert r.content == "falafel"

        r = HTTPResponse.wrap(netlib.tutils.tresp())
        r.headers["content-encoding"] = "identity"
        r.content = "falafel"
        r.encode("identity")
        assert r.headers["content-encoding"] == "identity"
        assert r.content == "falafel"

        r = HTTPResponse.wrap(netlib.tutils.tresp())
        r.headers["content-encoding"] = "identity"
        r.content = "falafel"
        r.encode("gzip")
        assert r.headers["content-encoding"] == "gzip"
        assert r.content != "falafel"
        assert r.decode()
        assert "content-encoding" not in r.headers
        assert r.content == "falafel"

        r.headers["content-encoding"] = "gzip"
        assert not r.decode()
        assert r.content == "falafel"
Ejemplo n.º 3
0
    def test_decodeencode(self):
        r = HTTPResponse.wrap(netlib.tutils.tresp())
        r.headers["content-encoding"] = ["identity"]
        r.content = "falafel"
        assert r.decode()
        assert not r.headers["content-encoding"]
        assert r.content == "falafel"

        r = HTTPResponse.wrap(netlib.tutils.tresp())
        r.headers["content-encoding"] = ["identity"]
        r.content = "falafel"
        r.encode("identity")
        assert r.headers["content-encoding"] == ["identity"]
        assert r.content == "falafel"

        r = HTTPResponse.wrap(netlib.tutils.tresp())
        r.headers["content-encoding"] = ["identity"]
        r.content = "falafel"
        r.encode("gzip")
        assert r.headers["content-encoding"] == ["gzip"]
        assert r.content != "falafel"
        assert r.decode()
        assert not r.headers["content-encoding"]
        assert r.content == "falafel"

        r.headers["content-encoding"] = ["gzip"]
        assert not r.decode()
        assert r.content == "falafel"
Ejemplo n.º 4
0
    def test_flow(self):
        """
            normal flow:

                connect -> request -> response
        """
        c = flow.State()
        f = tutils.tflow()
        c.add_flow(f)
        assert f
        assert c.flow_count() == 1
        assert c.active_flow_count() == 1

        newf = tutils.tflow()
        assert c.add_flow(newf)
        assert c.active_flow_count() == 2

        f.response = HTTPResponse.wrap(netlib.tutils.tresp())
        assert c.update_flow(f)
        assert c.flow_count() == 2
        assert c.active_flow_count() == 1

        _ = HTTPResponse.wrap(netlib.tutils.tresp())
        assert not c.update_flow(None)
        assert c.active_flow_count() == 1

        newf.response = HTTPResponse.wrap(netlib.tutils.tresp())
        assert c.update_flow(newf)
        assert c.active_flow_count() == 0
Ejemplo n.º 5
0
    def test_set_limit(self):
        c = flow.State()

        f = tutils.tflow()
        assert len(c.view) == 0

        c.add_flow(f)
        assert len(c.view) == 1

        c.set_limit("~s")
        assert c.limit_txt == "~s"
        assert len(c.view) == 0
        f.response = HTTPResponse.wrap(netlib.tutils.tresp())
        c.update_flow(f)
        assert len(c.view) == 1
        c.set_limit(None)
        assert len(c.view) == 1

        f = tutils.tflow()
        c.add_flow(f)
        assert len(c.view) == 2
        c.set_limit("~q")
        assert len(c.view) == 1
        c.set_limit("~s")
        assert len(c.view) == 1

        assert "Invalid" in c.set_limit("~")
Ejemplo n.º 6
0
    def test_set_limit(self):
        c = flow.State()

        f = tutils.tflow()
        assert len(c.view) == 0

        c.add_flow(f)
        assert len(c.view) == 1

        c.set_limit("~s")
        assert c.limit_txt == "~s"
        assert len(c.view) == 0
        f.response = HTTPResponse.wrap(netlib.tutils.tresp())
        c.update_flow(f)
        assert len(c.view) == 1
        c.set_limit(None)
        assert len(c.view) == 1

        f = tutils.tflow()
        c.add_flow(f)
        assert len(c.view) == 2
        c.set_limit("~q")
        assert len(c.view) == 1
        c.set_limit("~s")
        assert len(c.view) == 1

        assert "Invalid" in c.set_limit("~")
Ejemplo n.º 7
0
 def test_replace(self):
     r = HTTPResponse.wrap(netlib.tutils.tresp())
     r.headers["Foo"] = "fOo"
     r.content = "afoob"
     assert r.replace("foo(?i)", "boo") == 3
     assert not "foo" in r.content
     assert r.headers["boo"] == "boo"
Ejemplo n.º 8
0
    def test_server_playback(self):
        s = flow.State()

        f = tutils.tflow()
        f.response = HTTPResponse.wrap(netlib.tutils.tresp(content=f.request))
        pb = [f]

        fm = flow.FlowMaster(None, s)
        fm.refresh_server_playback = True
        assert not fm.do_server_playback(tutils.tflow())

        fm.start_server_playback(pb, False, [], False, False, None, False,
                                 None, False)
        assert fm.do_server_playback(tutils.tflow())

        fm.start_server_playback(pb, False, [], True, False, None, False, None,
                                 False)
        r = tutils.tflow()
        r.request.content = "gibble"
        assert not fm.do_server_playback(r)
        assert fm.do_server_playback(tutils.tflow())

        fm.start_server_playback(pb, False, [], True, False, None, False, None,
                                 False)
        q = Queue.Queue()
        fm.tick(q, 0)
        assert fm.should_exit.is_set()

        fm.stop_server_playback()
        assert not fm.server_playback
Ejemplo n.º 9
0
def tflow(client_conn=True, server_conn=True, req=True, resp=None, err=None):
    """
    @type client_conn: bool | None | libmproxy.proxy.connection.ClientConnection
    @type server_conn: bool | None | libmproxy.proxy.connection.ServerConnection
    @type req:         bool | None | libmproxy.protocol.http.HTTPRequest
    @type resp:        bool | None | libmproxy.protocol.http.HTTPResponse
    @type err:         bool | None | libmproxy.protocol.primitives.Error
    @return:           bool | None | libmproxy.protocol.http.HTTPFlow
    """
    if client_conn is True:
        client_conn = tclient_conn()
    if server_conn is True:
        server_conn = tserver_conn()
    if req is True:
        req = netlib.tutils.treq()
    if resp is True:
        resp = netlib.tutils.tresp()
    if err is True:
        err = terr()

    if req:
        req = HTTPRequest.wrap(req)
    if resp:
        resp = HTTPResponse.wrap(resp)

    f = HTTPFlow(client_conn, server_conn)
    f.request = req
    f.response = resp
    f.error = err
    f.reply = controller.DummyReply()
    return f
Ejemplo n.º 10
0
def tflow(client_conn=True, server_conn=True, req=True, resp=None, err=None):
    """
    @type client_conn: bool | None | libmproxy.proxy.connection.ClientConnection
    @type server_conn: bool | None | libmproxy.proxy.connection.ServerConnection
    @type req:         bool | None | libmproxy.protocol.http.HTTPRequest
    @type resp:        bool | None | libmproxy.protocol.http.HTTPResponse
    @type err:         bool | None | libmproxy.protocol.primitives.Error
    @return:           bool | None | libmproxy.protocol.http.HTTPFlow
    """
    if client_conn is True:
        client_conn = tclient_conn()
    if server_conn is True:
        server_conn = tserver_conn()
    if req is True:
        req = netlib.tutils.treq()
    if resp is True:
        resp = netlib.tutils.tresp()
    if err is True:
        err = terr()

    if req:
        req = HTTPRequest.wrap(req)
    if resp:
        resp = HTTPResponse.wrap(resp)

    f = HTTPFlow(client_conn, server_conn)
    f.request = req
    f.response = resp
    f.error = err
    f.reply = controller.DummyReply()
    return f
Ejemplo n.º 11
0
 def test_replace(self):
     r = HTTPResponse.wrap(netlib.tutils.tresp())
     r.headers["Foo"] = "fOo"
     r.content = "afoob"
     assert r.replace("foo(?i)", "boo") == 3
     assert not "foo" in r.content
     assert r.headers["boo"] == "boo"
Ejemplo n.º 12
0
    def test_server_playback(self):
        s = flow.State()

        f = tutils.tflow()
        f.response = HTTPResponse.wrap(netlib.tutils.tresp(content=f.request))
        pb = [f]

        fm = flow.FlowMaster(None, s)
        fm.refresh_server_playback = True
        assert not fm.do_server_playback(tutils.tflow())

        fm.start_server_playback(pb, False, [], False, False, None, False,
                                 None, False)
        assert fm.do_server_playback(tutils.tflow())

        fm.start_server_playback(pb, False, [], True, False, None, False, None,
                                 False)
        r = tutils.tflow()
        r.request.content = "gibble"
        assert not fm.do_server_playback(r)
        assert fm.do_server_playback(tutils.tflow())

        fm.start_server_playback(pb, False, [], True, False, None, False, None,
                                 False)
        q = Queue.Queue()
        fm.tick(q, 0)
        assert fm.should_exit.is_set()

        fm.stop_server_playback()
        assert not fm.server_playback
Ejemplo n.º 13
0
    def test_refresh_cookie(self):
        r = HTTPResponse.wrap(netlib.tutils.tresp())

        # Invalid expires format, sent to us by Reddit.
        c = "rfoo=bar; Domain=reddit.com; expires=Thu, 31 Dec 2037 23:59:59 GMT; Path=/"
        assert r._refresh_cookie(c, 60)

        c = "MOO=BAR; Expires=Tue, 08-Mar-2011 00:20:38 GMT; Path=foo.com; Secure"
        assert "00:21:38" in r._refresh_cookie(c, 60)
Ejemplo n.º 14
0
    def test_refresh_cookie(self):
        r = HTTPResponse.wrap(netlib.tutils.tresp())

        # Invalid expires format, sent to us by Reddit.
        c = "rfoo=bar; Domain=reddit.com; expires=Thu, 31 Dec 2037 23:59:59 GMT; Path=/"
        assert r._refresh_cookie(c, 60)

        c = "MOO=BAR; Expires=Tue, 08-Mar-2011 00:20:38 GMT; Path=foo.com; Secure"
        assert "00:21:38" in r._refresh_cookie(c, 60)
Ejemplo n.º 15
0
 def test_backup(self):
     f = tutils.tflow()
     f.response = HTTPResponse.wrap(netlib.tutils.tresp())
     f.request.content = "foo"
     assert not f.modified()
     f.backup()
     f.request.content = "bar"
     assert f.modified()
     f.revert()
     assert f.request.content == "foo"
Ejemplo n.º 16
0
 def test_backup(self):
     f = tutils.tflow()
     f.response = HTTPResponse.wrap(netlib.tutils.tresp())
     f.request.content = "foo"
     assert not f.modified()
     f.backup()
     f.request.content = "bar"
     assert f.modified()
     f.revert()
     assert f.request.content == "foo"
Ejemplo n.º 17
0
 def test_missing_content(self):
     cs = StringIO()
     o = dump.Options(flow_detail=3)
     m = dump.DumpMaster(None, o, outfile=cs)
     f = tutils.tflow()
     f.request.content = CONTENT_MISSING
     m.handle_request(f)
     f.response = HTTPResponse.wrap(netlib.tutils.tresp())
     f.response.content = CONTENT_MISSING
     m.handle_response(f)
     assert "content missing" in cs.getvalue()
Ejemplo n.º 18
0
 def test_missing_content(self):
     cs = StringIO()
     o = dump.Options(flow_detail=3)
     m = dump.DumpMaster(None, o, outfile=cs)
     f = tutils.tflow()
     f.request.content = CONTENT_MISSING
     m.handle_request(f)
     f.response = HTTPResponse.wrap(netlib.tutils.tresp())
     f.response.content = CONTENT_MISSING
     m.handle_response(f)
     assert "content missing" in cs.getvalue()
Ejemplo n.º 19
0
def test_strfuncs():
    t = HTTPResponse.wrap(netlib.tutils.tresp())
    t.is_replay = True
    dump.str_response(t)

    f = tutils.tflow()
    f.client_conn = None
    f.request.stickycookie = True
    assert "stickycookie" in dump.str_request(f, False)
    assert "stickycookie" in dump.str_request(f, True)
    assert "replay" in dump.str_request(f, False)
    assert "replay" in dump.str_request(f, True)
Ejemplo n.º 20
0
 def _cycle(self, m, content):
     f = tutils.tflow(req=netlib.tutils.treq(content=content))
     l = Log("connect")
     l.reply = mock.MagicMock()
     m.handle_log(l)
     m.handle_clientconnect(f.client_conn)
     m.handle_serverconnect(f.server_conn)
     m.handle_request(f)
     f.response = HTTPResponse.wrap(netlib.tutils.tresp(content=content))
     f = m.handle_response(f)
     m.handle_clientdisconnect(f.client_conn)
     return f
Ejemplo n.º 21
0
def test_strfuncs():
    t = HTTPResponse.wrap(netlib.tutils.tresp())
    t.is_replay = True
    dump.str_response(t)

    f = tutils.tflow()
    f.client_conn = None
    f.request.stickycookie = True
    assert "stickycookie" in dump.str_request(f, False)
    assert "stickycookie" in dump.str_request(f, True)
    assert "replay" in dump.str_request(f, False)
    assert "replay" in dump.str_request(f, True)
Ejemplo n.º 22
0
 def _cycle(self, m, content):
     f = tutils.tflow(req=netlib.tutils.treq(body=content))
     l = Log("connect")
     l.reply = mock.MagicMock()
     m.handle_log(l)
     m.handle_clientconnect(f.client_conn)
     m.handle_serverconnect(f.server_conn)
     m.handle_request(f)
     f.response = HTTPResponse.wrap(netlib.tutils.tresp(body=content))
     f = m.handle_response(f)
     m.handle_clientdisconnect(f.client_conn)
     return f
Ejemplo n.º 23
0
    def test_server_playback_kill(self):
        s = flow.State()
        f = tutils.tflow()
        f.response = HTTPResponse.wrap(netlib.tutils.tresp(content=f.request))
        pb = [f]
        fm = flow.FlowMaster(None, s)
        fm.refresh_server_playback = True
        fm.start_server_playback(pb, True, [], False, False, None, False, None,
                                 False)

        f = tutils.tflow()
        f.request.host = "nonexistent"
        fm.process_new_request(f)
        assert "killed" in f.error.msg
Ejemplo n.º 24
0
    def test_refresh_cookie(self):
        r = HTTPResponse.wrap(netlib.tutils.tresp())

        # Invalid expires format, sent to us by Reddit.
        c = "rfoo=bar; Domain=reddit.com; expires=Thu, 31 Dec 2037 23:59:59 GMT; Path=/"
        assert r._refresh_cookie(c, 60)

        c = "MOO=BAR; Expires=Tue, 08-Mar-2011 00:20:38 GMT; Path=foo.com; Secure"
        assert "00:21:38" in r._refresh_cookie(c, 60)

        # https://github.com/mitmproxy/mitmproxy/issues/773
        c = ">=A"
        with tutils.raises(ValueError):
            r._refresh_cookie(c, 60)
Ejemplo n.º 25
0
    def test_refresh_cookie(self):
        r = HTTPResponse.wrap(netlib.tutils.tresp())

        # Invalid expires format, sent to us by Reddit.
        c = "rfoo=bar; Domain=reddit.com; expires=Thu, 31 Dec 2037 23:59:59 GMT; Path=/"
        assert r._refresh_cookie(c, 60)

        c = "MOO=BAR; Expires=Tue, 08-Mar-2011 00:20:38 GMT; Path=foo.com; Secure"
        assert "00:21:38" in r._refresh_cookie(c, 60)

        # https://github.com/mitmproxy/mitmproxy/issues/773
        c = ">=A"
        with tutils.raises(ValueError):
            r._refresh_cookie(c, 60)
Ejemplo n.º 26
0
    def test_server_playback_kill(self):
        s = flow.State()
        f = tutils.tflow()
        f.response = HTTPResponse.wrap(netlib.tutils.tresp(content=f.request))
        pb = [f]
        fm = flow.FlowMaster(None, s)
        fm.refresh_server_playback = True
        fm.start_server_playback(pb, True, [], False, False, None, False, None,
                                 False)

        f = tutils.tflow()
        f.request.host = "nonexistent"
        fm.process_new_request(f)
        assert "killed" in f.error.msg
Ejemplo n.º 27
0
    def test_refresh(self):
        r = HTTPResponse.wrap(netlib.tutils.tresp())
        n = time.time()
        r.headers["date"] = email.utils.formatdate(n)
        pre = r.headers["date"]
        r.refresh(n)
        assert pre == r.headers["date"]
        r.refresh(n + 60)

        d = email.utils.parsedate_tz(r.headers["date"])
        d = email.utils.mktime_tz(d)
        # Weird that this is not exact...
        assert abs(60 - (d - n)) <= 1

        r.headers["set-cookie"] = "MOO=BAR; Expires=Tue, 08-Mar-2011 00:20:38 GMT; Path=foo.com; Secure"
        r.refresh()
Ejemplo n.º 28
0
    def test_refresh(self):
        r = HTTPResponse.wrap(netlib.tutils.tresp())
        n = time.time()
        r.headers["date"] = email.utils.formatdate(n)
        pre = r.headers["date"]
        r.refresh(n)
        assert pre == r.headers["date"]
        r.refresh(n + 60)

        d = email.utils.parsedate_tz(r.headers["date"])
        d = email.utils.mktime_tz(d)
        # Weird that this is not exact...
        assert abs(60 - (d - n)) <= 1

        r.headers["set-cookie"] = "MOO=BAR; Expires=Tue, 08-Mar-2011 00:20:38 GMT; Path=foo.com; Secure"
        r.refresh()
Ejemplo n.º 29
0
    def test_all(self):
        s = flow.State()
        fm = flow.FlowMaster(None, s)
        fm.anticache = True
        fm.anticomp = True
        f = tutils.tflow(req=None)
        fm.handle_clientconnect(f.client_conn)
        f.request = HTTPRequest.wrap(netlib.tutils.treq())
        fm.handle_request(f)
        assert s.flow_count() == 1

        f.response = HTTPResponse.wrap(netlib.tutils.tresp())
        fm.handle_response(f)
        assert not fm.handle_response(None)
        assert s.flow_count() == 1

        fm.handle_clientdisconnect(f.client_conn)

        f.error = Error("msg")
        f.error.reply = controller.DummyReply()
        fm.handle_error(f)

        fm.load_script(tutils.test_data.path("scripts/a.py"))
        fm.shutdown()
Ejemplo n.º 30
0
    def test_all(self):
        s = flow.State()
        fm = flow.FlowMaster(None, s)
        fm.anticache = True
        fm.anticomp = True
        f = tutils.tflow(req=None)
        fm.handle_clientconnect(f.client_conn)
        f.request = HTTPRequest.wrap(netlib.tutils.treq())
        fm.handle_request(f)
        assert s.flow_count() == 1

        f.response = HTTPResponse.wrap(netlib.tutils.tresp())
        fm.handle_response(f)
        assert not fm.handle_response(None)
        assert s.flow_count() == 1

        fm.handle_clientdisconnect(f.client_conn)

        f.error = Error("msg")
        f.error.reply = controller.DummyReply()
        fm.handle_error(f)

        fm.load_script(tutils.test_data.path("scripts/a.py"))
        fm.shutdown()
Ejemplo n.º 31
0
 def handle_request(self, f):
     resp = HTTPResponse.wrap(netlib.tutils.tresp())
     resp.content = CONTENT_MISSING
     f.reply(resp)
Ejemplo n.º 32
0
 def handle_request(self, f):
     resp = HTTPResponse.wrap(netlib.tutils.tresp())
     f.reply(resp)
Ejemplo n.º 33
0
 def test_get_content_type(self):
     h = odict.ODictCaseless()
     h["Content-Type"] = ["text/plain"]
     resp = HTTPResponse.wrap(netlib.tutils.tresp())
     resp.headers = h
     assert resp.headers.get_first("content-type") == "text/plain"
Ejemplo n.º 34
0
 def test_get_content_type(self):
     resp = HTTPResponse.wrap(netlib.tutils.tresp())
     resp.headers = Headers(content_type="text/plain")
     assert resp.headers["content-type"] == "text/plain"
Ejemplo n.º 35
0
 def _add_response(self, state):
     f = tutils.tflow()
     state.add_flow(f)
     f.response = HTTPResponse.wrap(netlib.tutils.tresp())
     state.update_flow(f)
Ejemplo n.º 36
0
 def test_get_content_type(self):
     resp = HTTPResponse.wrap(netlib.tutils.tresp())
     resp.headers = Headers(content_type="text/plain")
     assert resp.headers["content-type"] == "text/plain"
Ejemplo n.º 37
0
 def _add_response(self, state):
     f = tutils.tflow()
     state.add_flow(f)
     f.response = HTTPResponse.wrap(netlib.tutils.tresp())
     state.update_flow(f)