Example #1
0
 def test_get_cookies_single(self):
     h = flow.ODictCaseless()
     h["Cookie"] = ["cookiename=cookievalue"]
     c = flow.ClientConnect(("addr", 2222))
     r = flow.Request(c, (1, 1), "host", 22, "https", "GET", "/", h,
                      "content")
     result = r.get_cookies()
     assert len(result) == 1
     assert result['cookiename'] == ('cookievalue', {})
Example #2
0
 def test_get_transmitted_size(self):
     h = flow.ODictCaseless()
     h["headername"] = ["headervalue"]
     c = flow.ClientConnect(("addr", 2222))
     r = flow.Request(c, (1, 1), "host", 22, "https", "GET", "/", h, "content")
     result = r.get_transmitted_size()
     assert result==len("content")
     r.content = None
     assert r.get_transmitted_size() == 0
Example #3
0
 def test_anticache(self):
     h = flow.ODictCaseless()
     r = flow.Request(None, (1, 1), "host", 22, "https", "GET", "/", h,
                      "content")
     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
Example #4
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
Example #5
0
 def test_get_cookies_no_value(self):
     h = flow.ODictCaseless()
     h["Set-Cookie"] = ["cookiename=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/"]
     resp = flow.Response(None, (1, 1), 200, "OK", h, "content", None)
     result = resp.get_cookies()
     assert len(result)==1
     assert "cookiename" in result
     assert result["cookiename"][0] == ""
     assert len(result["cookiename"][1])==2
Example #6
0
 def test_get_cookies_simple(self):
     h = flow.ODictCaseless()
     h["Set-Cookie"] = ["cookiename=cookievalue"]
     resp = tutils.tresp()
     resp.headers = h
     result = resp.get_cookies()
     assert len(result) == 1
     assert "cookiename" in result
     assert result["cookiename"] == ("cookievalue", {})
Example #7
0
def treq(content="content", scheme="http", host="address", port=22):
    """
    @return: libmproxy.protocol.http.HTTPRequest
    """
    headers = flow.ODictCaseless()
    headers["header"] = ["qvalue"]
    req = http.HTTPRequest("relative", "GET", scheme, host, port, "/path",
                           (1, 1), headers, content, None, None, None)
    return req
Example #8
0
def tresp(req=None):
    if not req:
        req = treq()
    headers = flow.ODictCaseless()
    headers["header_response"] = ["svalue"]
    cert = certutils.SSLCert.from_der(file(test_data.path("data/dercert"),"rb").read())
    resp = flow.Response(req, (1, 1), 200, "message", headers, "content_response", cert)
    resp.reply = controller.DummyReply()
    return resp
Example #9
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',{})
Example #10
0
def treq(conn=None):
    if not conn:
        conn = flow.ClientConnect(("address", 22))
    conn.reply = controller.DummyReply()
    headers = flow.ODictCaseless()
    headers["header"] = ["qvalue"]
    r = flow.Request(conn, (1, 1), "host", 80, "http", "GET", "/path", headers, "content")
    r.reply = controller.DummyReply()
    return r
Example #11
0
 def test_get_cookies_twocookies(self):
     h = flow.ODictCaseless()
     h["Set-Cookie"] = ["cookiename=cookievalue","othercookie=othervalue"]
     resp = flow.Response(None, (1, 1), 200, "OK", h, "content", None)
     result = resp.get_cookies()
     assert len(result)==2
     assert "cookiename" in result
     assert result["cookiename"] == ("cookievalue", {})
     assert "othercookie" in result
     assert result["othercookie"] == ("othervalue", {})
Example #12
0
 def test_get_url(self):
     h = flow.ODictCaseless()
     h["test"] = ["test"]
     c = flow.ClientConnect(("addr", 2222))
     r = flow.Request(c, (1, 1), "host", 22, "https", "GET", "/", h, "content")
     assert r.get_url() == "https://host:22/"
     assert r.get_url(hostheader=True) == "https://host:22/"
     r.headers["Host"] = ["foo.com"]
     assert r.get_url() == "https://host:22/"
     assert r.get_url(hostheader=True) == "https://foo.com:22/"
Example #13
0
 def test_get_cookies_no_value(self):
     h = flow.ODictCaseless()
     h["Set-Cookie"] = ["cookiename=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/"]
     resp = tutils.tresp()
     resp.headers = h
     result = resp.get_cookies()
     assert len(result)==1
     assert "cookiename" in result
     assert result["cookiename"][0] == ""
     assert len(result["cookiename"][1])==2
