Skip to content
Merged
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 @@ -70,6 +70,11 @@ public class WebResponseObject
/// </summary>
public string StatusDescription => WebResponseHelper.GetStatusDescription(BaseResponse);

/// <summary>
/// Gets or sets the output file path.
/// </summary>
public string? OutFile { get; internal set; }

#endregion Properties

#region Protected Fields
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ internal override void ProcessResponse(HttpResponseMessage response)
ArgumentNullException.ThrowIfNull(response);
TimeSpan perReadTimeout = ConvertTimeoutSecondsToTimeSpan(OperationTimeoutSeconds);
Stream responseStream = StreamHelper.GetResponseStream(response, _cancelToken.Token);
string outFilePath = WebResponseHelper.GetOutFilePath(response, _qualifiedOutFile);

if (ShouldWriteToPipeline)
{
// Creating a MemoryStream wrapper to response stream here to support IsStopping.
Expand All @@ -50,6 +52,7 @@ internal override void ProcessResponse(HttpResponseMessage response)
_cancelToken.Token);
WebResponseObject ro = WebResponseHelper.IsText(response) ? new BasicHtmlWebResponseObject(response, responseStream, perReadTimeout, _cancelToken.Token) : new WebResponseObject(response, responseStream, perReadTimeout, _cancelToken.Token);
ro.RelationLink = _relationLink;
ro.OutFile = outFilePath;
WriteObject(ro);

// Use the rawcontent stream from WebResponseObject for further
Expand All @@ -61,9 +64,7 @@ internal override void ProcessResponse(HttpResponseMessage response)

if (ShouldSaveToOutFile)
{
string outFilePath = WebResponseHelper.GetOutFilePath(response, _qualifiedOutFile);

WriteVerbose(string.Create(System.Globalization.CultureInfo.InvariantCulture, $"File Name: {Path.GetFileName(outFilePath)}"));
WriteVerbose($"File Name: {Path.GetFileName(outFilePath)}");

// ContentLength is always the partial length, while ContentRange is the full length
// Without Request.Range set, ContentRange is null and partial length (ContentLength) equals to full length
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,28 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" {
Get-Item $outFile | Select-Object -ExpandProperty Length | Should -Be $content.Content.Length
}

It "Invoke-WebRequest -PassThru -OutFile <directory> saves the downloaded file path as a property in WebResponseObject"{
$uri = Get-WebListenerUrl -Test 'Get'
$content = Invoke-WebRequest -Uri $uri -PassThru -OutFile $TestDrive
$content.OutFile | Should -exist
$content.OutFile | Should -Be (Join-Path $TestDrive 'Get')
}

It "Invoke-WebRequest -PassThru -OutFile <file> saves the downloaded file path as a property in WebResponseObject"{
$uri = Get-WebListenerUrl -Test 'Get'
$filePath = Join-Path $TestDrive "pestertest-outfile"
$content = Invoke-WebRequest -Uri $uri -PassThru -OutFile $filePath
$content.OutFile | Should -exist
$content.OutFile | Should -Be $filePath
}

It "Invoke-WebRequest -PassThru -OutFile -Verbose File Name reflects the downloaded file name" {
$uri = Get-WebListenerUrl -Test 'Get'
$filePath = Join-Path $TestDrive "pestertest-outfile"
$content = Invoke-WebRequest -Verbose -Uri $uri -PassThru -OutFile $filePath 4>variable:verbo
$verbo[-1].Message | Should -Match "pestertest-outfile"
}

It "Invoke-WebRequest should fail if -OutFile is <Name>." -TestCases @(
@{ Name = "empty"; Value = [string]::Empty }
@{ Name = "null"; Value = $null }
Expand Down Expand Up @@ -4638,7 +4660,6 @@ Describe 'Invoke-WebRequest and Invoke-RestMethod support OperationTimeoutSecond
}
}


Describe "Invoke-RestMethod should run in the default synchronization context (threadpool)" -Tag "CI" {
BeforeAll {
$oldProgress = $ProgressPreference
Expand Down