模拟Microsoft 365 API 的限制

通常,测试 限制 很困难,因为它很少发生,当Microsoft 365 服务器负载过大时。 使用开发代理可以模拟限制响应,并检查应用程序是否正确处理它。

若要模拟对 Microsoft 365 API 的限制,请使用 GraphRandomErrorPluginRetryAfterPlugin。 返回 GraphRandomErrorPlugin Microsoft 365 API 的限制响应。 验证 RetryAfterPlugin 应用是否按 API 指示退让。

若要开始,请在开发代理配置文件中启用 GraphRandomErrorPluginRetryAfterPlugin 启用。

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v0.24.0/rc.schema.json",
  "plugins": [
    {
      "name": "RetryAfterPlugin",
      "enabled": true,
      "pluginPath": "~appFolder/plugins/dev-proxy-plugins.dll"
    },
    {
      "name": "GraphRandomErrorPlugin",
      "enabled": true,
      "pluginPath": "~appFolder/plugins/dev-proxy-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/*",
    "!https://*.sharepoint.*/*_api/web/GetClientSideComponents",
    "https://*.sharepoint.*/*_api/*",
    "https://*.sharepoint.*/*_vti_bin/*",
    "https://*.sharepoint-df.*/*_api/*",
    "https://*.sharepoint-df.*/*_vti_bin/*"
  ]
}

注意

RetryAfterPlugin 配置文件中添加前面 GraphRandomErrorPlugin 。 如果添加后,请求将在处理请求之前RetryAfterPlugin失败GraphRandomErrorPlugin

接下来,配置 GraphRandomErrorPlugin 模拟限制错误。

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v0.24.0/rc.schema.json",
  "plugins": [
    {
      "name": "RetryAfterPlugin",
      "enabled": true,
      "pluginPath": "~appFolder/plugins/dev-proxy-plugins.dll"
    },
    {
      "name": "GraphRandomErrorPlugin",
      "enabled": true,
      "pluginPath": "~appFolder/plugins/dev-proxy-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/*",
    "!https://*.sharepoint.*/*_api/web/GetClientSideComponents",
    "https://*.sharepoint.*/*_api/*",
    "https://*.sharepoint.*/*_vti_bin/*",
    "https://*.sharepoint-df.*/*_api/*",
    "https://*.sharepoint-df.*/*_vti_bin/*"
  ],
  "graphRandomErrorPlugin": {
    "allowedErrors": [ 429 ]
  }
}

使用配置文件启动开发代理并测试应用,以了解它如何处理限制。

如果应用程序在受限制时退让,但不会等待请求指定的时间量,则会看到类似于 Calling https://graph.microsoft.com/v1.0/endpoint again before waiting for the Retry-After period. Request will be throttled的消息。

此消息指示应用程序未正确处理限制,并且不必要地延长限制。 若要改进应用处理限制的方式,请在重试请求之前更新代码以等待标头中指定的 Retry-After 时间量。