Xml
XML body builder for Pact contract testing.
This module provides builder functions for constructing XML request and response bodies with embedded Pact matchers. It abstracts the FFI's internal XML description format, so test authors only need to describe the XML structure and attach matchers where desired.
from pact import match, xml
response = xml.body(
xml.element(
"user",
xml.element("id", match.int(123)),
xml.element("name", match.str("Alice")),
)
)
pact.with_body(response, content_type="application/xml")
The returned dict is passed to
with_body() with
content_type="application/xml". The Pact FFI detects the JSON-formatted body
and extracts matching rules automatically.
Individual functions can also be imported directly:
Attributes¶
XmlContent: TypeAlias = Union['XmlElement', 'AbstractMatcher[Any]', str, int, float, bool]
module-attribute
¶
Valid types for element children.
These can be child elements, matchers (text with rules), or literal primitive values (text without rules).
Classes¶
XmlElement(tag: str, *children: XmlContent, attrs: dict[str, Any] | None = None)
¶
Represents an XML element for use in Pact body descriptions.
Do not instantiate directly, use element() instead.
| PARAMETER | DESCRIPTION |
|---|---|
tag
|
Element tag name, optionally including a namespace prefix
(e.g.
TYPE:
|
*children
|
Child elements or text content. Each positional argument is one of:
TYPE:
|
attrs
|
Element attributes as a mapping of attribute name to value.
Values may be plain primitives or
|
Source code in src/pact/xml.py
Functions¶
each(*, min: int = 1, max: int | None = None, examples: int | None = None) -> XmlElement
¶
Mark this element as a type-matched repeating element.
Causes a type matching rule to be registered for this element in the
generated pact file, meaning the provider must return at least min
instances of this element structure. Use max to also enforce an upper
bound.
This method is typically used when the element appears as a child
inside another element's children list, but it can also be applied to
the root element passed to body().
| PARAMETER | DESCRIPTION |
|---|---|
min
|
Minimum number of matching elements required in the provider
response. Defaults to
TYPE:
|
max
|
Maximum number of matching elements (optional).
TYPE:
|
examples
|
Number of example copies of this element generated in the pact
body. Defaults to
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
XmlElement
|
|
Source code in src/pact/xml.py
to_serializable_dict() -> dict[str, Any]
¶
Convert this element to a serialisable dict.
The returned dict is suitable for
IntegrationJSONEncoder.
AbstractMatcher objects within
it are left as Python objects so that IntegrationJSONEncoder
serialises them to their integration-JSON form when
with_body()
calls json.dumps.
If .each() was called on this element,
the returned dict is wrapped in the FFI type-matcher envelope
({"pact:matcher:type": "type", "value": ..., "examples": N}).
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Any]
|
A dict representing this element in the FFI XML description format. |
Source code in src/pact/xml.py
Functions¶
body(root: XmlElement) -> dict[str, Any]
¶
Wrap a root XmlElement as an XML body description.
The returned dict is suitable for passing to
with_body()
with content_type="application/xml". The Pact FFI detects the
JSON-formatted body, generates the example XML, and extracts matching rules
into the contract file.
| PARAMETER | DESCRIPTION |
|---|---|
root
|
The root element of the XML body.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Any]
|
A dict in the FFI XML description format, ready for |
dict[str, Any]
|
|
Example
Source code in src/pact/xml.py
element(tag: str, *children: XmlContent, attrs: dict[str, Any] | None = None) -> XmlElement
¶
Create an XML element for use in a Pact body description.
| PARAMETER | DESCRIPTION |
|---|---|
tag
|
Element tag name, optionally including a namespace prefix
(e.g.
TYPE:
|
*children
|
Child elements or text content. Each positional argument is one of:
TYPE:
|
attrs
|
Element attributes as a mapping of attribute name to value. Values
may be |
| RETURNS | DESCRIPTION |
|---|---|
XmlElement
|
An |