模拟慢速 API 响应

概览
目标: 为 API 响应添加人为延迟以进行测试
时间: 5 分钟
Plugins:LatencyPlugin
先决条件:设置开发代理

开发代理允许您使用 LatencyPlugin 模拟慢速 API 响应。

首先,通过在开发代理配置文件中启用插件:

文件: devproxyrc.json

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v2.0.0/rc.schema.json",
  "plugins": [
    {
      "name": "LatencyPlugin",
      "enabled": true,
      "pluginPath": "~appFolder/plugins/DevProxy.Plugins.dll",
      "configSection": "latencyPlugin"
    }
  ],
  "urlsToWatch": []
}

接下来,指定为 API 模拟的最小和最大延迟(以毫秒为单位)。

文件: devproxyrc.json

"latencyPlugin": {
  "minMs": 200,
  "maxMs": 10000
}

完整的配置文件如下所示。

文件: devproxyrc.json

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v2.0.0/rc.schema.json",
  "plugins": [
    {
      "name": "LatencyPlugin",
      "enabled": true,
      "pluginPath": "~appFolder/plugins/DevProxy.Plugins.dll",
      "configSection": "latencyPlugin"
    }
  ],
  "urlsToWatch": [
    "https://api.example.com/*"
  ],
  "latencyPlugin": {
    "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v2.0.0/latencyplugin.schema.json",
    "minMs": 200,
    "maxMs": 10000
  }
}

当响应延迟时,开发代理会显示在控制台输出中延迟的总持续时间。

另请参阅