Add rate limits to a mock api
Rate limits are defined in your mock api settings page. You can choose one of your rate limits to be the default rate limit for the mock API, which means it will apply to all stubs, unless a different rate limit is selected for a specific stub.
Add rate limit to a stub
Rate limits can be applied to a stub in the “Response” section.
Creating a rate limiter via API
A rate limiter is defined by an object in your mock API’s settings document. The JSON attribute key is then used to apply the rate limiter to specific stub mappings. A rate limiter has two mandatory parameters:unit
- the time unit the rate is being expressed in e.g.nanoseconds
,seconds
,minutes
rate
- the number of requests per the time unit permitted e.g.15
burst
- the number of requests that can be made in a burst over the set rate limit
PUT
request to https://<your mock API>.wiremockapi.cloud/__admin/ext/settings/extended/rateLimits
containing the JSON object configuring all of your rate limits e.g.
Applying to your stubs
To rate limit a particular stub according to one of your named configurations you need to create or edit the stub via the API, so that you can enable therate-limit
transformer and set the name of the rate limit to be used.
You do this by POST
ing the JSON to https://<your mock API>.wiremockapi.cloud/__admin/mappings
.
Taking the above example, if I wanted to use the “authentication” rate limit in my
login handler stub, I’d do as follows:
rate-limit
element in the transformers
array,
and "rateLimitName": "authentication"
under transformerParameters
.
Once you’ve created a stub this way you will start to see 429 responses when the
request rate to all stubs associated with the named rate limit exceeds the limit.