模擬 API 回應Mock API responses

後端 API 可以匯入至 APIM 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 上的原則,讓它能夠傳回模擬的回應。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 確定]。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.

    提示

    具有已啟用模擬字樣的黃色列,表示從 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.

    啟用回應模擬

影片Video

後續步驟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: