mitmproxy/test/test_http.py

164 lines
4.7 KiB
Python
Raw Normal View History

2012-06-18 21:42:32 +00:00
import cStringIO, textwrap
2012-06-23 01:56:17 +00:00
from netlib import http, odict
2012-06-18 21:42:32 +00:00
import tutils
def test_has_chunked_encoding():
h = odict.ODictCaseless()
2012-06-23 01:56:17 +00:00
assert not http.has_chunked_encoding(h)
2012-06-18 21:42:32 +00:00
h["transfer-encoding"] = ["chunked"]
2012-06-23 01:56:17 +00:00
assert http.has_chunked_encoding(h)
2012-06-18 21:42:32 +00:00
def test_read_chunked():
s = cStringIO.StringIO("1\r\na\r\n0\r\n")
2012-06-23 01:56:17 +00:00
tutils.raises(IOError, http.read_chunked, s, None)
2012-06-18 21:42:32 +00:00
s = cStringIO.StringIO("1\r\na\r\n0\r\n\r\n")
2012-06-23 01:56:17 +00:00
assert http.read_chunked(s, None) == "a"
2012-06-18 21:42:32 +00:00
s = cStringIO.StringIO("\r\n")
2012-06-23 01:56:17 +00:00
tutils.raises(IOError, http.read_chunked, s, None)
2012-06-18 21:42:32 +00:00
s = cStringIO.StringIO("1\r\nfoo")
2012-06-23 01:56:17 +00:00
tutils.raises(IOError, http.read_chunked, s, None)
2012-06-18 21:42:32 +00:00
s = cStringIO.StringIO("foo\r\nfoo")
2012-06-23 01:56:17 +00:00
tutils.raises(http.HttpError, http.read_chunked, s, None)
2012-06-18 21:42:32 +00:00
def test_request_connection_close():
h = odict.ODictCaseless()
2012-06-23 01:56:17 +00:00
assert http.request_connection_close((1, 0), h)
assert not http.request_connection_close((1, 1), h)
2012-06-18 21:42:32 +00:00
h["connection"] = ["keep-alive"]
2012-06-23 01:56:17 +00:00
assert not http.request_connection_close((1, 1), h)
2012-06-18 21:42:32 +00:00
def test_read_http_body():
h = odict.ODict()
s = cStringIO.StringIO("testing")
2012-06-23 01:56:17 +00:00
assert http.read_http_body(s, h, False, None) == ""
2012-06-18 21:42:32 +00:00
h["content-length"] = ["foo"]
s = cStringIO.StringIO("testing")
2012-06-23 01:56:17 +00:00
tutils.raises(http.HttpError, http.read_http_body, s, h, False, None)
2012-06-18 21:42:32 +00:00
h["content-length"] = [5]
s = cStringIO.StringIO("testing")
2012-06-23 01:56:17 +00:00
assert len(http.read_http_body(s, h, False, None)) == 5
2012-06-18 21:42:32 +00:00
s = cStringIO.StringIO("testing")
2012-06-23 01:56:17 +00:00
tutils.raises(http.HttpError, http.read_http_body, s, h, False, 4)
2012-06-18 21:42:32 +00:00
h = odict.ODict()
s = cStringIO.StringIO("testing")
2012-06-23 01:56:17 +00:00
assert len(http.read_http_body(s, h, True, 4)) == 4
2012-06-18 21:42:32 +00:00
s = cStringIO.StringIO("testing")
2012-06-23 01:56:17 +00:00
assert len(http.read_http_body(s, h, True, 100)) == 7
2012-06-18 21:42:32 +00:00
def test_parse_http_protocol():
2012-06-23 01:56:17 +00:00
assert http.parse_http_protocol("HTTP/1.1") == (1, 1)
assert http.parse_http_protocol("HTTP/0.0") == (0, 0)
assert not http.parse_http_protocol("foo/0.0")
2012-06-18 21:42:32 +00:00
def test_parse_init_connect():
2012-06-23 01:56:17 +00:00
assert http.parse_init_connect("CONNECT host.com:443 HTTP/1.0")
assert not http.parse_init_connect("bogus")
assert not http.parse_init_connect("GET host.com:443 HTTP/1.0")
assert not http.parse_init_connect("CONNECT host.com443 HTTP/1.0")
assert not http.parse_init_connect("CONNECT host.com:443 foo/1.0")
2012-06-18 21:42:32 +00:00
def test_prase_init_proxy():
u = "GET http://foo.com:8888/test HTTP/1.1"
2012-06-23 01:56:17 +00:00
m, s, h, po, pa, httpversion = http.parse_init_proxy(u)
2012-06-18 21:42:32 +00:00
assert m == "GET"
assert s == "http"
assert h == "foo.com"
assert po == 8888
assert pa == "/test"
assert httpversion == (1, 1)
2012-06-23 01:56:17 +00:00
assert not http.parse_init_proxy("invalid")
assert not http.parse_init_proxy("GET invalid HTTP/1.1")
assert not http.parse_init_proxy("GET http://foo.com:8888/test foo/1.1")
2012-06-18 21:42:32 +00:00
def test_parse_init_http():
u = "GET /test HTTP/1.1"
2012-06-23 01:56:17 +00:00
m, u, httpversion= http.parse_init_http(u)
2012-06-18 21:42:32 +00:00
assert m == "GET"
assert u == "/test"
assert httpversion == (1, 1)
2012-06-23 01:56:17 +00:00
assert not http.parse_init_http("invalid")
assert not http.parse_init_http("GET invalid HTTP/1.1")
assert not http.parse_init_http("GET /test foo/1.1")
2012-06-18 21:42:32 +00:00
class TestReadHeaders:
def test_read_simple(self):
data = """
Header: one
Header2: two
\r\n
"""
data = textwrap.dedent(data)
data = data.strip()
s = cStringIO.StringIO(data)
2012-06-23 01:56:17 +00:00
h = http.read_headers(s)
2012-06-18 21:42:32 +00:00
assert h == [["Header", "one"], ["Header2", "two"]]
def test_read_multi(self):
data = """
Header: one
Header: two
\r\n
"""
data = textwrap.dedent(data)
data = data.strip()
s = cStringIO.StringIO(data)
2012-06-23 01:56:17 +00:00
h = http.read_headers(s)
2012-06-18 21:42:32 +00:00
assert h == [["Header", "one"], ["Header", "two"]]
def test_read_continued(self):
data = """
Header: one
\ttwo
Header2: three
\r\n
"""
data = textwrap.dedent(data)
data = data.strip()
s = cStringIO.StringIO(data)
2012-06-23 01:56:17 +00:00
h = http.read_headers(s)
2012-06-18 21:42:32 +00:00
assert h == [["Header", "one\r\n two"], ["Header2", "three"]]
def test_parse_url():
2012-06-23 01:56:17 +00:00
assert not http.parse_url("")
2012-06-18 21:42:32 +00:00
u = "http://foo.com:8888/test"
2012-06-23 01:56:17 +00:00
s, h, po, pa = http.parse_url(u)
2012-06-18 21:42:32 +00:00
assert s == "http"
assert h == "foo.com"
assert po == 8888
assert pa == "/test"
2012-06-23 01:56:17 +00:00
s, h, po, pa = http.parse_url("http://foo/bar")
2012-06-18 21:42:32 +00:00
assert s == "http"
assert h == "foo"
assert po == 80
assert pa == "/bar"
2012-06-23 01:56:17 +00:00
s, h, po, pa = http.parse_url("http://foo")
2012-06-18 21:42:32 +00:00
assert pa == "/"
2012-06-23 01:56:17 +00:00
s, h, po, pa = http.parse_url("https://foo")
2012-06-18 21:42:32 +00:00
assert po == 443
2012-06-23 01:56:17 +00:00
assert not http.parse_url("https://foo:bar")
assert not http.parse_url("https://foo:")
2012-06-18 21:42:32 +00:00