mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Fixing 'InFile parameter of Invoke-WebRequest doesn't work' and adding aliases (iwr for invoke-webrequest and irm for invoke-restmethod) to the web cmdlets. (#2848)
* Fixing Invoke-WebRequest InFile parameter * Adding aliases for Invoke-RestMethod and Invoke-WebRequest * Adding test cases for Web cmdlets -InFile parameter * Adding tests for Invoke-WebRequest (iwr) and Invoke-RestMethod (irm) using the cmdlet aliases. * Remove the extra leading space
This commit is contained in:
committed by
Dongbo Wang
parent
1431105251
commit
89a5c17d6b
@@ -643,3 +643,127 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" {
|
||||
$result.Error | Should BeNullOrEmpty
|
||||
}
|
||||
}
|
||||
|
||||
Describe "Validate Invoke-WebRequest and Invoke-RestMethod -InFile" -Tags "Feature" {
|
||||
|
||||
Context "InFile parameter negative tests" {
|
||||
|
||||
$testCases = @(
|
||||
#region INVOKE-WEBREQUEST
|
||||
@{
|
||||
Name = 'Validate error for Invoke-WebRequest -InFile ""'
|
||||
ScriptBlock = {Invoke-WebRequest -Uri http://httpbin.org/post -Method Post -InFile ""}
|
||||
ExpectedFullyQualifiedErrorId = 'WebCmdletInFileNotFilePathException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand'
|
||||
}
|
||||
|
||||
@{
|
||||
Name = 'Validate error for Invoke-WebRequest -InFile'
|
||||
ScriptBlock = {Invoke-WebRequest -Uri http://httpbin.org/post -Method Post -InFile}
|
||||
ExpectedFullyQualifiedErrorId = 'MissingArgument,Microsoft.PowerShell.Commands.InvokeWebRequestCommand'
|
||||
}
|
||||
|
||||
@{
|
||||
Name = "Validate error for Invoke-WebRequest -InFile $TestDrive\content.txt"
|
||||
ScriptBlock = {Invoke-WebRequest -Uri http://httpbin.org/post -Method Post -InFile $TestDrive\content.txt}
|
||||
ExpectedFullyQualifiedErrorId = 'PathNotFound,Microsoft.PowerShell.Commands.InvokeWebRequestCommand'
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region INVOKE-RESTMETHOD
|
||||
@{
|
||||
Name = "Validate error for Invoke-RestMethod -InFile ''"
|
||||
ScriptBlock = {Invoke-RestMethod -Uri http://httpbin.org/post -Method Post -InFile ''}
|
||||
ExpectedFullyQualifiedErrorId = 'WebCmdletInFileNotFilePathException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand'
|
||||
}
|
||||
|
||||
@{
|
||||
Name = "Validate error for Invoke-RestMethod -InFile <null>"
|
||||
ScriptBlock = {Invoke-RestMethod -Uri http://httpbin.org/post -Method Post -InFile}
|
||||
ExpectedFullyQualifiedErrorId = 'MissingArgument,Microsoft.PowerShell.Commands.InvokeRestMethodCommand'
|
||||
}
|
||||
|
||||
@{
|
||||
Name = "Validate error for Invoke-RestMethod -InFile $TestDrive\content.txt"
|
||||
ScriptBlock = {Invoke-RestMethod -Uri http://httpbin.org/post -Method Post -InFile $TestDrive\content.txt}
|
||||
ExpectedFullyQualifiedErrorId = 'PathNotFound,Microsoft.PowerShell.Commands.InvokeRestMethodCommand'
|
||||
}
|
||||
#endregion
|
||||
)
|
||||
|
||||
It "<Name>" -TestCases $testCases {
|
||||
param ($scriptblock, $expectedFullyQualifiedErrorId)
|
||||
|
||||
try
|
||||
{
|
||||
& $scriptblock
|
||||
throw "No Exception!"
|
||||
}
|
||||
catch
|
||||
{
|
||||
$_.FullyQualifiedErrorId | should be $ExpectedFullyQualifiedErrorId
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Context "InFile parameter positive tests" {
|
||||
|
||||
BeforeAll {
|
||||
$filePath = Join-Path $TestDrive test.txt
|
||||
New-Item -Path $filePath -Value "hello" -ItemType File -Force
|
||||
}
|
||||
|
||||
It "Invoke-WebRequest -InFile" {
|
||||
$result = Invoke-WebRequest -InFile $filePath -Uri http://httpbin.org/post -Method Post
|
||||
$content = $result.Content | ConvertFrom-Json
|
||||
$content.form | Should Match "hello"
|
||||
}
|
||||
|
||||
It "Invoke-RestMethod -InFile" {
|
||||
$result = Invoke-RestMethod -InFile $filePath -Uri http://httpbin.org/post -Method Post
|
||||
$result.form | Should Match "hello"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Describe "Web cmdlets tests using the cmdlet's aliases" -Tags "CI" {
|
||||
|
||||
function SearchEngineIsOnline
|
||||
{
|
||||
param (
|
||||
[ValidateNotNullOrEmpty()]
|
||||
$webAddress
|
||||
)
|
||||
$ping = new-object System.Net.NetworkInformation.Ping
|
||||
$sendPing = $ping.SendPingAsync($webAddress)
|
||||
return ($sendPing.Result.Status -eq "Success")
|
||||
}
|
||||
|
||||
# Make sure either www.bing.com or www.google.com are online to send a request.
|
||||
$endPointToUse = $null
|
||||
foreach ($uri in @("www.bing.com", "www.google.com"))
|
||||
{
|
||||
if (SearchEngineIsOnline $uri)
|
||||
{
|
||||
$endPointToUse = $uri
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
# If neither www.bing.com nor www.google.com are online, then skip the tests.
|
||||
$skipTests = ($endPointToUse -eq $null)
|
||||
$finalUri = $endPointToUse + "?q=how+many+feet+in+a+mile"
|
||||
|
||||
It "Execute Invoke-WebRequest --> 'iwr -URI $finalUri'" -Skip:$skipTests {
|
||||
$result = iwr -URI $finalUri -TimeoutSec 5
|
||||
$result.StatusCode | Should Be "200"
|
||||
$result.Links | Should Not Be $null
|
||||
}
|
||||
|
||||
It "Execute Invoke-RestMethod --> 'irm -URI $finalUri'" -Skip:$skipTests {
|
||||
$result = irm -URI $finalUri -TimeoutSec 5
|
||||
foreach ($word in @("200", "how", "many", "feet", "in", "mile"))
|
||||
{
|
||||
$result | Should Match $word
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user