Skip to content

docs(examples): add http+xml example#1507

Open
benaduo wants to merge 5 commits intopact-foundation:mainfrom
benaduo:main
Open

docs(examples): add http+xml example#1507
benaduo wants to merge 5 commits intopact-foundation:mainfrom
benaduo:main

Conversation

@benaduo
Copy link

@benaduo benaduo commented Mar 19, 2026

Closes #372

This PR adds a complete HTTP XML contract testing example to examples/http/xml_example/, demonstrating how to use pact-python with XML payloads.

What's included:

  • consumer.py: A UserClient that requests XML from a provider using Accept: application/xml and parses responses with xml.etree.ElementTree
  • provider.py: A FastAPI app with a /users/{id} endpoint returning Content-Type: application/xml responses
  • test_consumer.py: 2 Pact consumer tests (200 with XML body, 404 for unknown user)
  • test_provider.py: 1 Pact provider verification test using uvicorn
  • conftest.py: Local pacts_path fixture
  • pyproject.toml: Example dependencies
  • Updated examples/http/README.md to list the new example

Tests: All 3 pass locally.

benaduo and others added 2 commits March 20, 2026 00:44
…on#372)

- Add examples/http/xml_example/ with consumer, provider, and tests
- Consumer uses xml.etree.ElementTree to parse XML responses
- Provider is a FastAPI app returning application/xml responses
- Tests cover GET user (200) and unknown user (404) scenarios
- Add local conftest.py with pacts_path fixture
- Update examples/http/README.md to list new example

Co-Authored-By: Abacus.AI CLI <agent@abacus.ai>
@benaduo
Copy link
Author

benaduo commented Mar 19, 2026

Hi @JP-Ellis, I've opened this PR to address issue #372, adding a complete XML contract testing example using xml.etree.ElementTree with FastAPI as the provider. All 3 tests pass locally. Would appreciate a review when you get the chance. Happy to make any changes you need.

@codecov
Copy link

codecov bot commented Mar 24, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 55%. Comparing base (a69ddc4) to head (2240654).
⚠️ Report is 3 commits behind head on main.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@         Coverage Diff          @@
##           main   #1507   +/-   ##
====================================
  Coverage    55%     55%           
====================================
  Files        32      32           
  Lines      3894    3894           
====================================
  Hits       2144    2144           
  Misses     1750    1750           
Flag Coverage Δ
tests 55% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

If the logger is instantiated multiple within a session, the ffi returns
an error as the subsequent call is an error. Within the context of the
examples, we silence the error as they all use the same logging
configuration.

Signed-off-by: JP-Ellis <josh@jpellis.me>
@JP-Ellis
Copy link
Contributor

Thanks for opening up the PR! There are a few changes I have made to the example itself in terms of linting/formatting. I've also expanded a fair bit on the docs aspect, as the examples are meant to be a learning resource as well. I'll push these changes up shortly.

Signed-off-by: JP-Ellis <josh@jpellis.me>
Copilot AI review requested due to automatic review settings March 24, 2026 07:13
@JP-Ellis JP-Ellis enabled auto-merge (squash) March 24, 2026 07:14
@JP-Ellis JP-Ellis changed the title feat(examples): add XML contract testing example (Issue #372) docs(examples): add http+xml example Mar 24, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new, self-contained examples/http/xml_example/ showing how to use pact-python for HTTP contract testing with XML payloads, and makes HTTP example logging setup more resilient.

Changes:

  • Introduces an XML consumer (requests) and provider (FastAPI) pair plus consumer/provider Pact tests and fixtures under examples/http/xml_example/.
  • Adds an example-local pyproject.toml for dependency setup and updates examples/http/README.md to list the new example.
  • Suppresses RuntimeError when configuring Pact FFI logging in existing HTTP examples’ conftest.py.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
examples/http/xml_example/test_provider.py Provider verification test with state handlers and uvicorn-backed server fixture.
examples/http/xml_example/test_consumer.py Consumer Pact tests using literal XML bodies and Accept: application/xml.
examples/http/xml_example/pyproject.toml Example dependency declaration and tooling config.
examples/http/xml_example/provider.py FastAPI provider returning XML responses built via xml.etree.ElementTree.
examples/http/xml_example/consumer.py requests client that requests/parses XML responses.
examples/http/xml_example/conftest.py Provides pacts_path fixture for the example.
examples/http/xml_example/init.py Marks the example as a package; disables missing docstring lint.
examples/http/requests_and_fastapi/conftest.py Wraps Pact logging initialization in contextlib.suppress(RuntimeError).
examples/http/aiohttp_and_flask/conftest.py Wraps Pact logging initialization in contextlib.suppress(RuntimeError).
examples/http/README.md Lists the new xml_example/ directory.

Comment on lines +41 to +49
hostname = "localhost"
port = pact._util.find_free_port() # noqa: SLF001
Thread(
target=uvicorn.run,
args=(app,),
kwargs={"host": hostname, "port": port},
daemon=True,
).start()
return f"http://{hostname}:{port}"
Copy link

Copilot AI Mar 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The server thread is started and the fixture returns immediately; provider verification can race the server startup and become flaky on slower machines/CI. Consider waiting until the port is accepting connections (or adding a small startup delay/poll) before returning the base URL.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a complete XML example

3 participants