Mocking API calls

Mocking API calls

Sequence API offers the ability to integrate API calls that do not yet exist in sequences with real API calls. This means you can develop workflows and tests that incrementally work as you flesh out the implementation.

For this exercise, we are going to use the following example, taken from this collection.

The mock operates at the level of an endpoint. An endpoint is treated as a mock just in case one of two conditions exist:

  1. The endpoint attribute mock is set to true.

2. The endpoint has no url defined. This is true if a url is neither defined, in the endpoint itself, nor within the dictionary for the sequence.

The mock uses the status and output goals for the endpoint as the response. Every endpoint implicitly has a 200 status response goal but no output goal. Therefore, you need to explicitly set these if you wish something different. In this example, I want to mock an endpoint that takes a user ID (sub) and password, and returns a formatted user token (a bad idea in real life, please don't send passwords over the wire!).

In this particular example, the endpoint outputs the response so that it is available to subsequent endpoints as the variable lorettaToken.

That is to say, the output of the mock, a formatted user token, underpins the execution of each remaining API call as it is passed as part of the authorization header.

When you run a sequence that includes mocks, they are clearly marked:

And in the summary after the run:

The editing screen also highlights mocks:

Mocks are a powerful development technique that allow a test-driven approach to API or serverless development. In Sequence API they are easy.