Mock and test

Mock the API, then test the real one

The front end is ready before the server is. Mock the contract on day one. When the server arrives, switch the mock into a validating proxy, and every response that passes through it is checked against the contract.

Start a mock serverInstall API Genie

A response with the wrong type, caught

The mock answers from the contract's examples and says so in a header. The same server with --proxy-to forwards every request to the real server and checks both directions: here a response with a string id where the contract requires an integer, named in the headers and counted in the contract report. With --report contract.json --fail-on errors the run writes the report and exits with 5 when it stops after an error.

$ apigenie-server openapi.yaml &
Listening on http://127.0.0.1:4010/v1 (spec: openapi.yaml, 12 operations)
$ curl -si http://127.0.0.1:4010/v1/pets/42 | grep "^HTTP\|X-Apigenie"
HTTP/1.1 200 OK
X-Apigenie-Mock-Operation: getPet
X-Apigenie-Mock-Source: example:cat

$ curl -s -X POST http://127.0.0.1:4010/__apigenie/shutdown
$ apigenie-server openapi.yaml --proxy-to http://localhost:8080 &
$ curl -si http://127.0.0.1:4010/v1/pets/42 | grep X-Apigenie
X-Apigenie-Mock-Source: proxy
X-Apigenie-Mock-Operation: getPet
X-Apigenie-Mock-Violations: 1
X-Apigenie-Mock-Violations-Detail: [{"location":["response","body","/id"],"check":"response.body.incompatible","code":"type","severity":"error","message":"expected type integer, found string"}]

What it covers

  • MocksExamples, stubs, generated data, CRUD resources and scripts.
  • Validating proxyCoverage and JSON or JUnit contract reports for CI.
  • apigenie test.http files and generated requests, checked against the contract.
  • Record and replayA recorded session, replayed in tests without the real services.
  • DashboardTraffic, operations, scenarios and the contract report in the browser.
  • Four kinds of APIOpenAPI and Swagger, AsyncAPI, gRPC and GraphQL.

The tools that do it

Step by step

Mock and contract-test an API: Serve an API from its contract before the server exists, then check the real server against the contract in CI.

The mock on day one, the proxy on release day

apigenie-server serves the contract now. Later, the same command with --proxy-to checks the real server, and --report with --fail-on makes the run a CI check.

Other use cases