共用方式為


模擬回應

使用開發 Proxy 是模擬 API 的最簡單方式。 無論您要建置前端和 API 尚未就緒,您都需要將後端與外部服務整合,或想要使用不同的響應來測試應用程式,開發 Proxy 可協助您模擬 API 回應。 使用 Dev Proxy 的絕佳方式是,它不需要對應用程式程式代碼進行任何變更。 您可以為應用程式與其互動的任何 API 定義模擬回應,而 Dev Proxy 會攔截要求,並使用您定義的模擬回應回應。

若要模擬 API 回應,您需要執行兩件事:

  1. 建立具有模擬回應的檔案。
  2. 設定 Dev Proxy 以使用模擬回應。

提示

如果您使用 Visual Studio Code,請考慮安裝 Dev Proxy 工具組 擴充功能。 其可大幅簡化使用 Dev Proxy 組態檔。

建立具有模擬回應的檔案

開發人員 Proxy 會使用 MockResponsePlugin模擬 API 回應。 外掛程式可讓您定義一組模擬回應。 您可以在 個別檔案中定義模擬,。 下列代碼段示範 GET 要求 https://jsonplaceholder.typicode.com/posts/1的簡單模擬回應。

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v0.24.0/mockresponseplugin.schema.json",
  "mocks": [
    {
      "request": {
        "url": "https://jsonplaceholder.typicode.com/posts/1",
        "method": "GET"
      },
      "response": {
        "statusCode": 200,
        "body": {
          "userId": 1,
          "id": 1,
          "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
          "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
        },
        "headers": [
          {
            "name": "Date",
            "value": "Wed, 19 Feb 2025 09:03:37 GMT"
          },
          {
            "name": "Content-Type",
            "value": "application/json; charset=utf-8"
          },
          {
            "name": "Content-Length",
            "value": "292"
          },
          // [...] trimmed for brevity
        ]
      }
    }
  ]
}

提示

您可以使用 MockGeneratorPlugin,根據攔截的要求產生模擬檔案,而不是手動建立模擬檔案。

優先順序

Dev Proxy 會按照您在模擬檔案中定義的順序匹配模擬數據。 如果您使用相同的 URL 和方法定義多個回應,Dev Proxy 會使用第一個相符的回應。

當您使用下列組態時,Proxy 會以 500 Internal Server Error回應所有發送至 https://graph.microsoft.com/v1.0/me/photoGET 請求。

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v0.24.0/mockresponseplugin.schema.json",
  "mocks": [
    {
      "request": {
        "url": "https://graph.microsoft.com/v1.0/me/photo",
        "method": "GET"
      },
      "response": {
        "statusCode": 500
      }
    },
    {
      "request": {
        "url": "https://graph.microsoft.com/v1.0/me/photo",
        "method": "GET"
      },
      "response": {
        "statusCode": 404
      }
    }
  ]
}

通配符支援

Dev Proxy 支援在 URL 屬性中使用通配符。 您可以使用星號字元 (*) 來比對 URL 中的任何一系列字元。

當您使用下列設定時,Dev Proxy 會用同樣的回應來回應所有要求,以取得任何使用者的資料。

{
  "request": {
    "url": "https://graph.microsoft.com/v1.0/users/*"
  },
  "response": {
    "body": {
      "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users/$entity",
      "businessPhones": ["+1 425 555 0109"],
      "displayName": "Adele Vance",
      "givenName": "Adele",
      "jobTitle": "Product Marketing Manager",
      "mail": "AdeleV@M365x214355.onmicrosoft.com",
      "mobilePhone": null,
      "officeLocation": "18/2111",
      "preferredLanguage": "en-US",
      "surname": "Vance",
      "userPrincipalName": "AdeleV@M365x214355.onmicrosoft.com",
      "id": "87d349ed-44d7-43e1-9a83-5f2406dee5bd"
    }
  }
}

當您使用下列設定時,當您要求取得任何使用者相片的二進位檔時,Dev Proxy 會從磁碟傳回相同的映像。

