X Tutup
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2145,6 +2145,16 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" {
$jsonError.error | Should -BeExactly 'Error: HTTP - 400 occurred.'
}

It "Invoke-WebRequest can retry - specified number of times following Retry-After header - error 429" {

$uri = Get-WebListenerUrl -Test 'Retry' -Query @{ sessionid = (New-Guid).Guid; failureCode = 429; failureCount = 2; retryAfter = 2 }
$verboseFile = Join-Path $TestDrive -ChildPath verbose.txt
$result = Invoke-WebRequest -Uri $uri -MaximumRetryCount 2 -RetryIntervalSec 1 -Verbose 4>>$verboseFile

$result.StatusCode | Should -Be "200"
$verboseFile | Should -FileContentMatch "Retrying after interval of 2 seconds. Status code for previous attempt: TooManyRequests"
}

It "Invoke-WebRequest can retry with POST" {

$uri = Get-WebListenerUrl -Test 'Retry'
Expand Down Expand Up @@ -4036,6 +4046,15 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" {
$result.output.sessionId | Should -BeExactly $sessionId
}

It "Invoke-RestMethod can retry - specified number of times following Retry-After header - error 429" {

$uri = Get-WebListenerUrl -Test 'Retry' -Query @{ sessionid = (New-Guid).Guid; failureCode = 429; failureCount = 2; retryAfter = 2 }
$verboseFile = Join-Path $TestDrive -ChildPath verbose.txt
$result = Invoke-RestMethod -Uri $uri -MaximumRetryCount 2 -RetryIntervalSec 1 -Verbose 4>>$verboseFile

$verboseFile | Should -FileContentMatch "Retrying after interval of 2 seconds. Status code for previous attempt: TooManyRequests"
}

It "Invoke-RestMethod can retry with POST" {

$uri = Get-WebListenerUrl -Test 'Retry'
Expand Down
8 changes: 7 additions & 1 deletion test/tools/WebListener/Controllers/RetryController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
Expand All @@ -21,10 +22,15 @@ public class RetryController : Controller
// Dictionary for sessionId as key and failureCode, failureCount and failureResponsesSent as the value.
private static Dictionary<string, Tuple<int, int, int>> retryInfo;

public JsonResult Retry(string sessionId, int failureCode, int failureCount)
public JsonResult Retry(string sessionId, int failureCode, int failureCount, int retryAfter = 0)
{
retryInfo ??= new Dictionary<string, Tuple<int, int, int>>();

if (failureCode == 429 && retryAfter > 0)
{
Response.Headers.Append("Retry-After", $"{retryAfter}");
}

if (retryInfo.TryGetValue(sessionId, out Tuple<int, int, int> retry))
{
// if failureResponsesSent is less than failureCount
Expand Down
X Tutup