Skip to main content

Pact stubs

A pact stub is a hosted mock that answers with what a provider's published pacts promise. The spec mock answers with everything the spec allows, so a consumer built against it can come to rely on fields no pact protects. A pact stub answers only with the pacts' own examples. Each provider state is a scenario.

To create one from the app, or with apiome pact stub, see Serve pacts as a mock. This page covers how the hosted mock answers.

The URL​

A stub answers under its own path on the mock host:

https://mock.apiome.dev/{tenant}/~pacts/{provider}/{name}/<API path>
  • {provider} is the provider's handle (billing-service).
  • {name} is the stub's name. It defaults to the pacts it serves (main, deployed-production, orders-ui-3-4-0).
  • ~pacts is reserved. A project's handle never starts with ~, so no project's mock is hidden.

Pacts are private to the workspace, so every call sends a workspace API key as X-Api-Key. A call without one, or with another workspace's key, gets a 401. An unknown stub gets a 404.

export STUB=https://mock.apiome.dev/acme/~pacts/billing-service/main

curl -H "X-Api-Key: $APIOME_API_KEY" -H 'X-Mock-Scenario: invoice 7 exists' "$STUB/invoices/7"

How a request is answered​

The stub looks for an interaction of the served pacts that matches the request, judged as a Pact mock server judges it:

  • the method, and the path (or the pact's path rule);
  • the query: every parameter the pact records, and no other;
  • the headers the pact records (others are ignored), under its header rules;
  • the body, when the pact records one, under its body rules. Extra JSON keys are allowed.

A match is answered with the interaction's status, headers and body exactly as the pact records them. Generators are not applied: the stub sends the recorded example.

Provider states. Send X-Mock-Scenario: <provider state> to be answered in that state. Without the header, an interaction recorded in no state is preferred, then the first in pact order. A scenario no matching interaction was recorded in is treated as no match.

No match. What happens depends on the stub's fallback:

  • Answer from the spec — the spec mock of the stub's fallback version answers, exactly as a call to that mock would be answered.
  • Return 501 — a 501 problem (pact-stub-no-match) says why, and lists the provider states the request has interactions for.

If the fallback version is deleted, the stub answers 501 and says so.

Response headers​

Every answer of a stub says where it came from:

Response headerValue
X-Mock-Stubpact (an interaction answered), fallback-spec (the spec mock answered) or no-match (the 501)
X-Mock-Stub-Interaction<consumer> <version>: <description> of the interaction that answered
X-Mock-Stub-ScenariosThe other provider states this request has interactions for, ; -separated
X-Mock-Stub-ReasonOn fallback-spec and no-match: why no interaction answered

What a stub does not serve​

A stub serves HTTP interactions. Anything else in the chosen pacts is listed, with its reason, in the stub's report (in the dialog, in the apiome pact stub output, and in GET /v1/tenants/{t}/pact-stubs/{provider}/{name}):

  • messages and asynchronous interactions, which have no HTTP request to answer;
  • plugin interactions (gRPC, protobuf) and other transports;
  • an interaction whose request or response cannot be read (an undecodable body, a status that is not HTTP);
  • an interaction that repeats an earlier one (same request, same states): the first answers;
  • a source that chose no pact, for example whatever is deployed when nothing is.

When the stub changes​

The hosted mock compiles a stub once, on its first call, and keeps it in memory. Serving a call reads nothing from the database. The stub is compiled again as soon as anything it is compiled from changes: the stub itself, a pact published for the provider, a version added to or removed from a branch, a deployment or release recorded or ended, or a pacticipant renamed or deleted. The database announces each change on apiome_pact_stub_changed; a mock that lost its connection compiles every stub again when it reconnects. A stub is also compiled again after five minutes (APIOME_MOCK_PACT_STUB_CACHE_TTL_SECONDS) as a backstop.

Calls answered from a pact count towards the workspace's mock requests, under the project ~pacts. Calls answered by the spec mock count as calls to that mock.