Home Explore Blog CI



docker

2nd chunk of `content/guides/wiremock.md`
6d3a64bc93f51f746e779c63f3a7b73cea7bee0afc6d7efe0000000100000fbd
    ![Diagram showing the WireMock container running on Docker Desktop ](./images/wiremock-using-docker.webp)


    You can check the logs by selecting the `wiremock-node-docker` container:

    ![Diagram showing the logs of WireMock container running on Docker Desktop ](./images/wiremock-logs-docker-desktop.webp)

 4. Test the Mock API.

    ```console
    $ curl http://localhost:8080/api/v1/getWeather\?city\=Bengaluru
    ```
 
    It will return the following canned response with mock data:

    ```plaintext
    {"city":"Bengaluru","temperature":27.1,"conditions":"Mostly cloudy","forecasts":[{"date":"2024-09-02T07:00:00+05:30","temperature":83,"conditions":"Partly sunny w/ t-storms"},{"date":"2024-09-03T07:00:00+05:30","temperature":83,"conditions":"Thunderstorms"},{"date":"2024-09-04T07:00:00+05:30","temperature":83,"conditions":"Intermittent clouds"},{"date":"2024-09-05T07:00:00+05:30","temperature":82,"conditions":"Dreary"},{"date":"2024-09-06T07:00:00+05:30","temperature":82,"conditions":"Dreary"}]}
    ```

    With WireMock, you define canned responses using mapping files.
    For this request, the mock data is defined in the JSON file at
    `wiremock-endpoint/mappings/getWeather/getWeatherBengaluru.json`.
    
    For more information about stubbing canned responses, refer to the
    [WireMock documentation](https://wiremock.org/docs/stubbing/).


## Using WireMock in development

Now that you have tried WireMock, let’s use it in development and testing. In this example, you will use a sample application that has a Node.js backend. This app stack has the following configuration:

  - Local Development Environment: The context in which the Node.js backend and WireMock are running.
  - Node.js Backend: Represents the backend application that handles HTTP requests.
  - External AccuWeather API: The real API from which live weather data is fetched.
  - WireMock: The mock server that simulates the API responses during testing. It runs as a Docker container.

  ![Diagram showing the architecture of WireMock in development ](./images/wiremock-arch.webp)

  - In development, the Node.js backend sends a request to WireMock instead of the actual AccuWeather API.
  - In production, it connects directly to the live AccuWeather API for real data.

## Use mock data in local development

Let’s set up a Node app to send requests to the WireMock container instead of the actual AccuWeather API.

### Prerequisite

- Install [Node.js and npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm)
- Ensure that WireMock container is up and running (see [Launching Wiremock](#launching-wiremock)


Follow the steps to setup a non-containerized Node application:

1. Navigate to the `accuweather-api` directory

   Make sure you're in the directory where your `package.json` file is located.

2. Set the environment variable.

   Open `.env` file placed under `accuweather-api/` directory. Remove the old entries and ensure that it just contains the following single line.
 
   ```plaintext
   API_ENDPOINT_BASE=http://localhost:8080
   ```

   This will tell your Node.js application to use the WireMock server for API calls. 

3. Examine the Application Entry Point

   - The main file for the application is `index.js`, located in the `accuweather-api/src/api` directory.
   - This file starts the `getWeather.js` module, which is essential for your Node.js application. It uses the `dotenv` package to load environment variables from the`.env` file.
   - Based on the value of `API_ENDPOINT_BASE`, the application routes requests either to the WireMock server (`http://localhost:8080`) or the AccuWeather API. In this setup, it uses the WireMock server.
   - The code ensures that the `ACCUWEATHER_API_KEY` is required only if the application is not using WireMock, enhancing efficiency and avoiding errors.


    ```javascript
    require("dotenv").config();

    const express = require("express");
    const axios = require("axios");

    const router = express.Router();

Title: Using WireMock in Development and Testing
Summary
This section explains how to use WireMock in development, focusing on a Node.js application example. It details setting up a local development environment where the Node.js backend sends requests to the WireMock container instead of the actual AccuWeather API, using mock data. The steps include navigating to the `accuweather-api` directory, setting the `API_ENDPOINT_BASE` environment variable to point to the WireMock server, and examining the application's entry point (`index.js`) to understand how it routes requests based on the environment.