Skip to main content

Provider states

A pact interaction often assumes a provider state, such as "an invoice 1042 exists". Before hosted verification replays that interaction, the provider has to be in that state. You register each state once, per provider, with how Apiome sets it up. Apiome then sets the state up before the interaction and tears it down after.

Until a provider registers at least one state, interactions that name a state are skipped with provider-state-not-set-up, as before.

Before you start​

  • A consumer has published a pact that names provider states.
  • The provider's running copy is registered as a verification target. See Verify a provider from Apiome.
  • To register states, your API key belongs to a member who holds Pacts → Create. To change or delete them, Pacts → Edit or Delete. To read and check them, Pacts → View.

Register a state​

Send a POST with the state's name, its parameters and its setup:

curl -X POST \
"$APIOME/v1/tenants/acme/pact-verifier/providers/ledger-api/provider-states" \
-H "Authorization: Bearer $APIOME_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"name": "invoice 1042 exists",
"description": "An open invoice with the given id.",
"parameters": [
{ "name": "id", "required": true },
{ "name": "currency", "default": "EUR" }
],
"setup": { "kind": "state_change", "url": "/_pact/provider-states" }
}'
FieldRequiredWhat it does
nameYesThe state exactly as consumers write it in the pact. It is unique for the provider.
descriptionNoWhat the state means, for the people reading the registry.
parametersNoThe parameters the state takes. A default fills in a value the interaction leaves out.
setupYesHow Apiome sets the state up. See Setups.
overridesNoA different setup on one verification target. See Per-target overrides.

The answer is 201 with the stored state and its id. Read the provider's states with GET …/provider-states, one with GET …/provider-states/{id}, replace one with PUT (same body), and delete one with DELETE.

Setups​

State-change call​

"kind": "state_change" sends the Pact state-change request, the one a Pact verifier's stateChangeUrl receives:

{ "state": "invoice 1042 exists", "params": { "id": 1042, "currency": "EUR" }, "action": "setup" }

After the interaction the same request is sent with "action": "teardown". Set "teardown": false to skip it.

urlSent toCredential
A path, such as /_pact/provider-statesThe verification target's base URL.The target's own credential.
An absolute https:// URLThat URL, through the same guard that keeps targets off private networks.None.

Recorded fixture​

"kind": "fixture" sends recorded requests to the verification target. requests put the target into the state, and teardown_requests take it out again. ${name} in a path, a header value or a string in the body is replaced with the parameter's value. A string that is only ${id} keeps the value's type, so 1042 stays a number:

{
"kind": "fixture",
"requests": [{ "method": "PUT", "path": "/test-data/invoices/${id}", "body": { "id": "${id}" } }],
"teardown_requests": [{ "method": "DELETE", "path": "/test-data/invoices/${id}" }]
}

Leave requests empty when the target already holds the data the state describes, for example a staging database seeded on every deploy.

Fixture requests carry the target's credential. A fixture may not store one itself: an Authorization, Cookie or API-key header is refused.

The target's policy still applies​

A setup that sends a mutating request to the target (a state-change call to a path, or a fixture with POST, PUT, PATCH or DELETE) is only run on a target whose policy allows mutating methods. On any other target the interaction is held back with provider-state-setup-blocked. A state-change URL off the target is not affected.

Per-target overrides​

Use overrides when one target needs a different setup, for example seeding staging by fixture while a local stack has a state-change endpoint:

{
"overrides": [
{ "target_ref": "ledger-staging", "setup": { "kind": "fixture", "requests": [] } }
]
}

target_ref is the target's slug or id in this workspace. A target the workspace does not have is refused with provider-state-invalid, pointing at the override.

Parameters​

When an interaction passes parameters ("params": { "id": 1042 }), Apiome compares them with the state's declared parameters:

The interactionResult
Leaves out a parameter that has a defaultThe default is sent.
Leaves out a required parameter with no defaultThe interaction is held back with provider-state-param-missing.
Passes a parameter the state does not declareIt is sent unchanged, and the check reports provider-state-param-undeclared as a warning.

Check before a run​

POST …/provider-states/check selects the pacts a verification would and reports every provider state their interactions name. Nothing is sent to the target.

curl -X POST \
"$APIOME/v1/tenants/acme/pact-verifier/providers/ledger-api/provider-states/check" \
-H "Authorization: Bearer $APIOME_API_KEY" \
-H "Content-Type: application/json" \
-d '{ "target_ref": "ledger-staging", "selection": { "consumerVersionSelectors": [{ "mainBranch": true }] } }'

selection is the same as on the verify request. target_ref is optional: with it, the check uses that target's overrides and its mutating-method policy.

The answer lists unregistered states first. For each state it gives whether it is registered, the setup it would use, how many interactions name it, and its problems. ready is true only when nothing would hold an interaction back. unused lists registered states that no selected pact names.

CodeSeverityMeaning
provider-state-not-registeredBlockingThe provider has not registered the state.
provider-state-param-missingBlockingAn interaction leaves out a required parameter.
provider-state-setup-blockedBlockingThe setup sends a mutating request to a target that forbids it.
provider-state-param-undeclaredWarningAn interaction passes a parameter the state does not declare.

See provider states in the app​

Open /ade/dashboard/pacts/provider-states and choose a provider. Before a run shows the check of its pacts against the registry, with each problem and the interaction it comes from. Registered states lists every state with its parameters, its setup and each target's override. States are registered through the REST API.

To write the handlers in the provider's own test instead, Provider test scaffolding on the Export page (or apiome pact scaffold-provider-tests) generates a verification test with one stub per state the pacts use, and flags each state that is not registered here — No handler in its states table, and a comment in the file.

The Provider states page for Ledger API: Before a run is Not ready, with the unregistered state 'the ledger has entries' listed first and an undeclared-parameter warning on 'invoice 1042 exists'; Registered states lists each state's parameters, setup and target override.The Provider states page for Ledger API: Before a run is Not ready, with the unregistered state 'the ledger has entries' listed first and an undeclared-parameter warning on 'invoice 1042 exists'; Registered states lists each state's parameters, setup and target override.
Route/ade/dashboard/pacts/provider-states

During a run​

With states registered, a hosted verification:

  1. Holds back, before anything is sent, every interaction whose states cannot be set up, with the blocking code above. Set "verification": { "replay_without_state_setup": true } to replay those interactions anyway, when the target already holds the data. They are marked "states_set_up": false.
  2. Sets each state of an interaction up, in the order the pact lists them, then replays it.
  3. Tears the states down after the interaction, in reverse order.

If a setup call fails (an answer outside 2xx, or no answer), the states already set up for that interaction are torn down, and it is skipped with provider-state-setup-failed naming the state and the call. A failed teardown does not change the result: it is listed in the pact's state_teardown_failures.

Values a state-change call returns are not used: a ProviderState generator gets no injected value.

What can go wrong​

StatusCodeMeaning
404pacticipant-not-foundNo such provider in this workspace.
404provider-state-not-foundNo such state for this provider.
409provider-state-name-takenThe provider already has a state with this name.
422provider-state-invalidThe body is malformed. pointer names the place, such as /setup/requests/0/path.
400pact-selector-invalidA selector in the check's selection is malformed.
422pact-verify-too-many-pactsThe check's selectors chose more than 25 pacts.

Who can do what​

ActionNeedsViewerEditorAdminOwner
List, read and check provider statespacts:view✓✓✓✓
Register a provider statepacts:create—✓✓✓
Replace a provider statepacts:edit—✓✓✓
Delete a provider statepacts:delete——✓✓