Skip to content

Commit

Permalink
Updates to parameter names as per WG recommendations
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenebutler committed Jun 7, 2023
1 parent 62fa4cd commit 6b78f78
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ internal override void ProcessResponse(HttpResponseMessage response)
ArgumentNullException.ThrowIfNull(response);
ArgumentNullException.ThrowIfNull(_cancelToken);

TimeSpan perReadTimeout = NetworkTimeout;
TimeSpan perReadTimeout = ConvertTimeoutSecondsToTimeSpan(OperationTimeoutSeconds);
Stream baseResponseStream = StreamHelper.GetResponseStream(response, _cancelToken.Token);

if (ShouldWriteToPipeline)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,25 +266,25 @@ public abstract class WebRequestPSCmdlet : PSCmdlet, IDisposable
public virtual SwitchParameter DisableKeepAlive { get; set; }

/// <summary>
/// Gets or sets the ConnectTimeOutSec property.
/// Gets or sets the ConnectionTimeoutSeconds property.
/// </summary>
/// <remarks>
/// This property applies to sending the request and receiving the response headers only.
/// </remarks>
[Alias("TimeoutSec")]
[Parameter]
[ValidateRange(0, int.MaxValue)]
public virtual int ConnectTimeoutSec { get; set; }
public virtual int ConnectionTimeoutSeconds { get; set; }

/// <summary>
/// Gets or sets the NetworkTimeoutSec property.
/// Gets or sets the OperationTimeoutSeconds property.
/// </summary>
/// <remarks>
/// This property applies to receiving the response body.
/// This property applies to each read operation when receiving the response body.
/// </remarks>
[Parameter]
[ValidateRange(0, int.MaxValue)]
public virtual int NetworkTimeoutSec { get; set; }
public virtual int OperationTimeoutSeconds { get; set; }

/// <summary>
/// Gets or sets the Headers property.
Expand Down Expand Up @@ -511,8 +511,6 @@ public virtual string CustomMethod

internal bool ShouldWriteToPipeline => !ShouldSaveToOutFile || PassThru;

internal TimeSpan NetworkTimeout => NetworkTimeoutSec > 0 ? TimeSpan.FromSeconds(NetworkTimeoutSec) : Timeout.InfiniteTimeSpan;

#endregion Helper Properties

#region Abstract Methods
Expand Down Expand Up @@ -586,7 +584,7 @@ protected override void ProcessRecord()
string respVerboseMsg = contentLength is null
? string.Format(CultureInfo.CurrentCulture, WebCmdletStrings.WebResponseNoSizeVerboseMsg, response.Version, contentType)
: string.Format(CultureInfo.CurrentCulture, WebCmdletStrings.WebResponseVerboseMsg, response.Version, contentLength, contentType);

WriteVerbose(respVerboseMsg);

bool _isSuccess = response.IsSuccessStatusCode;
Expand Down Expand Up @@ -638,7 +636,7 @@ protected override void ProcessRecord()
try
{
// We can't use ReadAsStringAsync because it doesn't have per read timeouts
TimeSpan perReadTimeout = NetworkTimeout;
TimeSpan perReadTimeout = ConvertTimeoutSecondsToTimeSpan(OperationTimeoutSeconds);
string characterSet = WebResponseHelper.GetCharacterSet(response);
var responseStream = StreamHelper.GetResponseStream(response, _cancelToken.Token);
int initialCapacity = (int)Math.Min(contentLength ?? StreamHelper.DefaultReadBuffer, StreamHelper.DefaultReadBuffer);
Expand Down Expand Up @@ -1043,7 +1041,7 @@ internal virtual void PrepareSession()
WebSession.RetryIntervalInSeconds = RetryIntervalSec;
}

WebSession.ConnectTimeoutSec = ConnectTimeoutSec;
WebSession.ConnectionTimeout = ConvertTimeoutSecondsToTimeSpan(ConnectionTimeoutSeconds);
}

internal virtual HttpClient GetHttpClient(bool handleRedirect)
Expand Down Expand Up @@ -1411,6 +1409,9 @@ internal virtual void UpdateSession(HttpResponseMessage response)
#endregion Virtual Methods

#region Helper Methods

internal static TimeSpan ConvertTimeoutSecondsToTimeSpan(int timeout) => timeout > 0 ? TimeSpan.FromSeconds(timeout) : Timeout.InfiniteTimeSpan;

