2011-03-09 07:05:30 +00:00
|
|
|
import cStringIO, time
|
2010-02-16 04:09:07 +00:00
|
|
|
import libpry
|
2011-02-02 23:16:03 +00:00
|
|
|
from libmproxy import proxy, controller, utils, dump, script
|
2011-03-09 07:05:30 +00:00
|
|
|
import email.utils
|
2011-03-05 02:58:48 +00:00
|
|
|
import tutils
|
2010-02-16 04:09:07 +00:00
|
|
|
|
|
|
|
|
2011-03-12 03:00:01 +00:00
|
|
|
class u_read_chunked(libpry.AutoTree):
|
|
|
|
def test_all(self):
|
|
|
|
s = cStringIO.StringIO("1\r\na\r\n0\r\n")
|
|
|
|
libpry.raises(IOError, proxy.read_chunked, s)
|
|
|
|
|
|
|
|
s = cStringIO.StringIO("1\r\na\r\n0\r\n\r\n")
|
|
|
|
assert proxy.read_chunked(s) == "a"
|
|
|
|
|
|
|
|
s = cStringIO.StringIO("\r\n")
|
|
|
|
libpry.raises(IOError, proxy.read_chunked, s)
|
|
|
|
|
|
|
|
s = cStringIO.StringIO("1\r\nfoo")
|
|
|
|
libpry.raises(IOError, proxy.read_chunked, s)
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-02-10 22:06:30 +00:00
|
|
|
class u_parse_request_line(libpry.AutoTree):
|
2010-02-16 04:09:07 +00:00
|
|
|
def test_simple(self):
|
2011-02-10 22:06:30 +00:00
|
|
|
libpry.raises(proxy.ProxyError, proxy.parse_request_line, "")
|
2010-02-16 04:09:07 +00:00
|
|
|
|
|
|
|
u = "GET ... HTTP/1.1"
|
2011-02-10 22:06:30 +00:00
|
|
|
libpry.raises("invalid url", proxy.parse_request_line, u)
|
2010-02-16 04:09:07 +00:00
|
|
|
|
|
|
|
u = "GET http://foo.com:8888/test HTTP/1.1"
|
2011-02-10 22:06:30 +00:00
|
|
|
m, s, h, po, pa, minor = proxy.parse_request_line(u)
|
2010-02-16 04:09:07 +00:00
|
|
|
assert m == "GET"
|
|
|
|
assert s == "http"
|
|
|
|
assert h == "foo.com"
|
|
|
|
assert po == 8888
|
|
|
|
assert pa == "/test"
|
2011-02-10 22:06:30 +00:00
|
|
|
assert minor == 1
|
2010-02-16 04:09:07 +00:00
|
|
|
|
|
|
|
def test_connect(self):
|
|
|
|
u = "CONNECT host.com:443 HTTP/1.0"
|
2011-02-10 22:06:30 +00:00
|
|
|
expected = ('CONNECT', None, 'host.com', 443, None, 0)
|
|
|
|
ret = proxy.parse_request_line(u)
|
2010-02-16 04:09:07 +00:00
|
|
|
assert expected == ret
|
|
|
|
|
|
|
|
def test_inner(self):
|
|
|
|
u = "GET / HTTP/1.1"
|
2011-02-10 22:06:30 +00:00
|
|
|
assert proxy.parse_request_line(u) == ('GET', None, None, None, '/', 1)
|
2010-02-16 04:09:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
class u_parse_url(libpry.AutoTree):
|
|
|
|
def test_simple(self):
|
|
|
|
assert not proxy.parse_url("")
|
|
|
|
|
|
|
|
u = "http://foo.com:8888/test"
|
|
|
|
s, h, po, pa = proxy.parse_url(u)
|
|
|
|
assert s == "http"
|
|
|
|
assert h == "foo.com"
|
|
|
|
assert po == 8888
|
|
|
|
assert pa == "/test"
|
|
|
|
|
|
|
|
s, h, po, pa = proxy.parse_url("http://foo/bar")
|
|
|
|
assert s == "http"
|
|
|
|
assert h == "foo"
|
|
|
|
assert po == 80
|
|
|
|
assert pa == "/bar"
|
|
|
|
|
|
|
|
s, h, po, pa = proxy.parse_url("http://foo")
|
|
|
|
assert pa == "/"
|
|
|
|
|
2011-02-02 23:16:03 +00:00
|
|
|
s, h, po, pa = proxy.parse_url("https://foo")
|
|
|
|
assert po == 443
|
|
|
|
|
2010-02-16 04:09:07 +00:00
|
|
|
|
|
|
|
class uFileLike(libpry.AutoTree):
|
|
|
|
def test_wrap(self):
|
|
|
|
s = cStringIO.StringIO("foobar\nfoobar")
|
|
|
|
s = proxy.FileLike(s)
|
|
|
|
s.flush()
|
|
|
|
assert s.readline() == "foobar\n"
|
|
|
|
assert s.readline() == "foobar"
|
2011-02-02 23:16:03 +00:00
|
|
|
# Test __getattr__
|
|
|
|
assert s.isatty
|
2010-02-16 04:09:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
class uRequest(libpry.AutoTree):
|
|
|
|
def test_simple(self):
|
|
|
|
h = utils.Headers()
|
|
|
|
h["test"] = ["test"]
|
2011-02-19 04:00:24 +00:00
|
|
|
c = proxy.ClientConnect(("addr", 2222))
|
2010-02-16 04:09:07 +00:00
|
|
|
r = proxy.Request(c, "host", 22, "https", "GET", "/", h, "content")
|
|
|
|
u = r.url()
|
|
|
|
assert r.set_url(u)
|
|
|
|
assert not r.set_url("")
|
|
|
|
assert r.url() == u
|
|
|
|
assert r.assemble()
|
|
|
|
|
2011-03-09 00:15:31 +00:00
|
|
|
def test_anticache(self):
|
|
|
|
h = utils.Headers()
|
|
|
|
r = proxy.Request(None, "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
|
|
|
|
|
2011-01-26 01:52:03 +00:00
|
|
|
def test_getset_state(self):
|
|
|
|
h = utils.Headers()
|
|
|
|
h["test"] = ["test"]
|
2011-02-19 04:00:24 +00:00
|
|
|
c = proxy.ClientConnect(("addr", 2222))
|
2011-01-26 01:52:03 +00:00
|
|
|
r = proxy.Request(c, "host", 22, "https", "GET", "/", h, "content")
|
|
|
|
state = r.get_state()
|
2011-02-19 04:00:24 +00:00
|
|
|
assert proxy.Request.from_state(state) == r
|
2011-01-26 01:52:03 +00:00
|
|
|
|
2011-02-19 04:03:44 +00:00
|
|
|
r.client_conn = None
|
|
|
|
state = r.get_state()
|
|
|
|
assert proxy.Request.from_state(state) == r
|
|
|
|
|
2011-02-19 20:36:13 +00:00
|
|
|
r2 = proxy.Request(c, "testing", 20, "http", "PUT", "/foo", h, "test")
|
|
|
|
assert not r == r2
|
|
|
|
r.load_state(r2.get_state())
|
|
|
|
assert r == r2
|
|
|
|
|
2010-02-16 04:09:07 +00:00
|
|
|
|
|
|
|
class uResponse(libpry.AutoTree):
|
|
|
|
def test_simple(self):
|
|
|
|
h = utils.Headers()
|
|
|
|
h["test"] = ["test"]
|
2011-02-19 04:00:24 +00:00
|
|
|
c = proxy.ClientConnect(("addr", 2222))
|
2010-02-16 04:09:07 +00:00
|
|
|
req = proxy.Request(c, "host", 22, "https", "GET", "/", h, "content")
|
2011-02-15 17:44:57 +00:00
|
|
|
resp = proxy.Response(req, 200, "msg", h.copy(), "content")
|
2010-02-16 04:09:07 +00:00
|
|
|
assert resp.assemble()
|
|
|
|
|
2011-03-09 07:05:30 +00:00
|
|
|
def test_refresh(self):
|
|
|
|
r = 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"][0])
|
|
|
|
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()
|
|
|
|
|
2011-03-10 22:56:10 +00:00
|
|
|
def test_refresh_cookie(self):
|
|
|
|
r = 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)
|
|
|
|
|
|
|
|
|
2011-01-26 01:52:03 +00:00
|
|
|
def test_getset_state(self):
|
|
|
|
h = utils.Headers()
|
|
|
|
h["test"] = ["test"]
|
2011-02-19 04:00:24 +00:00
|
|
|
c = proxy.ClientConnect(("addr", 2222))
|
2011-01-26 01:52:03 +00:00
|
|
|
r = proxy.Request(c, "host", 22, "https", "GET", "/", h, "content")
|
|
|
|
req = proxy.Request(c, "host", 22, "https", "GET", "/", h, "content")
|
2011-02-15 22:20:00 +00:00
|
|
|
resp = proxy.Response(req, 200, "msg", h.copy(), "content")
|
2011-01-26 01:52:03 +00:00
|
|
|
|
|
|
|
state = resp.get_state()
|
|
|
|
assert proxy.Response.from_state(req, state) == resp
|
|
|
|
|
2011-02-19 20:36:13 +00:00
|
|
|
resp2 = proxy.Response(req, 220, "foo", h.copy(), "test")
|
|
|
|
assert not resp == resp2
|
|
|
|
resp.load_state(resp2.get_state())
|
|
|
|
assert resp == resp2
|
|
|
|
|
2011-01-26 01:52:03 +00:00
|
|
|
|
|
|
|
class uError(libpry.AutoTree):
|
|
|
|
def test_getset_state(self):
|
|
|
|
e = proxy.Error(None, "Error")
|
|
|
|
state = e.get_state()
|
|
|
|
assert proxy.Error.from_state(state) == e
|
|
|
|
|
2011-02-03 00:30:47 +00:00
|
|
|
assert e.copy()
|
|
|
|
|
2011-02-19 20:36:13 +00:00
|
|
|
e2 = proxy.Error(None, "bar")
|
|
|
|
assert not e == e2
|
|
|
|
e.load_state(e2.get_state())
|
|
|
|
assert e == e2
|
|
|
|
|
|
|
|
|
2010-02-16 04:09:07 +00:00
|
|
|
class uProxyError(libpry.AutoTree):
|
|
|
|
def test_simple(self):
|
|
|
|
p = proxy.ProxyError(111, "msg")
|
|
|
|
assert repr(p)
|
|
|
|
|
|
|
|
|
2011-02-19 20:36:13 +00:00
|
|
|
class uClientConnect(libpry.AutoTree):
|
|
|
|
def test_state(self):
|
|
|
|
c = proxy.ClientConnect(("a", 22))
|
|
|
|
assert proxy.ClientConnect.from_state(c.get_state()) == c
|
|
|
|
|
|
|
|
c2 = proxy.ClientConnect(("a", 25))
|
|
|
|
assert not c == c2
|
|
|
|
|
|
|
|
c.load_state(c2.get_state())
|
|
|
|
assert c == c2
|
|
|
|
|
|
|
|
|
2010-02-16 04:09:07 +00:00
|
|
|
tests = [
|
|
|
|
uProxyError(),
|
|
|
|
uRequest(),
|
|
|
|
uResponse(),
|
|
|
|
uFileLike(),
|
2011-02-10 22:06:30 +00:00
|
|
|
u_parse_request_line(),
|
2010-02-16 04:09:07 +00:00
|
|
|
u_parse_url(),
|
2011-01-26 01:52:03 +00:00
|
|
|
uError(),
|
2011-02-19 20:36:13 +00:00
|
|
|
uClientConnect(),
|
2011-03-12 03:00:01 +00:00
|
|
|
u_read_chunked(),
|
2010-02-16 04:09:07 +00:00
|
|
|
]
|