Example #14
0
def tresp(content="message"):
    """
    @return: libmproxy.protocol.http.HTTPResponse
    """

    headers = flow.ODictCaseless()
    headers["header_response"] = ["svalue"]

    resp = http.HTTPResponse((1, 1), 200, "OK", headers, content, time(),
                             time())
    return resp
    def test_view_multipart(self):
        view = cv.ViewMultipart()
        v = """
--AaB03x
Content-Disposition: form-data; name="submit-name"

Larry
--AaB03x
        """.strip()
        h = flow.ODictCaseless([("Content-Type",
                                 "multipart/form-data; boundary=AaB03x")])
        assert view(h, v, 1000)

        h = flow.ODictCaseless()
        assert not view(h, v, 1000)

        h = flow.ODictCaseless([("Content-Type", "multipart/form-data")])
        assert not view(h, v, 1000)

        h = flow.ODictCaseless([("Content-Type", "unparseable")])
        assert not view(h, v, 1000)
Example #16
0
def treq(conn=None, content="content"):
    if not conn:
        conn = tclient_conn()
    server_conn = tserver_conn()
    headers = flow.ODictCaseless()
    headers["header"] = ["qvalue"]

    f = http.HTTPFlow(conn, server_conn)
    f.request = http.HTTPRequest("relative", "GET", None, None, None, "/path",
                                 (1, 1), headers, content, None, None, None)
    f.request.reply = controller.DummyReply()
    return f.request
Example #17
0
    def test_getset_form_urlencoded(self):
        h = flow.ODictCaseless()
        h["content-type"] = [flow.HDR_FORM_URLENCODED]
        d = flow.ODict([("one", "two"), ("three", "four")])
        r = flow.Request(None, (1, 1), "host", 22, "https", "GET", "/", h, utils.urlencode(d.lst))
        assert r.get_form_urlencoded() == d

        d = flow.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()
Example #18
0
 def test_get_cookies_with_parameters(self):
     h = flow.ODictCaseless()
     h["Set-Cookie"] = ["cookiename=cookievalue;domain=example.com;expires=Wed Oct  21 16:29:41 2015;path=/; HttpOnly"]
     resp = flow.Response(None, (1, 1), 200, "OK", h, "content", None)
     result = resp.get_cookies()
     assert len(result)==1
     assert "cookiename" in result
     assert result["cookiename"][0] == "cookievalue"
     assert len(result["cookiename"][1])==4
     assert result["cookiename"][1]["domain"]=="example.com"
     assert result["cookiename"][1]["expires"]=="Wed Oct  21 16:29:41 2015"
     assert result["cookiename"][1]["path"]=="/"
     assert result["cookiename"][1]["httponly"]==""
Example #19
0
 def test_get_cookies_with_parameters(self):
     h = flow.ODictCaseless()
     h["Set-Cookie"] = ["cookiename=cookievalue;domain=example.com;expires=Wed Oct  21 16:29:41 2015;path=/; HttpOnly"]
     resp = tutils.tresp()
     resp.headers = h
     result = resp.get_cookies()
     assert len(result)==1
     assert "cookiename" in result
     assert result["cookiename"][0] == "cookievalue"
     assert len(result["cookiename"][1])==4
     assert result["cookiename"][1]["domain"]=="example.com"
     assert result["cookiename"][1]["expires"]=="Wed Oct  21 16:29:41 2015"
     assert result["cookiename"][1]["path"]=="/"
     assert result["cookiename"][1]["httponly"]==""
Example #20
0
    def test_getset_state(self):
        h = flow.ODictCaseless()
        h["test"] = ["test"]
        c = flow.ClientConnect(("addr", 2222))
        req = flow.Request(c, "host", 22, "https", "GET", "/", h, "content")
        resp = flow.Response(req, 200, "msg", h.copy(), "content")

        state = resp._get_state()
        assert flow.Response._from_state(req, state) == resp

        resp2 = flow.Response(req, 220, "foo", h.copy(), "test")
        assert not resp == resp2
        resp._load_state(resp2._get_state())
        assert resp == resp2
Example #21
0
    def test_simple(self):
        h = flow.ODictCaseless()
        h["test"] = ["test"]
        c = flow.ClientConnect(("addr", 2222))
        req = flow.Request(c, "host", 22, "https", "GET", "/", h, "content")
        resp = flow.Response(req, 200, "msg", h.copy(), "content")
        assert resp._assemble()

        resp2 = resp.copy()
        assert resp2 == resp

        resp.content = None
        assert resp._assemble()

        resp.request.client_conn.close = True
        assert "connection: close" in resp._assemble()
