Beispiel #1
0
    def test_decodeencode(self):
        r = HTTPRequest.wrap(netlib.tutils.treq())
        r.headers["content-encoding"] = "identity"
        r.content = "falafel"
        r.decode()
        assert "content-encoding" not in r.headers
        assert r.content == "falafel"

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

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

        r = HTTPRequest.wrap(netlib.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 "content-encoding" not in r.headers
        assert r.content == "falafel"
Beispiel #2
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
Beispiel #3
0
 def test_anticache(self):
     r = HTTPRequest.wrap(netlib.tutils.treq())
     r.headers = Headers()
     r.headers["if-modified-since"] = "test"
     r.headers["if-none-match"] = "test"
     r.anticache()
     assert not "if-modified-since" in r.headers
     assert not "if-none-match" in r.headers
Beispiel #4
0
def test_app_registry():
    ar = flow.AppRegistry()
    ar.add("foo", "domain", 80)

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

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

    r = HTTPRequest.wrap(netlib.tutils.treq())
    r.host = "domain2"
    r.port = 80
    assert not ar.get(r)
    r.headers["host"] = "domain"
    assert ar.get(r)
Beispiel #5
0
    def test_get_decoded_content(self):
        r = HTTPRequest.wrap(netlib.tutils.treq())
        r.content = None
        r.headers["content-encoding"] = "identity"
        assert r.get_decoded_content() is None

        r.content = "falafel"
        r.encode("gzip")
        assert r.get_decoded_content() == "falafel"
Beispiel #6
0
    def test_constrain_encoding(self):
        r = HTTPRequest.wrap(netlib.tutils.treq())
        r.headers["accept-encoding"] = "gzip, oink"
        r.constrain_encoding()
        assert "oink" not in r.headers["accept-encoding"]

        r.headers.set_all("accept-encoding", ["gzip", "oink"])
        r.constrain_encoding()
        assert "oink" not in r.headers["accept-encoding"]
Beispiel #7
0
 def test_replace(self):
     r = HTTPRequest.wrap(netlib.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"
Beispiel #8
0
 def test_anticache(self):
     h = odict.ODictCaseless()
     r = HTTPRequest.wrap(netlib.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
Beispiel #9
0
    def test_getset_form_urlencoded(self):
        d = odict.ODict([("one", "two"), ("three", "four")])
        r = HTTPRequest.wrap(netlib.tutils.treq(body=netlib.utils.urlencode(d.lst)))
        r.headers["content-type"] = HDR_FORM_URLENCODED
        assert r.get_form_urlencoded() == d

        d = odict.ODict([("x", "y")])
        r.set_form_urlencoded(d)
        assert r.get_form_urlencoded() == d

        r.headers["content-type"] = "foo"
        assert not r.get_form_urlencoded()
Beispiel #10
0
    def test_getset_form_urlencoded(self):
        d = odict.ODict([("one", "two"), ("three", "four")])
        r = HTTPRequest.wrap(netlib.tutils.treq(content=netlib.utils.urlencode(d.lst)))
        r.headers["content-type"] = "application/x-www-form-urlencoded"
        assert r.get_form_urlencoded() == d

        d = odict.ODict([("x", "y")])
        r.set_form_urlencoded(d)
        assert r.get_form_urlencoded() == d

        r.headers["content-type"] = "foo"
        assert not r.get_form_urlencoded()
Beispiel #11
0
    def test_path_components(self):
        r = HTTPRequest.wrap(netlib.tutils.treq())
        r.path = "/"
        assert r.get_path_components() == []
        r.path = "/foo/bar"
        assert r.get_path_components() == ["foo", "bar"]
        q = odict.ODict()
        q["test"] = ["123"]
        r.set_query(q)
        assert r.get_path_components() == ["foo", "bar"]

        r.set_path_components([])
        assert r.get_path_components() == []
        r.set_path_components(["foo"])
        assert r.get_path_components() == ["foo"]
        r.set_path_components(["/oo"])
        assert r.get_path_components() == ["/oo"]
        assert "%2F" in r.path
Beispiel #12
0
def test_decoded():
    r = HTTPRequest.wrap(netlib.tutils.treq())
    assert r.content == "content"
    assert "content-encoding" not in r.headers
    r.encode("gzip")
    assert r.headers["content-encoding"]
    assert r.content != "content"
    with decoded(r):
        assert "content-encoding" not in r.headers
        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"
Beispiel #13
0
    def test_get_url(self):
        r = HTTPRequest.wrap(netlib.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 == "https://address:22/path"
        r.headers["Host"] = "foo.com"
        assert r.url == "https://address:22/path"
        assert r.pretty_url == "https://foo.com:22/path"
Beispiel #14
0
    def test_getset_query(self):
        r = HTTPRequest.wrap(netlib.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(odict.ODict([]))
        assert not r.get_query()
        qv = odict.ODict([("a", "b"), ("c", "d")])
        r.set_query(qv)
        assert r.get_query() == qv
Beispiel #15
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()
Beispiel #16
0
 def test_constrain_encoding(self):
     r = HTTPRequest.wrap(netlib.tutils.treq())
     r.headers["accept-encoding"] = ["gzip", "oink"]
     r.constrain_encoding()
     assert "oink" not in r.headers["accept-encoding"]