2015-09-25 22:39:04 +00:00
|
|
|
from __future__ import absolute_import, print_function, division
|
|
|
|
|
2016-04-02 20:49:05 +00:00
|
|
|
import email
|
|
|
|
|
|
|
|
import time
|
2016-02-08 03:28:49 +00:00
|
|
|
|
2015-09-26 22:49:41 +00:00
|
|
|
from netlib.http import Headers
|
2016-08-23 07:17:06 +00:00
|
|
|
from netlib.http import Response
|
2016-05-19 05:50:19 +00:00
|
|
|
from netlib.http.cookies import CookieAttrs
|
2015-09-26 22:49:41 +00:00
|
|
|
from netlib.tutils import raises, tresp
|
|
|
|
from .test_message import _test_passthrough_attr, _test_decoded_attr
|
|
|
|
|
|
|
|
|
2016-10-17 04:11:21 +00:00
|
|
|
class TestResponseData():
|
2015-09-26 22:49:41 +00:00
|
|
|
def test_init(self):
|
2016-05-19 01:46:42 +00:00
|
|
|
with raises(ValueError):
|
2015-09-26 22:49:41 +00:00
|
|
|
tresp(headers="foobar")
|
|
|
|
|
2016-05-29 02:31:43 +00:00
|
|
|
assert isinstance(tresp(headers=()).headers, Headers)
|
2015-09-26 22:49:41 +00:00
|
|
|
|
|
|
|
|
2016-10-17 04:11:21 +00:00
|
|
|
class TestResponseCore():
|
2015-09-26 22:49:41 +00:00
|
|
|
"""
|
|
|
|
Tests for builtins and the attributes that are directly proxied from the data structure
|
|
|
|
"""
|
|
|
|
def test_repr(self):
|
|
|
|
response = tresp()
|
2016-05-31 08:19:54 +00:00
|
|
|
assert repr(response) == "Response(200 OK, unknown content type, 7b)"
|
2015-09-26 22:49:41 +00:00
|
|
|
response.content = None
|
|
|
|
assert repr(response) == "Response(200 OK, no content)"
|
|
|
|
|
2016-08-23 07:17:06 +00:00
|
|
|
def test_make(self):
|
|
|
|
r = Response.make()
|
|
|
|
assert r.status_code == 200
|
|
|
|
assert r.content == b""
|
|
|
|
|
2016-09-22 03:34:01 +00:00
|
|
|
r = Response.make(418, "teatime")
|
|
|
|
assert r.status_code == 418
|
|
|
|
assert r.content == b"teatime"
|
|
|
|
assert r.headers["content-length"] == "7"
|
|
|
|
|
2016-08-23 07:17:06 +00:00
|
|
|
Response.make(content=b"foo")
|
|
|
|
Response.make(content="foo")
|
|
|
|
with raises(TypeError):
|
|
|
|
Response.make(content=42)
|
|
|
|
|
|
|
|
r = Response.make(headers=[(b"foo", b"bar")])
|
|
|
|
assert r.headers["foo"] == "bar"
|
|
|
|
|
|
|
|
r = Response.make(headers=({"foo": "baz"}))
|
|
|
|
assert r.headers["foo"] == "baz"
|
|
|
|
|
|
|
|
with raises(TypeError):
|
|
|
|
Response.make(headers=42)
|
|
|
|
|
2015-09-26 22:49:41 +00:00
|
|
|
def test_status_code(self):
|
|
|
|
_test_passthrough_attr(tresp(), "status_code")
|
|
|
|
|
|
|
|
def test_reason(self):
|
|
|
|
_test_decoded_attr(tresp(), "reason")
|
|
|
|
|
|
|
|
|
2016-10-17 04:11:21 +00:00
|
|
|
class TestResponseUtils():
|
2015-09-26 22:49:41 +00:00
|
|
|
"""
|
|
|
|
Tests for additional convenience methods.
|
|
|
|
"""
|
|
|
|
def test_get_cookies_none(self):
|
|
|
|
resp = tresp()
|
|
|
|
resp.headers = Headers()
|
|
|
|
assert not resp.cookies
|
|
|
|
|
|
|
|
def test_get_cookies_empty(self):
|
|
|
|
resp = tresp()
|
|
|
|
resp.headers = Headers(set_cookie="")
|
|
|
|
assert not resp.cookies
|
|
|
|
|
|
|
|
def test_get_cookies_simple(self):
|
|
|
|
resp = tresp()
|
|
|
|
resp.headers = Headers(set_cookie="cookiename=cookievalue")
|
|
|
|
result = resp.cookies
|
|
|
|
assert len(result) == 1
|
|
|
|
assert "cookiename" in result
|
2016-05-19 05:50:19 +00:00
|
|
|
assert result["cookiename"] == ("cookievalue", CookieAttrs())
|
2015-09-26 22:49:41 +00:00
|
|
|
|
|
|
|
def test_get_cookies_with_parameters(self):
|
|
|
|
resp = tresp()
|
2016-05-29 11:33:20 +00:00
|
|
|
cookie = "cookiename=cookievalue;domain=example.com;expires=Wed Oct 21 16:29:41 2015;path=/; HttpOnly"
|
|
|
|
resp.headers = Headers(set_cookie=cookie)
|
2015-09-26 22:49:41 +00:00
|
|
|
result = resp.cookies
|
|
|
|
assert len(result) == 1
|
|
|
|
assert "cookiename" in result
|
2016-05-19 05:50:19 +00:00
|
|
|
assert result["cookiename"][0] == "cookievalue"
|
|
|
|
attrs = result["cookiename"][1]
|
2015-09-26 22:49:41 +00:00
|
|
|
assert len(attrs) == 4
|
2016-05-19 05:50:19 +00:00
|
|
|
assert attrs["domain"] == "example.com"
|
|
|
|
assert attrs["expires"] == "Wed Oct 21 16:29:41 2015"
|
|
|
|
assert attrs["path"] == "/"
|
|
|
|
assert attrs["httponly"] is None
|
2015-09-26 22:49:41 +00:00
|
|
|
|
|
|
|
def test_get_cookies_no_value(self):
|
|
|
|
resp = tresp()
|
|
|
|
resp.headers = Headers(set_cookie="cookiename=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/")
|
|
|
|
result = resp.cookies
|
|
|
|
assert len(result) == 1
|
|
|
|
assert "cookiename" in result
|
2016-05-19 05:50:19 +00:00
|
|
|
assert result["cookiename"][0] == ""
|
|
|
|
assert len(result["cookiename"][1]) == 2
|
2015-09-26 22:49:41 +00:00
|
|
|
|
|
|
|
def test_get_cookies_twocookies(self):
|
|
|
|
resp = tresp()
|
|
|
|
resp.headers = Headers([
|
|
|
|
[b"Set-Cookie", b"cookiename=cookievalue"],
|
|
|
|
[b"Set-Cookie", b"othercookie=othervalue"]
|
|
|
|
])
|
|
|
|
result = resp.cookies
|
|
|
|
assert len(result) == 2
|
|
|
|
assert "cookiename" in result
|
2016-05-19 05:50:19 +00:00
|
|
|
assert result["cookiename"] == ("cookievalue", CookieAttrs())
|
2015-09-26 22:49:41 +00:00
|
|
|
assert "othercookie" in result
|
2016-05-19 05:50:19 +00:00
|
|
|
assert result["othercookie"] == ("othervalue", CookieAttrs())
|
2015-09-26 22:49:41 +00:00
|
|
|
|
|
|
|
def test_set_cookies(self):
|
|
|
|
resp = tresp()
|
2016-05-19 05:50:19 +00:00
|
|
|
resp.cookies["foo"] = ("bar", {})
|
2015-09-26 22:49:41 +00:00
|
|
|
|
2016-05-19 05:50:19 +00:00
|
|
|
assert len(resp.cookies) == 1
|
|
|
|
assert resp.cookies["foo"] == ("bar", CookieAttrs())
|
2016-04-02 20:49:05 +00:00
|
|
|
|
|
|
|
def test_refresh(self):
|
|
|
|
r = 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"])
|
|
|
|
d = email.utils.mktime_tz(d)
|
|
|
|
# Weird that this is not exact...
|
|
|
|
assert abs(60 - (d - n)) <= 1
|
|
|
|
|
|
|
|
cookie = "MOO=BAR; Expires=Tue, 08-Mar-2011 00:20:38 GMT; Path=foo.com; Secure"
|
|
|
|
r.headers["set-cookie"] = cookie
|
|
|
|
r.refresh()
|
|
|
|
# Cookie refreshing is tested in test_cookies, we just make sure that it's triggered here.
|
|
|
|
assert cookie != r.headers["set-cookie"]
|