Example #22
0
def tresp(req=None, content="message"):
    if not req:
        req = treq()
    f = req.flow

    headers = flow.ODictCaseless()
    headers["header_response"] = ["svalue"]
    cert = certutils.SSLCert.from_der(
        file(test_data.path("data/dercert"), "rb").read())
    f.server_conn = ServerConnection._from_state(
        dict(address=dict(address=("address", 22), use_ipv6=True),
             source_address=None,
             cert=cert.to_pem()))
    f.response = http.HTTPResponse((1, 1), 200, "OK", headers, content, time(),
                                   time())
    f.response.reply = controller.DummyReply()
    return f.response
Example #23
0
    def test_path_components(self):
        h = flow.ODictCaseless()
        c = flow.ClientConnect(("addr", 2222))
        r = flow.Request(c, (1, 1), "host", 22, "https", "GET", "/", h, "content")
        assert r.get_path_components() == []
        r = flow.Request(c, (1, 1), "host", 22, "https", "GET", "/foo/bar", h, "content")
        assert r.get_path_components() == ["foo", "bar"]
        q = flow.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
Example #24
0
    def test_simple(self):
        h = flow.ODictCaseless()
        h["test"] = ["test"]
        c = flow.ClientConnect(("addr", 2222))
        req = flow.Request(c, (1, 1), "host", 22, "https", "GET", "/", h, "content")
        resp = flow.Response(req, (1, 1), 200, "msg", h.copy(), "content", None)
        assert resp._assemble()
        assert resp.size() == len(resp._assemble())


        resp2 = resp.copy()
        assert resp2 == resp

        resp.content = None
        assert resp._assemble()
        assert resp.size() == len(resp._assemble())

        resp.content = flow.CONTENT_MISSING
        assert not resp._assemble()
Example #25
0
 def req(self):
     headers = flow.ODictCaseless()
     headers["header"] = ["qvalue"]
     req = http.HTTPRequest(
         "absolute",
         "GET",
         "http",
         "host",
         80,
         "/path",
         (1, 1),
         headers,
         "content_request",
         None,
         None
     )
     f = http.HTTPFlow(tutils.tclient_conn(), None)
     f.request = req
     return f
Example #26
0
    def test_getset_state(self):
        h = flow.ODictCaseless()
        h["test"] = ["test"]
        c = flow.ClientConnect(("addr", 2222))
        r = flow.Request(c, "host", 22, "https", "GET", "/", h, "content")
        state = r._get_state()
        assert flow.Request._from_state(state) == r

        r.client_conn = None
        state = r._get_state()
        assert flow.Request._from_state(state) == r

        r2 = flow.Request(c, "testing", 20, "http", "PUT", "/foo", h, "test")
        assert not r == r2
        r._load_state(r2._get_state())
        assert r == r2

        r2.client_conn = None
        r._load_state(r2._get_state())
        assert not r.client_conn
Example #27
0
    def test_simple(self):
        h = flow.ODictCaseless()
        h["test"] = ["test"]
        c = flow.ClientConnect(("addr", 2222))
        r = flow.Request(c, "host", 22, "https", "GET", "/", h, "content")
        u = r.get_url()
        assert r.set_url(u)
        assert not r.set_url("")
        assert r.get_url() == u
        assert r._assemble()

        r2 = r.copy()
        assert r == r2

        r.content = None
        assert r._assemble()

        r.close = True
        assert "connection: close" in r._assemble()

        assert r._assemble(True)
Example #28
0
    def test_getset_query(self):
        h = flow.ODictCaseless()

        r = flow.Request(None, (1, 1), "host", 22, "https", "GET", "/foo?x=y&a=b", h, "content")
        q = r.get_query()
        assert q.lst == [("x", "y"), ("a", "b")]

        r = flow.Request(None, (1, 1), "host", 22, "https", "GET", "/", h, "content")
        q = r.get_query()
        assert not q

        r = flow.Request(None, (1, 1), "host", 22, "https", "GET", "/?adsfa", h, "content")
        q = r.get_query()
        assert q.lst == [("adsfa", "")]

        r = flow.Request(None, (1, 1), "host", 22, "https", "GET", "/foo?x=y&a=b", h, "content")
        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
Example #29
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
Example #30
0
 def test_get_content_type(self):
     h = flow.ODictCaseless()
     h["Content-Type"] = ["text/plain"]
     resp = tutils.tresp()
     resp.headers = h
     assert resp.headers.get_first("content-type") == "text/plain"