mitmproxy/test/http/test_models.py

95 lines
3.2 KiB
Python
Raw Normal View History

2015-07-14 21:02:14 +00:00
2015-08-10 18:36:47 +00:00
from netlib import tutils
2015-09-15 22:04:23 +00:00
from netlib.odict import ODict, ODictCaseless
2015-09-26 18:07:11 +00:00
from netlib.http import Response, Headers, CONTENT_MISSING
2015-07-14 21:02:14 +00:00
2015-08-05 19:32:53 +00:00
class TestResponse(object):
2015-09-05 16:15:47 +00:00
def test_headers(self):
2015-09-15 22:04:23 +00:00
tutils.raises(AssertionError, Response,
2015-09-16 16:43:24 +00:00
b"HTTP/1.1",
2015-08-05 19:32:53 +00:00
200,
headers='foobar',
)
2015-08-01 08:39:14 +00:00
2015-09-15 22:04:23 +00:00
resp = Response(
2015-09-16 16:43:24 +00:00
b"HTTP/1.1",
2015-08-05 19:32:53 +00:00
200,
)
2015-09-15 22:04:23 +00:00
assert isinstance(resp.headers, Headers)
2015-08-01 08:39:14 +00:00
2015-08-05 19:32:53 +00:00
def test_equal(self):
2015-09-17 14:31:50 +00:00
a = tutils.tresp(timestamp_start=42, timestamp_end=43)
b = tutils.tresp(timestamp_start=42, timestamp_end=43)
2015-08-05 19:32:53 +00:00
assert a == b
2015-08-01 08:39:14 +00:00
2015-08-05 19:32:53 +00:00
assert not a == 'foo'
assert not b == 'foo'
assert not 'foo' == a
assert not 'foo' == b
2015-08-01 08:39:14 +00:00
def test_repr(self):
r = tutils.tresp()
assert "unknown content type" in repr(r)
2015-09-05 16:15:47 +00:00
r.headers["content-type"] = "foo"
2015-08-01 08:39:14 +00:00
assert "foo" in repr(r)
2015-09-26 15:39:50 +00:00
assert repr(tutils.tresp(content=CONTENT_MISSING))
2015-08-01 08:39:14 +00:00
def test_get_cookies_none(self):
resp = tutils.tresp()
2015-09-15 22:04:23 +00:00
resp.headers = Headers()
2015-08-01 08:39:14 +00:00
assert not resp.get_cookies()
def test_get_cookies_simple(self):
resp = tutils.tresp()
2015-09-15 22:04:23 +00:00
resp.headers = Headers(set_cookie="cookiename=cookievalue")
2015-08-01 08:39:14 +00:00
result = resp.get_cookies()
assert len(result) == 1
assert "cookiename" in result
2015-09-15 22:04:23 +00:00
assert result["cookiename"][0] == ["cookievalue", ODict()]
2015-08-01 08:39:14 +00:00
def test_get_cookies_with_parameters(self):
resp = tutils.tresp()
2015-09-15 22:04:23 +00:00
resp.headers = Headers(set_cookie="cookiename=cookievalue;domain=example.com;expires=Wed Oct 21 16:29:41 2015;path=/; HttpOnly")
2015-08-01 08:39:14 +00:00
result = resp.get_cookies()
assert len(result) == 1
assert "cookiename" in result
assert result["cookiename"][0][0] == "cookievalue"
attrs = result["cookiename"][0][1]
assert len(attrs) == 4
assert attrs["domain"] == ["example.com"]
assert attrs["expires"] == ["Wed Oct 21 16:29:41 2015"]
assert attrs["path"] == ["/"]
assert attrs["httponly"] == [None]
def test_get_cookies_no_value(self):
resp = tutils.tresp()
2015-09-15 22:04:23 +00:00
resp.headers = Headers(set_cookie="cookiename=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/")
2015-08-01 08:39:14 +00:00
result = resp.get_cookies()
assert len(result) == 1
assert "cookiename" in result
assert result["cookiename"][0][0] == ""
assert len(result["cookiename"][0][1]) == 2
def test_get_cookies_twocookies(self):
resp = tutils.tresp()
2015-09-15 22:04:23 +00:00
resp.headers = Headers([
2015-09-20 22:44:17 +00:00
[b"Set-Cookie", b"cookiename=cookievalue"],
[b"Set-Cookie", b"othercookie=othervalue"]
2015-09-05 16:15:47 +00:00
])
2015-08-01 08:39:14 +00:00
result = resp.get_cookies()
assert len(result) == 2
assert "cookiename" in result
2015-09-15 22:04:23 +00:00
assert result["cookiename"][0] == ["cookievalue", ODict()]
2015-08-01 08:39:14 +00:00
assert "othercookie" in result
2015-09-15 22:04:23 +00:00
assert result["othercookie"][0] == ["othervalue", ODict()]
2015-08-01 08:39:14 +00:00
def test_set_cookies(self):
resp = tutils.tresp()
v = resp.get_cookies()
2015-09-15 22:04:23 +00:00
v.add("foo", ["bar", ODictCaseless()])
2015-08-01 08:39:14 +00:00
resp.set_cookies(v)
v = resp.get_cookies()
assert len(v) == 1
2015-09-15 22:04:23 +00:00
assert v["foo"] == [["bar", ODictCaseless()]]