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 @@ -81,11 +81,11 @@ internal override void ProcessResponse(HttpResponseMessage response)
ArgumentNullException.ThrowIfNull(_cancelToken);

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

if (ShouldWriteToPipeline)
{
using BufferingStreamReader responseStream = new(baseResponseStream, perReadTimeout, _cancelToken.Token);
responseStream = new BufferingStreamReader(responseStream, perReadTimeout, _cancelToken.Token);

// First see if it is an RSS / ATOM feed, in which case we can
// stream it - unless the user has overridden it with a return type of "XML"
Expand All @@ -110,7 +110,7 @@ internal override void ProcessResponse(HttpResponseMessage response)
}

// NOTE: Tests use this verbose output to verify the encoding.
WriteVerbose(string.Create(System.Globalization.CultureInfo.InvariantCulture, $"Content encoding: {encodingVerboseName}"));
WriteVerbose($"Content encoding: {encodingVerboseName}");

// Determine the response type
RestReturnType returnType = CheckReturnType(response);
Expand All @@ -137,14 +137,17 @@ internal override void ProcessResponse(HttpResponseMessage response)

WriteObject(obj);
}

responseStream.Position = 0;
}
else if (ShouldSaveToOutFile)

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

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

StreamHelper.SaveStreamToFile(baseResponseStream, outFilePath, this, response.Content.Headers.ContentLength.GetValueOrDefault(), perReadTimeout, _cancelToken.Token);
StreamHelper.SaveStreamToFile(responseStream, outFilePath, this, response.Content.Headers.ContentLength.GetValueOrDefault(), perReadTimeout, _cancelToken.Token);
}

if (!string.IsNullOrEmpty(StatusCodeVariable))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2876,6 +2876,27 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" {
Get-Item $outFile | Select-Object -ExpandProperty Length | Should -Be $content.Content.Length
}

It "Invoke-RestMethod -PassThru -OutFile Downloads the file and pipes it" {
$uri = Get-WebListenerUrl -Test 'Get'
$content = Invoke-WebRequest -Uri $uri
$outFile = Join-Path $TestDrive $content.BaseResponse.RequestMessage.RequestUri.Segments[-1]

# ensure the file does not exist
Remove-Item -Force -ErrorAction Ignore -Path $outFile
$response = Invoke-RestMethod -Uri $uri -PassThru -OutFile $outFile

# check if the file is downloaded.
Test-Path $outFile | Should -Be $true

# check if the file is correctly downloaded
Get-Content -Path $outFile | Should -BeExactly $content.Content

# check if the response stores the downloaded file contents
# response is a PSCustomObject so converted it string for comparison
$responseAsJsonString = $response | ConvertTo-Json -Compress
$responseAsJsonString | Should -BeExactly $content.Content
}

It "Invoke-RestMethod should fail if -OutFile is <Name>." -TestCases @(
@{ Name = "empty"; Value = [string]::Empty }
@{ Name = "null"; Value = $null }
Expand Down