How to build an online mock REST API in WireMock Cloud for functional, integration and performance testing
GET /v1/contacts
with JSON like:
GET
with the exact
URL path /v1/contacts
. Go to the Stubs page under your new mock API and hit the
new stub button:
GET
, the URL to /v1/contacts
and the URL match type to Path
:
Content-Type: application/json
header:
/v1/contacts?companyId=123
would return:
companyId
equalling 123
:
equalTo
and contains
).
GET
to /v1/contacts/22222
.
We can stub a single data item in a very similar manner to the contact list we
created first, relying on exact URL path equality to match the request:
Path regex
URL match type with the regular expression /v1/contacts/[0-9]{1,10}
as the value.
This will match any URL path starting with /v1/contacts
and ending with any
numeric ID between 1 and 10 characters long:
id
is now set from the 3rd segment of the incoming request URL’s path, so it will always be the same as the requested contact ID.firstName
, lastName
and email
are now random alphanumeric text (with a fixed domain name in the case of email
).dateAdded
is set to 3 months before today’s date.POST
request to the URL for a collection resource as a means to add new data items.
So our contact manager might accept POST /v1/contacts
, returning a
response with status code 201 Created
and an empty body:
201
response.
If we want to ensure the stub is only matched when correctly structured JSON is
sent in the request but without requiring a set of exact values, we can add a body
matcher by clicking New body matcher and using JSONUnit as wildcards:
name
instead of
firstName
and lastName
), we’ll get a 404 Not Found
response
containing a diff report showing which part of the request didn’t match:
GET
request for
the collection or the individual resource. However, mock APIs by default don’t
store any state, so making a request to add a new contact will have no effect on
the data returned later.
For most testing scenarios this isn’t an issue, but in cases where more realistic
behaviour is required WireMock Cloud supports the concept of “stateful scenarios” whereby
the state of a scenario can be used to determine which stub to match.
If we wanted to, for instance, create a test case in which posting a new company
results in it appearing in the companies collection we can achieve this by creating three
stubs, all associated with the same scenario.
Firstly, we’d stub the initial response for GET /v1/companies
(in much the same manner as we did
for contacts), returning a single company in the collection:
GET
stub cloned from the first but with a second
company added to the collection:
POST
to advance the state of the scenario
so that it appears to have the effect of storing the new company:
GET
our companies we should see a single item in the collection:
POST
a new company:
503 Service Unavailable
response instead of the expected 201
.
If we configure a stub that expects specific data in the request body and give it
a higher priority than the existing POST
stub that returns 201
then we can
send a request with appropriate data and see the error returned.
Start by cloning the existing POST
stub for new contacts. Change the Priority value to a number
lower than 5
(1
is highest).
equalToJson
body match we already have and this would
work fine if we were confident our test could produce exactly the same values each time.
However, we can give ourselves a bit more flexibility by choosing one specific bit of
data and matching it using matchesJsonPath
.
Let’s say that if we receive a specific contact ID then we’ll trigger the error.
To do this, change the body match type to matchesJsonPath
and the expression to
$.contact.id
equalTo
666
:
503
, and let’s also add a plain text
error message supported by a Content-Type: text/plain
header:
note The message in the red text in this case indicates that WireMock Cloud couldn’t automatically generate a valid request body to match our JSONPath expression. This can be safely ignored as we’ve created our own request body.