Skip to content

Commit

Permalink
Upgrade and pin test dependency on HTTPX (#29)
Browse files Browse the repository at this point in the history
* Upgrade and pin test dependency on HTTPX

* Lint
  • Loading branch information
florimondmanca committed Feb 8, 2020
1 parent 32ccb56 commit 8f50722
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ flake8
flake8-bugbear
flake8-comprehensions
isort
httpx
httpx==0.11.*
mypy
pytest
pytest-asyncio
Expand Down
42 changes: 27 additions & 15 deletions tests/test_trace_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,17 @@ def tracer() -> Tracer:


@pytest.fixture
def client(application: ASGIApp, tracer: Tracer) -> typing.Iterator[httpx.Client]:
async def client(
application: ASGIApp, tracer: Tracer
) -> typing.AsyncIterator[httpx.AsyncClient]:
app = TraceMiddleware(application, tracer=tracer, service="test.asgi.service")
with httpx.Client(app=app, base_url="http://testserver") as client:
async with httpx.AsyncClient(app=app, base_url="http://testserver") as client:
yield client


def test_app(client: httpx.Client, tracer: DummyTracer) -> None:
r = client.get("/example")
@pytest.mark.asyncio
async def test_app(client: httpx.AsyncClient, tracer: DummyTracer) -> None:
r = await client.get("/example")
assert r.status_code == 200
assert r.text == "Hello, world!"

Expand Down Expand Up @@ -85,9 +88,10 @@ async def invalid(scope: Scope, receive: Receive, send: Send) -> None:
await app(mock_http_scope, mock_receive, mock_send)


def test_child(client: httpx.Client, tracer: Tracer) -> None:
@pytest.mark.asyncio
async def test_child(client: httpx.AsyncClient, tracer: Tracer) -> None:
start = time.time()
r = client.get("/child")
r = await client.get("/child")
end = time.time()
assert r.status_code == 200
assert r.text == "Hello, child!"
Expand Down Expand Up @@ -172,9 +176,12 @@ def trace_query_string() -> typing.Iterator[None]:
yield


@pytest.mark.asyncio
@pytest.mark.usefixtures("trace_query_string")
def test_trace_query_string(client: httpx.Client, tracer: DummyTracer) -> None:
r = client.get("/example", params={"foo": "bar"})
async def test_trace_query_string(
client: httpx.AsyncClient, tracer: DummyTracer
) -> None:
r = await client.get("/example", params={"foo": "bar"})
assert r.status_code == 200
assert r.text == "Hello, world!"

Expand All @@ -186,10 +193,11 @@ def test_trace_query_string(client: httpx.Client, tracer: DummyTracer) -> None:
assert span.get_tag(http_ext.QUERY_STRING) == "foo=bar"


def test_app_exception(client: httpx.Client, tracer: DummyTracer) -> None:
@pytest.mark.asyncio
async def test_app_exception(client: httpx.AsyncClient, tracer: DummyTracer) -> None:
with pytest.raises(RuntimeError):
start = time.time()
client.get("/exception")
await client.get("/exception")
end = time.time()

# Ensure any open span was closed.
Expand All @@ -210,12 +218,15 @@ def test_app_exception(client: httpx.Client, tracer: DummyTracer) -> None:
assert span.get_tag(http_ext.METHOD) == "GET"


def test_distributed_tracing(client: httpx.Client, tracer: DummyTracer) -> None:
@pytest.mark.asyncio
async def test_distributed_tracing(
client: httpx.AsyncClient, tracer: DummyTracer
) -> None:
headers = {
http_propagation.HTTP_HEADER_TRACE_ID: "1234",
http_propagation.HTTP_HEADER_PARENT_ID: "5678",
}
r = client.get("/example", headers=headers)
r = await client.get("/example", headers=headers)
assert r.status_code == 200
assert r.text == "Hello, world!"

Expand All @@ -228,6 +239,7 @@ def test_distributed_tracing(client: httpx.Client, tracer: DummyTracer) -> None:
assert span.parent_id == 5678


@pytest.mark.asyncio
@pytest.mark.parametrize(
"tags, expected_tags",
[
Expand All @@ -242,7 +254,7 @@ def test_distributed_tracing(client: httpx.Client, tracer: DummyTracer) -> None:
("env-testing", ValueError),
],
)
def test_tags(
async def test_tags(
application: ASGIApp,
tracer: DummyTracer,
tags: typing.Union[str, dict],
Expand All @@ -259,8 +271,8 @@ def test_tags(
application, tracer=tracer, service="test.asgi.service", tags=tags,
)

with httpx.Client(app=app, base_url="http://testserver") as client:
r = client.get("/example")
async with httpx.AsyncClient(app=app, base_url="http://testserver") as client:
r = await client.get("/example")
assert r.status_code == 200
assert r.text == "Hello, world!"

Expand Down

0 comments on commit 8f50722

Please sign in to comment.