Skip to content

kassah/mockserver-pres

Repository files navigation

MockServer

Purpose

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.

Usage

Starting up

make up will start mockserver and an nginx proxy in front of it.

Shutting down

make down will shutdown mockserver and it's nginx proxy.

Dashboard

make ui will pop the browser to the mockserver dashboard.

Clear

make clear will clear out logs so that you can isolate the requests generated by a specific action.

REST API

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

Dependencies Update

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.

Configuration

Configuring Domains

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.

Bypass MockServer for a domain

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!

Forward Requests for a domain

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.

Configuring Expectations

All expectations that the server starts with are pre-loaded in JSON by mockserver/initializerJson.json

About

Presentation to demonstrate MockServer

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published