Gateway scenarios
A gateway scenario proves one gateway feature end to end. Pick the outcome, and Rustybin prepares the upstream the test needs, tells you how to point the gateway at it, sends test traffic through the gateway, and compares what the client got with what reached the upstream. Ending the run removes what it created and nothing else. No gateway product is assumed: the steps are written for any gateway. There are scenarios for HTTP gateways (the table below), for AI gateways and for MCP gateways (tool poisoning, rug pulls, tool shadowing, result injection, oversized results and exfiltration links, against this instance's MCP attack servers).
| Scenario | What a pass proves | Upstream the run prepares |
|---|---|---|
retries | The gateway retries a request the upstream fails with 503, and the client sees the success | /flaky/recover/1 (fails each request's first attempt) |
rate_limiting | The gateway answers 429 past the limit, and the requests over it never reach the upstream | a mock with GET /items |
credential_injection | Clients send no credential; the gateway adds the upstream's own, and the upstream accepts every request | a mock that requires X-Upstream-Key with a key made for the run |
timeouts | The gateway stops waiting for a slow upstream that has received the request, and answers a gateway error before the upstream would have | a mock whose GET /slow answers after 3 seconds |
caching | Repeated GETs come from the gateway's cache: only the first reaches the upstream | a mock whose GET /catalog answers Cache-Control: public, max-age=300 |
circuit_breaker | The gateway stops forwarding to an upstream that keeps failing and keeps answering on its own (three requests in a row, into the half-open time), then lets a trial request through after the half-open time | /flaky/after/0 (answers 503 to every request) |
request_size_limit | The gateway refuses a body over the limit with 413 before the upstream sees it, and forwards one under it | a mock with POST /upload (any body) |
cors | The gateway answers a CORS preflight from an allowed origin (and not from another), and a request from the allowed origin gets Access-Control-Allow-Origin | a mock with GET /cors and OPTIONS /cors that answers no CORS of its own (unless you say the upstream answers CORS) |
header_transformation | The gateway adds a request header on the way to the upstream, removes one the client sent, and adds a response header on the way back | a mock with GET /headers |
What to turn on at the gateway for the resilience scenarios:
-
circuit_breaker: a circuit breaker on the route (often called passive health checks or outlier detection on the upstream) that opens after a few failed requests in a row, answers on its own with a5xxwhile open, and lets a trial request through after a half-open time. The check sends requests until one is answered without reaching the upstream (at most 20), then two more to see the circuit stay open: one at once, one half the half-open time later. Only when none of the three reached the upstream is the circuit open. One such answer alone proves nothing, because a gateway that lost one call to the upstream (a dropped connection, a timeout, an overloaded machine) also answers502or504on its own: when a request after it reaches the upstream again, the verdict names it as one failed call and the check goes on. A429without the upstream is a rate limit, not an open circuit. The check then waits the half-open time you give it (half_open_ms, 100 to 60000, default 10000) plus a tenth, counted from the first of the three, and sends up to three trials: a pass needs one of them to reach the upstream. The upstream keeps failing, so the trial fails and the circuit opens again: wait for it to close before checking again.What the check cannot tell from an open circuit: a link between the gateway and the upstream that is down for more than half the half-open time and back before the trial. Give the gateway's real half-open time, and read the statuses in the verdict (a breaker answers the same status at once each time).
-
timeouts(no setting but the gateway's timeout): a pass needs the gateway error, an answer before the upstream's, and the upstream to have received the request. A route that does not reach the upstream answers502at once too, and that is not a timeout: the verdict says the request never reached the upstream. -
request_size_limit: a request body size limit on the route. Give the check the limit in bytes (size_limit, 16 to 1048576, default 1048576): it sendsPOST /uploadwith a body half that size, then one twice that size (so a limit set in kilobytes or megabytes, either 1000 or 1024 to the unit, still falls between them). The body under the limit must also fit this instance's ownRUSTYBIN_BODY_LIMIT.
What to turn on at the gateway for the two HTTP scenarios:
cors: a CORS policy on the route that allows one origin and theGETmethod, allows no other origin, and answers preflight requests at the gateway. Give the check the origin you allowed (allowed_origin, defaulthttps://app.example.com) and one you did not (refused_origin, defaulthttps://elsewhere.example.net), each written as a browser sends it,scheme://host[:port]. The check sends anOPTIONSpreflight (OriginandAccess-Control-Request-Method: GET) from each origin, then aGETfrom the allowed one. A pass needs the allowed preflight answered2xxwithAccess-Control-Allow-Origin(that origin or*) andAccess-Control-Allow-MethodswithGET, without reaching the upstream; noAccess-Control-Allow-Originfor the refused origin; andAccess-Control-Allow-Originon theGET. When the gateway should only let preflights through and the upstream answers CORS, check withupstream_cors: true: the run's mock then answers CORS forallowed_origin, and the allowed preflight must reach it. The run's mock never gets this instance's own CORS (RUSTYBIN_CORS_ORIGINS), so what the check sees comes from the gateway, or from the mock when you asked for it.header_transformation: request and response header rules on the route: add a request header to every request it forwards, remove one the client sends, and add a response header to every answer. Give the check the names you used (add_request_header, defaultX-Gateway-Added;remove_request_header, defaultX-Client-Internal;add_response_header, defaultX-Gateway-Response) and, when the value matters,add_request_value. The check sends aGET /headerswith the header to remove (a value made for the check), then reads the request inspector's capture of what reached the upstream and the answer. Name headers neither side sets on its own:Host,Content-Type,Origin,X-Request-Id,X-Rustybin-*andAccess-Control-*, among others, are refused.
AI gateway scenarios
The AI scenarios prove what an AI gateway does in front of a model. Their upstream is
this instance's AI mock in the OpenAI chat completions format
(/ai/openai/v1/chat/completions, and a second target at /ai/v1/chat/completions,
the same API at another path, for the two scenarios that need two). A run steers the
mock for its own requests only: the check's requests carry a secret of the run in
X-Rustybin-Session, and the mock applies the run's profile (fail the primary target,
answer a customer record) to those and to nothing else, so other callers of the AI
mock see no change, and ending the run ends the profile. The check sends POST
requests in the OpenAI format (model gpt-4o-mini, max_tokens 100) to the URL you
give, so point it at the URL the gateway serves the route's chat completions at.
| Scenario | What a pass proves | What the mock does for the run |
|---|---|---|
ai_token_limit | The gateway limits by tokens, not requests: once the budget is spent it answers 429, the refused requests never reach the model, and the first refusal comes after about the budget | Answers each request with a little over 100 tokens of usage |
ai_model_fallback | When the primary target answers a provider error, the gateway sends the request to a fallback and the client sees a success | The primary target answers every request with the error of fail_kind; the second target answers normally |
ai_semantic_cache | Prompts that mean the same (other case and punctuation, a word more) come from the cache; a different prompt still reaches the model | Answers normally; its embeddings keep similar texts close, for a gateway that asks this instance for them |
ai_prompt_guard | Personal data and a prompt injection never reach the model (the gateway refuses the prompt or redacts it), and a clean prompt does | Answers normally; the check reads what reached it |
ai_response_guard | The gateway removes personal data from the model's answer before the client sees it | Answers every request with a fake customer record (SSN, card number, email, phone) |
ai_provider_balance | The gateway spreads the requests across two targets, each getting at least its share, and every request succeeds | Two targets answer normally |
What each check sends and how it judges:
ai_token_limit: the same long prompt until the gateway refuses one (at most 40 requests, or until twice the budget went to the model). Give the check the budget you set (token_limit, 100 to 2000, default 500), counted in prompt plus completion tokens. The check readsusage.total_tokensfrom each answer; a pass needs a429, no429that reached the model, and the first429after the tokens that reached the model were between the budget less two requests' worth and a quarter over the budget plus two requests' worth (a gateway that counts completion tokens only, or checks a request'smax_tokensup front, falls inside). A request limit set instead refuses too early and fails this line. Wait for the gateway's window to pass before checking again.ai_model_fallback: three requests. The primary target fails each with the provider error offail_kind(rate_limit429,overloadedandunavailable503,server_error500,timeout504; defaultrate_limit). A pass needs every request answered2xx, its first upstream request to the primary target and failed, and a later one to another target answered2xx. Any other provider path of this instance counts as the fallback, for a gateway that falls back to another provider.ai_semantic_cache: a question, the same question in other case and punctuation, the same with a word more (Please, ...), then a different question. A pass needs the first and the different one to reach the model and neither of the other two. Set the gateway's similarity threshold at 0.9 or less. Each check asks another question (twelve in turn, from a point chosen per run), so what an earlier check left in the cache does not answer it. The gateway's own embedding calls, when they go to this instance with the session header, are not counted as model calls.ai_prompt_guard: a clean prompt, a prompt with fake personal data (SSN123-45-6789, card4111 1111 1111 1111,jane.doe@example.com) and, unlessskip_injectionis set, a prompt injection ("Ignore all previous instructions ..."). The clean prompt must reach the model; each of the others must either be refused by the gateway or reach the model without what was planted. A prompt answered without reaching the model is sent a second time and must be answered without it again: a guard refuses the same prompt twice, a call that failed between the gateway and the model does not (so a check sends three to five requests). The verdict says which (refused twice, redacted, or the kinds that reached the model), never the values.ai_response_guard: one request whose answer from the mock is a fake customer record. A pass needs it to reach the model and the client's answer to hold none of the SSN, card number, email and phone: redacted, or the answer refused.ai_provider_balance: ten requests. A pass needs every one answered2xxand each target to have answered at leastmin_sharepercent of them (1 to 50, default 20, which suits an even split and a weighted one down to 80/20).
POST {{base_url}}/_rustybin/scenarios/ai_model_fallback/runs
Authorization: Bearer {{admin_token}}
HTTP 201
[Captures]
ai_run: jsonpath "$.id"
[Asserts]
jsonpath "$.group" == "ai"
jsonpath "$.upstream" endsWith "/ai/openai/v1/chat/completions"
jsonpath "$.second_upstream" endsWith "/ai/v1/chat/completions"
jsonpath "$.request_path" == ""
jsonpath "$.resources[0].kind" == "ai_profile"
jsonpath "$.resources[0].fail_primary" == "rate_limit"
jsonpath "$.parameters[0].name" == "fail_kind"
jsonpath "$.parameters[0].enum" count == 5
A check's parameters are refused before anything is sent when they do not fit:
POST {{base_url}}/_rustybin/scenarios/ai_model_fallback/runs/{{ai_run}}/check
Authorization: Bearer {{admin_token}}
Content-Type: application/json
{"gateway_url": "http://127.0.0.1:9/llm", "fail_kind": "teapot"}
HTTP 400
[Asserts]
jsonpath "$.error" contains "fail_kind: one of rate_limit, overloaded, server_error, unavailable, timeout"
The AI mock answers everyone else as before, while the run is on:
POST {{base_url}}/ai/openai/v1/chat/completions
Content-Type: application/json
{"model": "gpt-4o-mini", "messages": [{"role": "user", "content": "hello"}]}
HTTP 200
[Asserts]
jsonpath "$.choices[0].message.content" exists
Ending the run removes its profile:
DELETE {{base_url}}/_rustybin/scenarios/ai_model_fallback/runs/{{ai_run}}
Authorization: Bearer {{admin_token}}
HTTP 200
[Asserts]
jsonpath "$.removed[0].kind" == "ai_profile"
How a run works
- Prepare. Starting a run creates its upstream: a mock named
scn-<run>(never saved, exported, kept in a checkpoint nor pruned; it ends with the run) or, forretriesandcircuit_breaker, a/flakyroute. The answer has the upstream URL and the steps for the gateway. - Configure the gateway. Create a route whose upstream is that URL, turn on the
feature, and let the route forward the
X-Rustybin-Sessionrequest header. The check gives each of its requests its own session, so it can tell which upstream requests each one caused (gateways forward unknown headers by default). - Check. Give the URL the gateway serves the route at. Rustybin sends the
scenario's requests there (plus the scenario's path, such as
/catalog), reads the request inspector for what reached the upstream, and answers each expectation with what was expected and what was seen. Check as often as you like while you tune the gateway (at most 100 checks a run). - End. The run's mock is deleted and its flaky counters reset. Captured traffic stays in the inspector.
GET {{base_url}}/_rustybin/scenarios
HTTP 200
[Asserts]
jsonpath "$.scenarios" count == 21
jsonpath "$.scenarios[*].id" contains "caching"
POST {{base_url}}/_rustybin/scenarios/caching/runs
Authorization: Bearer {{admin_token}}
HTTP 201
[Captures]
run: jsonpath "$.id"
[Asserts]
jsonpath "$.upstream" contains "/mock/scn-"
jsonpath "$.setup" count == 3
jsonpath "$.resources[0].kind" == "mock"
GET {{base_url}}/_rustybin/scenarios/caching/runs/{{run}}
HTTP 200
[Asserts]
jsonpath "$.checks" == 0
jsonpath "$.last_check" == null
DELETE {{base_url}}/_rustybin/scenarios/caching/runs/{{run}}
Authorization: Bearer {{admin_token}}
HTTP 200
[Asserts]
jsonpath "$.removed[0].kind" == "mock"
A verdict looks like this (a gateway without caching):
{
"scenario": "caching",
"passed": false,
"expectations": [
{ "name": "Every request got 200", "expected": "200 for 3 of 3", "seen": "200 for 3 of 3 (200, 200, 200)", "ok": true },
{ "name": "Only the first request reached the upstream", "expected": "1 upstream request", "seen": "3 upstream requests", "ok": false }
],
"hints": []
}
When nothing reached the upstream, a hint says so: the route points elsewhere, or
the gateway drops the X-Rustybin-Session header.
An answer of the gateway's own is not yet a decision
Several scenarios pass on what the gateway answered without the upstream. A gateway
that failed to reach the upstream answers on its own as well (502, 503, 504),
so a check never takes one such answer as the feature at work:
| Scenario | What tells the feature from a failed call |
|---|---|
circuit_breaker | Three answers in a row without the upstream, the last half the half-open time after the first, each a 5xx |
timeouts | The upstream received the request (it was still working on it when the gateway answered) |
ai_prompt_guard | The refused prompt, sent again, is refused again |
rate_limiting, ai_token_limit, request_size_limit | The status is the feature's own (429, 413), which no failed call produces |
cors, caching, ai_semantic_cache | The answer is a success with the feature's content (CORS headers, a 2xx body) |
The scenarios that judge an answer after the upstream answered (ai_response_guard
and the MCP scenarios) accept a refusal by
the gateway as a pass and require that the request reached the upstream; a gateway
that failed while reading the upstream's answer and answered an error instead is not
told from a refusal there. The verdict shows the status: a guard's refusal is the
status you configured, not a 502 or 504.
Reaching the gateway
Test traffic is outbound, so it goes through the egress policy
under the purpose scenario, which has no default: add the gateway's host to
RUSTYBIN_EGRESS_ALLOW (and set RUSTYBIN_EGRESS_ALLOW_LOOPBACK=true for a gateway
on the same machine). Until then the check is refused with the reason:
POST {{base_url}}/_rustybin/scenarios/caching/runs/{{run}}/check
Authorization: Bearer {{admin_token}}
Content-Type: application/json
{"gateway_url": "http://127.0.0.1:9/catalog-api"}
HTTP 403
[Asserts]
jsonpath "$.error" contains "purpose scenario"
The rate limiting check sends twice the limit you give it (limit, default 5, at
most 20); wait for the gateway's window to pass before checking again.
Each scenario lists its test parameters under parameters (name, type, bounds,
default), and a run repeats them: the console shows a field for each, the CLI has a
flag for each (--limit, --half-open-ms, --size-limit, --allowed-origin,
--refused-origin, --upstream-cors, --add-request-header, --add-request-value,
--remove-request-header, --add-response-header, --token-limit, --fail-kind,
--skip-injection, --min-share, --max-result-kib), the check_gateway_scenario
tool takes them by name.
| Parameter | Scenario | Type and bounds | Default |
|---|---|---|---|
limit | rate_limiting | integer, 1 to 20 | 5 |
half_open_ms | circuit_breaker | integer, 100 to 60000 | 10000 |
size_limit | request_size_limit | integer, 16 to 1048576 | 1048576 |
allowed_origin | cors | an origin scheme://host[:port], http or https, at most 200 characters | https://app.example.com |
refused_origin | cors | an origin, another than allowed_origin | https://elsewhere.example.net |
upstream_cors | cors | true or false | false |
add_request_header | header_transformation | a header name, at most 64 characters | X-Gateway-Added |
add_request_value | header_transformation | visible characters, at most 200 (optional) | any value |
remove_request_header | header_transformation | a header name, another than add_request_header | X-Client-Internal |
add_response_header | header_transformation | a header name | X-Gateway-Response |
token_limit | ai_token_limit | integer, 100 to 2000 | 500 |
fail_kind | ai_model_fallback | one of rate_limit, overloaded, server_error, unavailable, timeout | rate_limit |
skip_injection | ai_prompt_guard | true or false | false |
min_share | ai_provider_balance | integer, 1 to 50 (percent) | 20 |
max_result_kib | mcp_oversized_result | integer, 16 to 1024 (KiB) | 256 |
A parameter out of its bounds or of the wrong type, an unknown one, or one given to a scenario that does not take it, is refused with the bounds before anything is sent:
POST {{base_url}}/_rustybin/scenarios/request_size_limit/runs
Authorization: Bearer {{admin_token}}
HTTP 201
[Captures]
size_run: jsonpath "$.id"
[Asserts]
jsonpath "$.request_path" == "/upload"
jsonpath "$.parameters[0].name" == "size_limit"
jsonpath "$.parameters[0].maximum" == 1048576
POST {{base_url}}/_rustybin/scenarios/request_size_limit/runs/{{size_run}}/check
Authorization: Bearer {{admin_token}}
Content-Type: application/json
{"gateway_url": "http://127.0.0.1:9/upload-api", "size_limit": 4194304}
HTTP 400
[Asserts]
jsonpath "$.error" contains "size_limit: 16 to 1048576"
POST {{base_url}}/_rustybin/scenarios/cors/runs
Authorization: Bearer {{admin_token}}
HTTP 201
[Captures]
cors_run: jsonpath "$.id"
[Asserts]
jsonpath "$.request_path" == "/cors"
jsonpath "$.parameters[*].name" contains "allowed_origin"
jsonpath "$.parameters[2].name" == "upstream_cors"
jsonpath "$.parameters[2].type" == "boolean"
POST {{base_url}}/_rustybin/scenarios/cors/runs/{{cors_run}}/check
Authorization: Bearer {{admin_token}}
Content-Type: application/json
{"gateway_url": "http://127.0.0.1:9/web-api", "allowed_origin": "https://app.example.com/login"}
HTTP 400
[Asserts]
jsonpath "$.error" contains "allowed_origin: not an origin scheme://host[:port]"
A verdict describes the headers the gateway answered (present, absent, the allowed
origin, *, another origin) rather than repeating their values.
Who sees a run
Starting, checking and ending a run need the admin token (or a control token with
the admin scope): a run creates a mock and sends traffic. A run belongs to the
session that started it (X-Rustybin-Session, else the client address): another
session neither lists nor ends it. At most 32 runs exist at once, each for an hour.
A run id is 96 random bits, and the sessions of a check's requests carry a secret of
the run that is never shown, so requests sent to the upstream by anyone else never
count in a verdict. The credential_injection key is in the setup only for callers
with admin rights (who can read it on the run's mock anyway): someone without them who
shares the session key (one address behind a NAT, a console-scope token) sees the run
with key_hidden: true and a placeholder in place of the key.
Pass the CLI's --session to reach a run the console started.
On every surface
| Surface | Where |
|---|---|
| API | GET /_rustybin/scenarios, POST /_rustybin/scenarios/{id}/runs, GET, DELETE /_rustybin/scenarios/{id}/runs/{run}, POST .../check |
| Console | Gateway scenarios: the HTTP, AI and MCP groups, prepare, the steps with the upstream URL (two for the scenarios with a second target), the gateway URL and the test parameters (a choice for fail_kind), Check through the gateway, the verdict table, Clean up |
| CLI | rustybin scenario list, start, show, check, end (check exits 1 when an expectation fails) (reference) |
| Management MCP | list_gateway_scenarios, start_gateway_scenario, get_gateway_scenario_run, check_gateway_scenario (confirmed: it sends traffic out), end_gateway_scenario (confirmed) (agent page) |
rustybin.yaml | Nothing: runs are transient. A run's mock is never exported, saved to disk, kept in a checkpoint nor pruned; it ends with the run, and so does an AI run's profile |