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 beb9ad93e7e..7744e894de9 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
@@ -358,6 +358,7 @@ public virtual string CustomMethod
/// Gets or sets the InFile property.
///
[Parameter]
+ [ValidateNotNullOrEmpty]
public virtual string InFile { get; set; }
///
@@ -373,6 +374,7 @@ public virtual string CustomMethod
/// Gets or sets the OutFile property.
///
[Parameter]
+ [ValidateNotNullOrEmpty]
public virtual string OutFile { get; set; }
///
diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1
index 285be044eb4..43363edb6eb 100644
--- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1
+++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1
@@ -704,6 +704,16 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" {
$jsonContent.headers.Host | Should -Be $uri.Authority
}
+ It "Invoke-WebRequest should fail if -OutFile is ." -TestCases @(
+ @{ Name = "empty"; Value = [string]::Empty }
+ @{ Name = "null"; Value = $null }
+ ) {
+ param ($value)
+ $uri = Get-WebListenerUrl -Test 'Get'
+ $errorId = "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeWebRequestCommand"
+ { Invoke-WebRequest -Uri $uri -OutFile $value} | Should -Throw -ErrorId $errorId
+ }
+
It "Validate Invoke-WebRequest handles missing Content-Type in response header" {
#Validate that exception is not thrown when response headers are missing Content-Type.
$uri = Get-WebListenerUrl -Test 'ResponseHeaders' -Query @{'Content-Type' = ''}
@@ -893,7 +903,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" {
$response.Error | Should -BeNullOrEmpty
$response.Content.Headers."Authorization" | Should -BeExactly "test"
}
-
+
It "Validates Invoke-WebRequest with -PreserveAuthorizationOnRedirect respects -MaximumRedirection on redirect: " -TestCases $redirectTests {
param($redirectType, $redirectedMethod)
$uri = Get-WebListenerUrl -Test 'Redirect' -TestValue '3' -Query @{type = $redirectType}
@@ -2390,6 +2400,16 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" {
$jsonContent.headers.Host | Should -Be $uri.Authority
}
+ It "Invoke-RestMethod should fail if -OutFile is ." -TestCases @(
+ @{ Name = "empty"; Value = [string]::Empty }
+ @{ Name = "null"; Value = $null }
+ ) {
+ param ($value)
+ $uri = Get-WebListenerUrl -Test 'Get'
+ $errorId = "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeRestMethodCommand"
+ { Invoke-RestMethod -Uri $uri -OutFile $value} | Should -Throw -ErrorId $errorId
+ }
+
It "Validate Invoke-RestMethod handles missing Content-Type in response header" {
#Validate that exception is not thrown when response headers are missing Content-Type.
$uri = Get-WebListenerUrl -Test 'ResponseHeaders' -Query @{'Content-Type' = ''}
@@ -3629,10 +3649,16 @@ Describe "Validate Invoke-WebRequest and Invoke-RestMethod -InFile" -Tags "Featu
$uri = Get-WebListenerUrl -Test 'Post'
$testCases = @(
#region INVOKE-WEBREQUEST
+ @{
+ Name = 'Validate error for Invoke-WebRequest -InFile null'
+ ScriptBlock = {Invoke-WebRequest -Uri $uri -Method Post -InFile $null}
+ ExpectedFullyQualifiedErrorId = 'ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeWebRequestCommand'
+ }
+
@{
Name = 'Validate error for Invoke-WebRequest -InFile ""'
ScriptBlock = {Invoke-WebRequest -Uri $uri -Method Post -InFile ""}
- ExpectedFullyQualifiedErrorId = 'WebCmdletInFileNotFilePathException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand'
+ ExpectedFullyQualifiedErrorId = 'ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeWebRequestCommand'
}
@{
@@ -3642,30 +3668,48 @@ Describe "Validate Invoke-WebRequest and Invoke-RestMethod -InFile" -Tags "Featu
}
@{
- Name = "Validate error for Invoke-WebRequest -InFile $TestDrive\content.txt"
- ScriptBlock = {Invoke-WebRequest -Uri $uri -Method Post -InFile $TestDrive\content.txt}
+ Name = "Validate error for Invoke-WebRequest -InFile $TestDrive\content.txt"
+ ScriptBlock = {Invoke-WebRequest -Uri $uri -Method Post -InFile $TestDrive\content.txt}
ExpectedFullyQualifiedErrorId = 'PathNotFound,Microsoft.PowerShell.Commands.InvokeWebRequestCommand'
}
+
+ @{
+ Name = "Validate error for Invoke-WebRequest -InFile $TestDrive"
+ ScriptBlock = {Invoke-WebRequest -Uri $uri -Method Post -InFile $TestDrive}
+ ExpectedFullyQualifiedErrorId = 'WebCmdletInFileNotFilePathException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand'
+ }
#endregion
#region INVOKE-RESTMETHOD
+ @{
+ Name = "Validate error for Invoke-RestMethod -InFile null"
+ ScriptBlock = {Invoke-RestMethod -Uri $uri -Method Post -InFile $null}
+ ExpectedFullyQualifiedErrorId = 'ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeRestMethodCommand'
+ }
+
@{
Name = "Validate error for Invoke-RestMethod -InFile ''"
ScriptBlock = {Invoke-RestMethod -Uri $uri -Method Post -InFile ''}
- ExpectedFullyQualifiedErrorId = 'WebCmdletInFileNotFilePathException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand'
+ ExpectedFullyQualifiedErrorId = 'ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeRestMethodCommand'
}
@{
- Name = "Validate error for Invoke-RestMethod -InFile "
+ Name = "Validate error for Invoke-RestMethod -InFile"
ScriptBlock = {Invoke-RestMethod -Uri $uri -Method Post -InFile}
ExpectedFullyQualifiedErrorId = 'MissingArgument,Microsoft.PowerShell.Commands.InvokeRestMethodCommand'
}
@{
- Name = "Validate error for Invoke-RestMethod -InFile $TestDrive\content.txt"
+ Name = "Validate error for Invoke-RestMethod -InFile $TestDrive\content.txt"
ScriptBlock = {Invoke-RestMethod -Uri $uri -Method Post -InFile $TestDrive\content.txt}
ExpectedFullyQualifiedErrorId = 'PathNotFound,Microsoft.PowerShell.Commands.InvokeRestMethodCommand'
}
+
+ @{
+ Name = "Validate error for Invoke-RestMethod -InFile $TestDrive"
+ ScriptBlock = {Invoke-RestMethod -Uri $uri -Method Post -InFile $TestDrive}
+ ExpectedFullyQualifiedErrorId = 'WebCmdletInFileNotFilePathException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand'
+ }
#endregion
)
}