Mocking API calls using WireMock

Running Wiremock in a Docker container

docker run -it --rm -p 8080:8080 rodolpheche/wiremock
/$$      /$$ /$$                     /$$      /$$                     /$$
| $$ /$ | $$|__/ | $$$ /$$$ | $$
| $$ /$$$| $$ /$$ /$$$$$$ /$$$$$$ | $$$$ /$$$$ /$$$$$$ /$$$$$$$| $$ /$$
| $$/$$ $$ $$| $$ /$$__ $$ /$$__ $$| $$ $$/$$ $$ /$$__ $$ /$$_____/| $$ /$$/
| $$$$_ $$$$| $$| $$ \__/| $$$$$$$$| $$ $$$| $$| $$ \ $$| $$ | $$$$$$/
| $$$/ \ $$$| $$| $$ | $$_____/| $$\ $ | $$| $$ | $$| $$ | $$_ $$
| $$/ \ $$| $$| $$ | $$$$$$$| $$ \/ | $$| $$$$$$/| $$$$$$$| $$ \ $$
|__/ \__/|__/|__/ \_______/|__/ |__/ \______/ \_______/|__/ \__/

port: 8080
enable-browser-proxying: false
disable-banner: false
no-request-journal: false
verbose: false
{
"mappings": [],
"meta": {
"total": 0
}
}

Adding mappings using the API

GET Request

curl --location --request POST 'http://localhost:8080/__admin/mappings/new' \
--header 'Content-Type: application/json' \
--data-raw '{
"request": {
"method": "GET",
"url": "/api/test"
},
"response": {
"status": 200,
"body": "{ \"test\": \"data\" }",
"headers": {
"Content-Type": "application/json"
}
}
}'
{
"test": "data"
}

POST Request

curl --location --request POST 'http://localhost:8080/__admin/mappings/new' \
--header 'Content-Type: application/json' \
--data-raw '{
"request": {
"method": "POST",
"url": "/api/test",
"bodyPatterns" : [{
"matchesJsonPath" : {
"expression": "$",
"equalToJson": "{ \"test\": \"banana\" }"
}
}]
},
"response": {
"status": 201,
"body": "{ \"test\": \"banana created\" }",
"headers": {
"Content-Type": "application/json"
}
}
}'
curl --location --request POST 'http://localhost:8080/__admin/mappings/new' \
--header 'Content-Type: application/json' \
--data-raw '{
"request": {
"method": "POST",
"url": "/api/test",
"bodyPatterns" : [{
"matchesJsonPath" : {
"expression": "$",
"equalToJson": "{ \"test\": \"apple\" }"
}
}]
},
"response": {
"status": 201,
"body": "{ \"test\": \"apple created\" }",
"headers": {
"Content-Type": "application/json"
}
}
}'
curl --location --request POST 'http://localhost:8080/api/test' \
--header 'Content-Type: application/json' \
--data-raw '{
"test": "banana"
}'

curl --location --request POST 'http://localhost:8080/api/test' \
--header 'Content-Type: application/json' \
--data-raw '{
"test": "apple"
}'

Resetting Mappings

curl --location --request POST 'http://localhost:8080/__admin/reset'

Adding mappings using files

version: '3.4'

services:
wiremock:
image: rodolpheche/wiremock
container_name: wiremock
volumes:
- ./__files:/home/wiremock/__files
- ./mappings:/home/wiremock/mappings
ports:
- 8080:8080
{
"request": {
"url": "/api/test",
"method": "GET"
},
"response": {
"status": 200,
"body": "{ \"test\": \"data\" }",
"headers": {
"Content-Type": "application/json"
}
}
}
curl --location --request POST 'http://localhost:8080/__admin/mappings/save'
curl --location --request POST 'http://localhost:8080/__admin/reset'
<!DOCTYPE html>
<html>
<body>
<h1>Mocked File</h1>
<p>Something interesting here</p>
</body>
</html>

Final Thoughts

--

--

Software Developer @ https://www.alexhyett.com

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store