Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_init(self):
with pytest.raises(UnicodeEncodeError):
treq(method="fööbär")
with pytest.raises(UnicodeEncodeError):
treq(scheme="fööbär")
assert treq(host="fööbär").host == "fööbär"
with pytest.raises(UnicodeEncodeError):
treq(path="/fööbär")
with pytest.raises(UnicodeEncodeError):
treq(http_version="föö/bä.r")
with pytest.raises(ValueError):
treq(headers="foobar")
with pytest.raises(ValueError):
treq(content="foobar")
assert isinstance(treq(headers=()).headers, Headers)
def test_assemble_request_head():
c = assemble_request_head(treq(content=b"foo"))
assert b"GET" in c
assert b"qvalue" in c
assert b"content-length" in c
assert b"foo" not in c
def test_get_host_header(self):
no_hdr = treq()
assert no_hdr.host_header is None
h1 = treq(headers=(
(b"host", b"example.com"),
))
assert h1.host_header == "example.com"
h2 = treq(headers=(
(b":authority", b"example.org"),
))
assert h2.host_header == "example.org"
both_hdrs = treq(headers=(
(b"host", b"example.org"),
(b":authority", b"example.com"),
))
assert both_hdrs.host_header == "example.com"
def test_assemble_request_line():
assert _assemble_request_line(treq().data) == b"GET /path HTTP/1.1"
authority_request = treq(method=b"CONNECT", first_line_format="authority").data
assert _assemble_request_line(authority_request) == b"CONNECT address:22 HTTP/1.1"
absolute_request = treq(first_line_format="absolute").data
assert _assemble_request_line(absolute_request) == b"GET http://address:22/path HTTP/1.1"
with pytest.raises(RuntimeError):
_assemble_request_line(treq(first_line_format="invalid_form").data)
def test_get_message_content_view():
r = tutils.treq()
desc, lines, err = cv.get_message_content_view("raw", r)
assert desc == "Raw"
r.encode("gzip")
desc, lines, err = cv.get_message_content_view("raw", r)
assert desc == "[decoded gzip] Raw"
r.headers["content-encoding"] = "deflate"
desc, lines, err = cv.get_message_content_view("raw", r)
assert desc == "[cannot decode] Raw"
r.content = None
desc, lines, err = cv.get_message_content_view("raw", r)
assert list(lines) == [[("error", "content missing")]]
def test_url(self):
request = treq()
assert request.url == "http://address:22/path"
request.url = "https://otheraddress:42/foo"
assert request.scheme == "https"
assert request.host == "otheraddress"
assert request.port == 42
assert request.path == "/foo"
with pytest.raises(ValueError):
request.url = "not-a-url"
assert "host" not in h1.headers
assert ":authority" not in h1.headers
h1.host_header = "example.com"
assert "host" in h1.headers
assert ":authority" not in h1.headers
h1.host_header = None
assert "host" not in h1.headers
h2 = treq(http_version=b"HTTP/2.0")
h2.host_header = "example.org"
assert "host" not in h2.headers
assert ":authority" in h2.headers
del h2.host_header
assert ":authority" not in h2.headers
both_hdrs = treq(headers=(
(b":authority", b"example.com"),
(b"host", b"example.org"),
))
both_hdrs.host_header = "foo.example.com"
assert both_hdrs.headers["Host"] == "foo.example.com"
assert both_hdrs.headers[":authority"] == "foo.example.com"
def test_assemble_request_headers_host_header():
r = treq()
r.headers = Headers()
c = _assemble_request_headers(r.data)
assert b"host" in c
r.host = None
c = _assemble_request_headers(r.data)
assert b"host" not in c
def test_get_host_header(self):
no_hdr = treq()
assert no_hdr.host_header is None
h1 = treq(headers=(
(b"host", b"example.com"),
))
assert h1.host_header == "example.com"
h2 = treq(headers=(
(b":authority", b"example.org"),
))
assert h2.host_header == "example.org"
both_hdrs = treq(headers=(
(b"host", b"example.org"),
(b":authority", b"example.com"),
))
def test_pretty_host(self):
request = treq()
# Without host header
assert request.pretty_host == "address"
assert request.host == "address"
# Same port as self.port (22)
request.headers["host"] = "other:22"
assert request.pretty_host == "other"
# Different ports
request.headers["host"] = "other"
assert request.pretty_host == "address"
assert request.host == "address"
# Empty host
request.host = None
assert request.pretty_host is None
assert request.host is None
# Invalid IDNA