Skip to content

Add server.url property in tests #288

Closed

Description

@florimondmanca

We are defining the URL to the uvicorn server that's running during our tests in lots of place, e.g.:

# tests/client/test_async_client.py

async def test_get(server, backend):
    url = "http://127.0.0.1:8000/"
    async with httpx.AsyncClient(backend=backend) as client:
        response = await client.get(url)
    # ...

async def test_raise_for_status(server, backend):
    async with httpx.AsyncClient(backend=backend) as client:
        for status_code in (200, 400, 404, 500, 505):
            response = await client.request(
                "GET", f"http://127.0.0.1:8000/status/{status_code}"
            )
        # ...

As suggested by @sethmlarson in #276 (comment), it would be better to use a URL property on the server provided by the server and https_server fixtures instead. This way, we can change the root URL in one place (close to wherre the server is actually defined), while being able to build child URLs using server.url.copy_with(path="/echo").

This can probably be done by creating and using a uvicorn Server subclass with the URL instance set as a class attribute.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions