概览
目标: 测试应用如何处理Microsoft图形 API 错误
时间: 10 分钟
Plugins:GraphRandomErrorPlugin
先决条件:设置开发代理
Microsoft Graph 是一组 API,可用于访问 Microsoft 365 上的数据和见解。 在应用中使用 Microsoft Graph 时,应测试应用如何处理 API 错误。 开发代理允许你使用 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,并使用应用查看它如何处理错误。 对于每个匹配请求,开发代理使用配置的失败率来决定是模拟错误,还是将请求传递到 Microsoft Graph。 当开发代理模拟错误时,它会随机选择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 Error、502 Bad Gateway、503 Service Unavailable、504 Gateway Timeout、507 Insufficient Storage |
PUT |
429 Too Many Requests、500 Internal Server Error、502 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 Error、502 Bad Gateway、503 Service Unavailable、504 Gateway Timeout、507 Insufficient Storage |
如果要测试特定行为(例如限制),可使用 --allowed-errors 选项将插件配置为仅使用相关错误。
devproxy --allowed-errors 429
或者,可以在配置文件中的graphRandomErrorPlugin对象中配置allowedErrors属性。
文件: devproxyrc.json
{
"graphRandomErrorPlugin": {
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.0.0/graphrandomerrorplugin.schema.json",
"allowedErrors": [ 429 ]
}
}
开发代理仅使用应用于每个 HTTP 方法的允许错误,如上表中所列。 如果没有任何允许的错误适用于某个方法,则 Dev Proxy 会将该方法的请求传递给 Microsoft Graph。 例如,如果仅允许 507,则开发代理会使 POST、PUT 和 DELETE 请求失败,但会放行 GET 和 PATCH 请求。 开发代理还会传递使用表中未列出方法的请求,例如 HEAD 或 OPTIONS。
模拟 Microsoft Graph 批处理请求中的错误
Dev Proxy 以与常规请求相同的方式模拟 Microsoft Graph 批处理请求中的错误。 当开发代理在批处理请求中使一个或多个请求失败时,它将为整个批处理请求返回 424 Failed Dependency 响应,就像 Microsoft Graph 一样。
如果批处理中的任何请求所使用的方法没有任何适用的允许错误,开发代理就会将整个批处理传递到 Microsoft Graph,因为批处理响应必须包含批处理中每个请求的响应。
另请参阅
- GraphRandomErrorPlugin - 完整参考
- 在 Microsoft 365 API 上模拟限流 - 限流专用测试
- 使用随机错误测试我的应用 - 通用的错误模拟
- 术语表 - 开发代理术语