Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions tests/client/test_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,26 @@ async def test_server_extensions(server):
response = await client.get(url)
assert response.status_code == 200
assert response.extensions["http_version"] == b"HTTP/1.1"


@pytest.mark.anyio
async def test_aclose_when_already_closed():
client = httpx.AsyncClient()
await client.aclose()

# Closing an already-closed client is a no-op.
await client.aclose()

assert client.is_closed


@pytest.mark.anyio
async def test_mounted_none_transport():
# Mounts may use `None` to fall back to the default transport for a
# given URL pattern. Entering, exiting and closing the client should
# skip over these `None` entries.
async with httpx.AsyncClient(mounts={"custom://": None}):
pass

client = httpx.AsyncClient(mounts={"custom://": None})
await client.aclose()
35 changes: 35 additions & 0 deletions tests/client/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,3 +460,38 @@ def cp1252_but_no_content_type(request):
assert response.reason_phrase == "OK"
assert response.encoding == "ISO-8859-1"
assert response.text == text


def test_build_request_with_timeout_extension():
with httpx.Client() as client:
request = client.build_request(
"GET",
"http://example.org",
extensions={"timeout": {"connect": 5.0}},
)

assert request.extensions == {"timeout": {"connect": 5.0}}


def test_close_when_already_closed():
client = httpx.Client()
client.close()

# Closing an already-closed client is a no-op.
client.close()

assert client.is_closed


def test_mounted_none_transport():
# Mounts may use `None` to fall back to the default transport for a
# given URL pattern. Entering, exiting and closing the client should
# skip over these `None` entries.
client = httpx.Client(mounts={"custom://": None})
with client:
pass

client = httpx.Client(mounts={"custom://": None})
client.close()

assert client.is_closed
12 changes: 12 additions & 0 deletions tests/models/test_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -1038,3 +1038,15 @@ def test_response_decode_text_using_explicit_encoding():
assert response.reason_phrase == "OK"
assert response.encoding == "cp1252"
assert response.text == text


def test_encoding_with_default_encoding_callable_and_unread_stream():
# For a streaming response that has not been read yet, a callable
# `default_encoding` cannot be applied, so the fallback is "utf-8".
response = httpx.Response(
200,
content=streaming_body(),
default_encoding=lambda content: "utf-16",
)

assert response.encoding == "utf-8"
41 changes: 41 additions & 0 deletions tests/test_asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,44 @@ async def test_asgi_exc_no_raise():
response = await client.get("http://www.example.org/")

assert response.status_code == 500


async def raise_exc_after_response_start(scope, receive, send):
status = 200
headers = [(b"content-type", "text/plain")]

await send({"type": "http.response.start", "status": status, "headers": headers})
raise RuntimeError()


@pytest.mark.anyio
async def test_asgi_exc_no_raise_after_response_start():
transport = httpx.ASGITransport(
app=raise_exc_after_response_start, raise_app_exceptions=False
)
async with httpx.AsyncClient(transport=transport) as client:
response = await client.get("http://www.example.org/")

assert response.status_code == 200
assert response.content == b""


@pytest.mark.anyio
async def test_asgi_ignores_debug_messages():
async def app(scope, receive, send):
await send({"type": "http.response.debug", "info": {"test": "example"}})
await send(
{
"type": "http.response.start",
"status": 200,
"headers": [(b"content-type", "text/plain")],
}
)
await send({"type": "http.response.body", "body": b"Hello, World!"})

transport = httpx.ASGITransport(app=app)
async with httpx.AsyncClient(transport=transport) as client:
response = await client.get("http://www.example.org/")

assert response.status_code == 200
assert response.text == "Hello, World!"
37 changes: 37 additions & 0 deletions tests/test_multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,3 +467,40 @@ def test_unicode_with_control_character(self):
files = {"upload": (filename, b"<file content>")}
request = httpx.Request("GET", "https://www.example.com", files=files)
assert expected in request.read()


def test_multipart_encode_filelike_without_seek() -> None:
"""
File-like objects that don't provide a `seek()` method at all are
supported, uploading with 'Transfer-Encoding: chunked'.
"""

class ReadOnlyFile:
def __init__(self, content: bytes) -> None:
self._content = content

def read(self, *args: typing.Any) -> bytes:
content, self._content = self._content, b""
return content

url = "https://www.example.com/"
headers = {"Content-Type": "multipart/form-data; boundary=BOUNDARY"}
fileobj: typing.Any = ReadOnlyFile(b"Hello, World!")
files = {"file": fileobj}

request = httpx.Request("POST", url, headers=headers, files=files)
request.read()

assert request.headers == {
"Host": "www.example.com",
"Content-Type": "multipart/form-data; boundary=BOUNDARY",
"Transfer-Encoding": "chunked",
}
assert request.content == (
b"--BOUNDARY\r\n"
b'Content-Disposition: form-data; name="file"; filename="upload"\r\n'
b"Content-Type: application/octet-stream\r\n"
b"\r\n"
b"Hello, World!\r\n"
b"--BOUNDARY--\r\n"
)
Loading