Test consumer
Consumer test demonstrating multipart/form-data with matching rules.
This test shows how to use Pact matching rules with multipart requests. The examples illustrates this with a request containing both JSON metadata and binary data (an image). The contract uses matching rules to validate structure and types rather than exact values, allowing flexibility in the data sent by the consumer and accepted by the provider.
Attributes¶
JPEG_BYTES = bytes([255, 216, 255, 224, 0, 16, 74, 70, 73, 70, 0, 1, 2])
module-attribute
¶
Some minimal JPEG bytes for testing multipart uploads.
In this example, we only need the JPEG magic bytes to validate the file type. This is not a complete JPEG file, but is sufficient for testing purposes.
Classes¶
Functions¶
pact() -> Generator[Pact, None, None]
¶
Set up Pact for consumer contract testing.
This fixture initializes a Pact instance for the consumer tests, specifying the consumer and provider names, and ensuring that the generated Pact files are written to the appropriate directory after the tests run.
Source code in examples/catalog/multipart_matching_rules/test_consumer.py
test_multipart_upload_with_matching_rules(pact: Pact) -> None
¶
Test multipart upload with matching of the contents.
This test builds a multipart/form-data request by hand, and then uses a
library (httpx) to send the request to the mock server started by Pact.
Unlike simpler payloads, the matching rules cannot be embedded within the
body itself. Instead, the body and matching rules are defined in separate
calls.
Some key points about this example:
- We use a matching rule for the
Content-Typeheader to allow any valid multipart boundary. This is important because many HTTP libraries generate random boundaries automatically without user control. - The body includes arbitrary binary data (a JPEG image) which cannot be
represented as a string. Therefore, it is critical that
with_binary_bodyis used to define the payload. -
Matching rules are defined for both the JSON metadata and the image part to allow flexibility in the values sent by the consumer. The general form to match a part within the multipart body is
$.<part name>. So to match a field in themetadatapart, we use$.metadata.<field>; or to match the content type of theimagepart, we use$.image:
Warning
Proper content types are essential when working with multipart data. This ensures that Pact can correctly identify and apply matching rules to each part of the multipart body. If content types are missing or incorrect, the matching rules may not be applied as expected, leading to test failures or incorrect behavior.
To view the implementation, expand the source code below.
Source code in examples/catalog/multipart_matching_rules/test_consumer.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | |