測試節流很困難,因為它很少發生,只有在裝載 API 的伺服器負載過重時。 使用開發代理服務,您可以模擬伺服器上的任何 API 降速,並檢查您的應用程式是否正確處理此情況。
若要模擬任何 API 上的 節流,請使用 GenericRandomErrorPlugin。 如果您使用的 API 會傳 Retry-After 回標頭,請使用 RetryAfterPlugin 來確認您的應用程式是否按 API 指示進行退轉。
在任何 API 上模擬節流
若要開始,請在您的 Dev Proxy 組態檔中開啟 GenericRandomErrorPlugin 。
{
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v1.0.0/rc.schema.json",
"plugins": [
{
"name": "GenericRandomErrorPlugin",
"enabled": true,
"pluginPath": "~appFolder/plugins/DevProxy.Plugins.dll",
"configSection": "errorsContosoApi",
"urlsToWatch": [
"https://api.contoso.com/*"
]
}
]
}
接下來,將外掛程式設定為使用包含您想要模擬錯誤的檔案。
{
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v1.0.0/rc.schema.json",
"plugins": [
{
"name": "GenericRandomErrorPlugin",
"enabled": true,
"pluginPath": "~appFolder/plugins/DevProxy.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/v1.0.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"
}
}
]
}
]
}
使用您的組態檔啟動 Dev Proxy,並測試您的應用程式,以瞭解其如何處理節流。
使用 Retry-After 標頭測試正確的退讓
許多 API 會使用 Retry-After 回應標頭來指示應用程式在特定時間內進行回復。 使用 Dev Proxy 模擬節流回應時,您可以將標頭設定 Retry-After 為靜態值,或使用動態值來測試您的應用程式是否如指示等候,然後再再次呼叫 API。
若要將 Retry-After 標頭設定為靜態值,請將標頭新增至您的節流回應:
{
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v1.0.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秒。 當您將標頭設定為靜態值時,Dev Proxy 不能控制您的應用程式在再次呼叫 API 前是否等待。
若要測試您的應用程式是否在再次呼叫 API 之前正確等候,請將標頭的值變更為 @dynamic:
{
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v1.0.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擴充您的開發 Proxy 組態。
{
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v1.0.0/rc.schema.json",
"plugins": [
{
"name": "RetryAfterPlugin",
"enabled": true,
"pluginPath": "~appFolder/plugins/DevProxy.Plugins.dll",
"urlsToWatch": [
"https://api.contoso.com/*"
]
},
{
"name": "GenericRandomErrorPlugin",
"enabled": true,
"pluginPath": "~appFolder/plugins/DevProxy.Plugins.dll",
"configSection": "errorsContosoApi",
"urlsToWatch": [
"https://api.contoso.com/*"
]
}
],
"errorsContosoApi": {
"errorsFile": "errors-contoso-api.json"
}
}
警告
請將RetryAfterPlugin新增在GenericRandomErrorPlugin之前的組態檔中。 如果您之後再新增它,請求會在GenericRandomErrorPlugin處理它之前RetryAfterPlugin失敗。
此外掛程式會追蹤節流回應,並強制使發送給依然處於節流狀態的 API 的要求失敗。