mitmproxy/test/test_proxy.py

74 lines
1.9 KiB
Python
Raw Normal View History

import cStringIO, time, re
2010-02-16 04:09:07 +00:00
import libpry
from libmproxy import proxy, controller, utils, dump
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 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)
tests = [
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(),
2010-02-16 04:09:07 +00:00
]