From 68e469a2e1c0ff06c11dc192583cd28d16e02a8c Mon Sep 17 00:00:00 2001 From: Ilya Date: Tue, 28 Jan 2020 16:53:12 +0500 Subject: [PATCH 1/6] Replace OutFile parameter with Path and LiteralPath in web cmdlets --- .../Common/WebRequestPSCmdlet.Common.cs | 51 +++++++++---- .../resources/WebCmdletStrings.resx | 7 +- .../WebCmdlets.Tests.ps1 | 71 +++++++++++++------ 3 files changed, 91 insertions(+), 38 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index ee56360e103..f9b47cd4422 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -364,10 +364,28 @@ public virtual string CustomMethod #region Output /// - /// Gets or sets the OutFile property. + /// Gets or sets the Path property. /// [Parameter] - public virtual string OutFile { get; set; } + [Alias("OutFile")] + public virtual string Path + { + get => _path; + set => _path = value; + } + + private string _path; + + /// + /// Gets or sets the LiteralPath property. + /// + [Parameter] + [Alias("LP")] + public string LiteralPath + { + get => _path; + set => _path = value; + } /// /// Gets or sets the PassThrough property. @@ -376,7 +394,7 @@ public virtual string CustomMethod public virtual SwitchParameter PassThru { get; set; } /// - /// Resumes downloading a partial or incomplete file. OutFile is required. + /// Resumes downloading a partial or incomplete file. Path/LiteralPath is required. /// [Parameter] public virtual SwitchParameter Resume { get; set; } @@ -395,6 +413,12 @@ public virtual string CustomMethod internal virtual void ValidateParameters() { + if (MyInvocation.BoundParameters.ContainsKey(nameof(Path)) && MyInvocation.BoundParameters.ContainsKey(nameof(LiteralPath))) + { + ErrorRecord error = GetValidationError(WebCmdletStrings.CannotSpecifyPathAndLiteralPath, "CannotSpecifyPathAndLiteralPath"); + ThrowTerminatingError(error); + } + // sessions if ((WebSession != null) && (SessionVariable != null)) { @@ -550,15 +574,15 @@ internal virtual void ValidateParameters() } // output ?? - if (PassThru && (OutFile == null)) + if (PassThru && (_path == null)) { ErrorRecord error = GetValidationError(WebCmdletStrings.OutFileMissing, "WebCmdletOutFileMissingException", nameof(PassThru)); ThrowTerminatingError(error); } - // Resume requires OutFile. - if (Resume.IsPresent && OutFile == null) + // Resume requires Path (or LiteralPath) parameter. + if (Resume.IsPresent && _path == null) { ErrorRecord error = GetValidationError(WebCmdletStrings.OutFileMissing, "WebCmdletOutFileMissingException", nameof(Resume)); @@ -690,12 +714,12 @@ internal virtual void PrepareSession() internal string QualifiedOutFile { - get { return (QualifyFilePath(OutFile)); } + get { return (QualifyFilePath(_path)); } } internal bool ShouldSaveToOutFile { - get { return (!string.IsNullOrEmpty(OutFile)); } + get { return (!string.IsNullOrEmpty(_path)); } } internal bool ShouldWriteToPipeline @@ -763,7 +787,7 @@ private Uri CheckProtocol(Uri uri) private string QualifyFilePath(string path) { - string resolvedFilePath = PathUtils.ResolveFilePath(path, this, false); + string resolvedFilePath = PathUtils.ResolveFilePath(path, this, isLiteralPath: MyInvocation.BoundParameters.ContainsKey("LiteralPath")); return resolvedFilePath; } @@ -1396,7 +1420,7 @@ internal virtual HttpResponseMessage GetResponse(HttpClient client, HttpRequestM WriteVerbose(WebCmdletStrings.WebMethodResumeFailedVerboseMsg); // Disable the Resume switch so the subsequent calls to GetResponse() and FillRequestStream() - // are treated as a standard -OutFile request. This also disables appending local file. + // are treated as a standard -Path/-LiteralPath request. This also disables appending local file. Resume = new SwitchParameter(false); using (HttpRequestMessage requestWithoutRange = GetRequest(currentUri)) @@ -1529,9 +1553,10 @@ protected override void ProcessRecord() response.Content.Headers.ContentRange.Length == _resumeFileSize) { _isSuccess = true; - WriteVerbose(string.Format(CultureInfo.CurrentCulture, WebCmdletStrings.OutFileWritingSkipped, OutFile)); - // Disable writing to the OutFile. - OutFile = null; + WriteVerbose(string.Format(CultureInfo.CurrentCulture, WebCmdletStrings.OutFileWritingSkipped, _path)); + + // Disable writing to the output file. + _path = null; } if (ShouldCheckHttpStatus && !_isSuccess) diff --git a/src/Microsoft.PowerShell.Commands.Utility/resources/WebCmdletStrings.resx b/src/Microsoft.PowerShell.Commands.Utility/resources/WebCmdletStrings.resx index 397718a09bb..b4010b202b1 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/resources/WebCmdletStrings.resx +++ b/src/Microsoft.PowerShell.Commands.Utility/resources/WebCmdletStrings.resx @@ -187,10 +187,10 @@ Path '{0}' is not a file system path. Please specify the path to a file in the file system. - The cmdlet cannot run because the following parameter is missing: OutFile. Provide a valid OutFile parameter value when using the {0} parameter, then retry. + The cmdlet cannot run because the following parameter is missing: Path (or LiteralPath). Provide a valid Path (or LiteralPath) parameter value when using the {0} parameter, then retry. - The file will not be re-downloaded because the remote file is the same size as the OutFile: {0} + The file will not be re-downloaded because the remote file is the same size as the output file: {0} The cmdlet cannot run because the following conflicting parameters are specified: ProxyCredential and ProxyUseDefaultCredentials. Specify either ProxyCredential or ProxyUseDefaultCredentials, then retry. @@ -261,4 +261,7 @@ Retrying after interval of {0} seconds. Status code for previous attempt: {1} + + You must specify either the -Path or -LiteralPath parameter, but not both. + diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index ce3720e49c5..d77efe5d098 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -27,7 +27,7 @@ function ExecuteWebCommand { return $result } -# This function calls either Invoke-WebRequest or Invoke-RestMethod using the OutFile parameter +# This function calls either Invoke-WebRequest or Invoke-RestMethod using the Path parameter # Then, the file content is read and return in a $result object. # function ExecuteRequestWithOutFile { @@ -44,9 +44,9 @@ function ExecuteRequestWithOutFile { $filePath = Join-Path $TestDrive ((Get-Random).ToString() + ".txt") try { if ($cmdletName -eq "Invoke-WebRequest") { - Invoke-WebRequest -Uri $uri -OutFile $filePath + Invoke-WebRequest -Uri $uri -Path $filePath } else { - Invoke-RestMethod -Uri $uri -OutFile $filePath + Invoke-RestMethod -Uri $uri -Path $filePath } $result.Output = Get-Content $filePath -Raw -ErrorAction SilentlyContinue } catch { @@ -641,7 +641,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { } } - It "Validate Invoke-WebRequest -OutFile" { + It "Validate Invoke-WebRequest -Path" { $uri = Get-WebListenerUrl -Test 'Get' $result = ExecuteRequestWithOutFile -cmdletName "Invoke-WebRequest" -uri $uri $jsonContent = $result.Output | ConvertFrom-Json @@ -814,6 +814,31 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { $result.error | Should -Not -BeNullOrEmpty } + Context "Path and LigeralPath parameters work" { + BeforeAll { + $uri = Get-WebListenerUrl -Test 'Get' + $testOutfile1 = Join-Path $TestDrive "[outfile1].txt" + $testOutfile2 = Join-Path $TestDrive "[outfile2].txt" + } + + It "Cannot use both Path and LigeralPath parameters" { + { Invoke-WebRequest -uri $uri -Path $testOutFile1 -LiteralPath $testOutFile1 -ErrorAction Stop } | Should -Throw -ErrorId "CannotSpecifyPAthAndLiteralPath,Microsoft.PowerShell.Commands.InvokeWebRequestCommand" + { Invoke-RestMethod -uri $uri -Path $testOutFile1 -LiteralPath $testOutFile1 -ErrorAction Stop } | Should -Throw -ErrorId "CannotSpecifyPAthAndLiteralPath,Microsoft.PowerShell.Commands.InvokeRestMethodCommand" + } + + It "Path and LigeralPath parameters work in Invoke-WebRequest" { + { Invoke-WebRequest -uri $uri -Path $testOutFile1 -ErrorAction Stop } | Should -Throw -ErrorId "FileOpenFailure,Microsoft.PowerShell.Commands.InvokeWebRequestCommand" + Invoke-WebRequest -uri $uri -LiteralPath $testOutFile1 -ErrorAction Stop + Test-Path -LiteralPath $testOutFile1 | Should -BeTrue + } + + It "Path and LigeralPath parameters work in Invoke-RestMethod" { + { Invoke-RestMethod -uri $uri -Path $testOutFile1 -ErrorAction Stop } | Should -Throw -ErrorId "FileOpenFailure,Microsoft.PowerShell.Commands.InvokeRestMethodCommand" + Invoke-RestMethod -uri $uri -LiteralPath $testOutFile2 -ErrorAction Stop + Test-Path -LiteralPath $testOutFile2 | Should -BeTrue + } + } + Context "Redirect" { It "Validates Invoke-WebRequest with -PreserveAuthorizationOnRedirect preserves the authorization header on redirect: " -TestCases $redirectTests { param($redirectType, $redirectedMethod) @@ -1754,7 +1779,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { # Download the entire file to reference in tests $referenceFile = Join-Path $TestDrive "reference.txt" $resumeUri = Get-WebListenerUrl -Test 'Resume' - Invoke-WebRequest -uri $resumeUri -OutFile $referenceFile -ErrorAction Stop + Invoke-WebRequest -uri $resumeUri -Path $referenceFile -ErrorAction Stop $referenceFileHash = Get-FileHash -Algorithm SHA256 -Path $referenceFile $referenceFileSize = Get-Item $referenceFile | Select-Object -ExpandProperty Length } @@ -1763,13 +1788,13 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { Remove-Item -Force -ErrorAction 'SilentlyContinue' -Path $outFile } - It "Invoke-WebRequest -Resume requires -OutFile" { + It "Invoke-WebRequest -Resume requires -Path" { { Invoke-WebRequest -Resume -Uri $resumeUri -ErrorAction Stop } | Should -Throw -ErrorId 'WebCmdletOutFileMissingException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand' } It "Invoke-WebRequest -Resume Downloads the whole file when the file does not exist" { - $response = Invoke-WebRequest -uri $resumeUri -OutFile $outFile -Resume -PassThru + $response = Invoke-WebRequest -uri $resumeUri -Path $outFile -Resume -PassThru $outFileHash = Get-FileHash -Algorithm SHA256 -Path $outFile $outFileHash.Hash | Should -BeExactly $referenceFileHash.Hash @@ -1786,7 +1811,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { 1..$largerFileSize | ForEach-Object { [Byte]$_ } | Set-Content -AsByteStream $outFile $largerFileSize = Get-Item $outFile | Select-Object -ExpandProperty Length - $response = Invoke-WebRequest -uri $resumeUri -OutFile $outFile -Resume -PassThru + $response = Invoke-WebRequest -uri $resumeUri -Path $outFile -Resume -PassThru $outFileHash = Get-FileHash -Algorithm SHA256 -Path $outFile $outFileHash.Hash | Should -BeExactly $referenceFileHash.Hash @@ -1804,7 +1829,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { $largerFileSize = Get-Item $outFile | Select-Object -ExpandProperty Length $uri = Get-WebListenerUrl -Test 'Resume' -TestValue 'NoResume' - $response = Invoke-WebRequest -Uri $uri -OutFile $outFile -Resume -PassThru + $response = Invoke-WebRequest -Uri $uri -Path $outFile -Resume -PassThru $outFileHash = Get-FileHash -Algorithm SHA256 -Path $outFile $outFileHash.Hash | Should -BeExactly $referenceFileHash.Hash @@ -1824,10 +1849,10 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { param($bytes, $statuscode) # Simulate partial download $uri = Get-WebListenerUrl -Test 'Resume' -TestValue "Bytes/$bytes" - $null = Invoke-WebRequest -uri $uri -OutFile $outFile + $null = Invoke-WebRequest -uri $uri -Path $outFile Get-Item $outFile | Select-Object -ExpandProperty Length | Should -Be $bytes - $response = Invoke-WebRequest -uri $resumeUri -OutFile $outFile -Resume -PassThru + $response = Invoke-WebRequest -uri $resumeUri -Path $outFile -Resume -PassThru $outFileHash = Get-FileHash -Algorithm SHA256 -Path $outFile $outFileHash.Hash | Should -BeExactly $referenceFileHash.Hash @@ -1841,10 +1866,10 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { It "Invoke-WebRequest -Resume assumes the file was successfully completed when the local and remote file are the same size." { # Download the entire file $uri = Get-WebListenerUrl -Test 'Resume' -TestValue 'NoResume' - $null = Invoke-WebRequest -uri $uri -OutFile $outFile + $null = Invoke-WebRequest -uri $uri -Path $outFile $fileSize = Get-Item $outFile | Select-Object -ExpandProperty Length - $response = Invoke-WebRequest -uri $resumeUri -OutFile $outFile -Resume -PassThru + $response = Invoke-WebRequest -uri $resumeUri -Path $outFile -Resume -PassThru $outFileHash = Get-FileHash -Algorithm SHA256 -Path $outFile $outFileHash.Hash | Should -BeExactly $referenceFileHash.Hash @@ -2209,7 +2234,7 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { } } - It "Validate Invoke-RestMethod -OutFile" { + It "Validate Invoke-RestMethod -Path" { $uri = Get-WebListenerUrl -Test 'Get' $result = ExecuteRequestWithOutFile -cmdletName "Invoke-RestMethod" -uri $uri $jsonContent = $result.Output | ConvertFrom-Json @@ -3278,7 +3303,7 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { # Download the entire file to reference in tests $referenceFile = Join-Path $TestDrive "reference.txt" $resumeUri = Get-WebListenerUrl -Test 'Resume' - Invoke-RestMethod -uri $resumeUri -OutFile $referenceFile -ErrorAction Stop + Invoke-RestMethod -uri $resumeUri -Path $referenceFile -ErrorAction Stop $referenceFileHash = Get-FileHash -Algorithm SHA256 -Path $referenceFile $referenceFileSize = Get-Item $referenceFile | Select-Object -ExpandProperty Length } @@ -3287,7 +3312,7 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { Remove-Item -Force -ErrorAction 'SilentlyContinue' -Path $outFile } - It "Invoke-RestMethod -Resume requires -OutFile" { + It "Invoke-RestMethod -Resume requires -Path" { { Invoke-RestMethod -Resume -Uri $resumeUri -ErrorAction Stop } | Should -Throw -ErrorId 'WebCmdletOutFileMissingException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand' } @@ -3296,7 +3321,7 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { # ensure the file does not exist Remove-Item -Force -ErrorAction 'SilentlyContinue' -Path $outFile - Invoke-RestMethod -uri $resumeUri -OutFile $outFile -ResponseHeadersVariable 'Headers' -Resume + Invoke-RestMethod -uri $resumeUri -Path $outFile -ResponseHeadersVariable 'Headers' -Resume $outFileHash = Get-FileHash -Algorithm SHA256 -Path $outFile $outFileHash.Hash | Should -BeExactly $referenceFileHash.Hash @@ -3312,7 +3337,7 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { 1..$largerFileSize | ForEach-Object { [Byte]$_ } | Set-Content -AsByteStream $outFile $largerFileSize = Get-Item $outFile | Select-Object -ExpandProperty Length - $response = Invoke-RestMethod -uri $resumeUri -OutFile $outFile -ResponseHeadersVariable 'Headers' -Resume + $response = Invoke-RestMethod -uri $resumeUri -Path $outFile -ResponseHeadersVariable 'Headers' -Resume $outFileHash = Get-FileHash -Algorithm SHA256 -Path $outFile $outFileHash.Hash | Should -BeExactly $referenceFileHash.Hash @@ -3329,7 +3354,7 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { $largerFileSize = Get-Item $outFile | Select-Object -ExpandProperty Length $uri = Get-WebListenerUrl -Test 'Resume' -TestValue 'NoResume' - $response = Invoke-RestMethod -uri $uri -OutFile $outFile -ResponseHeadersVariable 'Headers' -Resume + $response = Invoke-RestMethod -uri $uri -Path $outFile -ResponseHeadersVariable 'Headers' -Resume $outFileHash = Get-FileHash -Algorithm SHA256 -Path $outFile $outFileHash.Hash | Should -BeExactly $referenceFileHash.Hash @@ -3349,10 +3374,10 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { param($bytes) # Simulate partial download $uri = Get-WebListenerUrl -Test 'Resume' -TestValue "Bytes/$bytes" - $null = Invoke-RestMethod -uri $uri -OutFile $outFile + $null = Invoke-RestMethod -uri $uri -Path $outFile Get-Item $outFile | Select-Object -ExpandProperty Length | Should -Be $bytes - $response = Invoke-RestMethod -uri $resumeUri -OutFile $outFile -ResponseHeadersVariable 'Headers' -Resume + $response = Invoke-RestMethod -uri $resumeUri -Path $outFile -ResponseHeadersVariable 'Headers' -Resume $outFileHash = Get-FileHash -Algorithm SHA256 -Path $outFile $outFileHash.Hash | Should -BeExactly $referenceFileHash.Hash @@ -3365,10 +3390,10 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { It "Invoke-RestMethod -Resume assumes the file was successfully completed when the local and remote file are the same size." { # Download the entire file $uri = Get-WebListenerUrl -Test 'Resume' -TestValue 'NoResume' - $null = Invoke-RestMethod -uri $uri -OutFile $outFile + $null = Invoke-RestMethod -uri $uri -Path $outFile $fileSize = Get-Item $outFile | Select-Object -ExpandProperty Length - $response = Invoke-RestMethod -uri $resumeUri -OutFile $outFile -ResponseHeadersVariable 'Headers' -Resume + $response = Invoke-RestMethod -uri $resumeUri -Path $outFile -ResponseHeadersVariable 'Headers' -Resume $outFileHash = Get-FileHash -Algorithm SHA256 -Path $outFile $outFileHash.Hash | Should -BeExactly $referenceFileHash.Hash From 8ccf5be0db1fd0b9947379fabfd2bcd5bf3f26bf Mon Sep 17 00:00:00 2001 From: Ilya Date: Thu, 30 Jan 2020 09:54:46 +0500 Subject: [PATCH 2/6] Fix typo in tests --- .../Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index d77efe5d098..3979f564e87 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -814,25 +814,25 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { $result.error | Should -Not -BeNullOrEmpty } - Context "Path and LigeralPath parameters work" { + Context "Path and LiteralPath parameters work" { BeforeAll { $uri = Get-WebListenerUrl -Test 'Get' $testOutfile1 = Join-Path $TestDrive "[outfile1].txt" $testOutfile2 = Join-Path $TestDrive "[outfile2].txt" } - It "Cannot use both Path and LigeralPath parameters" { + It "Cannot use both Path and LiteralPath parameters" { { Invoke-WebRequest -uri $uri -Path $testOutFile1 -LiteralPath $testOutFile1 -ErrorAction Stop } | Should -Throw -ErrorId "CannotSpecifyPAthAndLiteralPath,Microsoft.PowerShell.Commands.InvokeWebRequestCommand" { Invoke-RestMethod -uri $uri -Path $testOutFile1 -LiteralPath $testOutFile1 -ErrorAction Stop } | Should -Throw -ErrorId "CannotSpecifyPAthAndLiteralPath,Microsoft.PowerShell.Commands.InvokeRestMethodCommand" } - It "Path and LigeralPath parameters work in Invoke-WebRequest" { + It "Path and LiteralPath parameters work in Invoke-WebRequest" { { Invoke-WebRequest -uri $uri -Path $testOutFile1 -ErrorAction Stop } | Should -Throw -ErrorId "FileOpenFailure,Microsoft.PowerShell.Commands.InvokeWebRequestCommand" Invoke-WebRequest -uri $uri -LiteralPath $testOutFile1 -ErrorAction Stop Test-Path -LiteralPath $testOutFile1 | Should -BeTrue } - It "Path and LigeralPath parameters work in Invoke-RestMethod" { + It "Path and LiteralPath parameters work in Invoke-RestMethod" { { Invoke-RestMethod -uri $uri -Path $testOutFile1 -ErrorAction Stop } | Should -Throw -ErrorId "FileOpenFailure,Microsoft.PowerShell.Commands.InvokeRestMethodCommand" Invoke-RestMethod -uri $uri -LiteralPath $testOutFile2 -ErrorAction Stop Test-Path -LiteralPath $testOutFile2 | Should -BeTrue From b0bda5a05f9e95b379866a39cef010999c970237 Mon Sep 17 00:00:00 2001 From: Ilya Date: Thu, 27 Feb 2020 23:12:41 +0500 Subject: [PATCH 3/6] Revert "Fix typo in tests" This reverts commit 36465ad61ff2a772a2d1185096ff779b4b637042. --- .../Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index 3979f564e87..d77efe5d098 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -814,25 +814,25 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { $result.error | Should -Not -BeNullOrEmpty } - Context "Path and LiteralPath parameters work" { + Context "Path and LigeralPath parameters work" { BeforeAll { $uri = Get-WebListenerUrl -Test 'Get' $testOutfile1 = Join-Path $TestDrive "[outfile1].txt" $testOutfile2 = Join-Path $TestDrive "[outfile2].txt" } - It "Cannot use both Path and LiteralPath parameters" { + It "Cannot use both Path and LigeralPath parameters" { { Invoke-WebRequest -uri $uri -Path $testOutFile1 -LiteralPath $testOutFile1 -ErrorAction Stop } | Should -Throw -ErrorId "CannotSpecifyPAthAndLiteralPath,Microsoft.PowerShell.Commands.InvokeWebRequestCommand" { Invoke-RestMethod -uri $uri -Path $testOutFile1 -LiteralPath $testOutFile1 -ErrorAction Stop } | Should -Throw -ErrorId "CannotSpecifyPAthAndLiteralPath,Microsoft.PowerShell.Commands.InvokeRestMethodCommand" } - It "Path and LiteralPath parameters work in Invoke-WebRequest" { + It "Path and LigeralPath parameters work in Invoke-WebRequest" { { Invoke-WebRequest -uri $uri -Path $testOutFile1 -ErrorAction Stop } | Should -Throw -ErrorId "FileOpenFailure,Microsoft.PowerShell.Commands.InvokeWebRequestCommand" Invoke-WebRequest -uri $uri -LiteralPath $testOutFile1 -ErrorAction Stop Test-Path -LiteralPath $testOutFile1 | Should -BeTrue } - It "Path and LiteralPath parameters work in Invoke-RestMethod" { + It "Path and LigeralPath parameters work in Invoke-RestMethod" { { Invoke-RestMethod -uri $uri -Path $testOutFile1 -ErrorAction Stop } | Should -Throw -ErrorId "FileOpenFailure,Microsoft.PowerShell.Commands.InvokeRestMethodCommand" Invoke-RestMethod -uri $uri -LiteralPath $testOutFile2 -ErrorAction Stop Test-Path -LiteralPath $testOutFile2 | Should -BeTrue From 879ed504c5bbe9052841a96193c16bf2b210b689 Mon Sep 17 00:00:00 2001 From: Ilya Date: Thu, 27 Feb 2020 23:12:46 +0500 Subject: [PATCH 4/6] Revert "Replace OutFile parameter with Path and LiteralPath in web cmdlets" This reverts commit 1e03c5f61e83a8de8f96766965010b9178c627cb. --- .../Common/WebRequestPSCmdlet.Common.cs | 51 ++++--------- .../resources/WebCmdletStrings.resx | 7 +- .../WebCmdlets.Tests.ps1 | 71 ++++++------------- 3 files changed, 38 insertions(+), 91 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index f9b47cd4422..ee56360e103 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -364,28 +364,10 @@ public virtual string CustomMethod #region Output /// - /// Gets or sets the Path property. + /// Gets or sets the OutFile property. /// [Parameter] - [Alias("OutFile")] - public virtual string Path - { - get => _path; - set => _path = value; - } - - private string _path; - - /// - /// Gets or sets the LiteralPath property. - /// - [Parameter] - [Alias("LP")] - public string LiteralPath - { - get => _path; - set => _path = value; - } + public virtual string OutFile { get; set; } /// /// Gets or sets the PassThrough property. @@ -394,7 +376,7 @@ public string LiteralPath public virtual SwitchParameter PassThru { get; set; } /// - /// Resumes downloading a partial or incomplete file. Path/LiteralPath is required. + /// Resumes downloading a partial or incomplete file. OutFile is required. /// [Parameter] public virtual SwitchParameter Resume { get; set; } @@ -413,12 +395,6 @@ public string LiteralPath internal virtual void ValidateParameters() { - if (MyInvocation.BoundParameters.ContainsKey(nameof(Path)) && MyInvocation.BoundParameters.ContainsKey(nameof(LiteralPath))) - { - ErrorRecord error = GetValidationError(WebCmdletStrings.CannotSpecifyPathAndLiteralPath, "CannotSpecifyPathAndLiteralPath"); - ThrowTerminatingError(error); - } - // sessions if ((WebSession != null) && (SessionVariable != null)) { @@ -574,15 +550,15 @@ internal virtual void ValidateParameters() } // output ?? - if (PassThru && (_path == null)) + if (PassThru && (OutFile == null)) { ErrorRecord error = GetValidationError(WebCmdletStrings.OutFileMissing, "WebCmdletOutFileMissingException", nameof(PassThru)); ThrowTerminatingError(error); } - // Resume requires Path (or LiteralPath) parameter. - if (Resume.IsPresent && _path == null) + // Resume requires OutFile. + if (Resume.IsPresent && OutFile == null) { ErrorRecord error = GetValidationError(WebCmdletStrings.OutFileMissing, "WebCmdletOutFileMissingException", nameof(Resume)); @@ -714,12 +690,12 @@ internal virtual void PrepareSession() internal string QualifiedOutFile { - get { return (QualifyFilePath(_path)); } + get { return (QualifyFilePath(OutFile)); } } internal bool ShouldSaveToOutFile { - get { return (!string.IsNullOrEmpty(_path)); } + get { return (!string.IsNullOrEmpty(OutFile)); } } internal bool ShouldWriteToPipeline @@ -787,7 +763,7 @@ private Uri CheckProtocol(Uri uri) private string QualifyFilePath(string path) { - string resolvedFilePath = PathUtils.ResolveFilePath(path, this, isLiteralPath: MyInvocation.BoundParameters.ContainsKey("LiteralPath")); + string resolvedFilePath = PathUtils.ResolveFilePath(path, this, false); return resolvedFilePath; } @@ -1420,7 +1396,7 @@ internal virtual HttpResponseMessage GetResponse(HttpClient client, HttpRequestM WriteVerbose(WebCmdletStrings.WebMethodResumeFailedVerboseMsg); // Disable the Resume switch so the subsequent calls to GetResponse() and FillRequestStream() - // are treated as a standard -Path/-LiteralPath request. This also disables appending local file. + // are treated as a standard -OutFile request. This also disables appending local file. Resume = new SwitchParameter(false); using (HttpRequestMessage requestWithoutRange = GetRequest(currentUri)) @@ -1553,10 +1529,9 @@ protected override void ProcessRecord() response.Content.Headers.ContentRange.Length == _resumeFileSize) { _isSuccess = true; - WriteVerbose(string.Format(CultureInfo.CurrentCulture, WebCmdletStrings.OutFileWritingSkipped, _path)); - - // Disable writing to the output file. - _path = null; + WriteVerbose(string.Format(CultureInfo.CurrentCulture, WebCmdletStrings.OutFileWritingSkipped, OutFile)); + // Disable writing to the OutFile. + OutFile = null; } if (ShouldCheckHttpStatus && !_isSuccess) diff --git a/src/Microsoft.PowerShell.Commands.Utility/resources/WebCmdletStrings.resx b/src/Microsoft.PowerShell.Commands.Utility/resources/WebCmdletStrings.resx index b4010b202b1..397718a09bb 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/resources/WebCmdletStrings.resx +++ b/src/Microsoft.PowerShell.Commands.Utility/resources/WebCmdletStrings.resx @@ -187,10 +187,10 @@ Path '{0}' is not a file system path. Please specify the path to a file in the file system. - The cmdlet cannot run because the following parameter is missing: Path (or LiteralPath). Provide a valid Path (or LiteralPath) parameter value when using the {0} parameter, then retry. + The cmdlet cannot run because the following parameter is missing: OutFile. Provide a valid OutFile parameter value when using the {0} parameter, then retry. - The file will not be re-downloaded because the remote file is the same size as the output file: {0} + The file will not be re-downloaded because the remote file is the same size as the OutFile: {0} The cmdlet cannot run because the following conflicting parameters are specified: ProxyCredential and ProxyUseDefaultCredentials. Specify either ProxyCredential or ProxyUseDefaultCredentials, then retry. @@ -261,7 +261,4 @@ Retrying after interval of {0} seconds. Status code for previous attempt: {1} - - You must specify either the -Path or -LiteralPath parameter, but not both. - diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index d77efe5d098..ce3720e49c5 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -27,7 +27,7 @@ function ExecuteWebCommand { return $result } -# This function calls either Invoke-WebRequest or Invoke-RestMethod using the Path parameter +# This function calls either Invoke-WebRequest or Invoke-RestMethod using the OutFile parameter # Then, the file content is read and return in a $result object. # function ExecuteRequestWithOutFile { @@ -44,9 +44,9 @@ function ExecuteRequestWithOutFile { $filePath = Join-Path $TestDrive ((Get-Random).ToString() + ".txt") try { if ($cmdletName -eq "Invoke-WebRequest") { - Invoke-WebRequest -Uri $uri -Path $filePath + Invoke-WebRequest -Uri $uri -OutFile $filePath } else { - Invoke-RestMethod -Uri $uri -Path $filePath + Invoke-RestMethod -Uri $uri -OutFile $filePath } $result.Output = Get-Content $filePath -Raw -ErrorAction SilentlyContinue } catch { @@ -641,7 +641,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { } } - It "Validate Invoke-WebRequest -Path" { + It "Validate Invoke-WebRequest -OutFile" { $uri = Get-WebListenerUrl -Test 'Get' $result = ExecuteRequestWithOutFile -cmdletName "Invoke-WebRequest" -uri $uri $jsonContent = $result.Output | ConvertFrom-Json @@ -814,31 +814,6 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { $result.error | Should -Not -BeNullOrEmpty } - Context "Path and LigeralPath parameters work" { - BeforeAll { - $uri = Get-WebListenerUrl -Test 'Get' - $testOutfile1 = Join-Path $TestDrive "[outfile1].txt" - $testOutfile2 = Join-Path $TestDrive "[outfile2].txt" - } - - It "Cannot use both Path and LigeralPath parameters" { - { Invoke-WebRequest -uri $uri -Path $testOutFile1 -LiteralPath $testOutFile1 -ErrorAction Stop } | Should -Throw -ErrorId "CannotSpecifyPAthAndLiteralPath,Microsoft.PowerShell.Commands.InvokeWebRequestCommand" - { Invoke-RestMethod -uri $uri -Path $testOutFile1 -LiteralPath $testOutFile1 -ErrorAction Stop } | Should -Throw -ErrorId "CannotSpecifyPAthAndLiteralPath,Microsoft.PowerShell.Commands.InvokeRestMethodCommand" - } - - It "Path and LigeralPath parameters work in Invoke-WebRequest" { - { Invoke-WebRequest -uri $uri -Path $testOutFile1 -ErrorAction Stop } | Should -Throw -ErrorId "FileOpenFailure,Microsoft.PowerShell.Commands.InvokeWebRequestCommand" - Invoke-WebRequest -uri $uri -LiteralPath $testOutFile1 -ErrorAction Stop - Test-Path -LiteralPath $testOutFile1 | Should -BeTrue - } - - It "Path and LigeralPath parameters work in Invoke-RestMethod" { - { Invoke-RestMethod -uri $uri -Path $testOutFile1 -ErrorAction Stop } | Should -Throw -ErrorId "FileOpenFailure,Microsoft.PowerShell.Commands.InvokeRestMethodCommand" - Invoke-RestMethod -uri $uri -LiteralPath $testOutFile2 -ErrorAction Stop - Test-Path -LiteralPath $testOutFile2 | Should -BeTrue - } - } - Context "Redirect" { It "Validates Invoke-WebRequest with -PreserveAuthorizationOnRedirect preserves the authorization header on redirect: " -TestCases $redirectTests { param($redirectType, $redirectedMethod) @@ -1779,7 +1754,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { # Download the entire file to reference in tests $referenceFile = Join-Path $TestDrive "reference.txt" $resumeUri = Get-WebListenerUrl -Test 'Resume' - Invoke-WebRequest -uri $resumeUri -Path $referenceFile -ErrorAction Stop + Invoke-WebRequest -uri $resumeUri -OutFile $referenceFile -ErrorAction Stop $referenceFileHash = Get-FileHash -Algorithm SHA256 -Path $referenceFile $referenceFileSize = Get-Item $referenceFile | Select-Object -ExpandProperty Length } @@ -1788,13 +1763,13 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { Remove-Item -Force -ErrorAction 'SilentlyContinue' -Path $outFile } - It "Invoke-WebRequest -Resume requires -Path" { + It "Invoke-WebRequest -Resume requires -OutFile" { { Invoke-WebRequest -Resume -Uri $resumeUri -ErrorAction Stop } | Should -Throw -ErrorId 'WebCmdletOutFileMissingException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand' } It "Invoke-WebRequest -Resume Downloads the whole file when the file does not exist" { - $response = Invoke-WebRequest -uri $resumeUri -Path $outFile -Resume -PassThru + $response = Invoke-WebRequest -uri $resumeUri -OutFile $outFile -Resume -PassThru $outFileHash = Get-FileHash -Algorithm SHA256 -Path $outFile $outFileHash.Hash | Should -BeExactly $referenceFileHash.Hash @@ -1811,7 +1786,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { 1..$largerFileSize | ForEach-Object { [Byte]$_ } | Set-Content -AsByteStream $outFile $largerFileSize = Get-Item $outFile | Select-Object -ExpandProperty Length - $response = Invoke-WebRequest -uri $resumeUri -Path $outFile -Resume -PassThru + $response = Invoke-WebRequest -uri $resumeUri -OutFile $outFile -Resume -PassThru $outFileHash = Get-FileHash -Algorithm SHA256 -Path $outFile $outFileHash.Hash | Should -BeExactly $referenceFileHash.Hash @@ -1829,7 +1804,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { $largerFileSize = Get-Item $outFile | Select-Object -ExpandProperty Length $uri = Get-WebListenerUrl -Test 'Resume' -TestValue 'NoResume' - $response = Invoke-WebRequest -Uri $uri -Path $outFile -Resume -PassThru + $response = Invoke-WebRequest -Uri $uri -OutFile $outFile -Resume -PassThru $outFileHash = Get-FileHash -Algorithm SHA256 -Path $outFile $outFileHash.Hash | Should -BeExactly $referenceFileHash.Hash @@ -1849,10 +1824,10 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { param($bytes, $statuscode) # Simulate partial download $uri = Get-WebListenerUrl -Test 'Resume' -TestValue "Bytes/$bytes" - $null = Invoke-WebRequest -uri $uri -Path $outFile + $null = Invoke-WebRequest -uri $uri -OutFile $outFile Get-Item $outFile | Select-Object -ExpandProperty Length | Should -Be $bytes - $response = Invoke-WebRequest -uri $resumeUri -Path $outFile -Resume -PassThru + $response = Invoke-WebRequest -uri $resumeUri -OutFile $outFile -Resume -PassThru $outFileHash = Get-FileHash -Algorithm SHA256 -Path $outFile $outFileHash.Hash | Should -BeExactly $referenceFileHash.Hash @@ -1866,10 +1841,10 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { It "Invoke-WebRequest -Resume assumes the file was successfully completed when the local and remote file are the same size." { # Download the entire file $uri = Get-WebListenerUrl -Test 'Resume' -TestValue 'NoResume' - $null = Invoke-WebRequest -uri $uri -Path $outFile + $null = Invoke-WebRequest -uri $uri -OutFile $outFile $fileSize = Get-Item $outFile | Select-Object -ExpandProperty Length - $response = Invoke-WebRequest -uri $resumeUri -Path $outFile -Resume -PassThru + $response = Invoke-WebRequest -uri $resumeUri -OutFile $outFile -Resume -PassThru $outFileHash = Get-FileHash -Algorithm SHA256 -Path $outFile $outFileHash.Hash | Should -BeExactly $referenceFileHash.Hash @@ -2234,7 +2209,7 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { } } - It "Validate Invoke-RestMethod -Path" { + It "Validate Invoke-RestMethod -OutFile" { $uri = Get-WebListenerUrl -Test 'Get' $result = ExecuteRequestWithOutFile -cmdletName "Invoke-RestMethod" -uri $uri $jsonContent = $result.Output | ConvertFrom-Json @@ -3303,7 +3278,7 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { # Download the entire file to reference in tests $referenceFile = Join-Path $TestDrive "reference.txt" $resumeUri = Get-WebListenerUrl -Test 'Resume' - Invoke-RestMethod -uri $resumeUri -Path $referenceFile -ErrorAction Stop + Invoke-RestMethod -uri $resumeUri -OutFile $referenceFile -ErrorAction Stop $referenceFileHash = Get-FileHash -Algorithm SHA256 -Path $referenceFile $referenceFileSize = Get-Item $referenceFile | Select-Object -ExpandProperty Length } @@ -3312,7 +3287,7 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { Remove-Item -Force -ErrorAction 'SilentlyContinue' -Path $outFile } - It "Invoke-RestMethod -Resume requires -Path" { + It "Invoke-RestMethod -Resume requires -OutFile" { { Invoke-RestMethod -Resume -Uri $resumeUri -ErrorAction Stop } | Should -Throw -ErrorId 'WebCmdletOutFileMissingException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand' } @@ -3321,7 +3296,7 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { # ensure the file does not exist Remove-Item -Force -ErrorAction 'SilentlyContinue' -Path $outFile - Invoke-RestMethod -uri $resumeUri -Path $outFile -ResponseHeadersVariable 'Headers' -Resume + Invoke-RestMethod -uri $resumeUri -OutFile $outFile -ResponseHeadersVariable 'Headers' -Resume $outFileHash = Get-FileHash -Algorithm SHA256 -Path $outFile $outFileHash.Hash | Should -BeExactly $referenceFileHash.Hash @@ -3337,7 +3312,7 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { 1..$largerFileSize | ForEach-Object { [Byte]$_ } | Set-Content -AsByteStream $outFile $largerFileSize = Get-Item $outFile | Select-Object -ExpandProperty Length - $response = Invoke-RestMethod -uri $resumeUri -Path $outFile -ResponseHeadersVariable 'Headers' -Resume + $response = Invoke-RestMethod -uri $resumeUri -OutFile $outFile -ResponseHeadersVariable 'Headers' -Resume $outFileHash = Get-FileHash -Algorithm SHA256 -Path $outFile $outFileHash.Hash | Should -BeExactly $referenceFileHash.Hash @@ -3354,7 +3329,7 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { $largerFileSize = Get-Item $outFile | Select-Object -ExpandProperty Length $uri = Get-WebListenerUrl -Test 'Resume' -TestValue 'NoResume' - $response = Invoke-RestMethod -uri $uri -Path $outFile -ResponseHeadersVariable 'Headers' -Resume + $response = Invoke-RestMethod -uri $uri -OutFile $outFile -ResponseHeadersVariable 'Headers' -Resume $outFileHash = Get-FileHash -Algorithm SHA256 -Path $outFile $outFileHash.Hash | Should -BeExactly $referenceFileHash.Hash @@ -3374,10 +3349,10 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { param($bytes) # Simulate partial download $uri = Get-WebListenerUrl -Test 'Resume' -TestValue "Bytes/$bytes" - $null = Invoke-RestMethod -uri $uri -Path $outFile + $null = Invoke-RestMethod -uri $uri -OutFile $outFile Get-Item $outFile | Select-Object -ExpandProperty Length | Should -Be $bytes - $response = Invoke-RestMethod -uri $resumeUri -Path $outFile -ResponseHeadersVariable 'Headers' -Resume + $response = Invoke-RestMethod -uri $resumeUri -OutFile $outFile -ResponseHeadersVariable 'Headers' -Resume $outFileHash = Get-FileHash -Algorithm SHA256 -Path $outFile $outFileHash.Hash | Should -BeExactly $referenceFileHash.Hash @@ -3390,10 +3365,10 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { It "Invoke-RestMethod -Resume assumes the file was successfully completed when the local and remote file are the same size." { # Download the entire file $uri = Get-WebListenerUrl -Test 'Resume' -TestValue 'NoResume' - $null = Invoke-RestMethod -uri $uri -Path $outFile + $null = Invoke-RestMethod -uri $uri -OutFile $outFile $fileSize = Get-Item $outFile | Select-Object -ExpandProperty Length - $response = Invoke-RestMethod -uri $resumeUri -Path $outFile -ResponseHeadersVariable 'Headers' -Resume + $response = Invoke-RestMethod -uri $resumeUri -OutFile $outFile -ResponseHeadersVariable 'Headers' -Resume $outFileHash = Get-FileHash -Algorithm SHA256 -Path $outFile $outFileHash.Hash | Should -BeExactly $referenceFileHash.Hash From ea421db311aeb1809099cba0bde980058a8101d4 Mon Sep 17 00:00:00 2001 From: Ilya Date: Thu, 27 Feb 2020 23:26:00 +0500 Subject: [PATCH 5/6] Address PowerShell Committee conclusion --- .../utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs | 2 +- .../Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index ee56360e103..b48835ca6e3 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -763,7 +763,7 @@ private Uri CheckProtocol(Uri uri) private string QualifyFilePath(string path) { - string resolvedFilePath = PathUtils.ResolveFilePath(path, this, false); + string resolvedFilePath = PathUtils.ResolveFilePath(filePath: path, command: this, isLiteralPath: true); return resolvedFilePath; } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index ce3720e49c5..cab3ed7feeb 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -41,7 +41,8 @@ function ExecuteRequestWithOutFile { ) $result = [PSObject]@{Output = $null; Error = $null} - $filePath = Join-Path $TestDrive ((Get-Random).ToString() + ".txt") + # We use '[outfile1]' in the file name to check that OutFile parameter is literal path + $filePath = Join-Path $TestDrive ((Get-Random).ToString() + "[outfile1].txt") try { if ($cmdletName -eq "Invoke-WebRequest") { Invoke-WebRequest -Uri $uri -OutFile $filePath From e0e2d5ac57ca05fc591fd8830d4f4d8c8a0e9f78 Mon Sep 17 00:00:00 2001 From: Ilya Date: Fri, 13 Mar 2020 19:51:23 +0500 Subject: [PATCH 6/6] Fix test --- .../Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index cab3ed7feeb..d4743c5f4dd 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -49,7 +49,7 @@ function ExecuteRequestWithOutFile { } else { Invoke-RestMethod -Uri $uri -OutFile $filePath } - $result.Output = Get-Content $filePath -Raw -ErrorAction SilentlyContinue + $result.Output = Get-Content -LiteralPath $filePath -Raw -ErrorAction SilentlyContinue } catch { $result.Error = $_ } finally {