Skip to content

Commit 0b2b4df

Browse files
authored
Merge pull request #296679 from v-albemi/mock-api-responses
AI Freshness Edit: Azure API Management
2 parents c510b7e + f775cb1 commit 0b2b4df

File tree

4 files changed

+63
-71
lines changed

4 files changed

+63
-71
lines changed
-5.55 KB
Loading
-23.7 KB
Loading
-939 Bytes
Loading
Lines changed: 63 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,36 @@
11
---
22
title: Tutorial - Mock API responses in API Management - Azure portal | Microsoft Docs
3-
description: In this tutorial, you use API Management to set a policy on an API. The policy returns a mocked response even if the backend isn't available to send real responses.
3+
description: Use Azure API Management to set a policy on an API. The policy returns a mock response even if the backend isn't available to send real responses.
44

55
author: dlepow
66
ms.service: azure-api-management
77
ms.custom: mvc, devx-track-azurecli, devdivchpfy22
88
ms.topic: tutorial
9-
ms.date: 12/17/2021
9+
ms.date: 03/24/2025
1010
ms.author: danlep
1111

12+
13+
#customer intent: As a developer, I want to set a policy on an API so that a mock response is returned.
1214
---
15+
1316
# Tutorial: Mock API responses
1417

1518
[!INCLUDE [api-management-availability-all-tiers](../../includes/api-management-availability-all-tiers.md)]
1619

17-
Backend APIs are imported into an API Management (APIM) API or created and managed manually. The steps in this tutorial, show you how to:
20+
Backend APIs are imported into an Azure API Management API or created and managed manually. The steps in this tutorial describe how to:
1821

19-
+ Use API Management to create a blank HTTP API
20-
+ Manage an HTTP API manually
21-
+ Set a policy on an API so it returns a mocked response
22+
+ Use API Management to create a blank HTTP API.
23+
+ Manually manage an HTTP API.
24+
+ Set a policy on an API so that it returns a mock response.
2225

23-
This method lets developers continue with the implementation and testing of the API Management instance even if the backend isn't available to send real responses.
26+
This method enables developers to continue with the implementation and testing of the API Management instance even if the backend isn't available to send real responses.
2427

2528
[!INCLUDE [api-management-workspace-try-it](../../includes/api-management-workspace-try-it.md)]
2629

27-
The ability to mock up responses is useful in many scenarios:
30+
The ability to create mock responses is useful in many scenarios:
2831

29-
+ When the API façade is designed first and the backend implementation comes later. Or, the backend is being developed in parallel.
30-
+ When the backend is temporarily not operational or not able to scale.
32+
+ When the API façade is designed first and the backend implementation occurs later, or when the backend is being developed in parallel.
33+
+ When the backend is temporarily not operational or is not able to scale.
3134

3235
In this tutorial, you learn how to:
3336

@@ -37,63 +40,63 @@ In this tutorial, you learn how to:
3740
> * Enable response mocking
3841
> * Test the mocked API
3942
40-
:::image type="content" source="media/mock-api-responses/mock-api-response-02.png" alt-text="Mocked API response":::
43+
:::image type="content" source="media/mock-api-responses/mock-api-response-02.png" alt-text="Screenshot that shows the APIs page in the Azure portal." lightbox="media/mock-api-responses/mock-api-response-02.png":::
4144

4245
## Prerequisites
4346

44-
+ Learn the [Azure API Management terminology](api-management-terminology.md).
45-
+ Understand the [concept of policies in Azure API Management](api-management-howto-policies.md).
46-
+ Complete the following quickstart: [Create an Azure API Management instance](get-started-create-service-instance.md).
47+
+ Learn [API Management terminology](api-management-terminology.md).
48+
+ Understand the [concept of policies in API Management](api-management-howto-policies.md).
49+
+ Complete the quickstart [Create an Azure API Management instance](get-started-create-service-instance.md).
4750

4851
## Create a test API
4952

5053
The steps in this section show how to create an HTTP API with no backend.
5154

5255
1. Sign in to the Azure portal, and then navigate to your API Management instance.
53-
1. Select **APIs** > **+ Add API** > **HTTP** tile.
56+
1. Select **APIs** > **+ Add API** > **HTTP** tile:
5457

55-
:::image type="content" source="media/mock-api-responses/http-api.png" alt-text="Define a HTTP API":::
58+
:::image type="content" source="media/mock-api-responses/http-api.png" alt-text="Screenshot that shows the first steps for defining an API." lightbox="media/mock-api-responses/http-api.png":::
5659

5760
1. In the **Create an HTTP API** window, select **Full**.
58-
1. Enter *Test API* for **Display name**.
59-
1. Select **Unlimited** for **Products**.
60-
1. Ensure that **Managed** is selected for **Gateways**.
61+
1. In **Display name**, enter *Test API*.
62+
1. In **Products**, select *Unlimited*, if that value is available. This value is available only in some tiers. You can leave the value blank for this tutorial, but you need to associate the API with a product to publish it. For more information, see [Import and publish your first API](import-and-publish.md#import-and-publish-a-backend-api).
63+
1. In **Gateways**, select **Managed** if this option is available. (This option is avaiable only in certain service tiers.)
6164
1. Select **Create**.
6265

63-
:::image type="content" source="media/mock-api-responses/create-http-api.png" alt-text="Create an HTTP API":::
66+
:::image type="content" source="media/mock-api-responses/create-http-api.png" alt-text="Screenshot that shows the Create an HTTP API window." lightbox="media/mock-api-responses/create-http-api.png":::
6467

6568
## Add an operation to the test API
6669

67-
An API exposes one or more operations. In this section, you'll add an operation to the HTTP API you created. Calling the operation after completing the steps in this section triggers an error. After you complete the steps in the [Enable response mocking](#enable-response-mocking) section, you'll get no errors.
70+
An API exposes one or more operations. In this section, you add an operation to the HTTP API you created. Calling the operation after completing the steps in this section triggers an error. After you complete the steps in the [Enable response mocking](#enable-response-mocking) section, you won't get an error.
6871

6972
### [Portal](#tab/azure-portal)
7073

71-
1. Select the API you created in the previous step.
74+
1. Select the API that you created in the previous step.
7275
1. Select **+ Add Operation**.
73-
1. In the **Frontend** window, enter the following values.
76+
1. In the **Frontend** window, enter the following values:
7477

75-
:::image type="content" source="media/mock-api-responses/frontend-window.png" alt-text="Frontend window":::
78+
| Setting | Value | Description |
79+
|------|------|-----------------------------------------|
80+
| **Display name** | *Test call* | The name that's displayed in the [developer portal](api-management-howto-developer-portal.md). |
81+
| **URL** (first box) | GET | Select one of the predefined HTTP verbs. |
82+
| **URL** (second box)| */test* | A URL path for the API. |
83+
| **Description** | | An optional description of the operation. It provides documentation in the developer portal to the developers who use the API. |
7684

77-
| Setting | Value | Description |
78-
|---------------------|-----------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
79-
| **Display name** | *Test call* | The name that is displayed in the [developer portal](api-management-howto-developer-portal.md). |
80-
| **URL** (HTTP verb) | GET | Select one of the predefined HTTP verbs. |
81-
| **URL** | */test* | A URL path for the API. |
82-
| **Description** | | Optional description of the operation, used to provide documentation in the developer portal to the developers using this API. |
85+
:::image type="content" source="media/mock-api-responses/frontend-window.png" alt-text="Screenshot that shows the Frontend window." lightbox="media/mock-api-responses/frontend-window.png":::
8386

84-
1. Select the **Responses** tab, located under the URL, Display name, and Description fields. Enter settings on this tab to define response status codes, content types, examples, and schemas.
85-
1. Select **+ Add response**, and select **200 OK** from the list.
87+
1. Select the **Responses** tab, which is located under the **URL**, **Display name**, and **Description** boxes. You'll enter values on this tab to define response status codes, content types, examples, and schemas.
88+
1. Select **+ Add response**, and then select **200 OK** from the list.
8689

87-
:::image type="content" source="media/mock-api-responses/add-response.png" alt-text="Add response to the API operation":::
90+
:::image type="content" source="media/mock-api-responses/add-response.png" alt-text="Screenshot that shows the Responses tab." lightbox="media/mock-api-responses/add-response.png":::
8891

89-
1. Under the **Representations** heading on the right, select **+ Add representation**.
90-
1. Enter *application/json* into the search box and select the **application/json** content type.
91-
1. In the **Sample** text box, enter `{ "sampleField" : "test" }`.
92+
1. In the **Representations** section, select **+ Add representation**.
93+
1. Enter *application/json* into the search box and then select the **application/json** content type.
94+
1. In the **Sample** box, enter `{ "sampleField" : "test" }`.
9295
1. Select **Save**.
9396

94-
:::image type="content" source="media/mock-api-responses/add-representation.png" alt-text="Add representation to the API operation":::
97+
:::image type="content" source="media/mock-api-responses/add-representation.png" alt-text="Screenshot that shows the Representations section." lightbox="media/mock-api-responses/add-representation.png":::
9598

96-
Although not required for this example, you can configure more settings for an API operation on other tabs, including:
99+
Although it's not required for this example, you can configure more settings for an API operation on other tabs, as described in the following table:
97100

98101
|Tab |Description |
99102
|---------|---------|
@@ -109,75 +112,64 @@ To begin using Azure CLI:
109112
To add an operation to your test API, run the [az apim api operation create](/cli/azure/apim/api/operation#az-apim-api-operation-create) command:
110113

111114
```azurecli
112-
az apim api operation create --resource-group apim-hello-word-resource-group \
115+
az apim api operation create --resource-group <resource-group> \
113116
--display-name "Test call" --api-id test-api --method GET \
114-
--url-template /test --service-name apim-hello-world
117+
--url-template /test --service-name <API-management-service-name>
115118
```
116119

117120
Run the [az apim api operation list](/cli/azure/apim/api/operation#az-apim-api-operation-list) command to see all your operations for an API:
118121

119122
```azurecli
120-
az apim api operation list --resource-group apim-hello-word-resource-group \
121-
--api-id test-api --service-name apim-hello-world --output table
123+
az apim api operation list --resource-group <resource-group-name> \
124+
--api-id test-api --service-name <API-management-service-name> --output table
122125
```
123126

124-
To remove an operation, use the [az apim api operation delete](/cli/azure/apim/api/operation#az-apim-api-operation-delete) command. Get the operation ID from the previous command.
127+
Keep this operation for use in the rest of this article. If you want to remove an operation, you can use the [az apim api operation delete](/cli/azure/apim/api/operation#az-apim-api-operation-delete) command. Get the operation ID from the previous command.
125128

126129
```azurecli
127-
az apim api operation delete --resource-group apim-hello-word-resource-group \
128-
--api-id test-api --operation-id 00000000000000000000000000000000 \
129-
--service-name apim-hello-world
130+
az apim api operation delete --resource-group <resource-group-name> \
131+
--api-id test-api --operation-id <ID> \
132+
--service-name <API-management-service-name>
130133
```
131134

132-
Keep this operation for use in the rest of this article.
133-
134135
---
135136

136137
## Enable response mocking
137138

138139
1. Select the API you created in [Create a test API](#create-a-test-api).
139-
1. In the window on the right, ensure that the **Design** tab is selected.
140+
1. Ensure that the **Design** tab is selected.
140141
1. Select the test operation that you added.
141-
1. In the **Inbound processing** window, select **+ Add policy**.
142+
1. In the **Inbound processing** section, select **+ Add policy**.
142143

143-
:::image type="content" source="media/mock-api-responses/add-policy.png" alt-text="Add processing policy" border="false":::
144+
:::image type="content" source="media/mock-api-responses/add-policy.png" alt-text="Screenshot that shows the first steps for enabling response mocking." lightbox="media/mock-api-responses/add-policy.png" :::
144145

145-
1. Select **Mock responses** from the gallery.
146+
1. Select the **Mock responses** tile from the gallery:
146147

147-
:::image type="content" source="media/mock-api-responses/mock-responses-policy-tile.png" alt-text="Mock responses policy tile" border="false":::
148+
:::image type="content" source="media/mock-api-responses/mock-responses-policy-tile.png" alt-text="Screenshot that shows the Mock responses tile." border="false":::
148149

149-
1. In the **API Management response** textbox, type **200 OK, application/json**. This selection indicates that your API should return the response sample you defined in the previous section.
150+
1. Ensure that **200 OK, application/json** appears in the **API Management response** box. This selection indicates that your API should return the response sample that you defined in the previous section.
150151

151-
:::image type="content" source="media/mock-api-responses/set-mocking-response.png" alt-text="Set mocking response":::
152+
:::image type="content" source="media/mock-api-responses/set-mocking-response.png" alt-text="Screenshot that shows the API Management response selection." lightbox="media/mock-api-responses/set-mocking-response.png":::
152153

153154
1. Select **Save**.
154155

155156
> [!TIP]
156-
> A yellow bar with the text **Mocking is enabled** displays. This indicates that the responses returned from API Management are mocked by the [mocking policy](mock-response-policy.md) and aren't produced by the backend.
157+
> A yellow bar displaying the text **Mocking is enabled** appears. This message indicates that the responses returned from API Management are mocked by the [mocking policy](mock-response-policy.md) and aren't produced by the backend.
157158
158159
## Test the mocked API
159160

160161
1. Select the API you created in [Create a test API](#create-a-test-api).
161-
1. Select the **Test** tab.
162-
1. Ensure that the **Test call** API is selected, and then select **Send** to make a test call.
163-
164-
:::image type="content" source="media/mock-api-responses/test-mock-api.png" alt-text="Test the mocked API":::
165-
166-
1. The **HTTP response** displays the JSON provided as a sample in the first section of the tutorial.
162+
1. On the **Test** tab, ensure that the **Test call** API is selected, and then select **Send** to make a test call:
167163

168-
:::image type="content" source="media/mock-api-responses/http-response.png" alt-text="Mock HTTP response":::
164+
:::image type="content" source="media/mock-api-responses/test-mock-api.png" alt-text="Screenshot that shows the steps for testing the mocked API." lightbox="media/mock-api-responses/test-mock-api.png":::
169165

170-
## Next steps
166+
1. The **HTTP response** displays the JSON provided as a sample in the first section of the tutorial:
171167

172-
In this tutorial, you learned how to:
168+
:::image type="content" source="media/mock-api-responses/http-response.png" alt-text="Screenshot that shows the mock HTTP response." lightbox="media/mock-api-responses/http-response.png":::
173169

174-
> [!div class="checklist"]
175-
> * Create a test API
176-
> * Add an operation to the test API
177-
> * Enable response mocking
178-
> * Test the mocked API
170+
## Next step
179171

180-
Advance to the next tutorial:
172+
Go to the next tutorial:
181173

182174
> [!div class="nextstepaction"]
183175
> [Transform and protect a published API](transform-api.md)

0 commit comments

Comments
 (0)