Compile a pact into a contract suite
Apiome can turn a published pact into a contract suite: one replayable case for each HTTP interaction the consumer recorded, and for each gRPC call the Pact protobuf plugin recorded. This is the same kind of suite Apiome compiles from a published API version (see Contract assurance). The same pact version always gives the same suite, down to the byte, so a provider verification can name exactly what it checked.
Compiling only reads the pact. It does not run anything and saves nothing. To replay the suite against your provider and publish the result, see Verify a provider from Apiome.
Start from generated tests
To verify a provider in its own build instead, start from a generated test: Provider test scaffolding on the Export page, or
apiome pact scaffold-provider-tests --provider "Ledger API" --language go -o ledger_api_pact_test.go
The file — pact-js, pact-jvm, pact-go or pact-python — fetches the provider's pacts from this
broker with the recommended selectors and pending pacts on, and has a state handler stub for every
provider state the pacts use. Fill in the stubs, set PACT_BROKER_BASE_URL, PACT_BROKER_TOKEN and
PROVIDER_BASE_URL, and run it like any other test; in CI it publishes the results.
Before you start
- The consumer has published the pact to your workspace.
- You have the pact version: the SHA-256 that names the pact's content. It is the
nameof the pact'spb:pact-versionlink (see Read a pact). - Your API key belongs to a member who holds Pacts → View. The built-in Viewer role does. Create one on API keys.
Compile the suite
Send a POST with the provider, the consumer and the pact version. The body is optional:
curl -X POST \
"$APIOME/v1/tenants/acme/pact-suites/provider/ledger-api/consumer/billing-service/pact-version/$PACT_SHA" \
-H "X-API-Key: $APIOME_API_KEY" \
-H "Content-Type: application/json" \
-d '{"options": {}}'
The response names the pair and the pact version, and carries the suite in manifest:
{
"consumer": "billing-service",
"provider": "ledger-api",
"pact_version_sha": "3f1c…",
"manifest": {
"digest": "sha256:9b2e…",
"pact": { "consumer": "Billing Service", "provider": "Ledger API", "interaction_count": 3, "…": "…" },
"cases": [ … ],
"unsupported": [ … ],
"findings": [ … ]
}
}
An unknown provider, consumer or pact version, or one in another workspace, is a 404. A pact
version that is not 64 hexadecimal characters is a 422.
What a case holds
Each HTTP interaction becomes one case with source: pact_interaction and synthetic: false,
because the consumer recorded it and Apiome made nothing up.
| Part of the case | What it holds |
|---|---|
request | The method, path, query, headers and body, exactly as the consumer recorded them. |
expect.status_codes | The recorded status, such as 200. A Pact 4 statusCode rule widens it to a class: success becomes 2XX, error becomes 4XX and 5XX. |
pact.provider_states | The states to set up before sending the request, with their parameters. Register how each is set up in the provider-state registry. |
pact.response | The response the consumer expects: status, headers, body and media type. A body the pact records as empty ("", null, or a Pact 4 body with empty content) is has_body: true with body: "", so the response must be empty too. |
pact.response.matching_rules, pact.request_matching_rules | Every matching rule, with its matcher exactly as written and its place in the pact file. |
pact.response.generators, pact.request_generators | Every generator, kept so nothing is lost. |
pact.index, pact.key, pact.pointer | Which interaction it is: its position, its Pact 4 key and where it sits in the file. |
pact.pending | Whether the consumer marked the Pact 4 interaction pending. The case is also tagged pending. |
Apiome reads Pact specifications 1 to 4 the same way. A Pact 2 query string and a Pact 3 query
object give the same parameters. A Pact 4 body object is unwrapped, and a base64 or JSON-text body
is decoded. Cases are grouped by METHOD path (a gRPC call by package.Service.Method) and then
keep the pact's own order.
gRPC and GraphQL interactions
Pact has no gRPC or GraphQL transport of its own. Consumer tests record them in two ways, and Apiome reads both.
gRPC calls from the protobuf plugin
The Pact protobuf plugin records a gRPC call
as a Pact 4 Synchronous/Messages interaction with transport: grpc. It also files the .proto
definitions, compiled, in the pact's metadata.plugins. Apiome reads the call through those
definitions and compiles it into the same kind of case it compiles from a published Protobuf
version:
| Part of the case | What it holds |
|---|---|
protocol | grpc. |
operation_key | package.Service.Method, such as acme.orders.v1.Orders.GetOrder. |
rpc.procedure | The call that is made: /acme.orders.v1.Orders/GetOrder. |
rpc.message | The request the consumer recorded, decoded to protobuf JSON: JSON field names (orderId for order_id), enums by name, 64-bit integers as strings. |
rpc.expect_code | ok, or the grpc-status the consumer recorded (not_found). |
rpc.idempotency_level | From the method's options. Only a no_side_effects method is sent without the mutating opt-in. |
pact.response.body | The reply the consumer expects, decoded the same way. An error reply records no body. |
pact.response.matching_rules | The reply's rules, with each path renamed to JSON field names ($.total_cents becomes $.totalCents). |
A suite whose cases are all gRPC calls has protocol: grpc and runs on a grpc or connect
verification target. A pact that mixes HTTP interactions and gRPC calls has protocol: http and
carries both kinds of case. See
gRPC and GraphQL pacts for how a run treats
them.
Protobuf JSON writes int64 and the other 64-bit integer types as decimal strings. An integer or
number rule on such a field would fail every correct reply, so it is applied as a digits-only
regular expression instead, and reported as a PACT_PLUGIN_PARTIAL info finding.
GraphQL over HTTP
A GraphQL operation is an ordinary HTTP interaction: a POST of query, variables and
operationName, a GET with those as query parameters, or a POST of an application/graphql
body. pact-js GraphQLInteraction writes this shape. Apiome recognises one when its query is a
GraphQL document, and adds the operation to the case:
| Part of the case | What it holds |
|---|---|
graphql.query, graphql.variables, graphql.operationName | The operation, as recorded. |
graphql.operation_type | query or mutation. It decides whether the case is mutating, because every GraphQL operation is a POST. |
graphql.field | The first root field the operation selects. |
tags | pact and graphql. |
The case keeps its HTTP request, so it is replayed on the recorded path, such as /graphql. A
query that is not GraphQL, such as a search box sending {"query": "red shoes"}, is left alone.
A GraphQL subscription cannot be replayed as one request and is listed under unsupported.
The same pact gives the same suite
The suite is built only from what the pact version covers: the interactions, the messages and
the specification version. It also uses the consumer and provider, and the options you send. The
consumer version that published the pact and the file's metadata are not used. So every
consumer version that shares a pact version gets the same suite and the same digest.
The one thing read from metadata is the protobuf plugin's compiled .proto definitions. Each
gRPC call names them by their descriptorKey, which the plugin derives from the definitions and
which is part of the interactions, so the suite still depends only on the pact version.
digest is sha256: followed by the SHA-256 of the suite's canonical bytes, taken with digest
itself left blank. Change an option, or publish different content, and the digest changes.
What is reported instead of compiled
Nothing is left out without a reason. An interaction Apiome cannot replay is listed under
unsupported and also appears in findings. Something compiled less strictly than the pact asks
is a degraded finding. Something the runner must provide is an info finding.
| Code | Level | Meaning |
|---|---|---|
UNSUPPORTED_OPERATION_KIND | unsupported | A message interaction (Pact 3 messages, Pact 4 Asynchronous/Messages, or a Synchronous/Messages interaction that is not a gRPC call). There is no HTTP exchange to replay. Asynchronous messages are checked against the provider's AsyncAPI document instead: see Message pacts. |
PACT_PLUGIN_INTERACTION | unsupported | A plugin interaction Apiome cannot replay: a plugin other than protobuf (CSV, Avro), a protobuf body over HTTP, or a gRPC call whose definitions, method or content cannot be read. The reason names the plugin, or the problem: grpc-descriptors-missing, grpc-method-not-found or grpc-content-unreadable. |
UNSUPPORTED_STREAMING | unsupported | A streaming gRPC method (grpc-streaming), or a GraphQL subscription. One request and one reply cannot express a stream. |
PACT_PLUGIN_PARTIAL | degraded / info | Part of a gRPC call is carried less strictly than the pact says: request metadata is not sent, and response metadata, metadata rules and grpc-message are not compared. Only the first of several responses is compared. As info, a numeric rule on a 64-bit integer field applied as a regular expression. |
PACT_GRAPHQL_UNREADABLE | degraded | The request carries a GraphQL operation that cannot be read: a document that does not parse on a …/graphql path, several operations and no operationName, or variables that are not an object. It is replayed as plain HTTP, and its method decides whether it is mutating. |
PACT_GRAPHQL_SELECTIONS_NOT_MAPPED | info | The pact's GraphQL operations are verified, but the fields they select are not yet mapped onto consumer contracts (see the Consumer contracts REST reference), so consumer impact on a classified diff does not see them. Reported once for each pact. |
PACT_INTERACTION_MALFORMED | unsupported | The interaction has no request method or path, so there is nothing to send. |
PACT_BODY_UNREADABLE | unsupported / degraded / info | A body that cannot be decoded. On the request, the case cannot be sent. On the response, only the status and headers can be compared — unless a contentType rule on $ judges the body by its bytes, which is info (see Supported matching rules). |
PACT_EMBEDDED_MATCHER | degraded | A Pact 1 matcher embedded in a body (json_class). The example values are kept, but the embedded rule is not. |
PACT_MATCHING_RULE_UNSUPPORTED | degraded | A matching rule with a matcher no Pact specification defines, a path that is not a JSONPath, or an unknown location. It is kept in the case. |
PACT_REQUEST_GENERATOR | degraded | The suite sends the recorded example where a verifier would generate a value. A ProviderState generator is not filled in from the provider state either: the recorded example is sent. |
PACT_RESPONSE_GENERATOR | info | Verifiers ignore response generators. The matching rules decide what the provider may return. |
PACT_PROVIDER_STATE | info | A provider state that must be set up before the interactions that name it. It is reported once for each state. |
PACT_PROVIDER_STATE_UNREADABLE | degraded | A provider state with no name. The interaction is replayed without it. |
PACT_SPECIFICATION_ASSUMED | info / degraded | The pact declares no specification version, or one Apiome does not know. |
STATUS_UNDECLARED | degraded | The response records no status. 200 is expected, which is what a Pact mock server answers. |
OPERATION_NOT_SELECTED, OPERATION_LIMIT_REACHED | info / degraded | An interactions name matched nothing, or max_interactions cut the suite short. |
NO_CASES_COMPILED | degraded | The pact has no HTTP interaction or gRPC call to replay. |
Options
| Option | Default | Effect |
|---|---|---|
interactions | every interaction | Compile only these interactions, each named by its description or its Pact 4 key. The order does not change the digest. |
max_interactions | 500 | The most interactions to compile (up to 2,000). The rest are counted in counts.operations_skipped and reported. |
The options are recorded in manifest.pact.options, and they change the digest. A suite compiled
from a pact has manifest.options set to null, because the version-suite options (seed,
generated bodies, negative cases) do not apply to a pact.
Who can do what
| Action | Needs | Viewer | Editor | Admin | Owner |
|---|---|---|---|---|---|
| Compile a pact into a contract suite | pacts:view | ✓ | ✓ | ✓ | ✓ |
Related
- Verify a provider from Apiome: replay the suite against a running provider and publish the result.
- Publish verification results: how Pact verifiers report back, and how to send a report produced elsewhere.
- Pact Broker concepts: pacticipants, versions and pact versions.
- Pact Broker REST reference: the full request and response schema.