httpx/tests/models/test_requests.py
2020-04-08 13:15:22 +01:00

121 lines
3.8 KiB
Python
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import pytest
import httpx
def test_request_repr():
request = httpx.Request("GET", "http://example.org")
assert repr(request) == "<Request('GET', 'http://example.org')>"
def test_no_content():
request = httpx.Request("GET", "http://example.org")
assert "Content-Length" not in request.headers
def test_content_length_header():
request = httpx.Request("POST", "http://example.org", data=b"test 123")
assert request.headers["Content-Length"] == "8"
def test_url_encoded_data():
request = httpx.Request("POST", "http://example.org", data={"test": "123"})
request.read()
assert request.headers["Content-Type"] == "application/x-www-form-urlencoded"
assert request.content == b"test=123"
def test_json_encoded_data():
request = httpx.Request("POST", "http://example.org", json={"test": 123})
request.read()
assert request.headers["Content-Type"] == "application/json"
assert request.content == b'{"test": 123}'
def test_read_and_stream_data():
# Ensure a request may still be streamed if it has been read.
# Needed for cases such as authentication classes that read the request body.
request = httpx.Request("POST", "http://example.org", json={"test": 123})
request.read()
content = b"".join([part for part in request.stream])
assert content == request.content
@pytest.mark.asyncio
async def test_aread_and_stream_data():
# Ensure a request may still be streamed if it has been read.
# Needed for cases such as authentication classes that read the request body.
request = httpx.Request("POST", "http://example.org", json={"test": 123})
await request.aread()
content = b"".join([part async for part in request.stream])
assert content == request.content
@pytest.mark.asyncio
async def test_cannot_access_content_without_read():
# Ensure a request may still be streamed if it has been read.
#  Needed for cases such as authentication classes that read the request body.
request = httpx.Request("POST", "http://example.org", json={"test": 123})
with pytest.raises(httpx.RequestNotRead):
request.content
def test_transfer_encoding_header():
async def streaming_body(data):
yield data # pragma: nocover
data = streaming_body(b"test 123")
request = httpx.Request("POST", "http://example.org", data=data)
assert "Content-Length" not in request.headers
assert request.headers["Transfer-Encoding"] == "chunked"
def test_override_host_header():
headers = {"host": "1.2.3.4:80"}
request = httpx.Request("GET", "http://example.org", headers=headers)
assert request.headers["Host"] == "1.2.3.4:80"
def test_override_accept_encoding_header():
headers = {"Accept-Encoding": "identity"}
request = httpx.Request("GET", "http://example.org", headers=headers)
assert request.headers["Accept-Encoding"] == "identity"
def test_override_content_length_header():
async def streaming_body(data):
yield data # pragma: nocover
data = streaming_body(b"test 123")
headers = {"Content-Length": "8"}
request = httpx.Request("POST", "http://example.org", data=data, headers=headers)
assert request.headers["Content-Length"] == "8"
def test_url():
url = "http://example.org"
request = httpx.Request("GET", url)
assert request.url.scheme == "http"
assert request.url.port == 80
assert request.url.full_path == "/"
url = "https://example.org/abc?foo=bar"
request = httpx.Request("GET", url)
assert request.url.scheme == "https"
assert request.url.port == 443
assert request.url.full_path == "/abc?foo=bar"
def test_invalid_urls():
with pytest.raises(httpx.InvalidURL):
httpx.Request("GET", "example.org")
with pytest.raises(httpx.InvalidURL):
httpx.Request("GET", "http:///foo")