Skip to content

Commit dd406b5

Browse files
authored
Add ValidateNotNullOrEmpty to OutFile and InFile parameters of WebCmdlets (PowerShell#19044)
1 parent 3e74bd0 commit dd406b5

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed

src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ public virtual string CustomMethod
382382
/// Gets or sets the InFile property.
383383
/// </summary>
384384
[Parameter]
385+
[ValidateNotNullOrEmpty]
385386
public virtual string InFile { get; set; }
386387

387388
/// <summary>
@@ -397,6 +398,7 @@ public virtual string CustomMethod
397398
/// Gets or sets the OutFile property.
398399
/// </summary>
399400
[Parameter]
401+
[ValidateNotNullOrEmpty]
400402
public virtual string OutFile { get; set; }
401403

402404
/// <summary>

test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,16 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" {
704704
$jsonContent.headers.Host | Should -Be $uri.Authority
705705
}
706706

707+
It "Invoke-WebRequest should fail if -OutFile is <Name>." -TestCases @(
708+
@{ Name = "empty"; Value = [string]::Empty }
709+
@{ Name = "null"; Value = $null }
710+
) {
711+
param ($value)
712+
$uri = Get-WebListenerUrl -Test 'Get'
713+
$errorId = "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeWebRequestCommand"
714+
{ Invoke-WebRequest -Uri $uri -OutFile $value} | Should -Throw -ErrorId $errorId
715+
}
716+
707717
It "Validate Invoke-WebRequest handles missing Content-Type in response header" {
708718
#Validate that exception is not thrown when response headers are missing Content-Type.
709719
$uri = Get-WebListenerUrl -Test 'ResponseHeaders' -Query @{'Content-Type' = ''}
@@ -893,7 +903,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" {
893903
$response.Error | Should -BeNullOrEmpty
894904
$response.Content.Headers."Authorization" | Should -BeExactly "test"
895905
}
896-
906+
897907
It "Validates Invoke-WebRequest with -PreserveAuthorizationOnRedirect respects -MaximumRedirection on redirect: <redirectType> <redirectedMethod>" -TestCases $redirectTests {
898908
param($redirectType, $redirectedMethod)
899909
$uri = Get-WebListenerUrl -Test 'Redirect' -TestValue '3' -Query @{type = $redirectType}
@@ -2390,6 +2400,16 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" {
23902400
$jsonContent.headers.Host | Should -Be $uri.Authority
23912401
}
23922402

