# Backend mocking
## Prerequisites:
- docker
- docker compose
These tools are part of the [Docker Desktop](https://www.docker.com/products/docker-desktop/) which should be sufficient for local development
## Toolset:
- Stoplight Prism v5
#### Stoplight
Stoplight is a platform for API design and development that includes a feature for generating a mock server. The mock server in Stoplight can be automatically generated from an OpenAPI specification, providing developers with a fast and simple way to create a simulated version of their API. With Prism, Stoplight’s open-source HTTP mock server, you can run fully functional fake API servers directly from an OpenAPI specification or Postman file.
Stoplight provide two types of fake responses:
- Static — useful for testing a specific request against a specific static JSON response.
- Dynamic — generates a random fake JSON response based on the schema from your API specification file.
## Set up
### Step 1: Create an OpenAPI Specification
First, you need an OpenAPI specification file. Let's assume you have an OpenAPI specification named `openapi-proposal.yml`. Make sure this file is in the root directory of your project.
### Step 2: Create a Docker Compose File
Create a `docker-compose.yml` file to define the services you'll use. In this case, you'll define a service for Stoplight Prism. Here's a simple example:
```yaml=
version: '3.9'
services:
prism:
image: stoplight/prism:5
command: 'mock -d -h 0.0.0.0 /tmp/openapi.yml'
volumes:
- ./openapi_proposal.yml:/tmp/openapi.yml:ro
ports:
# Serve the mocked API locally as available on port 4010
- '4010:4010'
```
This configuration:
- Uses the official Stoplight Prism Docker image.
- Maps the local (openapi_proposal.yml) OpenAPI specification file into the container.
- Specifies the command to start Prism in mock mode, dynamically (-d flag) generating responses based on the specification.
- Exposes port 4010 for accessing the mock service.
### Step 3: Start the Mock Service
Run the following command in your terminal to start the Stoplight Prism service:
```bash=
docker compose up -d
```
This will download the Prism Docker image and start the mock service in the background.
In case you using several docker-compose configurations you may need to save the composer file by different name (e.g. `docker-compose-prism.yml`). In this case you should specify the file your docker compose should use for setting up the mock service.
E.g.:
```bash=
docker compose -f docker-compose-prism.yml up -d
```
Info: `-d` flag indicates "detached" state, which means that the service will be running in the background. If you need to have it as an active process in the terminal (e.g. to see logs without attaching to background service) you should remove this flag.
### Step 4: Access the Mock Service
Once the service is running, you can access the mock service at http://localhost:4010. You can make requests to the defined endpoints in your OpenAPI specification, and Prism will respond with dynamically generated mock data.
### Step 5: Stop the Mock Service
When you're done testing, you can stop the Docker containers by running:
```bash=
docker compose down
```
In case you using several docker-compose configurations you may need to save the composer file by different name (e.g. `docker-compose-prism.yml`). In this case you should specify the file your docker compose should use for tearing down the mock service. E.g.:
```bash=
docker compose -f docker-compose-prism.yml down
```
This will stop and remove the containers.
## Additional Notes:
- Ensure that Docker is installed on your machine before starting this process.
- Make sure your OpenAPI specification is correctly defined to ensure accurate mock responses.
- Remember that while Prism can dynamically generate responses based on the OpenAPI specification, the quality of the responses depends on the quality and accuracy of your specification. It's a great tool for quick testing and development.