Answer third‑party API calls yourself
Decoy takes over the calls your application makes to outside services, such as a payment provider, and answers them with the responses you define. It records every call, so a test can check what was sent. You do not change the application.
-
The app calls the real hostname
$ decoy run -- curl -s -w '\nHTTP %{http_code}\n' -X POST https://api.stripe.com/v1/payment_intents -d amount=1000 -
A Rule answers. The call never leaves the machine
{"error":{"code":"card_declined"}} HTTP 402 -
The Call Journal shows what was sent
{"id":"c2","method":"POST","path":"/v1/payment_intents","outcome":"rule","rule_id":"r1","status":402}
- Intercepted Host
- A hostname Decoy takes over, for example
api.stripe.com. Every other hostname resolves and connects normally. - Rule
- A registered behavior for requests to an Intercepted Host. It matches on host, path, method and headers, and decides the response.
- Call Journal
- The record of every intercepted request and the response Decoy gave. You query it to assert on what was called.
Why Decoy
-
Your application keeps calling the real hostname
The app still calls
api.stripe.com. There is no second base URL to configure and no test-only client code. You point the client at Decoy with a proxy setting or with Decoy's DNS, and you make it trust the Decoy CA. That is configuration, not a code change.Only the hosts you intercept are affected. Every other host is tunnelled to the real destination untouched.
-
You choose the answer for each test
You register Rules at runtime through a local control API. A test can add a Rule that makes a payment return a declined card, run the scenario, and delete that Rule by its label afterwards. Rules can be scoped by header, so parallel scenarios can share one Decoy.
curl -X POST http://127.0.0.1:7411/rules -d '{ "label": "demo", "host": "api.stripe.com", "method": "POST", "path": {"prefix": "/v1/payment_intents"}, "respond": {"status": 402, "json": {"error": {"code": "card_declined"}}} }'When a fixed answer is not enough, a Rule can run code. A Handler is a Workers-style module that receives a request and returns a response:
export default { async fetch(request: Request, env: { decoy: { ruleId: string } }) { return Response.json({ error: { type: "card_error", code: "card_declined" } }, { status: 402 }); }, };Handlers run in a Rule Host, a Node, Bun or Deno process, so npm imports work. With no runtime installed, Embedded Mode runs the same file inside Decoy, without npm imports.
-
An unexpected call fails loudly
Each Intercepted Host has an Unmatched Policy for requests that no Rule matches. It can fail with a
502whose body names the request, forward the request to the real service, or answer with a canned response. The default is to fail, so a call you did not plan for shows up instead of quietly reaching the real service. -
You can see what your app sent
The Call Journal keeps each intercepted request and the answer Decoy gave. Filter it by host, method, path, header, Rule or outcome. Read back only the calls since you last looked.
-
One binary, set up for the client you have
Decoy is one binary. It is the DNS server and the HTTP(S) proxy, and it mints certificates for Intercepted Hosts from its own CA the first time they are needed. Setup Helpers point a client at Decoy and make it trust the CA:
decoy run -- <command>starts one command with the proxy and trust settings.decoy setup browserprints browser proxy and trust settings.decoy setup dockeranddecoy setup kuberneteswrite Compose and ConfigMap settings.
There are two ways to reach Decoy. In Proxy Mode the client uses Decoy as its HTTP(S) proxy. In Transparent Mode the client uses Decoy's DNS, so an Intercepted Host resolves to Decoy and no proxy setting is needed.
Works with Hunt and Jetlag
Decoy runs on its own. These two SlopKit products use it.
Hunt hunt.slopkit.dev
Hunt uses Decoy for stubs. A Scenario registers Decoy Rules that apply to that Scenario only, reads the Call Journal to assert on what was called, and removes its Rules when the Scenario ends.
Jetlag jetlag.slopkit.dev
Decoy's certificates are valid from 2020 until 2099, so a client whose clock Jetlag has moved far forward or back still accepts them.
Limits
- HTTP(S) only. SMTP STARTTLS and raw TCP are not intercepted, and clients that connect by IP address are not either.
- Bodies are buffered, so there is no streaming or server-sent events through Decoy.
- Clients that ignore the system trust store or pin certificates reject Decoy's certificates. Some Rust clients with bundled roots are one example.
- Rules, hosts and the journal live in memory. Only the Decoy CA persists.
- The control API has no authentication. Keep it on localhost.
- Do not trust the Decoy CA outside test environments.
Answer your first call
Download the binary, intercept api.stripe.com, register a Rule, and read the call back from the Call Journal. No account and no Nix. The reference covers the control API, Rules, Handlers, Setup Helpers and flags.
mkdir -p ~/.local/bin curl -fsSL -o ~/.local/bin/decoy "https://gitlab.techtailors.net/api/v4/projects/slopkit%2Fdecoy/packages/generic/decoy/0.1.0/decoy-linux-amd64" chmod +x ~/.local/bin/decoy decoy --version