模擬來自 Microsoft Graph API 的錯誤

一目了然
目標: 測試你的應用程式如何處理 Microsoft 圖形 API 錯誤
時間: 10分鐘
插件:GraphRandomErrorPlugin
前置條件:設定開發代理

Microsoft Graph 是 API 的集合,可讓您存取 Microsoft 365 上的數據和深入解析。 當您在應用程式中使用 Microsoft Graph 時,您應該測試應用程式如何處理 API 錯誤。 Dev Proxy 可讓您使用 GraphRandomErrorPlugin 模擬在任何 Microsoft 圖形 API 上的錯誤。

GraphRandomErrorPlugin已優化以使用 Microsoft Graph,並模擬Microsoft Graph 可以傳回的特定錯誤。

若要開始,請在組態檔中開啟 GraphRandomErrorPlugin 。

檔案: devproxyrc.json

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.0.0/rc.schema.json",
  "plugins": [
    {
      "name": "GraphRandomErrorPlugin",
      "enabled": true,
      "pluginPath": "~appFolder/plugins/DevProxy.Plugins.dll",
      "configSection": "graphRandomErrorPlugin",
      "urlsToWatch": [
        "https://graph.microsoft.com/v1.0/*",
        "https://graph.microsoft.com/beta/*",
        "https://graph.microsoft.us/v1.0/*",
        "https://graph.microsoft.us/beta/*",
        "https://dod-graph.microsoft.us/v1.0/*",
        "https://dod-graph.microsoft.us/beta/*",
        "https://microsoftgraph.chinacloudapi.cn/v1.0/*",
        "https://microsoftgraph.chinacloudapi.cn/beta/*"
      ]
    }
  ]
}

提示

上述程式碼片段會監聽所有 Microsoft 雲端中的 Microsoft Graph 要求。 如果您只想要模擬特定Microsoft雲端中的錯誤,請移除您不需要的 URL。

使用您的組態檔啟動 Dev Proxy,並使用您的應用程式來查看其如何處理錯誤。 針對每個比對要求,Dev Proxy 會決定使用設定的失敗率模擬錯誤或將要求傳遞至 Microsoft Graph。 當 Dev Proxy 模擬錯誤時,它會隨機選取其中一個Microsoft Graph 使用的錯誤,並傳回應用程式的錯誤回應。

設定錯誤以模擬

預設情況下,GraphRandomErrorPlugin 模擬以下錯誤。

HTTP 方法 可能的錯誤
GET 429 Too Many Requests、、 500 Internal Server Error、 502 Bad Gateway、 503 Service Unavailable、 504 Gateway Timeout
POST 429 Too Many Requests、、500 Internal Server Error502 Bad Gateway、503 Service Unavailable、、504 Gateway Timeout、507 Insufficient Storage
PUT 429 Too Many Requests、、500 Internal Server Error502 Bad Gateway、503 Service Unavailable、、504 Gateway Timeout、507 Insufficient Storage
PATCH 429 Too Many Requests、、 500 Internal Server Error、 502 Bad Gateway、 503 Service Unavailable、 504 Gateway Timeout
DELETE 429 Too Many Requests、、500 Internal Server Error502 Bad Gateway、503 Service Unavailable、、504 Gateway Timeout、507 Insufficient Storage

如果您想要測試特定行為,例如流量限制,請將外掛程式設定為只使用由 --allowed-errors 選項指定的相關錯誤。

devproxy --allowed-errors 429

或者,您可以在組態檔中的 allowedErrors 物件中設定 graphRandomErrorPlugin 屬性。

檔案: devproxyrc.json

{
  "graphRandomErrorPlugin": {
    "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.0.0/graphrandomerrorplugin.schema.json",
    "allowedErrors": [ 429 ]
  }
}

Dev Proxy 只使用適用於每個 HTTP 方法的允許錯誤,如前述表格所示。 如果沒有任何允許的錯誤適用於某個方法,Dev Proxy 會將使用該方法的請求轉交給 Microsoft Graph。 例如,如果你只允許 507,Dev Proxy 會讓 POST、PUT 和 DELETE 請求失敗,但會讓 GET 和 PATCH 請求通過。 Dev Proxy 也會傳遞表格中未列出的方法請求,例如 HEAD 或 OPTIONS。

在 Microsoft Graph 批次請求中模擬錯誤

Dev Proxy 會以對 Microsoft Graph 一般要求相同的方式,模擬批次要求中的錯誤。 當 Dev Proxy 在批次要求中失敗一個或多個請求時,它會像 Microsoft Graph 一樣,傳回 424 Failed Dependency 作為整個批次要求的回應。

如果批次中的任何請求使用的方法沒有任何適用的允許錯誤,Dev Proxy 會將整個批次傳給 Microsoft Graph,因為批次回應必須包含批次中每個請求的回應。

另請參閱