docs(examples): add http+xml example#1507
docs(examples): add http+xml example#1507benaduo wants to merge 5 commits intopact-foundation:mainfrom
Conversation
…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>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1507 +/- ##
====================================
Coverage 55% 55%
====================================
Files 32 32
Lines 3894 3894
====================================
Hits 2144 2144
Misses 1750 1750
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
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>
|
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>
There was a problem hiding this comment.
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 underexamples/http/xml_example/. - Adds an example-local
pyproject.tomlfor dependency setup and updatesexamples/http/README.mdto list the new example. - Suppresses
RuntimeErrorwhen 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. |
| 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}" |
There was a problem hiding this comment.
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.
Closes #372
This PR adds a complete HTTP XML contract testing example to
examples/http/xml_example/, demonstrating how to usepact-pythonwith XML payloads.What's included:
consumer.py: AUserClientthat requests XML from a provider usingAccept: application/xmland parses responses withxml.etree.ElementTreeprovider.py: A FastAPI app with a/users/{id}endpoint returningContent-Type: application/xmlresponsestest_consumer.py: 2 Pact consumer tests (200 with XML body, 404 for unknown user)test_provider.py: 1 Pact provider verification test using uvicornconftest.py: Localpacts_pathfixturepyproject.toml: Example dependenciesexamples/http/README.mdto list the new exampleTests: All 3 pass locally.