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

                connect -> request -> response
        """
        bc = flow.ClientConnect(("address", 22))
        c = flow.State()

        req = tutils.treq(bc)
        f = c.add_request(req)
        assert f
        assert c.flow_count() == 1
        assert c._flow_map.get(req)
        assert c.active_flow_count() == 1

        newreq = tutils.treq()
        assert c.add_request(newreq)
        assert c._flow_map.get(newreq)
        assert c.active_flow_count() == 2

        resp = tutils.tresp(req)
        assert c.add_response(resp)
        assert c.flow_count() == 2
        assert c._flow_map.get(resp.request)
        assert c.active_flow_count() == 1

        unseen_resp = tutils.tresp()
        assert not c.add_response(unseen_resp)
        assert not c._flow_map.get(unseen_resp.request)
        assert c.active_flow_count() == 1

        resp = tutils.tresp(newreq)
        assert c.add_response(resp)
        assert c.active_flow_count() == 0
Ejemplo n.º 2
0
    def test_flow(self):
        """
            normal flow:

                connect -> request -> response
        """
        bc = tutils.tclient_conn()
        c = flow.State()

        req = tutils.treq(bc)
        f = c.add_request(req)
        assert f
        assert c.flow_count() == 1
        assert c.active_flow_count() == 1

        newreq = tutils.treq()
        assert c.add_request(newreq)
        assert c.active_flow_count() == 2

        resp = tutils.tresp(req)
        assert c.add_response(resp)
        assert c.flow_count() == 2
        assert c.active_flow_count() == 1

        unseen_resp = tutils.tresp()
        unseen_resp.flow = None
        assert not c.add_response(unseen_resp)
        assert c.active_flow_count() == 1

        resp = tutils.tresp(newreq)
        assert c.add_response(resp)
        assert c.active_flow_count() == 0
Ejemplo n.º 3
0
    def test_set_limit(self):
        c = flow.State()

        req = tutils.treq()
        assert len(c.view) == 0

        c.add_request(req)
        assert len(c.view) == 1

        c.set_limit("~s")
        assert c.limit_txt == "~s"
        assert len(c.view) == 0
        resp = tutils.tresp(req)
        c.add_response(resp)
        assert len(c.view) == 1
        c.set_limit(None)
        assert len(c.view) == 1

        req = tutils.treq()
        c.add_request(req)
        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.º 4
0
    def test_set_limit(self):
        c = flow.State()

        req = tutils.treq()
        c.clientconnect(req.client_conn)
        assert len(c.view) == 0

        f = c.add_request(req)
        assert len(c.view) == 1

        c.set_limit("~s")
        assert len(c.view) == 0
        resp = tutils.tresp(req)
        c.add_response(resp)
        assert len(c.view) == 1
        c.set_limit(None)
        assert len(c.view) == 1

        req = tutils.treq()
        c.clientconnect(req.client_conn)
        c.add_request(req)
        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.º 5
0
    def test_decodeencode(self):
        r = tutils.treq()
        r.headers["content-encoding"] = ["identity"]
        r.content = "falafel"
        r.decode()
        assert not r.headers["content-encoding"]
        assert r.content == "falafel"

        r = tutils.treq()
        r.content = "falafel"
        assert not r.decode()

        r = tutils.treq()
        r.headers["content-encoding"] = ["identity"]
        r.content = "falafel"
        r.encode("identity")
        assert r.headers["content-encoding"] == ["identity"]
        assert r.content == "falafel"

        r = tutils.treq()
        r.headers["content-encoding"] = ["identity"]
        r.content = "falafel"
        r.encode("gzip")
        assert r.headers["content-encoding"] == ["gzip"]
        assert r.content != "falafel"
        r.decode()
        assert not r.headers["content-encoding"]
        assert r.content == "falafel"
Ejemplo n.º 6
0
    def test_killall(self):
        s = flow.State()
        fm = flow.FlowMaster(None, s)

        r = tutils.treq()
        fm.handle_request(r)

        r = tutils.treq()
        fm.handle_request(r)

        for i in s.view:
            assert not i.request.acked
        s.killall(fm)
        for i in s.view:
            assert i.request.acked
Ejemplo n.º 7
0
    def test_get_cert(self):
        req = tutils.treq()
        resp = flow.Response(req, 200, "msg", flow.ODictCaseless(), "content", file("data/dercert").read())
        assert resp.get_cert()

        resp = tutils.tresp()
        assert not resp.get_cert()
Ejemplo n.º 8
0
 def test_header_size(self):
     h = flow.ODictCaseless()
     h["headername"] = ["headervalue"]
     r = tutils.treq()
     r.headers = h
     raw = r._assemble_headers()
     assert len(raw) == 62
Ejemplo n.º 9
0
    def test_concurrent(self):
        s = flow.State()
        fm = flow.FlowMaster(None, s)
        fm.load_script(tutils.test_data.path("scripts/concurrent_decorator.py"))

        with mock.patch("libmproxy.controller.DummyReply.__call__") as m:
            r1, r2 = tutils.treq(), tutils.treq()
            t_start = time.time()
            fm.handle_request(r1)
            r1.reply()
            fm.handle_request(r2)
            r2.reply()

            # Two instantiations
            assert m.call_count == 2
            assert (time.time() - t_start) < 0.09
Ejemplo n.º 10
0
 def test_script_reqerr(self):
     s = flow.State()
     fm = flow.FlowMaster(None, s)
     assert not fm.load_script(tutils.test_data.path("scripts/reqerr.py"))
     req = tutils.treq()
     fm.handle_clientconnect(req.client_conn)
     assert fm.handle_request(req)
Ejemplo n.º 11
0
    def test_backup(self):
        c = flow.State()
        req = tutils.treq()
        f = c.add_request(req)

        f.backup()
        c.revert(f)
Ejemplo n.º 12
0
    def test_all(self):
        s = flow.State()
        fm = flow.FlowMaster(None, s)
        fm.anticache = True
        fm.anticomp = True
        req = tutils.treq()
        fm.handle_clientconnect(req.flow.client_conn)

        f = fm.handle_request(req)
        assert s.flow_count() == 1

        resp = tutils.tresp(req)
        fm.handle_response(resp)
        assert s.flow_count() == 1

        rx = tutils.tresp()
        rx.flow = None
        assert not fm.handle_response(rx)

        fm.handle_clientdisconnect(req.flow.client_conn)

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

        fm.load_script(tutils.test_data.path("scripts/a.py"))
        fm.shutdown()
Ejemplo n.º 13
0
    def test_all(self):
        s = flow.State()
        fm = flow.FlowMaster(None, s)
        fm.anticache = True
        fm.anticomp = True
        req = tutils.treq()
        fm.handle_clientconnect(req.client_conn)

        f = fm.handle_request(req)
        assert s.flow_count() == 1

        resp = tutils.tresp(req)
        fm.handle_response(resp)
        assert s.flow_count() == 1

        rx = tutils.tresp()
        assert not fm.handle_response(rx)

        dc = flow.ClientDisconnect(req.client_conn)
        req.client_conn.requestcount = 1
        fm.handle_clientdisconnect(dc)

        err = flow.Error(f.request, "msg")
        fm.handle_error(err)

        fm.load_script(tutils.test_data.path("scripts/a.py"))
        fm.shutdown()
Ejemplo n.º 14
0
    def test_script(self):
        s = flow.State()
        fm = flow.FlowMaster(None, s)
        assert not fm.load_script(tutils.test_data.path("scripts/all.py"))
        req = tutils.treq()
        fm.handle_clientconnect(req.flow.client_conn)
        assert fm.scripts[0].ns["log"][-1] == "clientconnect"
        sc = ServerConnection((req.get_host(), req.get_port()), None)
        sc.reply = controller.DummyReply()
        fm.handle_serverconnection(sc)
        assert fm.scripts[0].ns["log"][-1] == "serverconnect"
        f = fm.handle_request(req)
        assert fm.scripts[0].ns["log"][-1] == "request"
        resp = tutils.tresp(req)
        fm.handle_response(resp)
        assert fm.scripts[0].ns["log"][-1] == "response"
        #load second script
        assert not fm.load_script(tutils.test_data.path("scripts/all.py"))
        assert len(fm.scripts) == 2
        fm.handle_clientdisconnect(sc)
        assert fm.scripts[0].ns["log"][-1] == "clientdisconnect"
        assert fm.scripts[1].ns["log"][-1] == "clientdisconnect"


        #unload first script
        fm.unload_scripts()
        assert len(fm.scripts) == 0

        assert not fm.load_script(tutils.test_data.path("scripts/all.py"))
        err = tutils.terr()
        err.reply = controller.DummyReply()
        fm.handle_error(err)
        assert fm.scripts[0].ns["log"][-1] == "error"
Ejemplo n.º 15
0
 def test_script(self):
     s = flow.State()
     fm = flow.FlowMaster(None, s)
     assert not fm.load_script([tutils.test_data.path("scripts/all.py")])
     req = tutils.treq()
     fm.handle_clientconnect(req.client_conn)
     assert fm.scripts[0].ns["log"][-1] == "clientconnect"
     f = fm.handle_request(req)
     assert fm.scripts[0].ns["log"][-1] == "request"
     resp = tutils.tresp(req)
     fm.handle_response(resp)
     assert fm.scripts[0].ns["log"][-1] == "response"
     #load second script
     assert not fm.load_script([tutils.test_data.path("scripts/all.py")])
     assert len(fm.scripts) == 2
     dc = flow.ClientDisconnect(req.client_conn)
     dc.reply = controller.DummyReply()
     fm.handle_clientdisconnect(dc)
     assert fm.scripts[0].ns["log"][-1] == "clientdisconnect"
     assert fm.scripts[1].ns["log"][-1] == "clientdisconnect"
     #unload first script
     fm.unload_script(fm.scripts[0])
     assert len(fm.scripts) == 1
     err = flow.Error(f.request, "msg")
     err.reply = controller.DummyReply()
     fm.handle_error(err)
     assert fm.scripts[0].ns["log"][-1] == "error"
Ejemplo n.º 16
0
    def test_get_form_for_urlencoded(self):
        r = tutils.treq()
        r.headers.add("content-type", "application/x-www-form-urlencoded")
        r.get_form_urlencoded = MagicMock()

        r.get_form()

        assert r.get_form_urlencoded.called
Ejemplo n.º 17
0
def test_app_registry():
    ar = flow.AppRegistry()
    ar.add("foo", "domain", 80)

    r = tutils.treq()
    r.set_url("http://domain:80/")
    assert ar.get(r)

    r.port = 81
    assert not ar.get(r)

    r = tutils.treq()
    r.host = "domain2"
    r.port = 80
    assert not ar.get(r)
    r.headers["host"] = ["domain"]
    assert ar.get(r)
Ejemplo n.º 18
0
 def _serve(self, app):
     w = wsgi.WSGIAdaptor(app, "foo", 80)
     r = tutils.treq()
     r.host = "foo"
     r.port = 80
     wfile = cStringIO.StringIO()
     err = w.serve(r, wfile)
     return wfile.getvalue()
Ejemplo n.º 19
0
    def test_backup(self):
        bc = proxy.ClientConnect(("address", 22))
        c = flow.State()
        req = tutils.treq()
        f = c.add_request(req)

        f.backup()
        c.revert(f)
Ejemplo n.º 20
0
    def test_concurrent(self):
        s = flow.State()
        fm = flow.FlowMaster(None, s)
        fm.load_script([tutils.test_data.path("scripts/concurrent_decorator.py")])

        reply = TCounter()
        r1, r2 = tutils.treq(), tutils.treq()
        r1.reply, r2.reply = reply, reply
        t_start = time.time()
        fm.handle_request(r1)
        r1.reply()
        fm.handle_request(r2)
        r2.reply()
        assert reply.count < 2
        assert (time.time() - t_start) < 0.09
        time.sleep(0.2)
        assert reply.count == 2
Ejemplo n.º 21
0
    def test_get_form_for_multipart(self):
        r = tutils.treq()
        r.headers.add("content-type", "multipart/form-data")
        r.get_form_multipart = MagicMock()

        r.get_form()

        assert r.get_form_multipart.called
Ejemplo n.º 22
0
 def test_get_cookies_single(self):
     h = flow.ODictCaseless()
     h["Cookie"] = ["cookiename=cookievalue"]
     r = tutils.treq()
     r.headers = h
     result = r.get_cookies()
     assert len(result)==1
     assert result['cookiename']==('cookievalue',{})
Ejemplo n.º 23
0
    def test_make_environ(self):
        w = wsgi.WSGIAdaptor(None, "foo", 80)
        tr = tutils.treq()
        assert w.make_environ(tr, None)

        tr.path = "/foo?bar=voing"
        r = w.make_environ(tr, None)
        assert r["QUERY_STRING"] == "bar=voing"
Ejemplo n.º 24
0
    def test_get_decoded_content(self):
        r = tutils.treq()
        r.content = None
        r.headers["content-encoding"] = ["identity"]
        assert r.get_decoded_content() == None

        r.content = "falafel"
        r.encode("gzip")
        assert r.get_decoded_content() == "falafel"
Ejemplo n.º 25
0
 def test_duplicate_flow(self):
     s = flow.State()
     fm = flow.FlowMaster(None, s)
     fm.load_script(tutils.test_data.path("scripts/duplicate_flow.py"))
     r = tutils.treq()
     fm.handle_request(r)
     assert fm.state.flow_count() == 2
     assert not fm.state.view[0].request.is_replay
     assert fm.state.view[1].request.is_replay
Ejemplo n.º 26
0
def test_app_registry():
    ar = proxy.AppRegistry()
    ar.add("foo", "domain", 80)

    r = tutils.treq()
    r.host = "domain"
    r.port = 80
    assert ar.get(r)

    r.port = 81
    assert not ar.get(r)

    r = tutils.treq()
    r.host = "domain2"
    r.port = 80
    assert not ar.get(r)
    r.headers["host"] = ["domain"]
    assert ar.get(r)
Ejemplo n.º 27
0
 def test_get_cookies_withequalsign(self):
     h = flow.ODictCaseless()
     h["Cookie"] = ["cookiename=coo=kievalue;othercookiename=othercookievalue"]
     r = tutils.treq()
     r.headers = h
     result = r.get_cookies()
     assert len(result)==2
     assert result['cookiename']==('coo=kievalue',{})
     assert result['othercookiename']==('othercookievalue',{})
Ejemplo n.º 28
0
 def test_anticache(self):
     h = flow.ODictCaseless()
     r = tutils.treq()
     r.headers = h
     h["if-modified-since"] = ["test"]
     h["if-none-match"] = ["test"]
     r.anticache()
     assert not "if-modified-since" in r.headers
     assert not "if-none-match" in r.headers
Ejemplo n.º 29
0
 def test_replace(self):
     r = tutils.treq()
     r.path = "path/foo"
     r.headers["Foo"] = ["fOo"]
     r.content = "afoob"
     assert r.replace("foo(?i)", "boo") == 4
     assert r.path == "path/boo"
     assert not "foo" in r.content
     assert r.headers["boo"] == ["boo"]
Ejemplo n.º 30
0
    def test_err(self):
        c = flow.State()
        req = tutils.treq()
        f = c.add_request(req)
        e = flow.Error(f.request, "message")
        assert c.add_error(e)

        e = flow.Error(tutils.tflow().request, "message")
        assert not c.add_error(e)
Ejemplo n.º 31
0
def test_decoded():
    r = tutils.treq()
    assert r.content == "content"
    assert not r.headers["content-encoding"]
    r.encode("gzip")
    assert r.headers["content-encoding"]
    assert r.content != "content"
    with decoded(r):
        assert not r.headers["content-encoding"]
        assert r.content == "content"
    assert r.headers["content-encoding"]
    assert r.content != "content"

    with decoded(r):
        r.content = "foo"

    assert r.content != "foo"
    r.decode()
    assert r.content == "foo"
Ejemplo n.º 32
0
    def test_get_url(self):
        r = tutils.treq()

        assert r.url == "http://address:22/path"

        r.scheme = "https"
        assert r.url == "https://address:22/path"

        r.host = "host"
        r.port = 42
        assert r.url == "https://host:42/path"

        r.host = "address"
        r.port = 22
        assert r.url == "https://address:22/path"

        assert r.pretty_url(True) == "https://address:22/path"
        r.headers["Host"] = ["foo.com"]
        assert r.pretty_url(False) == "https://address:22/path"
        assert r.pretty_url(True) == "https://foo.com:22/path"
Ejemplo n.º 33
0
 def test_script(self):
     s = flow.State()
     fm = flow.FlowMaster(None, s)
     assert not fm.load_script(tutils.test_data.path("scripts/all.py"))
     req = tutils.treq()
     fm.handle_clientconnect(req.client_conn)
     assert fm.script.ns["log"][-1] == "clientconnect"
     f = fm.handle_request(req)
     assert fm.script.ns["log"][-1] == "request"
     resp = tutils.tresp(req)
     fm.handle_response(resp)
     assert fm.script.ns["log"][-1] == "response"
     dc = flow.ClientDisconnect(req.client_conn)
     dc.reply = controller.DummyReply()
     fm.handle_clientdisconnect(dc)
     assert fm.script.ns["log"][-1] == "clientdisconnect"
     err = flow.Error(f.request, "msg")
     err.reply = controller.DummyReply()
     fm.handle_error(err)
     assert fm.script.ns["log"][-1] == "error"
Ejemplo n.º 34
0
    def test_all(self):
        s = flow.State()
        fm = flow.FlowMaster(None, s)
        fm.anticache = True
        fm.anticomp = True
        req = tutils.treq()
        fm.handle_clientconnect(req.client_conn)

        f = fm.handle_request(req)
        assert s.flow_count() == 1

        resp = tutils.tresp(req)
        fm.handle_response(resp)
        assert s.flow_count() == 1

        rx = tutils.tresp()
        assert not fm.handle_response(rx)

        dc = flow.ClientDisconnect(req.client_conn)
        fm.handle_clientdisconnect(dc)

        err = flow.Error(f.request, "msg")
        fm.handle_error(err)
Ejemplo n.º 35
0
    def test_getset_query(self):
        h = flow.ODictCaseless()

        r = tutils.treq()
        r.path = "/foo?x=y&a=b"
        q = r.get_query()
        assert q.lst == [("x", "y"), ("a", "b")]

        r.path = "/"
        q = r.get_query()
        assert not q

        r.path = "/?adsfa"
        q = r.get_query()
        assert q.lst == [("adsfa", "")]

        r.path = "/foo?x=y&a=b"
        assert r.get_query()
        r.set_query(flow.ODict([]))
        assert not r.get_query()
        qv = flow.ODict([("a", "b"), ("c", "d")])
        r.set_query(qv)
        assert r.get_query() == qv
Ejemplo n.º 36
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 = tutils.treq()
        fm.handle_request(f)
        assert s.flow_count() == 1

        f.response = 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.º 37
0
 def _add_request(self, state):
     req = tutils.treq()
     f = state.add_request(req)
     return f
Ejemplo n.º 38
0
 def test_serialization(self):
     f = flow.Flow(None)
     f.request = tutils.treq()
Ejemplo n.º 39
0
 def _add_request(self, state):
     r = tutils.treq()
     return state.add_request(r)
Ejemplo n.º 40
0
 def test_get_cookies_none(self):
     h = odict.ODictCaseless()
     r = tutils.treq()
     r.headers = h
     assert len(r.get_cookies()) == 0
Ejemplo n.º 41
0
 def test_repr(self):
     r = tutils.treq()
     assert repr(r)
Ejemplo n.º 42
0
 def test_assemble_unknown_form(self):
     r = tutils.treq()
     tutils.raises("Invalid request form", r.assemble, "antiauthority")
Ejemplo n.º 43
0
 def test_get_cookies_none(self):
     h = flow.ODictCaseless()
     r = tutils.treq()
     r.headers = h
     assert r.get_cookies() is None
Ejemplo n.º 44
0
 def test_constrain_encoding(self):
     r = tutils.treq()
     r.headers["accept-encoding"] = ["gzip", "oink"]
     r.constrain_encoding()
     assert "oink" not in r.headers["accept-encoding"]
Ejemplo n.º 45
0
 def _add_error(self, state):
     req = tutils.treq()
     f = state.add_request(req)
     f.error = flow.Error(f.request, "msg")
Ejemplo n.º 46
0
 def _response(self, cookie, host):
     s = flow.StickyCookieState(filt.parse(".*"))
     f = tutils.tflow(req=tutils.treq(host=host, port=80), resp=True)
     f.response.headers["Set-Cookie"] = [cookie]
     s.handle_response(f)
     return s, f
Ejemplo n.º 47
0
 def _add_error(self, state):
     req = tutils.treq()
     f = state.add_request(req)
     f.error = Error("msg")
Ejemplo n.º 48
0
 def _add_response(self, state):
     req = tutils.treq()
     state.add_request(req)
     resp = tutils.tresp(req)
     state.add_response(resp)