2403+
It "Invoke-RestMethod should fail if -OutFile is <Name>." -TestCases @(
2404+
@{ Name = "empty"; Value = [string]::Empty }
2405+
@{ Name = "null"; Value = $null }
2406+
) {
2407+
param ($value)
2408+
$uri = Get-WebListenerUrl -Test 'Get'
2409+
$errorId = "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeRestMethodCommand"
2410+
{ Invoke-RestMethod -Uri $uri -OutFile $value} | Should -Throw -ErrorId $errorId
2411+
}
2412+
23932413
It "Validate Invoke-RestMethod handles missing Content-Type in response header" {
23942414
#Validate that exception is not thrown when response headers are missing Content-Type.
23952415
$uri = Get-WebListenerUrl -Test 'ResponseHeaders' -Query @{'Content-Type' = ''}
@@ -3629,10 +3649,16 @@ Describe "Validate Invoke-WebRequest and Invoke-RestMethod -InFile" -Tags "Featu
36293649
$uri = Get-WebListenerUrl -Test 'Post'
36303650
$testCases = @(
36313651
#region INVOKE-WEBREQUEST
3652+
@{
3653+
Name = 'Validate error for Invoke-WebRequest -InFile null'
3654+
ScriptBlock = {Invoke-WebRequest -Uri $uri -Method Post -InFile $null}
3655+
ExpectedFullyQualifiedErrorId = 'ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeWebRequestCommand'
3656+
}
3657+
36323658
@{
36333659
Name = 'Validate error for Invoke-WebRequest -InFile ""'
36343660
ScriptBlock = {Invoke-WebRequest -Uri $uri -Method Post -InFile ""}
3635-
ExpectedFullyQualifiedErrorId = 'WebCmdletInFileNotFilePathException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand'
3661+
ExpectedFullyQualifiedErrorId = 'ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeWebRequestCommand'
36363662
}
36373663

36383664
@{
@@ -3642,30 +3668,48 @@ Describe "Validate Invoke-WebRequest and Invoke-RestMethod -InFile" -Tags "Featu
36423668
}
36433669

36443670
@{
3645-
Name = "Validate error for Invoke-WebRequest -InFile $TestDrive\content.txt"
3646-
ScriptBlock = {Invoke-WebRequest -Uri $uri -Method Post -InFile $TestDrive\content.txt}
3671+
Name = "Validate error for Invoke-WebRequest -InFile $TestDrive\content.txt"
3672+
ScriptBlock = {Invoke-WebRequest -Uri $uri -Method Post -InFile $TestDrive\content.txt}
36473673
ExpectedFullyQualifiedErrorId = 'PathNotFound,Microsoft.PowerShell.Commands.InvokeWebRequestCommand'
36483674
}
3675+
3676+
@{
3677+
Name = "Validate error for Invoke-WebRequest -InFile $TestDrive"
3678+
ScriptBlock = {Invoke-WebRequest -Uri $uri -Method Post -InFile $TestDrive}
3679+
ExpectedFullyQualifiedErrorId = 'WebCmdletInFileNotFilePathException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand'
3680+
}
36493681
#endregion
36503682

36513683
#region INVOKE-RESTMETHOD
3684+
@{
3685+
Name = "Validate error for Invoke-RestMethod -InFile null"
3686+
ScriptBlock = {Invoke-RestMethod -Uri $uri -Method Post -InFile $null}
3687+
ExpectedFullyQualifiedErrorId = 'ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeRestMethodCommand'
3688+
}
3689+
36523690
@{
36533691
Name = "Validate error for Invoke-RestMethod -InFile ''"
36543692
ScriptBlock = {Invoke-RestMethod -Uri $uri -Method Post -InFile ''}
3655-
ExpectedFullyQualifiedErrorId = 'WebCmdletInFileNotFilePathException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand'
3693+
ExpectedFullyQualifiedErrorId = 'ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeRestMethodCommand'
36563694
}
36573695

36583696
@{
3659-
Name = "Validate error for Invoke-RestMethod -InFile <null>"
3697+
Name = "Validate error for Invoke-RestMethod -InFile"
36603698
ScriptBlock = {Invoke-RestMethod -Uri $uri -Method Post -InFile}
36613699
ExpectedFullyQualifiedErrorId = 'MissingArgument,Microsoft.PowerShell.Commands.InvokeRestMethodCommand'
36623700
}
36633701

36643702
@{
3665-
Name = "Validate error for Invoke-RestMethod -InFile $TestDrive\content.txt"
3703+
Name = "Validate error for Invoke-RestMethod -InFile $TestDrive\content.txt"
36663704
ScriptBlock = {Invoke-RestMethod -Uri $uri -Method Post -InFile $TestDrive\content.txt}
36673705
ExpectedFullyQualifiedErrorId = 'PathNotFound,Microsoft.PowerShell.Commands.InvokeRestMethodCommand'
36683706
}
3707+
3708+
@{
3709+
Name = "Validate error for Invoke-RestMethod -InFile $TestDrive"
3710+
ScriptBlock = {Invoke-RestMethod -Uri $uri -Method Post -InFile $TestDrive}
3711+
ExpectedFullyQualifiedErrorId = 'WebCmdletInFileNotFilePathException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand'
3712+
}
36693713
#endregion
36703714
)
36713715
}

0 commit comments

Comments
 (0)