Mock API Responses in Azure API Management

Mock API responses in Azure API Management (APIM) are a powerful feature that allows developers to simulate API behavior without needing to implement the actual backend service. This is particularly useful during the development, testing, and documentation phases of an API lifecycle. By mocking API responses, you can:
  1. Test API Consumers: Allow frontend or client developers to test their applications against a simulated API.
  2. Parallel Development: Enable backend and frontend teams to work in parallel by providing a mock API.
  3. Documentation: Provide example responses for API documentation.
  4. Error Handling: Simulate error responses to test how clients handle failures.
In Azure API Management, you can create mock APIs using policies. These policies allow you to define static responses or dynamic responses based on conditions.

Steps to Mock API Responses in Azure API Management

1. Create an API in Azure API Management

  • Go to the Azure portal and navigate to your API Management instance.
  • Under the APIs section, click Add API.
  • Choose a blank API or import an existing API definition (e.g., OpenAPI/Swagger).
  • Define the API’s operations (e.g., GET /usersPOST /users).

2. Add Mocking Policies

Azure API Management uses policies to mock responses. Policies are XML-based configurations that define how the API behaves. Here’s an example of a mock policy for a GET /users operation:
<policies>
    <inbound>
        <base />
        <!-- Mock response for GET /users -->
        <mock-response status-code="200" content-type="application/json">
            <set-body>
                {
                    "users": [
                        { "id": 1, "name": "John Doe" },
                        { "id": 2, "name": "Jane Smith" }
                    ]
                }
            </set-body>
        </mock-response>
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

3. Test the Mock API

  • After adding the policy, save and test the API using the Test tab in the Azure portal.
  • Send a GET request to /users. You should receive the mocked response:
    {
        "users": [
            { "id": 1, "name": "John Doe" },
            { "id": 2, "name": "Jane Smith" }
        ]
    }

Advanced Mocking with Conditions

You can use conditions to return different responses based on query parameters, headers, or other request data.

Example: Conditional Mocking

Suppose you want to return a 404 Not Found response if a user ID is not found. Here’s how you can do it:
<policies>
    <inbound>
        <base />
        <!-- Check for a specific user ID -->
        <choose>
            <when condition="@(context.Request.MatchedParameters["id"] == "1")">
                <mock-response status-code="200" content-type="application/json">
                    <set-body>
                        {
                            "id": 1,
                            "name": "John Doe"
                        }
                    </set-body>
                </mock-response>
            </when>
            <otherwise>
                <mock-response status-code="404" content-type="application/json">
                    <set-body>
                        {
                            "error": "User not found"
                        }
                    </set-body>
                </mock-response>
            </otherwise>
        </choose>
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>
  • If you call GET /users/1, you’ll get:
    {
        "id": 1,
        "name": "John Doe"
    }
  • If you call GET /users/2, you’ll get:
    {
        "error": "User not found"
    }

Live Example: Mocking a Weather API

Let’s say you want to mock a weather API that returns temperature data based on a city name.

1. Define the API

  • Create an API with an operation GET /weather.
  • Add a query parameter city.

2. Add Mocking Policy

<policies>
    <inbound>
        <base />
        <choose>
            <when condition="@(context.Request.Url.Query.GetValueOrDefault("city") == "London")">
                <mock-response status-code="200" content-type="application/json">
                    <set-body>
                        {
                            "city": "London",
                            "temperature": 15,
                            "unit": "Celsius"
                        }
                    </set-body>
                </mock-response>
            </when>
            <when condition="@(context.Request.Url.Query.GetValueOrDefault("city") == "New York")">
                <mock-response status-code="200" content-type="application/json">
                    <set-body>
                        {
                            "city": "New York",
                            "temperature": 22,
                            "unit": "Celsius"
                        }
                    </set-body>
                </mock-response>
            </when>
            <otherwise>
                <mock-response status-code="404" content-type="application/json">
                    <set-body>
                        {
                            "error": "City not found"
                        }
                    </set-body>
                </mock-response>
            </otherwise>
        </choose>
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

3. Test the API

  • Call GET /weather?city=London:
    {
        "city": "London",
        "temperature": 15,
        "unit": "Celsius"
    }
  • Call GET /weather?city=Paris:
    {
        "error": "City not found"
    }

Benefits of Mocking in Azure API Management

  1. Faster Development: Frontend and backend teams can work independently.
  2. Cost-Effective: No need to deploy or maintain backend services during early development.
  3. Improved Testing: Simulate various scenarios, including error conditions.
  4. Better Documentation: Provide realistic examples for API consumers.
By leveraging Azure API Management’s mocking capabilities, you can streamline API development and testing while ensuring a seamless experience for API consumers.
Post a comment

Leave a Comment

Scroll to Top