Skip to content
Merged
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
2 changes: 1 addition & 1 deletion pact-python-cli/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ requires-python = ">=3.10"
[dependency-groups]
# Linting and formatting tools use a more narrow specification to ensure
# developper consistency. All other dependencies are as above.
dev = ["ruff==0.16.7", { include-group = "test" }, { include-group = "types" }]
dev = ["ruff==0.16.8", { include-group = "test" }, { include-group = "types" }]
test = ["pytest~=9.0", "pytest-cov~=7.0"]
types = ["mypy==2.3.1"]

Expand Down
2 changes: 1 addition & 1 deletion pact-python-ffi/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ dependencies = ["cffi~=2.0"]
"Repository" = "https://github.com/pact-foundation/pact-python"

[dependency-groups]
dev = ["ruff==0.16.7", { include-group = "test" }, { include-group = "types" }]
dev = ["ruff==0.16.8", { include-group = "test" }, { include-group = "types" }]
test = ["pytest~=9.0", "pytest-cov~=7.0"]
types = ["mypy==2.3.1", "typing-extensions~=4.0"]

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ dependencies = [
# Linting and formatting tools use a more narrow specification to ensure
# developper consistency. All other dependencies are as above.
dev = [
"ruff==0.16.7",
"ruff==0.16.8",
{ include-group = "docs" },
{ include-group = "example" },
{ include-group = "example-v2" },
Expand Down
180 changes: 107 additions & 73 deletions tests/interaction/test_http_interaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,11 @@ async def test_basic_response_status(pact: Pact, status: int) -> None:
.will_respond_with(status)
)
with pact.serve() as srv:
async with aiohttp.ClientSession(srv.url) as session:
async with session.request("GET", "/") as resp:
assert resp.status == status
async with (
aiohttp.ClientSession(srv.url) as session,
session.request("GET", "/") as resp,
):
assert resp.status == status


@pytest.mark.parametrize(
Expand All @@ -114,9 +116,11 @@ async def test_with_header_request(
.will_respond_with(200)
)
with pact.serve() as srv:
async with aiohttp.ClientSession(srv.url) as session:
async with session.request("GET", "/", headers=headers) as resp:
assert resp.status == 200
async with (
aiohttp.ClientSession(srv.url) as session,
session.request("GET", "/", headers=headers) as resp,
):
assert resp.status == 200


@pytest.mark.parametrize(
Expand All @@ -140,12 +144,14 @@ async def test_with_header_response(
.with_headers(headers)
)
with pact.serve() as srv:
async with aiohttp.ClientSession(srv.url) as session:
async with session.request("GET", "/") as resp:
assert resp.status == 200
response_headers = [(h.lower(), v) for h, v in resp.headers.items()]
for header, value in headers:
assert (header.lower(), value) in response_headers
async with (
aiohttp.ClientSession(srv.url) as session,
session.request("GET", "/") as resp,
):
assert resp.status == 200
response_headers = [(h.lower(), v) for h, v in resp.headers.items()]
for header, value in headers:
assert (header.lower(), value) in response_headers


@pytest.mark.asyncio
Expand All @@ -158,13 +164,15 @@ async def test_with_header_dict(pact: Pact) -> None:
.will_respond_with(200)
)
with pact.serve() as srv:
async with aiohttp.ClientSession(srv.url) as session:
async with session.request(
async with (
aiohttp.ClientSession(srv.url) as session,
session.request(
"GET",
"/",
headers={"X-Test": "true", "X-Foo": "bar"},
) as resp:
assert resp.status == 200
) as resp,
):
assert resp.status == 200


@pytest.mark.parametrize(
Expand All @@ -187,9 +195,11 @@ async def test_set_header_request(
.will_respond_with(200)
)
with pact.serve() as srv:
async with aiohttp.ClientSession(srv.url) as session:
async with session.request("GET", "/", headers=headers) as resp:
assert resp.status == 200
async with (
aiohttp.ClientSession(srv.url) as session,
session.request("GET", "/", headers=headers) as resp,
):
assert resp.status == 200


@pytest.mark.asyncio
Expand Down Expand Up @@ -241,12 +251,14 @@ async def test_set_header_response(
.set_headers(headers)
)
with pact.serve() as srv:
async with aiohttp.ClientSession(srv.url) as session:
async with session.request("GET", "/") as resp:
assert resp.status == 200
response_headers = [(h.lower(), v) for h, v in resp.headers.items()]
for header, value in headers:
assert (header.lower(), value) in response_headers
async with (
aiohttp.ClientSession(srv.url) as session,
session.request("GET", "/") as resp,
):
assert resp.status == 200
response_headers = [(h.lower(), v) for h, v in resp.headers.items()]
for header, value in headers:
assert (header.lower(), value) in response_headers


@pytest.mark.asyncio
Expand All @@ -264,12 +276,14 @@ async def test_set_header_response_repeat(
.set_headers(headers)
)
with pact.serve() as srv:
async with aiohttp.ClientSession(srv.url) as session:
async with session.request("GET", "/", headers=headers) as resp:
assert resp.status == 200
response_headers = [(h.lower(), v) for h, v in resp.headers.items()]
assert ("x-test", "2") in response_headers
assert ("x-test", "1") not in response_headers
async with (
aiohttp.ClientSession(srv.url) as session,
session.request("GET", "/", headers=headers) as resp,
):
assert resp.status == 200
response_headers = [(h.lower(), v) for h, v in resp.headers.items()]
assert ("x-test", "2") in response_headers
assert ("x-test", "1") not in response_headers


@pytest.mark.asyncio
Expand All @@ -282,13 +296,15 @@ async def test_set_header_dict(pact: Pact) -> None:
.will_respond_with(200)
)
with pact.serve() as srv:
async with aiohttp.ClientSession(srv.url) as session:
async with session.request(
async with (
aiohttp.ClientSession(srv.url) as session,
session.request(
"GET",
"/",
headers={"X-Test": "true", "X-Foo": "bar"},
) as resp:
assert resp.status == 200
) as resp,
):
assert resp.status == 200


@pytest.mark.parametrize(
Expand Down Expand Up @@ -382,13 +398,15 @@ async def test_with_body_request(pact: Pact, method: str) -> None:
.will_respond_with(200)
)
with pact.serve() as srv:
async with aiohttp.ClientSession(srv.url) as session:
async with session.request(
async with (
aiohttp.ClientSession(srv.url) as session,
session.request(
method,
"/",
json={"test": True},
) as resp:
assert resp.status == 200
) as resp,
):
assert resp.status == 200


@pytest.mark.parametrize(
Expand All @@ -407,14 +425,16 @@ async def test_with_body_response(pact: Pact, method: str) -> None:
.with_body(json.dumps({"test": True}), "application/json")
)
with pact.serve() as srv:
async with aiohttp.ClientSession(srv.url) as session:
async with session.request(
async with (
aiohttp.ClientSession(srv.url) as session,
session.request(
method,
"/",
json={"test": True},
) as resp:
assert resp.status == 200
assert json.loads(await resp.content.read()) == {"test": True}
) as resp,
):
assert resp.status == 200
assert json.loads(await resp.content.read()) == {"test": True}


@pytest.mark.asyncio
Expand All @@ -428,14 +448,16 @@ async def test_with_body_explicit(pact: Pact) -> None:
.with_body(json.dumps({"response": True}), "application/json", "Response")
)
with pact.serve() as srv:
async with aiohttp.ClientSession(srv.url) as session:
async with session.request(
async with (
aiohttp.ClientSession(srv.url) as session,
session.request(
"GET",
"/",
json={"request": True},
) as resp:
assert resp.status == 200
assert json.loads(await resp.content.read()) == {"response": True}
) as resp,
):
assert resp.status == 200
assert json.loads(await resp.content.read()) == {"response": True}


def test_with_body_invalid(pact: Pact) -> None:
Expand Down Expand Up @@ -499,14 +521,18 @@ async def test_binary_file_request(pact: Pact) -> None:
.will_respond_with(200)
)
with pact.serve() as srv:
async with aiohttp.ClientSession(srv.url) as session:
async with session.post("/", data=payload) as resp:
assert resp.status == 200
async with (
aiohttp.ClientSession(srv.url) as session,
session.post("/", data=payload) as resp,
):
assert resp.status == 200

