测试应用程序是否正确处理限制

测试限制很困难,因为它很少发生,仅当托管 API 的服务器负载过大时。 使用开发代理,可以模拟任何 API 上的限制,并检查应用程序是否正确处理它。

若要模拟任何 API 上的限制,请使用 GenericRandomErrorPlugin。 如果使用的 API 返回 Retry-After 标头,请使用 RetryAfterPlugin 验证应用是否按 API 指示退让。

模拟任何 API 上的限制

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

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v0.24.0/rc.schema.json",
  "plugins": [
    {
      "name": "GenericRandomErrorPlugin",
      "enabled": true,
      "pluginPath": "~appFolder/plugins/dev-proxy-plugins.dll",
      "configSection": "errorsContosoApi",
      "urlsToWatch": [
        "https://api.contoso.com/*"
      ]
    }
  ]
}

接下来,将插件配置为使用包含要模拟的错误的文件。

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v0.24.0/rc.schema.json",
  "plugins": [
    {
      "name": "GenericRandomErrorPlugin",
      "enabled": true,
      "pluginPath": "~appFolder/plugins/dev-proxy-plugins.dll",
      "configSection": "errorsContosoApi",
      "urlsToWatch": [
        "https://api.contoso.com/*"
      ]
    }
  ],
  "errorsContosoApi": {
    "errorsFile": "errors-contoso-api.json"
  }
}

在错误文件中,定义限制响应,使其与 API 的实际限制响应匹配:

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v0.24.0/genericrandomerrorplugin.schema.json",
  "errors": [
    {
      "request": {
        "url": "https://api.contoso.com/*"
      },
      "responses": [
        {
          "statusCode": 429,
          "headers": [
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ],
          "body": {
            "code": "TooManyRequests",
            "message": "Too many requests"
          }
        }
      ]
    }
  ]
}

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

使用 Retry-After 标头测试正确的后退

许多 API 使用 Retry-After 响应标头来指示应用在特定时间内回退。 使用开发代理模拟限制响应时,可以将标头配置为 Retry-After 静态值,或使用动态值来测试应用是否按指示等待,然后再再次调用 API。

若要将 Retry-After 标头配置为静态值,请将标头添加到限制响应:

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v0.24.0/genericrandomerrorplugin.schema.json",
  "errors": [
    {
      "request": {
        "url": "https://api.contoso.com/*"
      },
      "responses": [
        {
          "statusCode": 429,
          "headers": [
            {
              "name": "Content-Type",
              "value": "application/json"
            },
            {
              "name": "Retry-After",
              "value": "60"
            }
          ],
          "body": {
            "code": "TooManyRequests",
            "message": "Too many requests"
          }
        }
      ]
    }
  ]
}

在此示例中, Retry-After 标头设置为 60 秒。 将标头配置为静态值时,开发代理不会控制应用是否在再次调用 API 之前正在等待。

若要测试应用是否在再次调用 API 之前正确等待,请将标头的值更改为 @dynamic

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v0.24.0/genericrandomerrorplugin.schema.json",
  "errors": [
    {
      "request": {
        "url": "https://api.contoso.com/*"
      },
      "responses": [
        {
          "statusCode": 429,
          "headers": [
            {
              "name": "Content-Type",
              "value": "application/json"
            },
            {
              "name": "Retry-After",
              "value": "@dynamic"
            }
          ],
          "body": {
            "code": "TooManyRequests",
            "message": "Too many requests"
          }
        }
      ]
    }
  ]
}

此外,使用 RetryAfterPlugin.. 扩展开发代理配置。

{
  "$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",
      "urlsToWatch": [
        "https://api.contoso.com/*"
      ]
    },
    {
      "name": "GenericRandomErrorPlugin",
      "enabled": true,
      "pluginPath": "~appFolder/plugins/dev-proxy-plugins.dll",
      "configSection": "errorsContosoApi",
      "urlsToWatch": [
        "https://api.contoso.com/*"
      ]
    }
  ],
  "errorsContosoApi": {
    "errorsFile": "errors-contoso-api.json"
  }
}

注意

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

此插件跟踪限制响应,并强制失败向仍受限制的 API 发出的请求。

详细信息