pin-an-api-contract-with-a-consumer-test
Use when a provider team can silently change a response your client depends on — record the consumer's real expectations as a pact and verify the provider against it in CI.
- License 许可
- MIT
- 最近更新
- 2026-11-15
- Stars
- ★ 81
- Forks
- 17
01 前言 Front Matter
- name
- pin-an-api-contract-with-a-consumer-test
- description
- Use when a provider team can silently change a response your client depends on — record the consumer's real expectations as a pact and verify the provider against it in CI.
02 资源文件 3
- 许可证LICENSE1.0 KB
- 参考文档SOURCE.md366 B
- 参考文档TRUST.auto.yaml2.2 KB
03 SKILL.md 正文
Pin an API contract with a consumer test
Unit tests mock the provider, so they pass while production breaks. A consumer-driven contract records what your client actually needs and fails the provider build when it stops honoring it.
Procedure
- Declare the exact request the consumer makes and the fields it reads:
from pact import Consumer, Provider
pact = Consumer("checkout-web").has_pact_with(Provider("orders-api"))
(pact.given("order 42 exists")
.upon_receiving("a request for order 42")
.with_request("GET", "/orders/42")
.will_respond_with(200, body={"id": 42, "status": "paid"}))
- Run the consumer tests against the mock provider Pact generates — this proves the consumer needs only what it declares:
pytest tests/consumer/ -q && ls pacts/checkout-web-orders-api.json
- Parse the response into a typed model so undeclared fields cannot silently become requirements:
class Order(BaseModel): # pydantic
id: int
status: Literal["paid", "pending"]
Add body fields to the model only when the code reads them. 4. Publish the pact to the broker:
pact-broker publish pacts/ --consumer-app-version=$(git rev-parse --short HEAD) --broker-base-url=$BROKER
- Verify on the provider side against a real, seeded instance:
pact-provider-verifier --provider-base-url=http://localhost:8080 \
--pact-broker-base-url=$BROKER --provider-app-version=$(git rev-parse --short HEAD)
Required: all interactions verified, exit 0. 6. Gate deployment:
pact-broker can-i-deploy --pacticipant checkout-web --version $(git rev-parse --short HEAD) --to-environment production
- Run consumer tests on every client PR and provider verification on every provider PR so a break is caught on whichever side moved.
Pitfalls
- A pact asserting the whole response body makes the provider unable to add fields; assert only consumed fields.
- Verifying against a stubbed provider is not verification. Point
--provider-base-urlat a real seeded instance. - Contracts drift if the client model is "just in case" broad. Keep it to fields the code reads.
can-i-deployneeds both versions published; a missing publish fails open. Fail closed if the broker call errors.
Verification
pact-broker can-i-deploy --pacticipant checkout-web --version $(git rev-parse --short HEAD) --to-environment production
Passes = prints Computer says yes. Report: "checkout-web 3f9a1c can-i-deploy=yes against orders-api, 4 interactions verified."
04 同仓库更多 Skills
recipe-performance
Diagnose a specific MetaMask user flow's real-app performance and Sentry critical-user-flow telemetry with deterministic mm-harness recipes. Use for CUF boundaries, cold/warm/background-resume behavior, latency regressions, lifecycle tags, duplicate or blocking network requests, long tasks, layout/paint cost, screen redraws, React rerenders, or browser trace analysis where tests alone cannot prove the real app.
mexc-spot-websocket
MEXC Spot WebSocket workflows for public streams, protobuf handling, depth maintenance, listen keys, and private streams without exposing API keys.
mexc-spot-rest
MEXC Spot REST workflows for public data, signed account requests, orders, wallet/admin actions, and secure signing without exposing API keys.
mexc-futures-websocket
MEXC Futures WebSocket workflows for public streams, private account/order/position streams, ping/pong, subscriptions, and depth maintenance.
0xklen-version-and-refresh-sanctions-lists
暂无简介
verify-tests-cover-changed-lines
Use when a PR adds code that the existing suite may not exercise. Measures patch coverage — the share of newly changed lines hit by tests — before approving.