2016-07-16 05:50:33 +00:00
|
|
|
from netlib.http.headers import Headers, parse_content_type, assemble_content_type
|
2015-09-21 23:48:35 +00:00
|
|
|
from netlib.tutils import raises
|
|
|
|
|
|
|
|
|
|
|
|
class TestHeaders(object):
|
|
|
|
def _2host(self):
|
|
|
|
return Headers(
|
2016-05-19 01:46:42 +00:00
|
|
|
(
|
|
|
|
(b"Host", b"example.com"),
|
|
|
|
(b"host", b"example.org")
|
|
|
|
)
|
2015-09-21 23:48:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_init(self):
|
|
|
|
headers = Headers()
|
|
|
|
assert len(headers) == 0
|
|
|
|
|
|
|
|
headers = Headers([[b"Host", b"example.com"]])
|
|
|
|
assert len(headers) == 1
|
|
|
|
assert headers["Host"] == "example.com"
|
|
|
|
|
|
|
|
headers = Headers(Host="example.com")
|
|
|
|
assert len(headers) == 1
|
|
|
|
assert headers["Host"] == "example.com"
|
|
|
|
|
|
|
|
headers = Headers(
|
|
|
|
[[b"Host", b"invalid"]],
|
|
|
|
Host="example.com"
|
|
|
|
)
|
|
|
|
assert len(headers) == 1
|
|
|
|
assert headers["Host"] == "example.com"
|
|
|
|
|
|
|
|
headers = Headers(
|
|
|
|
[[b"Host", b"invalid"], [b"Accept", b"text/plain"]],
|
|
|
|
Host="example.com"
|
|
|
|
)
|
|
|
|
assert len(headers) == 2
|
|
|
|
assert headers["Host"] == "example.com"
|
|
|
|
assert headers["Accept"] == "text/plain"
|
|
|
|
|
2016-05-19 01:46:42 +00:00
|
|
|
with raises(TypeError):
|
2015-09-26 18:07:11 +00:00
|
|
|
Headers([[b"Host", u"not-bytes"]])
|
|
|
|
|
2016-05-20 18:04:27 +00:00
|
|
|
def test_bytes(self):
|
2015-09-21 23:48:35 +00:00
|
|
|
headers = Headers(Host="example.com")
|
|
|
|
assert bytes(headers) == b"Host: example.com\r\n"
|
|
|
|
|
|
|
|
headers = Headers([
|
|
|
|
[b"Host", b"example.com"],
|
|
|
|
[b"Accept", b"text/plain"]
|
|
|
|
])
|
|
|
|
assert bytes(headers) == b"Host: example.com\r\nAccept: text/plain\r\n"
|
|
|
|
|
|
|
|
headers = Headers()
|
|
|
|
assert bytes(headers) == b""
|
|
|
|
|
2016-04-02 12:38:33 +00:00
|
|
|
def test_replace_simple(self):
|
|
|
|
headers = Headers(Host="example.com", Accept="text/plain")
|
|
|
|
replacements = headers.replace("Host: ", "X-Host: ")
|
|
|
|
assert replacements == 1
|
|
|
|
assert headers["X-Host"] == "example.com"
|
|
|
|
assert "Host" not in headers
|
|
|
|
assert headers["Accept"] == "text/plain"
|
|
|
|
|
|
|
|
def test_replace_multi(self):
|
|
|
|
headers = self._2host()
|
|
|
|
headers.replace(r"Host: example\.com", r"Host: example.de")
|
|
|
|
assert headers.get_all("Host") == ["example.de", "example.org"]
|
|
|
|
|
|
|
|
def test_replace_remove_spacer(self):
|
|
|
|
headers = Headers(Host="example.com")
|
|
|
|
replacements = headers.replace(r"Host: ", "X-Host ")
|
|
|
|
assert replacements == 0
|
|
|
|
assert headers["Host"] == "example.com"
|
2016-05-31 07:07:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_parse_content_type():
|
|
|
|
p = parse_content_type
|
|
|
|
assert p("text/html") == ("text", "html", {})
|
|
|
|
assert p("text") is None
|
|
|
|
|
|
|
|
v = p("text/html; charset=UTF-8")
|
|
|
|
assert v == ('text', 'html', {'charset': 'UTF-8'})
|
2016-07-16 05:50:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_assemble_content_type():
|
|
|
|
p = assemble_content_type
|
|
|
|
assert p("text", "html", {}) == "text/html"
|
|
|
|
assert p("text", "html", {"charset": "utf8"}) == "text/html; charset=utf8"
|
|
|
|
assert p("text", "html", {"charset": "utf8", "foo": "bar"}) == "text/html; charset=utf8; foo=bar"
|