private Uri PrepareUri(Uri uri)
{
uri = CheckProtocol(uri);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public InvokeWebRequestCommand() : base()
internal override void ProcessResponse(HttpResponseMessage response)
{
ArgumentNullException.ThrowIfNull(response);
TimeSpan perReadTimeout = NetworkTimeout;
TimeSpan perReadTimeout = ConvertTimeoutSecondsToTimeSpan(OperationTimeoutSeconds);
Stream responseStream = StreamHelper.GetResponseStream(response, _cancelToken.Token);
if (ShouldWriteToPipeline)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class WebRequestSession : IDisposable
private bool _skipCertificateCheck;
private bool _noProxy;
private bool _disposed;
private int _connectTimeoutSec;
private TimeSpan _connectionTimeout;

/// <summary>
/// Contains true if an existing HttpClient had to be disposed and recreated since the WebSession was last used.
Expand Down Expand Up @@ -142,7 +142,7 @@ public WebRequestSession()

internal bool SkipCertificateCheck { set => SetStructVar(ref _skipCertificateCheck, value); }

internal int ConnectTimeoutSec { set => SetStructVar(ref _connectTimeoutSec, value); }
internal TimeSpan ConnectionTimeout { set => SetStructVar(ref _connectionTimeout, value); }

internal bool NoProxy
{
Expand Down Expand Up @@ -240,7 +240,7 @@ private HttpClient CreateHttpClient()
// Check timeout setting (in seconds instead of milliseconds as in HttpWebRequest)
return new HttpClient(handler)
{
Timeout = _connectTimeoutSec > 0 ? TimeSpan.FromSeconds(_connectTimeoutSec) : Timeout.InfiniteTimeSpan
Timeout = _connectionTimeout
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,10 @@ function ExecuteRequestWithCustomUserAgent {

try {
$Params = @{
Uri = $Uri
ConnectTimeoutSec = 5
UserAgent = $UserAgent
SkipHeaderValidation = $SkipHeaderValidation.IsPresent
Uri = $Uri
ConnectionTimeoutSeconds = 5
UserAgent = $UserAgent
SkipHeaderValidation = $SkipHeaderValidation.IsPresent
}
if ($Cmdlet -eq 'Invoke-WebRequest') {
$result.Output = Invoke-WebRequest @Params
Expand Down Expand Up @@ -608,9 +608,9 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" {
$Result.Output.Content | Should -Match '测试123'
}

It "Invoke-WebRequest validate ConnectTimeoutSec option" {
It "Invoke-WebRequest validate ConnectionTimeoutSeconds option" {
$uri = Get-WebListenerUrl -Test 'Delay' -TestValue '5'
$command = "Invoke-WebRequest -Uri '$uri' -ConnectTimeoutSec 2"
$command = "Invoke-WebRequest -Uri '$uri' -ConnectionTimeoutSeconds 2"

$result = ExecuteWebCommand -command $command
$result.Error.FullyQualifiedErrorId | Should -Be "System.Threading.Tasks.TaskCanceledException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand"
Expand Down Expand Up @@ -2636,9 +2636,9 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" {
$Result.Output | Should -Match '测试123'
}

It "Invoke-RestMethod validate ConnectTimeoutSec option" {
It "Invoke-RestMethod validate ConnectionTimeoutSeconds option" {
$uri = Get-WebListenerUrl -Test 'Delay' -TestValue '5'
$command = "Invoke-RestMethod -Uri '$uri' -ConnectTimeoutSec 2"
$command = "Invoke-RestMethod -Uri '$uri' -ConnectionTimeoutSeconds 2"

$result = ExecuteWebCommand -command $command
$result.Error.FullyQualifiedErrorId | Should -Be "System.Threading.Tasks.TaskCanceledException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand"
Expand Down Expand Up @@ -4422,7 +4422,7 @@ Describe 'Invoke-WebRequest and Invoke-RestMethod support Cancellation through C
}
}

Describe 'Invoke-WebRequest and Invoke-RestMethod support NetworkTimeoutSec' -Tags "CI", "RequireAdminOnWindows" {
Describe 'Invoke-WebRequest and Invoke-RestMethod support OperationTimeoutSeconds' -Tags "CI", "RequireAdminOnWindows" {
BeforeAll {
$oldProgress = $ProgressPreference
$ProgressPreference = 'SilentlyContinue'
Expand All @@ -4439,13 +4439,13 @@ Describe 'Invoke-WebRequest and Invoke-RestMethod support NetworkTimeoutSec' -Ta
[string]$Command = 'Invoke-WebRequest',
[string]$Arguments = '',
[uri]$Uri,
[int]$NetworkTimeoutSec,
[int]$OperationTimeoutSeconds,
[switch]$WillTimeout
)

$invoke = "$Command -Uri `"$Uri`" $Arguments"
if ($PSBoundParameters.ContainsKey('NetworkTimeoutSec')) {
$invoke = "$invoke -NetworkTimeoutSec $NetworkTimeoutSec"
if ($PSBoundParameters.ContainsKey('OperationTimeoutSeconds')) {
$invoke = "$invoke -OperationTimeoutSeconds $OperationTimeoutSeconds"
}

$result = ExecuteWebCommand -command $invoke
Expand All @@ -4458,43 +4458,43 @@ Describe 'Invoke-WebRequest and Invoke-RestMethod support NetworkTimeoutSec' -Ta
}
}

It 'Invoke-WebRequest: NetworkTimeoutSec does not cancel if stalls shorter than timeout but download takes longer than timeout' {
It 'Invoke-WebRequest: OperationTimeoutSeconds does not cancel if stalls shorter than timeout but download takes longer than timeout' {
$uri = Get-WebListenerUrl -Test Stall -TestValue '2' -Query @{ chunks = 5 }
RunWithNetworkTimeout -Uri $uri -NetworkTimeoutSec 4
RunWithNetworkTimeout -Uri $uri -OperationTimeoutSeconds 4
}

It 'Invoke-WebRequest: NetworkTimeoutSec cancels if stall lasts longer than NetworkTimeoutSec value' {
It 'Invoke-WebRequest: OperationTimeoutSeconds cancels if stall lasts longer than OperationTimeoutSeconds value' {
$uri = Get-WebListenerUrl -Test Stall -TestValue 30
RunWithNetworkTimeout -Uri $uri -NetworkTimeoutSec 3 -WillTimeout
RunWithNetworkTimeout -Uri $uri -OperationTimeoutSeconds 3 -WillTimeout
}

It 'Invoke-WebRequest: NetworkTimeoutSec cancels if stall lasts longer than NetworkTimeoutSec value for HTTPS/gzip compression' {
It 'Invoke-WebRequest: OperationTimeoutSeconds cancels if stall lasts longer than OperationTimeoutSeconds value for HTTPS/gzip compression' {
$uri = Get-WebListenerUrl -Https -Test StallGzip -TestValue 30
RunWithNetworkTimeout -Uri $uri -NetworkTimeoutSec 3 -WillTimeout -Arguments '-SkipCertificateCheck'
RunWithNetworkTimeout -Uri $uri -OperationTimeoutSeconds 3 -WillTimeout -Arguments '-SkipCertificateCheck'
}

It 'Invoke-RestMethod: NetworkTimeoutSec does not cancel if stalls shorter than timeout but download takes longer than timeout' {
It 'Invoke-RestMethod: OperationTimeoutSeconds does not cancel if stalls shorter than timeout but download takes longer than timeout' {
$uri = Get-WebListenerUrl -Test Stall -TestValue '2' -Query @{ chunks = 5 }
RunWithNetworkTimeout -Command Invoke-RestMethod -Uri $uri -NetworkTimeoutSec 4
RunWithNetworkTimeout -Command Invoke-RestMethod -Uri $uri -OperationTimeoutSeconds 4
}

It 'Invoke-RestMethod: NetworkTimeoutSec cancels if stall lasts longer than NetworkTimeoutSec value' {
It 'Invoke-RestMethod: OperationTimeoutSeconds cancels if stall lasts longer than OperationTimeoutSeconds value' {
$uri = Get-WebListenerUrl -Test Stall -TestValue 30
RunWithNetworkTimeout -Command Invoke-RestMethod -Uri $uri -NetworkTimeoutSec 2 -WillTimeout
RunWithNetworkTimeout -Command Invoke-RestMethod -Uri $uri -OperationTimeoutSeconds 2 -WillTimeout
}

It 'Invoke-RestMethod: NetworkTimeoutSec cancels when doing XML atom processing' {
It 'Invoke-RestMethod: OperationTimeoutSeconds cancels when doing XML atom processing' {
$uri = Get-WebListenerUrl -Test Stall -TestValue '30/application%2fxml'
RunWithNetworkTimeout -Command Invoke-RestMethod -Uri $uri -NetworkTimeoutSec 2 -WillTimeout
RunWithNetworkTimeout -Command Invoke-RestMethod -Uri $uri -OperationTimeoutSeconds 2 -WillTimeout
}

It 'Invoke-RestMethod: NetworkTimeoutSec cancels when doing JSON processing' {
It 'Invoke-RestMethod: OperationTimeoutSeconds cancels when doing JSON processing' {
$uri = Get-WebListenerUrl -Test Stall -TestValue '30/application%2fjson'
RunWithNetworkTimeout -Command Invoke-RestMethod -Uri $uri -NetworkTimeoutSec 2 -WillTimeout
RunWithNetworkTimeout -Command Invoke-RestMethod -Uri $uri -OperationTimeoutSeconds 2 -WillTimeout
}

It 'Invoke-RestMethod: NetworkTimeoutSec cancels when doing XML atom processing for HTTPS/gzip compression' {
It 'Invoke-RestMethod: OperationTimeoutSeconds cancels when doing XML atom processing for HTTPS/gzip compression' {
$uri = Get-WebListenerUrl -Https -Test StallGzip -TestValue 30/application%2fXML
RunWithNetworkTimeout -Command Invoke-RestMethod -Uri $uri -NetworkTimeoutSec 2 -WillTimeout -Arguments '-SkipCertificateCheck'
RunWithNetworkTimeout -Command Invoke-RestMethod -Uri $uri -OperationTimeoutSeconds 2 -WillTimeout -Arguments '-SkipCertificateCheck'
}
}

0 comments on commit 6b78f78

Please sign in to comment.