with pytest.raises(MismatchesError), pact.serve() as srv: # noqa: PT012
async with aiohttp.ClientSession(srv.url) as session:
async with session.post("/", data=payload[:2]) as resp:
assert resp.status == 200
with pytest.raises(MismatchesError), pact.serve() as srv:
async with (
aiohttp.ClientSession(srv.url) as session,
session.post("/", data=payload[:2]) as resp,
):
assert resp.status == 200


@pytest.mark.asyncio
Expand All @@ -520,12 +546,14 @@ async def test_binary_file_response(pact: Pact) -> None:
.with_binary_body(payload, "application/bytes")
)
with pact.serve() as srv:
async with aiohttp.ClientSession(srv.url) as session:
async with session.get("/") as resp:
assert resp.status == 200
assert await resp.read() == payload
assert payload == bytes(range(5)) # to make sure it's not mutated
assert resp.headers["Content-Type"] == "application/bytes"
async with (
aiohttp.ClientSession(srv.url) as session,
session.get("/") as resp,
):
assert resp.status == 200
assert await resp.read() == payload
assert payload == bytes(range(5)) # to make sure it's not mutated
assert resp.headers["Content-Type"] == "application/bytes"


@pytest.mark.skip(reason="Not working yet")
Expand Down Expand Up @@ -583,10 +611,12 @@ async def test_name(pact: Pact) -> None:
.will_respond_with(200)
)
with pact.serve() as srv:
async with aiohttp.ClientSession(srv.url) as session:
async with session.get("/") as resp:
assert resp.status == 200
assert await resp.read() == b""
async with (
aiohttp.ClientSession(srv.url) as session,
session.get("/") as resp,
):
assert resp.status == 200
assert await resp.read() == b""


def test_add_external_reference(pact: Pact, tmp_path: Path) -> None:
Expand Down Expand Up @@ -638,10 +668,12 @@ async def test_with_plugin(pact: Pact) -> None:
.will_respond_with(200)
)
with pact.serve() as srv:
async with aiohttp.ClientSession(srv.url) as session:
async with session.get("/") as resp:
assert resp.status == 200
assert await resp.read() == b""
async with (
aiohttp.ClientSession(srv.url) as session,
session.get("/") as resp,
):
assert resp.status == 200
assert await resp.read() == b""


@pytest.mark.asyncio
Expand All @@ -659,9 +691,11 @@ async def test_pact_server_verbose(
caplog.at_level(logging.WARNING, logger="pact.pact"),
pact.serve(raises=False, verbose=True) as srv,
):
async with aiohttp.ClientSession(srv.url) as session:
async with session.get("/bar") as resp:
assert resp.status == 500
async with (
aiohttp.ClientSession(srv.url) as session,
session.get("/bar") as resp,
):
assert resp.status == 500

assert len(caplog.records) == 1
for record in caplog.records:
Expand Down
Loading