{
  "request": {
    "url": "https://graph.microsoft.com/v1.0/users/*/photo/$value"
  },
  "response": {
    "body": "@picture.jpg",
    "headers": [
      {
        "name": "content-type",
        "value": "image/jpeg"
      }
    ]
  }
}

當您使用下列設定時,當您要求使用任何查詢字串參數取得目前使用者配置檔時,Dev Proxy 會傳回相同的回應。

{
  "request": {
    "url": "https://graph.microsoft.com/v1.0/me?*"
  },
  "response": {
    "body": {
      "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users/$entity",
      "businessPhones": [
        "+1 412 555 0109"
      ],
      "displayName": "Megan Bowen",
      "givenName": "Megan",
      "jobTitle": "Auditor",
      "mail": "MeganB@M365x214355.onmicrosoft.com",
      "mobilePhone": null,
      "officeLocation": "12/1110",
      "preferredLanguage": "en-US",
      "surname": "Bowen",
      "userPrincipalName": "MeganB@M365x214355.onmicrosoft.com",
      "id": "48d31887-5fad-4d73-a9f5-3c356e68a038"
    }
  }
},

回應檔案的內容

若要讓模擬檔案保持乾淨且組織,您可以將響應的內容儲存在個別的檔案中,並在模擬檔案中加以參考。 若要指示 Dev Proxy 從檔案載入模擬回應內容,請將 body 屬性設為 @,並在後面接上相對於模擬檔案的檔案路徑。

例如,下列模擬回應組態會指示 Dev Proxy 回應任何發送至 https://graph.microsoft.com/v1.0/me 的要求,並使用與模擬檔案位於相同資料夾中的 response.json 檔案的內容。

{
  "request": {
    "url": "https://graph.microsoft.com/v1.0/me",
    "method": "GET"
  },
  "response": {
    "body": "@response.json",
    "headers": [
      {
        "name": "content-type",
        "value": "application/json; odata.metadata=minimal"
      }
    ]
  }
}

使用 @-token 可與文字和 二進位檔搭配使用,

設定 Dev Proxy 以使用模擬回應

建立模擬檔案之後,您必須將 Dev Proxy 設定為使用模擬回應。 若要將 Dev Proxy 設定為模擬回應,請將 MockResponsePlugin 新增至 devproxyrc 檔案中的外掛程式清單。

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v0.24.0/rc.schema.json",
  "plugins": [
    {
      "name": "MockResponsePlugin",
      "enabled": true,
      "pluginPath": "~appFolder/plugins/dev-proxy-plugins.dll",
      "configSection": "mockResponsePlugin"
    }
  ],
  "urlsToWatch": [
    "https://jsonplaceholder.typicode.com/*"
  ],
  "mockResponsePlugin": {
    "mocksFile": "mocks.json"
  },
  "logLevel": "information",
  "newVersionNotification": "stable",
  "showSkipMessages": true
}

首先,您會將 MockResponsePlugin 新增至外掛程式清單。 您會引用其設定區段,並在其中指定模擬檔案的路徑。

當您啟動 Dev Proxy 時,它會讀取模擬檔案,並使用模擬回應來回應符合已定義仿真的要求。

未模擬的請求支援

開發 Proxy 支援在 Proxy 攔截未仿真的請求時丟出錯誤。 識別未模擬的要求失敗的能力對於找出您在模擬檔案中遺漏的要求非常有用。

若要啟用此功能,請在 devproxyrc 檔案中的 MockResponsePlugin 組態區段,新增並啟用 blockUnmockedRequests 設定。

{
  "mocksPlugin": {
    "mocksFile": "mocks.json",
    "blockUnmockedRequests": true
  }
}

當 Dev Proxy 攔截無法仿真的要求時,它會傳回 502 Bad Gateway 回應。

下一步

深入瞭解MockResponsePlugin。

樣品

另請參閱相關的開發 Proxy 範例: