模拟 API 响应Mock API responses

可将后端 API 导入 APIM API,或者手动创建和管理后端 API。Backend APIs can be imported into an APIM API or created and managed manually. 本教程中的步骤说明如何使用 APIM 创建空白 API 并手动对其进行管理。The steps in this tutorial show you how to use APIM to create a blank API and manage it manually. 本教程介绍如何对 API 设置一个策略,使该 API 返回模拟响应。The tutorial shows how to set a policy on an API so it returns a mocked response. 即使后端不可用于发送实际响应,开发人员也仍可以使用此方法继续实现和测试 APIM 实例。This method enables developers to proceed with implementation and testing of the APIM instance even if the backend is not available to send real responses. 在如下所述的多种情况下,如果能够模拟响应,则很有帮助:Ability to mock up responses can be useful in a number of scenarios:

  • 当以 API 的结构设计为主,后端实现为辅时。When the API façade is designed first and the backend implementation comes later. 或者并行开发后端时。Or, the backend is being developed in parallel.
  • 当后端暂时不可正常运行或不可缩放时。When the backend is temporarily not operational or not able to scale.

本教程介绍如何执行下列操作:In this tutorial, you learn how to:

  • 创建测试 APICreate a test API
  • 将操作添加到测试 APIAdd an operation to the test API
  • 启用响应模拟Enable response mocking
  • 测试模拟 APITest the mocked API

模拟操作响应

先决条件Prerequisites

创建测试 APICreate a test API

本部分中的步骤说明如何创建不带后端的空白 API。The steps in this section show how to create a blank API with no backend. 此外,说明如何将某个操作添加到该 API。It also shows how to add an operation to the API. 完成本部分中的步骤之后调用该操作会生成错误。Calling the operation after completing steps in this section produces an error. 完成“启用响应模拟”部分中的步骤后,不会出现任何错误。You will get no errors after you complete steps in the "Enable response mocking" section.

创建空白 API

  1. 在“API 管理”服务中,选择“API”。Select APIs from the API Management service.
  2. 在左侧菜单中,选择“+ 添加 API”。From the left menu, select + Add API.
  3. 从列表中选择“空白 API”。Select Blank API from the list.
  4. 输入“测试 API”作为“显示名称”。Enter "Test API" for Display name.
  5. 输入“无限制”作为“产品”。Enter "Unlimited" for Products.
  6. 选择“创建” 。Select Create.

将操作添加到测试 APIAdd an operation to the test API

向 API 添加操作

  1. 选择上一步中创建的 API。Select the API you created in the previous step.

  2. 单击“+ 添加操作”。Click + Add Operation.

    设置Setting Value 说明Description
    显示名称Display name 测试调用Test call 显示在开发人员门户中的名称。The name that is displayed in the Developer portal.
    URL(HTTP 谓词)URL (HTTP verb) GETGET 可以选择预定义的 HTTP 谓词之一。You can choose from one of the predefined HTTP verbs.
    URLURL /test/test API 的 URL 路径。A URL path for the API.
    说明Description 输入用于在开发人员门户中通过此 API 向开发人员提供文档的操作的说明。Provide a description of the operation that is used to provide documentation to the developers using this API in the Developer portal.
    “查询”选项卡Query tab 可以添加查询参数。You can add query parameters. 除了提供名称和说明以外,还可以提供可分配到此参数的值。Besides providing a name and description, you can provide values that can be assigned to this parameter. 其中一个值可被标记为默认(可选)。One of the values can be marked as default (optional).
    “请求”选项卡Request tab 可以定义请求内容类型、示例和架构。You can define request content types, examples, and schemas.
    “响应”选项卡Response tab 请参阅此表格后面的步骤。See steps that follow this table. 定义响应状态代码、内容类型、示例和架构。Define response status codes, content types, examples, and schemas.
  3. 选择“URL”、“显示名称”和“说明”字段下面的“响应”选项卡。Select the Response tab, located under the URL, Display name, and Description fields.

  4. 单击“+ 添加响应”。Click + Add response.

  5. 从列表中选择“200 OK”。Select 200 OK from the list.

  6. 在右侧的“表示形式”标题下,选择“+ 添加表示形式”。Under the Representations heading on the right, select + Add representation.

  7. 在搜索框中输入“application/json”,选择“application/json”内容类型。Enter "application/json" into the search box and select the application/json content type.

  8. 在“示例”文本框中,输入“{ 'sampleField' : 'test' }”。In the Sample text box, enter { 'sampleField' : 'test' }.

  9. 选择“创建” 。Select Create.

启用响应模拟Enable response mocking

启用响应模拟

  1. 选择在“创建测试 API”步骤中创建的 API。Select the API you created in the "Create a test API" step.

  2. 选择添加的测试操作。Select the test operation that you added.

  3. 在右侧窗口中,单击“设计”选项卡。In the window on the right, click the Design tab.

  4. 在“入站处理”窗口中,单击“+ 添加策略”。In the Inbound processing window, click + Add policy.

  5. 从库中选择“模拟响应”磁贴。Select the Mock responses tile from the gallery.

    模拟响应策略磁贴

  6. 在“API 管理响应”文本框中,键入 200 OK, application/jsonIn the API Management response textbox, type 200 OK, application/json. 此项选择指示 API 应返回上一部分中定义的响应示例。This selection indicates that your API should return the response sample you defined in the previous section.

    启用响应模拟

  7. 单击“保存” 。Click Save.

测试模拟 APITest the mocked API

测试模拟 API

  1. 选择在“创建测试 API”步骤中创建的 API。Select the API you created in the "Create a test API" step.

  2. 打开“测试”选项卡。Open the Test tab.

  3. 确保“测试调用”API 已选中。Ensure the Test call API is selected.

    Tip

    包含文本“模拟已启用”的黄色条形表示从 API 管理返回的响应发送了模拟策略,而不是实际的后端响应。A yellow bar with the text Mocking is enabled indicates that responses returned from the API Management, sends a mocking policy and not an actual backend response.

  4. 选择“发送”以发出测试调用。Select Send to make a test call.

  5. “HTTP 响应”显示提供的 JSON,如本教程第一部分中的示例所示。The HTTP response displays the JSON provided as a sample in the first section of the tutorial.

    启用响应模拟

后续步骤Next steps

在本教程中,你已学习了如何执行以下操作:In this tutorial, you learned how to:

  • 创建测试 APICreate a test API
  • 将操作添加到测试 APIAdd an operation to the test API
  • 启用响应模拟Enable response mocking
  • 测试模拟 APITest the mocked API

转到下一教程:Advance to the next tutorial: