MockServer intercepts external API calls, captures them and responds in an appropriate manner. This assists with local development so that it doesn't actually hit the remote API for calls that would happen during normal testing and development of the application.
make up
will start mockserver and an nginx proxy in front of it.
make down
will shutdown mockserver and it's nginx proxy.
make ui
will pop the browser to the mockserver dashboard.
make clear
will clear out logs so that you can isolate the requests generated by a specific action.
From development prompt, the MockServer API can be used via https://mockserver.lightningdev.com/mockserver/".
For instance to clear all logs and requests:
curl -v -X PUT "https://mockserver.lightningdev.com/mockserver/clear?type=log"
For info on the API, see docs: http://www.mock-server.com/mock_server/clearing_and_resetting.html
In order to tell PHP that it has a valid SSL, it has to override the valid CAs in our docker-compose.yaml
These files are tracked within the mockserver
directory. To update them to the latest versions run the make mockserver-cert
command.
In order to configure the domains that the mockserver responds to, you will need to modify
docker-compose.yml
.
- Modify services.mockserver.networks.default.aliases to include the domain.
- Modify services.mockserver.entrypoint[5] to add the domain to the SSL cert generation.
When you want to bypass MockServer and allow the app to connect to the service in question directly,
edit the docker-compose.yml
and remove the dns from services.mockserver.networks.default.aliases
temporarily.
Please remember to not commit this bypass!
Let's set an environment variable for our domain
DOMAIN="api.externalservice.com"
Start by clearing every expectation for the domain:
curl -v -X PUT "https://mockserver.lightningdev.com/mockserver/clear" -d "
{
'headers': {
'host': [
'${DOMAIN}'
]
}
}
"
Use one of the IPs to hardwire an IP by adding the follow expectation:
curl -v -X PUT "https://mockserver.lightningdev.com/mockserver/expectation" -d "
{
'httpRequest': {
'headers': {
'host': [
'${DOMAIN}'
]
}
},
'httpForward': {
'host': '${DOMAINIP}',
'port': 443,
'scheme': 'HTTPS'
}
}
"
Using this method instead of bypassing the MockServer entirely allows MockServer Dashboard to still record your and display your requests without getting in the way of your requests submission to the external API.
All expectations that the server starts with are pre-loaded in JSON by mockserver/initializerJson.json