Skip to content

Commit

Permalink
fix(ffi): use with_binary_body
Browse files Browse the repository at this point in the history
The `with_binary_file` FFI function is perhaps poorly named, as it
doesn't just set the (binary) body of an interaction, but it also sets
the matcher to be a content type matcher.

It would be best to have two separate FFI calls (which are now
supported) of `with_binary_body` and `with_matching_rule`.

Signed-off-by: JP-Ellis <[email protected]>
  • Loading branch information
JP-Ellis committed Jul 17, 2024
1 parent 9b7948c commit 5381b6a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
18 changes: 17 additions & 1 deletion src/pact/v3/ffi.py
Original file line number Diff line number Diff line change
Expand Up @@ -6179,7 +6179,23 @@ def with_binary_body(
RuntimeError:
If the body could not be modified.
"""
raise NotImplementedError
if len(gc.get_referrers(body)) == 0:
warnings.warn(
"Make sure to assign the body to a variable to avoid having the byte array"
" modified.",
UserWarning,
stacklevel=3,
)
success: bool = lib.pactffi_with_binary_body(
interaction._ref,
part.value,
content_type.encode("utf-8") if content_type else ffi.NULL,
body if body else ffi.NULL,
len(body) if body else 0,
)
if not success:
msg = f"Unable to set body for {interaction}."
raise RuntimeError(msg)


def with_binary_file(
Expand Down
2 changes: 1 addition & 1 deletion src/pact/v3/interaction/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ def with_binary_body(
body:
Body of the request.
"""
pact.v3.ffi.with_binary_file(
pact.v3.ffi.with_binary_body(
self._handle,
self._parse_interaction_part(part),
content_type,
Expand Down
11 changes: 6 additions & 5 deletions tests/v3/test_http_interaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import pytest

from pact.v3 import Pact
from pact.v3.pact import MismatchesError

if TYPE_CHECKING:
from pathlib import Path
Expand Down Expand Up @@ -362,7 +363,7 @@ async def test_with_body_response(pact: Pact, method: str) -> None:
json={"test": True},
) as resp:
assert resp.status == 200
assert await resp.json() == {"test": True}
assert json.loads(await resp.content.read()) == {"test": True}


@pytest.mark.asyncio()
Expand All @@ -382,7 +383,7 @@ async def test_with_body_explicit(pact: Pact) -> None:
json={"request": True},
) as resp:
assert resp.status == 200
assert await resp.json() == {"response": True}
assert json.loads(await resp.content.read()) == {"response": True}


def test_with_body_invalid(pact: Pact) -> None:
Expand Down Expand Up @@ -444,10 +445,10 @@ async def test_binary_file_request(pact: Pact) -> None:
async with aiohttp.ClientSession(srv.url) as session:
async with 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:
# The match _only_ checks the content type, not the content
# itself. See
# https://pact-foundation.slack.com/archives/C02BXLDJ7JR/p1697032990681329
assert resp.status == 200


Expand Down

0 comments on commit 5381b6a

Please sign in to comment.