WireMock is a tool for mocking HTTP-based APIs that runs in the unit tests, on the desktop, or in the test environment. Read more about WireMock in this article: Introduction to WireMock. One of the most important features of WireMock is Request Matching. WireMock supports matching of requests to stubs and verification queries using the following attributes: URL, HTTP Method, Query parameters, Headers, Cookies, Request body, etc. So in this article, we are going to see how Request Matching works with JSON Mappings in WireMock.
Prerequisites:
- Refer to this article and install WireMock on your local machine: How to Download and Install WireMock?
- Refer to this article and install Postman on your local machine: How to Download and Install Postman on Windows?
- Create Your First Mocked API Endpoint with WireMock
1. URL Matching
In WireMock, the URLs can be matched either by equality or by regular expression. And you can match just the path of the URL or the path and query together. Let’s see both scenarios with examples.
Example: Equality matching on the path only
Syntax:
{ "request": { "urlPath": "provide your url here" ... }, ... }
Now let’s demonstrate this with the help of Postman. Open your Postman and send a POST request to the following URL
http://<host>:<port>/__admin/mappings
with the following JSON request body
{ "request": { "method": "GET", "urlPath": "put your url as per your requirement" }, "response": { "status": 200, "body": "put your body here" } }
For example in this article our URL is
URL:
http://localhost:8080/__admin/mappings
And the JSON request body is
Request Body:
{ "request": { "urlPath": "/gfg/user1", "method": "GET" }, "response": { "status": 200, "body": "Welcome to Lazyroar!" } }
Click on the Send button now and in the Response, you can see the following Response Body with the status code 201 Created.
Response Body:
{ "id": "0eb73cc1-2070-44be-a1c9-3db40a97cea9", "request": { "urlPath": "/gfg/user1", "method": "GET" }, "response": { "status": 200, "body": "Welcome to Lazyroar!" }, "uuid": "0eb73cc1-2070-44be-a1c9-3db40a97cea9" }
Please refer to the below image if you are stuck somewhere
Now, let’s test our Stubbed API. Again open a new tab in Postman and send a GET request to the following URL
http://localhost:8080/gfg/user1
And you are going to get the following response in the Response body.
Welcome to Lazyroar!
Please refer to the below image if you are stuck somewhere
Example: Regex matching on the path only
Syntax:
{ "request": { "urlPathPattern": "/your/([a-z1-9]*)" ... }, ... }
Now let’s demonstrate this with the help of Postman. Open your Postman and send a POST request to the following URL
http://localhost:8080/__admin/mappings
And the JSON request body is
Request Body:
{ "request": { "urlPathPattern": "/gfg/([a-z1-9]*)", "method": "GET" }, "response": { "status": 200, "body": "Welcome to Lazyroar Portal!" } }
Click on the Send button now and in the Response, you can see the following Response Body with the status code 201 Created.
Response Body:
{ "id": "ed9fe262-2b79-4862-9889-39378d553a35", "request": { "urlPathPattern": "/gfg/([a-z1-9]*)", "method": "GET" }, "response": { "status": 200, "body": "Welcome to Lazyroar Portal!" }, "uuid": "ed9fe262-2b79-4862-9889-39378d553a35" }
Please refer to the below image if you are stuck somewhere
Now, let’s test our Stubbed API. Again open a new tab in Postman and send a GET request to the following URL
http://localhost:8080/gfg/user1345689
Note: You can put anything lies in a-z and 1-9 after /gfg/
And you are going to get the following response in the Response body.
Welcome to Lazyroar Portal!
Please refer to the below image if you are stuck somewhere
Similarly, you can also try the demonstration in your Postman for the following Request Matching.
Equality matching on path and query
Syntax:
{ "request": { "url": "/your/url?and=query" ... }, ... }
Regex matching on path and query
Syntax:
{ "request": { "urlPattern": "/your/([a-z1-9]*)\\?and=query" ... }, ... }
2. Matching Other Attributes
Apart from the URL, all request attributes can be matched using some operators.
Equality
Syntax:
{ "request": { ... "headers": { "Content-Type": { "equalTo": "application/xml" } } ... }, ... }
Case-insensitive equality
Syntax:
{ "request": { ... "headers": { "Content-Type": { "equalTo": "application/xml", "caseInsensitive": true } } ... }, ... }
3. JSON Equality
It will be a match if the attribute is valid JSON and is a semantic match for the expected value.
Syntax:
{ "request": { ... "bodyPatterns" : [ { "equalToJson" : { "name": "Lazyroar" } } ] ... }, ... }
4. XML Equality
It will be a match if the attribute value is valid XML and is semantically equal to the expected XML document.
Syntax:
{ "request": { ... "bodyPatterns" : [ { "equalToXml" : "<thing>Lazyroar</thing>" } ] ... }, ... }
5. Basic Authentication Matching
In WireMock, you can also match on HTTP basic authentication.
Syntax:
{ "request": { "method": "GET", "url": "/basic-auth", "basicAuth": { "username": "your user name", "password": "your password" } }, "response": { "status": 200 } }