2012-02-19 22:04:07 +00:00
|
|
|
import cStringIO, textwrap
|
2012-02-19 21:34:32 +00:00
|
|
|
from cStringIO import StringIO
|
2010-02-16 04:09:07 +00:00
|
|
|
import libpry
|
2012-02-19 22:04:07 +00:00
|
|
|
from libmproxy import proxy, flow
|
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")
|
2011-09-09 02:49:34 +00:00
|
|
|
libpry.raises(IOError, proxy.read_chunked, s, None)
|
2011-03-12 03:00:01 +00:00
|
|
|
|
|
|
|
s = cStringIO.StringIO("1\r\na\r\n0\r\n\r\n")
|
2011-09-09 02:49:34 +00:00
|
|
|
assert proxy.read_chunked(s, None) == "a"
|
2011-03-12 03:00:01 +00:00
|
|
|
|
|
|
|
s = cStringIO.StringIO("\r\n")
|
2011-09-09 02:49:34 +00:00
|
|
|
libpry.raises(IOError, proxy.read_chunked, s, None)
|
2011-03-12 03:00:01 +00:00
|
|
|
|
|
|
|
s = cStringIO.StringIO("1\r\nfoo")
|
2011-09-09 02:49:34 +00:00
|
|
|
libpry.raises(IOError, proxy.read_chunked, s, None)
|
2011-03-12 03:00:01 +00:00
|
|
|
|
2011-09-09 02:49:34 +00:00
|
|
|
s = cStringIO.StringIO("foo\r\nfoo")
|
|
|
|
libpry.raises(proxy.ProxyError, proxy.read_chunked, s, None)
|
|
|
|
|
|
|
|
|
|
|
|
class Dummy: pass
|
|
|
|
|
2012-02-24 23:19:54 +00:00
|
|
|
|
2011-09-09 02:49:34 +00:00
|
|
|
class u_read_http_body(libpry.AutoTree):
|
|
|
|
def test_all(self):
|
|
|
|
|
|
|
|
d = Dummy()
|
2012-02-19 21:34:32 +00:00
|
|
|
h = flow.ODict()
|
2011-09-09 02:49:34 +00:00
|
|
|
s = cStringIO.StringIO("testing")
|
|
|
|
assert proxy.read_http_body(s, d, h, False, None) == ""
|
|
|
|
|
|
|
|
h["content-length"] = ["foo"]
|
|
|
|
s = cStringIO.StringIO("testing")
|
|
|
|
libpry.raises(proxy.ProxyError, proxy.read_http_body, s, d, h, False, None)
|
|
|
|
|
|
|
|
h["content-length"] = [5]
|
|
|
|
s = cStringIO.StringIO("testing")
|
|
|
|
assert len(proxy.read_http_body(s, d, h, False, None)) == 5
|
|
|
|
s = cStringIO.StringIO("testing")
|
|
|
|
libpry.raises(proxy.ProxyError, proxy.read_http_body, s, d, h, False, 4)
|
|
|
|
|
2012-02-19 21:34:32 +00:00
|
|
|
h = flow.ODict()
|
2011-09-09 02:49:34 +00:00
|
|
|
s = cStringIO.StringIO("testing")
|
|
|
|
assert len(proxy.read_http_body(s, d, h, True, 4)) == 4
|
|
|
|
s = cStringIO.StringIO("testing")
|
|
|
|
assert len(proxy.read_http_body(s, d, h, True, 100)) == 7
|
2011-03-12 03:00:01 +00:00
|
|
|
|
|
|
|
|
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 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
|
|
|
|
|
|
|
|
2011-03-20 05:52:16 +00:00
|
|
|
|
2010-02-16 04:09:07 +00:00
|
|
|
class uProxyError(libpry.AutoTree):
|
|
|
|
def test_simple(self):
|
|
|
|
p = proxy.ProxyError(111, "msg")
|
|
|
|
assert repr(p)
|
|
|
|
|
|
|
|
|
2012-02-19 21:34:32 +00:00
|
|
|
class u_read_headers(libpry.AutoTree):
|
|
|
|
def test_read_simple(self):
|
|
|
|
data = """
|
|
|
|
Header: one
|
|
|
|
Header2: two
|
|
|
|
\r\n
|
|
|
|
"""
|
|
|
|
data = textwrap.dedent(data)
|
|
|
|
data = data.strip()
|
|
|
|
s = StringIO(data)
|
|
|
|
headers = proxy.read_headers(s)
|
|
|
|
assert headers["header"] == ["one"]
|
|
|
|
assert headers["header2"] == ["two"]
|
|
|
|
|
|
|
|
def test_read_multi(self):
|
|
|
|
data = """
|
|
|
|
Header: one
|
|
|
|
Header: two
|
|
|
|
\r\n
|
|
|
|
"""
|
|
|
|
data = textwrap.dedent(data)
|
|
|
|
data = data.strip()
|
|
|
|
s = StringIO(data)
|
|
|
|
headers = proxy.read_headers(s)
|
|
|
|
assert headers["header"] == ["one", "two"]
|
|
|
|
|
|
|
|
def test_read_continued(self):
|
|
|
|
data = """
|
|
|
|
Header: one
|
|
|
|
\ttwo
|
|
|
|
Header2: three
|
|
|
|
\r\n
|
|
|
|
"""
|
|
|
|
data = textwrap.dedent(data)
|
|
|
|
data = data.strip()
|
|
|
|
s = StringIO(data)
|
|
|
|
headers = proxy.read_headers(s)
|
|
|
|
assert headers["header"] == ['one\r\n two']
|
|
|
|
|
|
|
|
|
2012-06-03 13:04:57 +00:00
|
|
|
class u_parse_http_protocol(libpry.AutoTree):
|
|
|
|
def test_simple(self):
|
|
|
|
assert proxy.parse_http_protocol("HTTP/1.1") == (1, 1)
|
|
|
|
assert proxy.parse_http_protocol("HTTP/0.0") == (0, 0)
|
|
|
|
assert not proxy.parse_http_protocol("foo/0.0")
|
|
|
|
|
|
|
|
|
|
|
|
class u_parse_init_connect(libpry.AutoTree):
|
|
|
|
def test_simple(self):
|
|
|
|
assert proxy.parse_init_connect("CONNECT host.com:443 HTTP/1.0")
|
|
|
|
assert not proxy.parse_init_connect("bogus")
|
|
|
|
assert not proxy.parse_init_connect("GET host.com:443 HTTP/1.0")
|
|
|
|
assert not proxy.parse_init_connect("CONNECT host.com443 HTTP/1.0")
|
|
|
|
assert not proxy.parse_init_connect("CONNECT host.com:443 foo/1.0")
|
|
|
|
|
|
|
|
|
|
|
|
class u_parse_init_proxy(libpry.AutoTree):
|
|
|
|
def test_simple(self):
|
|
|
|
u = "GET http://foo.com:8888/test HTTP/1.1"
|
|
|
|
m, s, h, po, pa, major, minor = proxy.parse_init_proxy(u)
|
|
|
|
assert m == "GET"
|
|
|
|
assert s == "http"
|
|
|
|
assert h == "foo.com"
|
|
|
|
assert po == 8888
|
|
|
|
assert pa == "/test"
|
|
|
|
assert major == 1
|
|
|
|
assert minor == 1
|
|
|
|
|
|
|
|
assert not proxy.parse_init_proxy("invalid")
|
|
|
|
assert not proxy.parse_init_proxy("GET invalid HTTP/1.1")
|
|
|
|
assert not proxy.parse_init_proxy("GET http://foo.com:8888/test foo/1.1")
|
|
|
|
|
|
|
|
|
|
|
|
class u_parse_init_http(libpry.AutoTree):
|
|
|
|
def test_simple(self):
|
|
|
|
u = "GET /test HTTP/1.1"
|
|
|
|
m, u, major, minor = proxy.parse_init_http(u)
|
|
|
|
assert m == "GET"
|
|
|
|
assert u == "/test"
|
|
|
|
assert major == 1
|
|
|
|
assert minor == 1
|
|
|
|
|
|
|
|
assert not proxy.parse_init_http("invalid")
|
|
|
|
assert not proxy.parse_init_http("GET invalid HTTP/1.1")
|
|
|
|
assert not proxy.parse_init_http("GET /test foo/1.1")
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-02-16 04:09:07 +00:00
|
|
|
tests = [
|
2012-06-03 13:04:57 +00:00
|
|
|
u_parse_http_protocol(),
|
|
|
|
u_parse_init_connect(),
|
|
|
|
u_parse_init_proxy(),
|
|
|
|
u_parse_init_http(),
|
2010-02-16 04:09:07 +00:00
|
|
|
uProxyError(),
|
|
|
|
uFileLike(),
|
2011-02-10 22:06:30 +00:00
|
|
|
u_parse_request_line(),
|
2011-03-12 03:00:01 +00:00
|
|
|
u_read_chunked(),
|
2011-09-09 02:49:34 +00:00
|
|
|
u_read_http_body(),
|
2012-02-19 21:34:32 +00:00
|
|
|
u_read_headers()
|
2010-02-16 04:09:07 +00:00
|
|
|
]
|