From 582c065abb57f067b9e9ec9add4b01b65a03646a Mon Sep 17 00:00:00 2001 From: Mark Kraus Date: Tue, 19 Dec 2017 11:17:56 -0600 Subject: [PATCH 1/2] [Feature] Convert Web Cmdlets test to OTBS Formating --- .../WebCmdlets.Tests.ps1 | 710 ++++++++---------- 1 file changed, 307 insertions(+), 403 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index 437dd80edda8..4737c244ca29 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -8,8 +8,7 @@ # Invokes the given command via script block invocation. # -function ExecuteWebCommand -{ +function ExecuteWebCommand { param ( [ValidateNotNullOrEmpty()] [string] @@ -18,13 +17,10 @@ function ExecuteWebCommand $result = [PSObject]@{Output = $null; Error = $null} - try - { + try { $scriptBlock = [scriptblock]::Create($command) - $result.Output = & $scriptBlock - } - catch - { + $result.Output = & $scriptBlock + } catch { $result.Error = $_ } @@ -34,38 +30,29 @@ function ExecuteWebCommand # 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 -{ +function ExecuteRequestWithOutFile { param ( [ValidateSet("Invoke-RestMethod", "Invoke-WebRequest" )] [string] $cmdletName, + [string] $uri = (Get-WebListenerUrl -Test 'Get') ) $result = [PSObject]@{Output = $null; Error = $null} $filePath = Join-Path $TestDrive ((Get-Random).ToString() + ".txt") - try - { - if ($cmdletName -eq "Invoke-WebRequest") - { + try { + if ($cmdletName -eq "Invoke-WebRequest") { Invoke-WebRequest -Uri $uri -OutFile $filePath - } - else - { + } else { Invoke-RestMethod -Uri $uri -OutFile $filePath } - $result.Output = Get-Content $filePath -Raw -ea SilentlyContinue - } - catch - { + $result.Output = Get-Content $filePath -Raw -ea SilentlyContinue + } catch { $result.Error = $_ - } - finally - { - if (Test-Path $filePath) - { + } finally { + if (Test-Path $filePath) { Remove-Item $filePath -Force -ea SilentlyContinue } } @@ -75,31 +62,25 @@ function ExecuteRequestWithOutFile # This function calls either Invoke-WebRequest or Invoke-RestMethod with the given uri # using the Headers parameter to disable keep-alive. # -function ExecuteRequestWithHeaders -{ +function ExecuteRequestWithHeaders { param ( [ValidateSet("Invoke-RestMethod", "Invoke-WebRequest" )] [string] $cmdletName, + [string] $uri = (Get-WebListenerUrl -Test 'Get') ) $result = [PSObject]@{Output = $null; Error = $null} - try - { + try { $headers = @{ Connection = 'close'} - if ($cmdletName -eq "Invoke-WebRequest") - { - $result.Output = Invoke-WebRequest -Uri $uri -TimeoutSec 5 -Headers $headers - } - else - { - $result.Output = Invoke-RestMethod -Uri $uri -TimeoutSec 5 -Headers $headers + if ($cmdletName -eq "Invoke-WebRequest") { + $result.Output = Invoke-WebRequest -Uri $uri -TimeoutSec 5 -Headers $headers + } else { + $result.Output = Invoke-RestMethod -Uri $uri -TimeoutSec 5 -Headers $headers } - } - catch - { + } catch { $result.Error = $_ } return $result @@ -107,9 +88,8 @@ function ExecuteRequestWithHeaders # Returns test data for the given content type. # -function GetTestData -{ - param( +function GetTestData { + param ( [ValidateSet("text/plain", "application/xml", "application/json")] [String] $contentType @@ -117,13 +97,9 @@ function GetTestData $testData = @{ItemID = 987123; Name = 'TestData'} - if ($contentType -eq "text/plain") - { + if ($contentType -eq "text/plain") { $body = $testData | Out-String - } - - elseif ($contentType -eq "application/xml") - { + } elseif ($contentType -eq "application/xml") { $body = ' @@ -133,51 +109,45 @@ function GetTestData ' - } - - else # "application/json" - { + } else { + # "application/json" $body = $testData | ConvertTo-Json -Compress } return $body } -function ExecuteRedirectRequest -{ +function ExecuteRedirectRequest { param ( [Parameter(Mandatory)] [string] $uri, [ValidateSet('Invoke-WebRequest', 'Invoke-RestMethod')] - [string] $Cmdlet = 'Invoke-WebRequest', + [string] + $Cmdlet = 'Invoke-WebRequest', [ValidateSet('POST', 'GET')] - [string] $Method = 'GET', + [string] + $Method = 'GET', - [switch] $PreserveAuthorizationOnRedirect + [switch] + $PreserveAuthorizationOnRedirect ) $result = [PSObject]@{Output = $null; Error = $null; Content = $null} - try - { + try { $headers = @{"Authorization" = "test"} - if ($Cmdlet -eq 'Invoke-WebRequest') - { + if ($Cmdlet -eq 'Invoke-WebRequest') { $result.Output = Invoke-WebRequest -Uri $uri -TimeoutSec 5 -Headers $headers -PreserveAuthorizationOnRedirect:$PreserveAuthorizationOnRedirect.IsPresent -Method $Method $result.Content = $result.Output.Content | ConvertFrom-Json - } - else - { + } else { $result.Output = Invoke-RestMethod -Uri $uri -TimeoutSec 5 -Headers $headers -PreserveAuthorizationOnRedirect:$PreserveAuthorizationOnRedirect.IsPresent -Method $Method # NOTE: $result.Output should already be a PSObject (Invoke-RestMethod converts the returned json automatically) # so simply reference $result.Output $result.Content = $result.Output } - } - catch - { + } catch { $result.Error = $_ } @@ -186,41 +156,37 @@ function ExecuteRedirectRequest # This function calls either Invoke-WebRequest or Invoke-RestMethod with the given uri # using the custum headers and the optional SkipHeaderValidation switch. -function ExecuteRequestWithCustomHeaders -{ +function ExecuteRequestWithCustomHeaders { param ( [Parameter(Mandatory)] [string] $Uri, [ValidateSet('Invoke-WebRequest', 'Invoke-RestMethod')] - [string] $Cmdlet = 'Invoke-WebRequest', + [string] + $Cmdlet = 'Invoke-WebRequest', [Parameter(Mandatory)] [ValidateNotNull()] - [Hashtable] $Headers, + [Hashtable] + $Headers, - [switch] $SkipHeaderValidation + [switch] + $SkipHeaderValidation ) $result = [PSObject]@{Output = $null; Error = $null; Content = $null} - try - { - if ($Cmdlet -eq 'Invoke-WebRequest') - { + try { + if ($Cmdlet -eq 'Invoke-WebRequest') { $result.Output = Invoke-WebRequest -Uri $Uri -TimeoutSec 5 -Headers $Headers -SkipHeaderValidation:$SkipHeaderValidation.IsPresent $result.Content = $result.Output.Content | ConvertFrom-Json - } - else - { + } else { $result.Output = Invoke-RestMethod -Uri $Uri -TimeoutSec 5 -Headers $Headers -SkipHeaderValidation:$SkipHeaderValidation.IsPresent # NOTE: $result.Output should already be a PSObject (Invoke-RestMethod converts the returned json automatically) # so simply reference $result.Output $result.Content = $result.Output } - } - catch - { + } catch { $result.Error = $_ } @@ -236,13 +202,16 @@ function ExecuteRequestWithCustomUserAgent { $Uri, [ValidateSet('Invoke-WebRequest', 'Invoke-RestMethod')] - [string] $Cmdlet = 'Invoke-WebRequest', + [string] + $Cmdlet = 'Invoke-WebRequest', [Parameter(Mandatory)] [ValidateNotNull()] - [string] $UserAgent, + [string] + $UserAgent, - [switch] $SkipHeaderValidation + [switch] + $SkipHeaderValidation ) $result = [PSObject]@{Output = $null; Error = $null; Content = $null} @@ -256,15 +225,13 @@ function ExecuteRequestWithCustomUserAgent { if ($Cmdlet -eq 'Invoke-WebRequest') { $result.Output = Invoke-WebRequest @Params $result.Content = $result.Output.Content | ConvertFrom-Json - } - else { + } else { $result.Output = Invoke-RestMethod @Params # NOTE: $result.Output should already be a PSObject (Invoke-RestMethod converts the returned json automatically) # so simply reference $result.Output $result.Content = $result.Output } - } - catch { + } catch { $result.Error = $_ } @@ -272,24 +239,21 @@ function ExecuteRequestWithCustomUserAgent { } # This function calls Invoke-WebRequest with the given uri -function ExecuteWebRequest -{ +function ExecuteWebRequest { param ( [Parameter(Mandatory)] [string] $Uri, - [switch] $UseBasicParsing + [switch] + $UseBasicParsing ) - $result = [PSObject]@{Output = $null; Error = $null; Content = $null} + $result = [PSObject]@{Output = $null; Error = $null; Content = $null} - try - { + try { $result.Output = Invoke-WebRequest -Uri $Uri -TimeoutSec 5 -UseBasicParsing:$UseBasicParsing.IsPresent $result.Content = $result.Output.Content - } - catch - { + } catch { $result.Error = $_ } @@ -299,59 +263,46 @@ function ExecuteWebRequest [string] $verboseEncodingPrefix = 'Content encoding: ' # This function calls Invoke-WebRequest with the given uri and # parses the verbose output to determine the encoding used for the content. -function ExecuteRestMethod -{ - param - ( +function ExecuteRestMethod { + param ( [Parameter(Mandatory)] [string] $Uri, - [switch] $UseBasicParsing + [switch] + $UseBasicParsing ) $result = @{Output = $null; Error = $null; Encoding = $null; Content = $null} $verbosePreferenceSave = $VerbosePreference $VerbosePreference = 'Continue' - try - { - + try { $verboseFile = Join-Path $TestDrive -ChildPath ExecuteRestMethod.verbose.txt $result.Output = Invoke-RestMethod -Uri $Uri -TimeoutSec 5 -UseBasicParsing:$UseBasicParsing.IsPresent -Verbose 4>$verboseFile $result.Content = $result.Output - if (Test-Path -Path $verboseFile) - { + if (Test-Path -Path $verboseFile) { $result.Verbose = Get-Content -Path $verboseFile - foreach ($item in $result.Verbose) - { + foreach ($item in $result.Verbose) { $line = $item.Trim() - if ($line.StartsWith($verboseEncodingPrefix)) - { + if ($line.StartsWith($verboseEncodingPrefix)) { $encodingName = $item.SubString($verboseEncodingPrefix.Length).Trim() $result.Encoding = [System.Text.Encoding]::GetEncoding($encodingName) break } } - if ($result.Encoding -eq $null) - { + if ($result.Encoding -eq $null) { throw "Encoding not found in verbose output. Lines: $($result.Verbose.Count) Content:$($result.Verbose)" } } - if ($result.Verbose -eq $null) - { + if ($result.Verbose -eq $null) { throw "No verbose output was found" } - } - catch - { + } catch { $result.Error = $_ | select-object * | Out-String - } - finally - { + } finally { $VerbosePreference = $verbosePreferenceSave - if (Test-Path -Path $verboseFile) - { + if (Test-Path -Path $verboseFile) { Remove-Item -Path $verboseFile -ErrorAction SilentlyContinue } } @@ -359,24 +310,23 @@ function ExecuteRestMethod return $result } -function GetMultipartBody -{ - param - ( - [Switch]$String, - [Switch]$File +function GetMultipartBody { + param ( + [Switch] + $String, + + [Switch] + $File ) $multipartContent = [System.Net.Http.MultipartFormDataContent]::new() - if ($String.IsPresent) - { + if ($String.IsPresent) { $stringHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data") $stringHeader.Name = "TestString" $StringContent = [System.Net.Http.StringContent]::new("TestValue") $StringContent.Headers.ContentDisposition = $stringHeader $multipartContent.Add($stringContent) } - if ($File.IsPresent) - { + if ($File.IsPresent) { $multipartFile = Join-Path $TestDrive 'multipart.txt' "TestContent" | Set-Content $multipartFile $FileStream = [System.IO.FileStream]::new($multipartFile, [System.IO.FileMode]::Open) @@ -389,7 +339,7 @@ function GetMultipartBody $multipartContent.Add($fileContent) } # unary comma required to prevent $multipartContent from being unwrapped/enumerated - return ,$multipartContent + return , $multipartContent } <# @@ -399,20 +349,20 @@ function GetMultipartBody for additonal details. #> $redirectTests = @( - @{redirectType = 'MultipleChoices'; redirectedMethod='POST'} - @{redirectType = 'Ambiguous'; redirectedMethod='POST'} # Synonym for MultipleChoices + @{redirectType = 'MultipleChoices'; redirectedMethod = 'POST'} + @{redirectType = 'Ambiguous'; redirectedMethod = 'POST'} # Synonym for MultipleChoices - @{redirectType = 'Moved'; redirectedMethod='GET'} - @{redirectType = 'MovedPermanently'; redirectedMethod='GET'} # Synonym for Moved + @{redirectType = 'Moved'; redirectedMethod = 'GET'} + @{redirectType = 'MovedPermanently'; redirectedMethod = 'GET'} # Synonym for Moved - @{redirectType = 'Found'; redirectedMethod='GET'} - @{redirectType = 'Redirect'; redirectedMethod='GET'} # Synonym for Found + @{redirectType = 'Found'; redirectedMethod = 'GET'} + @{redirectType = 'Redirect'; redirectedMethod = 'GET'} # Synonym for Found - @{redirectType = 'redirectMethod'; redirectedMethod='GET'} - @{redirectType = 'SeeOther'; redirectedMethod='GET'} # Synonym for RedirectMethod + @{redirectType = 'redirectMethod'; redirectedMethod = 'GET'} + @{redirectType = 'SeeOther'; redirectedMethod = 'GET'} # Synonym for RedirectMethod - @{redirectType = 'TemporaryRedirect'; redirectedMethod='GET'} - @{redirectType = 'RedirectKeepVerb'; redirectedMethod='GET'} # Synonym for TemporaryRedirect + @{redirectType = 'TemporaryRedirect'; redirectedMethod = 'GET'} + @{redirectType = 'RedirectKeepVerb'; redirectedMethod = 'GET'} # Synonym for TemporaryRedirect ) $PendingCertificateTest = $false @@ -422,7 +372,6 @@ if ( $IsMacOS ) { $PendingCertificateTest = $true } if ( test-path /etc/centos-release ) { $PendingCertificateTest = $true } Describe "Invoke-WebRequest tests" -Tags "Feature" { - BeforeAll { $response = Start-HttpListener -Port 8080 $WebListener = Start-WebListener @@ -435,8 +384,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { # Validate the output of Invoke-WebRequest # - function ValidateResponse - { + function ValidateResponse { param ($response) $response.Error | Should Be $null @@ -455,7 +403,6 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { #User-Agent changes on different platforms, so tests should only be run if on the correct platform It "Invoke-WebRequest returns Correct User-Agent on MacOSX" -Skip:(!$IsMacOS) { - $uri = Get-WebListenerUrl -Test 'Get' $command = "Invoke-WebRequest -Uri '$uri' -TimeoutSec 5" @@ -468,7 +415,6 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { } It "Invoke-WebRequest returns Correct User-Agent on Linux" -Skip:(!$IsLinux) { - $uri = Get-WebListenerUrl -Test 'Get' $command = "Invoke-WebRequest -Uri '$uri' -TimeoutSec 5" @@ -481,7 +427,6 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { } It "Invoke-WebRequest returns Correct User-Agent on Windows" -Skip:(!$IsWindows) { - $uri = Get-WebListenerUrl -Test 'Get' $command = "Invoke-WebRequest -Uri '$uri' -TimeoutSec 5" @@ -494,7 +439,6 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { } It "Invoke-WebRequest returns headers dictionary" { - $uri = Get-WebListenerUrl -Test 'Get' $command = "Invoke-WebRequest -Uri '$uri' -TimeoutSec 5" @@ -507,7 +451,6 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { } It "Validate Invoke-WebRequest -DisableKeepAlive" { - # Operation options $uri = Get-WebListenerUrl -Test 'Get' $command = "Invoke-WebRequest -Uri $uri -TimeoutSec 5 -DisableKeepAlive" @@ -519,7 +462,6 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { } It "Validate Invoke-WebRequest -MaximumRedirection" { - $uri = Get-WebListenerUrl -Test 'Redirect' -TestValue '3' $command = "Invoke-WebRequest -Uri '$uri' -MaximumRedirection 4" @@ -532,7 +474,6 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { } It "Validate Invoke-WebRequest error for -MaximumRedirection" { - $uri = Get-WebListenerUrl -Test 'Redirect' -TestValue '3' $command = "Invoke-WebRequest -Uri '$uri' -MaximumRedirection 2" @@ -541,7 +482,6 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { } It "Invoke-WebRequest supports request that returns page containing UTF-8 data." { - $uri = Get-WebListenerUrl -Test 'Encoding' -TestValue 'Utf8' $command = "Invoke-WebRequest -Uri '$uri'" @@ -553,7 +493,6 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { } It "Invoke-WebRequest validate timeout option" { - $uri = Get-WebListenerUrl -Test 'Delay' -TestValue '5' $command = "Invoke-WebRequest -Uri '$uri' -TimeoutSec 2" @@ -562,7 +501,6 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { } It "Validate Invoke-WebRequest error with -Proxy and -NoProxy option" { - $uri = Get-WebListenerUrl -Test 'Delay' -TestValue '10' $command = "Invoke-WebRequest -Uri '$uri' -Proxy 'http://localhost:8080' -NoProxy -TimeoutSec 2" @@ -645,10 +583,8 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { $testMethods = @("POST", "PATCH", "PUT", "DELETE") $contentTypes = @("text/plain", "application/xml", "application/json") - foreach ($contentType in $contentTypes) - { - foreach ($method in $testMethods) - { + foreach ($contentType in $contentTypes) { + foreach ($method in $testMethods) { # Operation options $uri = Get-WebListenerUrl -Test $method $body = GetTestData -contentType $contentType @@ -670,7 +606,6 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { } It "Validate Invoke-WebRequest -Headers --> Set KeepAlive to false via headers" { - $uri = Get-WebListenerUrl -Test 'Get' $result = ExecuteRequestWithHeaders -cmdletName Invoke-WebRequest -uri $uri ValidateResponse -response $result @@ -678,15 +613,15 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { } # Validate all available user agents for Invoke-WebRequest - $agents = @{InternetExplorer = "MSIE 9.0" - Chrome = "Chrome" - Opera = "Opera" - Safari = "Safari" - FireFox = "Firefox" - } + $agents = @{ + InternetExplorer = "MSIE 9.0" + Chrome = "Chrome" + Opera = "Opera" + Safari = "Safari" + FireFox = "Firefox" + } - foreach ($agentName in $agents.Keys) - { + foreach ($agentName in $agents.Keys) { $expectedAgent = $agents[$agentName] $uri = Get-WebListenerUrl -Test 'Get' $userAgent = "[Microsoft.PowerShell.Commands.PSUserAgent]::$agentName" @@ -705,7 +640,6 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { } It "Validate Invoke-WebRequest -OutFile" { - $uri = Get-WebListenerUrl -Test 'Get' $result = ExecuteRequestWithOutFile -cmdletName "Invoke-WebRequest" -uri $uri $jsonContent = $result.Output | ConvertFrom-Json @@ -713,7 +647,6 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { } 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' = ''} $command = "Invoke-WebRequest -Uri '$uri'" @@ -737,7 +670,6 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { } It "Validate Invoke-WebRequest default ContentType for CustomMethod POST" { - $uri = Get-WebListenerUrl -Test 'Post' $command = "Invoke-WebRequest -Uri '$uri' -CustomMethod POST -Body 'testparam=testvalue'" $result = ExecuteWebCommand -command $command @@ -747,7 +679,6 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { } It "Validate Invoke-WebRequest body is converted to query params for CustomMethod GET" { - $uri = Get-WebListenerUrl -Test 'Get' $command = "Invoke-WebRequest -Uri '$uri' -CustomMethod GET -Body @{'testparam'='testvalue'}" $result = ExecuteWebCommand -command $command @@ -755,13 +686,12 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { } It "Validate Invoke-WebRequest returns HTTP errors in exception" { - $query = @{ - body = "I am a teapot!!!" - statuscode = 418 + body = "I am a teapot!!!" + statuscode = 418 responsephrase = "I am a teapot" } - $uri = Get-WebListenerUrl -Test 'Response' -Query $query + $uri = Get-WebListenerUrl -Test 'Response' -Query $query $command = "Invoke-WebRequest -Uri '$uri'" $result = ExecuteWebCommand -command $command @@ -774,7 +704,6 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { } It "Validate Invoke-WebRequest returns empty RelationLink property if there is no Link Header" { - $uri = $uri = Get-WebListenerUrl -Test 'Get' $command = "Invoke-WebRequest -Uri '$uri'" $result = ExecuteWebCommand -command $command @@ -783,7 +712,6 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { } It "Validate Invoke-WebRequest returns valid RelationLink property with absolute uris if Link Header is present" { - $command = "Invoke-WebRequest -Uri 'http://localhost:8080/PowerShell?test=linkheader&maxlinks=5'" $result = ExecuteWebCommand -command $command $result.Output.RelationLink.Count | Should BeExactly 2 @@ -792,11 +720,11 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { } # Test pending support for multiple header capable server on Linux/macOS see issue #4639 - It "Validate Invoke-WebRequest returns valid RelationLink property with absolute uris if Multiple Link Headers are present" -Pending:$(!$IsWindows){ + It "Validate Invoke-WebRequest returns valid RelationLink property with absolute uris if Multiple Link Headers are present" -Pending:$(!$IsWindows) { $Query = @{ - body = "ok" - contenttype = 'text/plain' - headers = @{ + body = "ok" + contenttype = 'text/plain' + headers = @{ Link = @( '; rel="self"' '; rel="next"' @@ -804,7 +732,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { ) } | ConvertTo-Json -Compress } - $Uri = Get-WebListenerUrl -Test 'Response' -Query $Query + $Uri = Get-WebListenerUrl -Test 'Response' -Query $Query $command = "Invoke-WebRequest -Uri '$uri'" $result = ExecuteWebCommand -command $command $result.Output.RelationLink.Count | Should BeExactly 3 @@ -940,9 +868,9 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { It "Verifies Invoke-WebRequest detects charset meta value when the ContentType header does not define it." { $query = @{ contenttype = 'text/html' - body = '' + body = '' } - $uri = Get-WebListenerUrl -Test 'Response' -Query $query + $uri = Get-WebListenerUrl -Test 'Response' -Query $query $expectedEncoding = [System.Text.Encoding]::GetEncoding('Unicode') $response = ExecuteWebRequest -Uri $uri -UseBasicParsing @@ -954,9 +882,9 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { It "Verifies Invoke-WebRequest detects charset meta value when newlines are encountered in the element." { $query = @{ contenttype = 'text/html' - body = "`n `n `n `n" + body = "`n `n `n `n" } - $uri = Get-WebListenerUrl -Test 'Response' -Query $query + $uri = Get-WebListenerUrl -Test 'Response' -Query $query $expectedEncoding = [System.Text.Encoding]::GetEncoding('Unicode') $response = ExecuteWebRequest -Uri $uri -UseBasicParsing @@ -968,9 +896,9 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { It "Verifies Invoke-WebRequest detects charset meta value when the attribute value is unquoted." { $query = @{ contenttype = 'text/html' - body = '' + body = '' } - $uri = Get-WebListenerUrl -Test 'Response' -Query $query + $uri = Get-WebListenerUrl -Test 'Response' -Query $query $expectedEncoding = [System.Text.Encoding]::GetEncoding('Unicode') $response = ExecuteWebRequest -Uri $uri -UseBasicParsing @@ -982,9 +910,9 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { It "Verifies Invoke-WebRequest detects http-equiv charset meta value when the ContentType header does not define it." { $query = @{ contenttype = 'text/html' - body = "`n`n`n" + body = "`n`n`n" } - $uri = Get-WebListenerUrl -Test 'Response' -Query $query + $uri = Get-WebListenerUrl -Test 'Response' -Query $query $expectedEncoding = [System.Text.Encoding]::GetEncoding('Unicode') $response = ExecuteWebRequest -Uri $uri -UseBasicParsing @@ -996,9 +924,9 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { It "Verifies Invoke-WebRequest detects http-equiv charset meta value newlines are encountered in the element." { $query = @{ contenttype = 'text/html' - body = "`n`n`n" + body = "`n`n`n" } - $uri = Get-WebListenerUrl -Test 'Response' -Query $query + $uri = Get-WebListenerUrl -Test 'Response' -Query $query $expectedEncoding = [System.Text.Encoding]::GetEncoding('Unicode') $response = ExecuteWebRequest -Uri $uri -UseBasicParsing @@ -1010,9 +938,9 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { It "Verifies Invoke-WebRequest ignores meta charset value when Content-Type header defines it." { $query = @{ contenttype = 'text/html; charset=utf-8' - body = '' + body = '' } - $uri = Get-WebListenerUrl -Test 'Response' -Query $query + $uri = Get-WebListenerUrl -Test 'Response' -Query $query # NOTE: meta charset should be ignored $expectedEncoding = [System.Text.Encoding]::UTF8 $response = ExecuteWebRequest -Uri $uri -UseBasicParsing @@ -1025,9 +953,9 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { It "Verifies Invoke-WebRequest honors non-utf8 charsets in the Content-Type header" { $query = @{ contenttype = 'text/html; charset=utf-16' - body = '' + body = '' } - $uri = Get-WebListenerUrl -Test 'Response' -Query $query + $uri = Get-WebListenerUrl -Test 'Response' -Query $query # NOTE: meta charset should be ignored $expectedEncoding = [System.Text.Encoding]::GetEncoding('utf-16') $response = ExecuteWebRequest -Uri $uri -UseBasicParsing @@ -1040,9 +968,9 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { It "Verifies Invoke-WebRequest defaults to iso-8859-1 when an unsupported/invalid charset is declared" { $query = @{ contenttype = 'text/html' - body = '' + body = '' } - $uri = Get-WebListenerUrl -Test 'Response' -Query $query + $uri = Get-WebListenerUrl -Test 'Response' -Query $query $expectedEncoding = [System.Text.Encoding]::GetEncoding('iso-8859-1') $response = ExecuteWebRequest -Uri $uri -UseBasicParsing @@ -1054,9 +982,9 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { It "Verifies Invoke-WebRequest defaults to iso-8859-1 when an unsupported/invalid charset is declared using http-equiv" { $query = @{ contenttype = 'text/html' - body = "`n`n`n" + body = "`n`n`n" } - $uri = Get-WebListenerUrl -Test 'Response' -Query $query + $uri = Get-WebListenerUrl -Test 'Response' -Query $query $expectedEncoding = [System.Text.Encoding]::GetEncoding('iso-8859-1') $response = ExecuteWebRequest -Uri $uri -UseBasicParsing @@ -1073,9 +1001,9 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { It "Verifies Invoke-WebRequest includes Content headers in Headers property" { $query = @{ contenttype = 'text/plain' - body ='OK' + body = 'OK' } - $uri = Get-WebListenerUrl -Test 'Response' -Query $query + $uri = Get-WebListenerUrl -Test 'Response' -Query $query $command = "Invoke-WebRequest -Uri '$uri'" $result = ExecuteWebCommand -command $command ValidateResponse $result @@ -1087,9 +1015,9 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { It "Verifies Invoke-WebRequest includes Content headers in RawContent property" { $query = @{ contenttype = 'text/plain' - body ='OK' + body = 'OK' } - $uri = Get-WebListenerUrl -Test 'Response' -Query $query + $uri = Get-WebListenerUrl -Test 'Response' -Query $query $command = "Invoke-WebRequest -Uri '$uri'" $result = ExecuteWebCommand -command $command ValidateResponse $result @@ -1103,12 +1031,12 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { It "Verifies Invoke-WebRequest Supports Multiple response headers with same name" -Pending:$(!$IsWindows) { $query = @{ contenttype = 'text/plain' - body ='OK' - headers = @{ - 'X-Fake-Header' = @('testvalue01','testvalue02') + body = 'OK' + headers = @{ + 'X-Fake-Header' = @('testvalue01', 'testvalue02') } | ConvertTo-Json -Compress } - $uri = Get-WebListenerUrl -Test 'Response' -Query $query + $uri = Get-WebListenerUrl -Test 'Response' -Query $query $command = "Invoke-WebRequest -Uri '$uri'" $result = ExecuteWebCommand -command $command ValidateResponse $result @@ -1147,7 +1075,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { $result.Error.FullyQualifiedErrorId | Should Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand" } - It "Verifies Invoke-WebRequest Certificate Authentication Fails without -Certificate" { + It "Verifies Invoke-WebRequest Certificate Authentication Fails without -Certificate" { $uri = Get-WebListenerUrl -Https -Test 'Cert' $result = Invoke-WebRequest -Uri $uri -SkipCertificateCheck | Select-Object -ExpandProperty Content | @@ -1209,7 +1137,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { BeforeAll { #[SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Demo/doc/test secret.")] $token = "testpassword" | ConvertTo-SecureString -AsPlainText -Force - $credential = [pscredential]::new("testuser",$token) + $credential = [pscredential]::new("testuser", $token) $httpUri = Get-WebListenerUrl -Test 'Get' $httpsUri = Get-WebListenerUrl -Test 'Get' -Https $httpBasicUri = Get-WebListenerUrl -Test 'Auth' -TestValue 'Basic' @@ -1222,9 +1150,9 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { It "Verifies Invoke-WebRequest -Authentication Basic" { $params = @{ - Uri = $httpsUri - Authentication = "Basic" - Credential = $credential + Uri = $httpsUri + Authentication = "Basic" + Credential = $credential SkipCertificateCheck = $true } $Response = Invoke-WebRequest @params @@ -1236,9 +1164,9 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { It "Verifies Invoke-WebRequest -Authentication " -TestCases $testCases { param($Authentication) $params = @{ - Uri = $httpsUri - Authentication = $Authentication - Token = $token + Uri = $httpsUri + Authentication = $Authentication + Token = $token SkipCertificateCheck = $true } $Response = Invoke-WebRequest @params @@ -1249,23 +1177,23 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { It "Verifies Invoke-WebRequest -Authentication does not support -UseDefaultCredentials" { $params = @{ - Uri = $httpsUri - Token = $token - Authentication = "OAuth" + Uri = $httpsUri + Token = $token + Authentication = "OAuth" UseDefaultCredentials = $true - ErrorAction = 'Stop' - SkipCertificateCheck = $true + ErrorAction = 'Stop' + SkipCertificateCheck = $true } { Invoke-WebRequest @params } | ShouldBeErrorId "WebCmdletAuthenticationConflictException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand" } It "Verifies Invoke-WebRequest -Authentication does not support Both -Credential and -Token" { $params = @{ - Uri = $httpsUri - Token = $token - Credential = $credential - Authentication = "OAuth" - ErrorAction = 'Stop' + Uri = $httpsUri + Token = $token + Credential = $credential + Authentication = "OAuth" + ErrorAction = 'Stop' SkipCertificateCheck = $true } { Invoke-WebRequest @params } | ShouldBeErrorId "WebCmdletAuthenticationTokenConflictException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand" @@ -1274,9 +1202,9 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { It "Verifies Invoke-WebRequest -Authentication requires -Token" -TestCases $testCases { param($Authentication) $params = @{ - Uri = $httpsUri - Authentication = $Authentication - ErrorAction = 'Stop' + Uri = $httpsUri + Authentication = $Authentication + ErrorAction = 'Stop' SkipCertificateCheck = $true } { Invoke-WebRequest @params } | ShouldBeErrorId "WebCmdletAuthenticationTokenNotSuppliedException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand" @@ -1284,9 +1212,9 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { It "Verifies Invoke-WebRequest -Authentication Basic requires -Credential" { $params = @{ - Uri = $httpsUri - Authentication = "Basic" - ErrorAction = 'Stop' + Uri = $httpsUri + Authentication = "Basic" + ErrorAction = 'Stop' SkipCertificateCheck = $true } { Invoke-WebRequest @params } | ShouldBeErrorId "WebCmdletAuthenticationCredentialNotSuppliedException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand" @@ -1294,19 +1222,19 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { It "Verifies Invoke-WebRequest -Authentication Requires HTTPS" { $params = @{ - Uri = $httpUri - Token = $token + Uri = $httpUri + Token = $token Authentication = "OAuth" - ErrorAction = 'Stop' + ErrorAction = 'Stop' } { Invoke-WebRequest @params } | ShouldBeErrorId "WebCmdletAllowUnencryptedAuthenticationRequiredException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand" } It "Verifies Invoke-WebRequest -Authentication Can use HTTP with -AllowUnencryptedAuthentication" { $params = @{ - Uri = $httpUri - Token = $token - Authentication = "OAuth" + Uri = $httpUri + Token = $token + Authentication = "OAuth" AllowUnencryptedAuthentication = $true } $Response = Invoke-WebRequest @params @@ -1317,8 +1245,8 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { It "Verifies Invoke-WebRequest Negotiated -Credential over HTTPS" { $params = @{ - Uri = $httpsBasicUri - Credential = $credential + Uri = $httpsBasicUri + Credential = $credential SkipCertificateCheck = $true } $Response = Invoke-WebRequest @params @@ -1329,8 +1257,8 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { It "Verifies Invoke-WebRequest Negotiated -Credential Requires HTTPS" { $params = @{ - Uri = $httpBasicUri - Credential = $credential + Uri = $httpBasicUri + Credential = $credential ErrorAction = 'Stop' } { Invoke-WebRequest @params } | ShouldBeErrorId "WebCmdletAllowUnencryptedAuthenticationRequiredException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand" @@ -1338,8 +1266,8 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { It "Verifies Invoke-WebRequest Negotiated -Credential Can use HTTP with -AllowUnencryptedAuthentication" { $params = @{ - Uri = $httpBasicUri - Credential = $credential + Uri = $httpBasicUri + Credential = $credential AllowUnencryptedAuthentication = $true } $Response = Invoke-WebRequest @params @@ -1355,9 +1283,9 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { ) { param($AuthType) $params = @{ - Uri = Get-WebListenerUrl -Test 'Auth' -TestValue $AuthType -Https + Uri = Get-WebListenerUrl -Test 'Auth' -TestValue $AuthType -Https UseDefaultCredentials = $true - SkipCertificateCheck = $true + SkipCertificateCheck = $true } $Response = Invoke-WebRequest @params $result = $response.Content | ConvertFrom-Json @@ -1368,9 +1296,9 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { # The error condition can at least be tested on all platforms. It "Verifies Invoke-WebRequest Negotiated -UseDefaultCredentials Requires HTTPS" { $params = @{ - Uri = $httpUri + Uri = $httpUri UseDefaultCredentials = $true - ErrorAction = 'Stop' + ErrorAction = 'Stop' } { Invoke-WebRequest @params } | ShouldBeErrorId "WebCmdletAllowUnencryptedAuthenticationRequiredException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand" } @@ -1382,8 +1310,8 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { ) { param($AuthType) $params = @{ - Uri = Get-WebListenerUrl -Test 'Auth' -TestValue $AuthType - UseDefaultCredentials = $true + Uri = Get-WebListenerUrl -Test 'Auth' -TestValue $AuthType + UseDefaultCredentials = $true AllowUnencryptedAuthentication = $true } $Response = Invoke-WebRequest @params @@ -1415,16 +1343,16 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { ) $testCases2 = @( - @{ Test = @{IntendedProtocol = 'Tls'; ActualProtocol = 'Tls12'}; Pending = $false } - @{ Test = @{IntendedProtocol = 'Tls'; ActualProtocol = 'Tls11'}; Pending = $false } + @{ Test = @{IntendedProtocol = 'Tls'; ActualProtocol = 'Tls12'}; Pending = $false } + @{ Test = @{IntendedProtocol = 'Tls'; ActualProtocol = 'Tls11'}; Pending = $false } @{ Test = @{IntendedProtocol = 'Tls11'; ActualProtocol = 'Tls12'}; Pending = $false } @{ Test = @{IntendedProtocol = 'Tls12'; ActualProtocol = 'Tls11'}; Pending = $false } @{ Test = @{IntendedProtocol = 'Tls11'; ActualProtocol = 'Tls'}; Pending = $false } @{ Test = @{IntendedProtocol = 'Tls12'; ActualProtocol = 'Tls'}; Pending = $false } # macOS does not support multiple SslProtocols - @{ Test = @{IntendedProtocol = 'Tls11, Tls12'; ActualProtocol = 'Tls'}; Pending = $IsMacOS } - @{ Test = @{IntendedProtocol = 'Tls, Tls12'; ActualProtocol = 'Tls11'}; Pending = $IsMacOS } - @{ Test = @{IntendedProtocol = 'Tls, Tls11'; ActualProtocol = 'Tls12'}; Pending = $IsMacOS } + @{ Test = @{IntendedProtocol = 'Tls11, Tls12'; ActualProtocol = 'Tls'}; Pending = $IsMacOS } + @{ Test = @{IntendedProtocol = 'Tls, Tls12'; ActualProtocol = 'Tls11'}; Pending = $IsMacOS } + @{ Test = @{IntendedProtocol = 'Tls, Tls11'; ActualProtocol = 'Tls12'}; Pending = $IsMacOS } ) } @@ -1432,8 +1360,8 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { It "Verifies Invoke-WebRequest -SslProtocol works on " -TestCases ($entry.Test) -Pending:($entry.Pending) { param($SslProtocol, $ActualProtocol) $params = @{ - Uri = Get-WebListenerUrl -Test 'Get' -Https -SslProtocol $ActualProtocol - SslProtocol = $SslProtocol + Uri = Get-WebListenerUrl -Test 'Get' -Https -SslProtocol $ActualProtocol + SslProtocol = $SslProtocol SkipCertificateCheck = $true } $response = Invoke-WebRequest @params @@ -1447,10 +1375,10 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { It "Verifies Invoke-WebRequest -SslProtocol -SslProtocol fails on a only connection" -TestCases ($entry.Test) -Pending:($entry.Pending) { param( $IntendedProtocol, $ActualProtocol) $params = @{ - Uri = Get-WebListenerUrl -Test 'Get' -Https -SslProtocol $ActualProtocol - SslProtocol = $IntendedProtocol + Uri = Get-WebListenerUrl -Test 'Get' -Https -SslProtocol $ActualProtocol + SslProtocol = $IntendedProtocol SkipCertificateCheck = $true - ErrorAction = 'Stop' + ErrorAction = 'Stop' } { Invoke-WebRequest @params } | ShouldBeErrorId 'WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand' } @@ -1485,7 +1413,6 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { } Describe "Invoke-RestMethod tests" -Tags "Feature" { - BeforeAll { $response = Start-HttpListener -Port 8081 $WebListener = Start-WebListener @@ -1498,7 +1425,6 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { #User-Agent changes on different platforms, so tests should only be run if on the correct platform It "Invoke-RestMethod returns Correct User-Agent on MacOSX" -Skip:(!$IsMacOS) { - $uri = Get-WebListenerUrl -Test 'Get' $command = "Invoke-RestMethod -Uri '$uri' -TimeoutSec 5" @@ -1509,7 +1435,6 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { } It "Invoke-RestMethod returns Correct User-Agent on Linux" -Skip:(!$IsLinux) { - $uri = Get-WebListenerUrl -Test 'Get' $command = "Invoke-RestMethod -Uri '$uri' -TimeoutSec 5" @@ -1520,7 +1445,6 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { } It "Invoke-RestMethod returns Correct User-Agent on Windows" -Skip:(!$IsWindows) { - $uri = Get-WebListenerUrl -Test 'Get' $command = "Invoke-RestMethod -Uri '$uri' -TimeoutSec 5" @@ -1531,7 +1455,6 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { } It "Invoke-RestMethod returns headers dictionary" { - $uri = Get-WebListenerUrl -Test 'Get' $command = "Invoke-RestMethod -Uri '$uri' -TimeoutSec 5" @@ -1542,7 +1465,6 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { } It "Validate Invoke-RestMethod -DisableKeepAlive" { - # Operation options $uri = Get-WebListenerUrl -Test 'Get' $command = "Invoke-RestMethod -Uri '$uri' -TimeoutSec 5 -DisableKeepAlive" @@ -1555,7 +1477,6 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { } It "Validate Invoke-RestMethod -MaximumRedirection" { - $uri = Get-WebListenerUrl -Test 'Redirect' -TestValue '3' $command = "Invoke-RestMethod -Uri '$uri' -MaximumRedirection 4" @@ -1566,7 +1487,6 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { } It "Validate Invoke-RestMethod error for -MaximumRedirection" { - $uri = Get-WebListenerUrl -Test 'Redirect' -TestValue '3' $command = "Invoke-RestMethod -Uri '$uri' -MaximumRedirection 2" @@ -1575,7 +1495,6 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { } It "Invoke-RestMethod supports request that returns page containing UTF-8 data." { - $uri = Get-WebListenerUrl -Test 'Encoding' -TestValue 'Utf8' $command = "Invoke-RestMethod -Uri '$uri'" @@ -1584,7 +1503,6 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { } It "Invoke-RestMethod validate timeout option" { - $uri = Get-WebListenerUrl -Test 'Delay' -TestValue '5' $command = "Invoke-RestMethod -Uri '$uri' -TimeoutSec 2" @@ -1593,7 +1511,6 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { } It "Validate Invoke-RestMethod error with -Proxy and -NoProxy option" { - $uri = Get-WebListenerUrl -Test 'Delay' -TestValue '10' $command = "Invoke-RestMethod -Uri '$uri' -Proxy 'http://localhost:8080' -NoProxy -TimeoutSec 2" @@ -1671,10 +1588,8 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { $testMethods = @("POST", "PATCH", "PUT", "DELETE") $contentTypes = @("text/plain", "application/xml", "application/json") - foreach ($contentType in $contentTypes) - { - foreach ($method in $testMethods) - { + foreach ($contentType in $contentTypes) { + foreach ($method in $testMethods) { # Operation options $uri = Get-WebListenerUrl -Test $method $body = GetTestData -contentType $contentType @@ -1695,7 +1610,6 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { } It "Validate Invoke-RestMethod -Headers --> Set KeepAlive to false via headers" { - $uri = Get-WebListenerUrl -Test 'Get' $result = ExecuteRequestWithHeaders -cmdletName Invoke-RestMethod -uri $uri @@ -1706,14 +1620,13 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { # Validate all available user agents for Invoke-RestMethod $agents = @{InternetExplorer = "MSIE 9.0" - Chrome = "Chrome" - Opera = "Opera" - Safari = "Safari" - FireFox = "Firefox" - } + Chrome = "Chrome" + Opera = "Opera" + Safari = "Safari" + FireFox = "Firefox" + } - foreach ($agentName in $agents.Keys) - { + foreach ($agentName in $agents.Keys) { $expectedAgent = $agents[$agentName] $uri = Get-WebListenerUrl -Test 'Get' $userAgent = "[Microsoft.PowerShell.Commands.PSUserAgent]::$agentName" @@ -1730,7 +1643,6 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { } It "Validate Invoke-RestMethod -OutFile" { - $uri = Get-WebListenerUrl -Test 'Get' $result = ExecuteRequestWithOutFile -cmdletName "Invoke-RestMethod" -uri $uri $jsonContent = $result.Output | ConvertFrom-Json @@ -1738,7 +1650,6 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { } 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' = ''} $command = "Invoke-RestMethod -Uri '$uri'" @@ -1761,7 +1672,6 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { } It "Validate Invoke-RestMethod default ContentType for CustomMethod POST" { - $uri = Get-WebListenerUrl -Test 'Post' $command = "Invoke-RestMethod -Uri '$uri' -CustomMethod POST -Body 'testparam=testvalue'" $result = ExecuteWebCommand -command $command @@ -1770,7 +1680,6 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { } It "Validate Invoke-RestMethod body is converted to query params for CustomMethod GET" { - $uri = Get-WebListenerUrl -Test 'Get' $command = "Invoke-RestMethod -Uri '$uri' -CustomMethod GET -Body @{'testparam'='testvalue'}" $result = ExecuteWebCommand -command $command @@ -1778,13 +1687,12 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { } It "Validate Invoke-RestMethod returns HTTP errors in exception" { - $query = @{ - body = "I am a teapot!!!" - statuscode = 418 + body = "I am a teapot!!!" + statuscode = 418 responsephrase = "I am a teapot" } - $uri = Get-WebListenerUrl -Test 'Response' -Query $query + $uri = Get-WebListenerUrl -Test 'Response' -Query $query $command = "Invoke-RestMethod -Uri '$uri'" $result = ExecuteWebCommand -command $command @@ -1797,12 +1705,11 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { } It "Validate Invoke-RestMethod -FollowRelLink doesn't fail if no Link Header is present" { - $query = @{ contenttype = 'application/json' - body = '"foo"' + body = '"foo"' } - $uri = Get-WebListenerUrl -Test 'Response' -Query $query + $uri = Get-WebListenerUrl -Test 'Response' -Query $query $command = "Invoke-RestMethod -Uri '$uri' -FollowRelLink" $result = ExecuteWebCommand -command $command @@ -1953,7 +1860,7 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { It "Validate Invoke-RestMethod -SkipCertificateCheck" { # HTTP method HEAD must be used to not retrieve an unparsable HTTP body # validate that exception is thrown for URI with expired certificate - $uri= Get-WebListenerUrl -Https + $uri = Get-WebListenerUrl -Https $command = "Invoke-RestMethod -Uri '$uri' -Method HEAD" $result = ExecuteWebCommand -command $command $result.Error.FullyQualifiedErrorId | Should Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand" @@ -2002,6 +1909,7 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { $result.Headers.'Content-Type' | Should Match 'multipart/form-data' $result.Items.TestString[0] | Should Be 'TestValue' } + It "Verifies Invoke-RestMethod Supports Multipart File Values" { $body = GetMultipartBody -File $uri = Get-WebListenerUrl -Test 'Multipart' @@ -2012,6 +1920,7 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { $result.Files[0].ContentType | Should Be 'text/plain' $result.Files[0].Content | Should Match 'TestContent' } + It "Verifies Invoke-RestMethod Supports Mixed Multipart String and File Values" { $body = GetMultipartBody -String -File $uri = Get-WebListenerUrl -Test 'Multipart' @@ -2031,9 +1940,9 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { It "Verifies Invoke-RestMethod detects charset meta value when the ContentType header does not define it." { $query = @{ contenttype = 'text/html' - body = '' + body = '' } - $uri = Get-WebListenerUrl -Test 'Response' -Query $query + $uri = Get-WebListenerUrl -Test 'Response' -Query $query $expectedEncoding = [System.Text.Encoding]::GetEncoding('Unicode') $response = ExecuteRestMethod -Uri $uri -UseBasicParsing @@ -2044,9 +1953,9 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { It "Verifies Invoke-WebRequest detects charset meta value when newlines are encountered in the element." { $query = @{ contenttype = 'text/html' - body = "`n `n `n `n" + body = "`n `n `n `n" } - $uri = Get-WebListenerUrl -Test 'Response' -Query $query + $uri = Get-WebListenerUrl -Test 'Response' -Query $query $expectedEncoding = [System.Text.Encoding]::GetEncoding('Unicode') $response = ExecuteRestMethod -Uri $uri -UseBasicParsing @@ -2057,9 +1966,9 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { It "Verifies Invoke-RestMethod detects charset meta value when the attribute value is unquoted." { $query = @{ contenttype = 'text/html' - body = '' + body = '' } - $uri = Get-WebListenerUrl -Test 'Response' -Query $query + $uri = Get-WebListenerUrl -Test 'Response' -Query $query $expectedEncoding = [System.Text.Encoding]::GetEncoding('Unicode') $response = ExecuteRestMethod -Uri $uri -UseBasicParsing @@ -2070,9 +1979,9 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { It "Verifies Invoke-RestMethod detects http-equiv charset meta value when the ContentType header does not define it." { $query = @{ contenttype = 'text/html' - body = "`n`n`n" + body = "`n`n`n" } - $uri = Get-WebListenerUrl -Test 'Response' -Query $query + $uri = Get-WebListenerUrl -Test 'Response' -Query $query $expectedEncoding = [System.Text.Encoding]::GetEncoding('Unicode') $response = ExecuteRestMethod -Uri $uri -UseBasicParsing @@ -2083,9 +1992,9 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { It "Verifies Invoke-RestMethod detects http-equiv charset meta value newlines are encountered in the element." { $query = @{ contenttype = 'text/html' - body = "`n`n`n`n" + body = "`n`n`n`n" } - $uri = Get-WebListenerUrl -Test 'Response' -Query $query + $uri = Get-WebListenerUrl -Test 'Response' -Query $query $expectedEncoding = [System.Text.Encoding]::GetEncoding('Unicode') $response = ExecuteRestMethod -Uri $uri -UseBasicParsing @@ -2096,9 +2005,9 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { It "Verifies Invoke-RestMethod ignores meta charset value when Content-Type header defines it." { $query = @{ contenttype = 'text/html; charset=utf-8' - body = '' + body = '' } - $uri = Get-WebListenerUrl -Test 'Response' -Query $query + $uri = Get-WebListenerUrl -Test 'Response' -Query $query # NOTE: meta charset should be ignored $expectedEncoding = [System.Text.Encoding]::UTF8 $response = ExecuteRestMethod -Uri $uri -UseBasicParsing @@ -2110,9 +2019,9 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { It "Verifies Invoke-RestMethod honors non-utf8 charsets in the Content-Type header" { $query = @{ contenttype = 'text/html; charset=utf-16' - body = '' + body = '' } - $uri = Get-WebListenerUrl -Test 'Response' -Query $query + $uri = Get-WebListenerUrl -Test 'Response' -Query $query # NOTE: meta charset should be ignored $expectedEncoding = [System.Text.Encoding]::GetEncoding('utf-16') $response = ExecuteRestMethod -Uri $uri -UseBasicParsing @@ -2124,9 +2033,9 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { It "Verifies Invoke-RestMethod defaults to iso-8859-1 when an unsupported/invalid charset is declared" { $query = @{ contenttype = 'text/html' - body = '' + body = '' } - $uri = Get-WebListenerUrl -Test 'Response' -Query $query + $uri = Get-WebListenerUrl -Test 'Response' -Query $query $expectedEncoding = [System.Text.Encoding]::GetEncoding('iso-8859-1') $response = ExecuteRestMethod -Uri $uri -UseBasicParsing @@ -2137,9 +2046,9 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { It "Verifies Invoke-RestMethod defaults to iso-8859-1 when an unsupported/invalid charset is declared using http-equiv" { $query = @{ contenttype = 'text/html' - body = "`n`n`n" + body = "`n`n`n" } - $uri = Get-WebListenerUrl -Test 'Response' -Query $query + $uri = Get-WebListenerUrl -Test 'Response' -Query $query $expectedEncoding = [System.Text.Encoding]::GetEncoding('iso-8859-1') $response = ExecuteRestMethod -Uri $uri -UseBasicParsing @@ -2174,7 +2083,7 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { BeforeAll { #[SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Demo/doc/test secret.")] $token = "testpassword" | ConvertTo-SecureString -AsPlainText -Force - $credential = [pscredential]::new("testuser",$token) + $credential = [pscredential]::new("testuser", $token) $httpUri = Get-WebListenerUrl -Test 'Get' $httpsUri = Get-WebListenerUrl -Test 'Get' -Https $httpBasicUri = Get-WebListenerUrl -Test 'Auth' -TestValue 'Basic' @@ -2187,9 +2096,9 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { It "Verifies Invoke-RestMethod -Authentication Basic" { $params = @{ - Uri = $httpsUri - Authentication = "Basic" - Credential = $credential + Uri = $httpsUri + Authentication = "Basic" + Credential = $credential SkipCertificateCheck = $true } $result = Invoke-RestMethod @params @@ -2200,9 +2109,9 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { It "Verifies Invoke-RestMethod -Authentication " -TestCases $testCases { param($Authentication) $params = @{ - Uri = $httpsUri - Authentication = $Authentication - Token = $token + Uri = $httpsUri + Authentication = $Authentication + Token = $token SkipCertificateCheck = $true } $result = Invoke-RestMethod @params @@ -2212,23 +2121,23 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { It "Verifies Invoke-RestMethod -Authentication does not support -UseDefaultCredentials" { $params = @{ - Uri = $httpsUri - Token = $token - Authentication = "OAuth" + Uri = $httpsUri + Token = $token + Authentication = "OAuth" UseDefaultCredentials = $true - ErrorAction = 'Stop' - SkipCertificateCheck = $true + ErrorAction = 'Stop' + SkipCertificateCheck = $true } { Invoke-RestMethod @params } | ShouldBeErrorId "WebCmdletAuthenticationConflictException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand" } It "Verifies Invoke-RestMethod -Authentication does not support Both -Credential and -Token" { $params = @{ - Uri = $httpsUri - Token = $token - Credential = $credential - Authentication = "OAuth" - ErrorAction = 'Stop' + Uri = $httpsUri + Token = $token + Credential = $credential + Authentication = "OAuth" + ErrorAction = 'Stop' SkipCertificateCheck = $true } { Invoke-RestMethod @params } | ShouldBeErrorId "WebCmdletAuthenticationTokenConflictException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand" @@ -2237,9 +2146,9 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { It "Verifies Invoke-RestMethod -Authentication requires -Token" -TestCases $testCases { param($Authentication) $params = @{ - Uri = $httpsUri - Authentication = $Authentication - ErrorAction = 'Stop' + Uri = $httpsUri + Authentication = $Authentication + ErrorAction = 'Stop' SkipCertificateCheck = $true } { Invoke-RestMethod @params } | ShouldBeErrorId "WebCmdletAuthenticationTokenNotSuppliedException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand" @@ -2247,9 +2156,9 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { It "Verifies Invoke-RestMethod -Authentication Basic requires -Credential" { $params = @{ - Uri = $httpsUri - Authentication = "Basic" - ErrorAction = 'Stop' + Uri = $httpsUri + Authentication = "Basic" + ErrorAction = 'Stop' SkipCertificateCheck = $true } { Invoke-RestMethod @params } | ShouldBeErrorId "WebCmdletAuthenticationCredentialNotSuppliedException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand" @@ -2257,19 +2166,19 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { It "Verifies Invoke-RestMethod -Authentication Requires HTTPS" { $params = @{ - Uri = $httpUri - Token = $token + Uri = $httpUri + Token = $token Authentication = "OAuth" - ErrorAction = 'Stop' + ErrorAction = 'Stop' } { Invoke-RestMethod @params } | ShouldBeErrorId "WebCmdletAllowUnencryptedAuthenticationRequiredException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand" } It "Verifies Invoke-RestMethod -Authentication Can use HTTP with -AllowUnencryptedAuthentication" { $params = @{ - Uri = $httpUri - Token = $token - Authentication = "OAuth" + Uri = $httpUri + Token = $token + Authentication = "OAuth" AllowUnencryptedAuthentication = $true } $result = Invoke-RestMethod @params @@ -2279,8 +2188,8 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { It "Verifies Invoke-RestMethod Negotiated -Credential over HTTPS" { $params = @{ - Uri = $httpsBasicUri - Credential = $credential + Uri = $httpsBasicUri + Credential = $credential SkipCertificateCheck = $true } $result = Invoke-RestMethod @params @@ -2290,8 +2199,8 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { It "Verifies Invoke-RestMethod Negotiated -Credential Requires HTTPS" { $params = @{ - Uri = $httpBasicUri - Credential = $credential + Uri = $httpBasicUri + Credential = $credential ErrorAction = 'Stop' } { Invoke-RestMethod @params } | ShouldBeErrorId "WebCmdletAllowUnencryptedAuthenticationRequiredException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand" @@ -2299,8 +2208,8 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { It "Verifies Invoke-RestMethod Negotiated -Credential Can use HTTP with -AllowUnencryptedAuthentication" { $params = @{ - Uri = $httpBasicUri - Credential = $credential + Uri = $httpBasicUri + Credential = $credential AllowUnencryptedAuthentication = $true } $result = Invoke-RestMethod @params @@ -2315,9 +2224,9 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { ) { param($AuthType) $params = @{ - Uri = Get-WebListenerUrl -Test 'Auth' -TestValue $AuthType -Https + Uri = Get-WebListenerUrl -Test 'Auth' -TestValue $AuthType -Https UseDefaultCredentials = $true - SkipCertificateCheck = $true + SkipCertificateCheck = $true } $result = Invoke-RestMethod @params @@ -2327,9 +2236,9 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { # The error condition can at least be tested on all platforms. It "Verifies Invoke-RestMethod Negotiated -UseDefaultCredentials Requires HTTPS" { $params = @{ - Uri = $httpUri + Uri = $httpUri UseDefaultCredentials = $true - ErrorAction = 'Stop' + ErrorAction = 'Stop' } { Invoke-RestMethod @params } | ShouldBeErrorId "WebCmdletAllowUnencryptedAuthenticationRequiredException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand" } @@ -2341,8 +2250,8 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { ) { param($AuthType) $params = @{ - Uri = Get-WebListenerUrl -Test 'Auth' -TestValue $AuthType - UseDefaultCredentials = $true + Uri = Get-WebListenerUrl -Test 'Auth' -TestValue $AuthType + UseDefaultCredentials = $true AllowUnencryptedAuthentication = $true } $result = Invoke-RestMethod @params @@ -2372,16 +2281,16 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { ) $testCases2 = @( - @{ Test = @{IntendedProtocol = 'Tls'; ActualProtocol = 'Tls12'}; Pending = $false } - @{ Test = @{IntendedProtocol = 'Tls'; ActualProtocol = 'Tls11'}; Pending = $false } + @{ Test = @{IntendedProtocol = 'Tls'; ActualProtocol = 'Tls12'}; Pending = $false } + @{ Test = @{IntendedProtocol = 'Tls'; ActualProtocol = 'Tls11'}; Pending = $false } @{ Test = @{IntendedProtocol = 'Tls11'; ActualProtocol = 'Tls12'}; Pending = $false } @{ Test = @{IntendedProtocol = 'Tls12'; ActualProtocol = 'Tls11'}; Pending = $false } @{ Test = @{IntendedProtocol = 'Tls11'; ActualProtocol = 'Tls'}; Pending = $false } @{ Test = @{IntendedProtocol = 'Tls12'; ActualProtocol = 'Tls'}; Pending = $false } # macOS does not support multiple SslProtocols - @{ Test = @{IntendedProtocol = 'Tls11, Tls12'; ActualProtocol = 'Tls'}; Pending = $IsMacOS } - @{ Test = @{IntendedProtocol = 'Tls, Tls12'; ActualProtocol = 'Tls11'}; Pending = $IsMacOS } - @{ Test = @{IntendedProtocol = 'Tls, Tls11'; ActualProtocol = 'Tls12'}; Pending = $IsMacOS } + @{ Test = @{IntendedProtocol = 'Tls11, Tls12'; ActualProtocol = 'Tls'}; Pending = $IsMacOS } + @{ Test = @{IntendedProtocol = 'Tls, Tls12'; ActualProtocol = 'Tls11'}; Pending = $IsMacOS } + @{ Test = @{IntendedProtocol = 'Tls, Tls11'; ActualProtocol = 'Tls12'}; Pending = $IsMacOS } ) } @@ -2389,8 +2298,8 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { It "Verifies Invoke-RestMethod -SslProtocol works on " -TestCases ($entry.Test) -Pending:($entry.Pending) { param($SslProtocol, $ActualProtocol) $params = @{ - Uri = Get-WebListenerUrl -Test 'Get' -Https -SslProtocol $ActualProtocol - SslProtocol = $SslProtocol + Uri = Get-WebListenerUrl -Test 'Get' -Https -SslProtocol $ActualProtocol + SslProtocol = $SslProtocol SkipCertificateCheck = $true } $result = Invoke-RestMethod @params @@ -2403,10 +2312,10 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { It "Verifies Invoke-RestMethod -SslProtocol fails on a only connection" -TestCases ($entry.Test) -Pending:($entry.Pending) { param( $IntendedProtocol, $ActualProtocol) $params = @{ - Uri = Get-WebListenerUrl -Test 'Get' -Https -SslProtocol $ActualProtocol - SslProtocol = $IntendedProtocol + Uri = Get-WebListenerUrl -Test 'Get' -Https -SslProtocol $ActualProtocol + SslProtocol = $IntendedProtocol SkipCertificateCheck = $true - ErrorAction = 'Stop' + ErrorAction = 'Stop' } { Invoke-RestMethod @params } | ShouldBeErrorId 'WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand' } @@ -2417,20 +2326,21 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { It "Invoke-RestMethod Supports a Single Value JSON null" { $query = @{ contenttype = 'application/json' - body = 'null' + body = 'null' } - $uri = Get-WebListenerUrl -Test 'Response' -Query $query + $uri = Get-WebListenerUrl -Test 'Response' -Query $query Invoke-RestMethod -Uri $uri | Should Be $null } + It "Invoke-RestMethod Supports a Single Value JSON null and ignores whitespace" { $query = @{ contenttype = 'application/json' - body = " null " + body = " null " } - $uri = Get-WebListenerUrl -Test 'Response' -Query $query + $uri = Get-WebListenerUrl -Test 'Response' -Query $query Invoke-RestMethod -Uri $uri | Should Be $null $query['body'] = " null `n" - $uri = Get-WebListenerUrl -Test 'Response' -Query $query + $uri = Get-WebListenerUrl -Test 'Response' -Query $query Invoke-RestMethod -Uri $uri | Should Be $null } } @@ -2463,70 +2373,65 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { } Describe "Validate Invoke-WebRequest and Invoke-RestMethod -InFile" -Tags "Feature" { - Context "InFile parameter negative tests" { BeforeAll { $uri = Get-WebListenerUrl -Test 'Post' $testCases = @( -#region INVOKE-WEBREQUEST + #region INVOKE-WEBREQUEST @{ - Name = 'Validate error for Invoke-WebRequest -InFile ""' - ScriptBlock = {Invoke-WebRequest -Uri $uri -Method Post -InFile ""} + Name = 'Validate error for Invoke-WebRequest -InFile ""' + ScriptBlock = {Invoke-WebRequest -Uri $uri -Method Post -InFile ""} ExpectedFullyQualifiedErrorId = 'WebCmdletInFileNotFilePathException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand' } @{ - Name = 'Validate error for Invoke-WebRequest -InFile' - ScriptBlock = {Invoke-WebRequest -Uri $uri -Method Post -InFile} + Name = 'Validate error for Invoke-WebRequest -InFile' + ScriptBlock = {Invoke-WebRequest -Uri $uri -Method Post -InFile} ExpectedFullyQualifiedErrorId = 'MissingArgument,Microsoft.PowerShell.Commands.InvokeWebRequestCommand' } @{ - 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' } -#endregion + #endregion -#region INVOKE-RESTMETHOD + #region INVOKE-RESTMETHOD @{ - Name = "Validate error for Invoke-RestMethod -InFile ''" - ScriptBlock = {Invoke-RestMethod -Uri $uri -Method Post -InFile ''} + Name = "Validate error for Invoke-RestMethod -InFile ''" + ScriptBlock = {Invoke-RestMethod -Uri $uri -Method Post -InFile ''} ExpectedFullyQualifiedErrorId = 'WebCmdletInFileNotFilePathException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand' } @{ - Name = "Validate error for Invoke-RestMethod -InFile " - ScriptBlock = {Invoke-RestMethod -Uri $uri -Method Post -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" - ScriptBlock = {Invoke-RestMethod -Uri $uri -Method Post -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' } -#endregion + #endregion ) } It "" -TestCases $testCases { param ($scriptblock, $expectedFullyQualifiedErrorId) - try - { + try { & $scriptblock throw "No Exception!" - } - catch - { + } catch { $_.FullyQualifiedErrorId | should be $ExpectedFullyQualifiedErrorId } } } Context "InFile parameter positive tests" { - BeforeAll { $filePath = Join-Path $TestDrive test.txt New-Item -Path $filePath -Value "hello=world" -ItemType File -Force @@ -2549,17 +2454,16 @@ Describe "Validate Invoke-WebRequest and Invoke-RestMethod -InFile" -Tags "Featu } Describe "Web cmdlets tests using the cmdlet's aliases" -Tags "CI" { - BeforeAll { $WebListener = Start-WebListener } It "Execute Invoke-WebRequest" { $query = @{ - body = "hello" - contenttype = 'text/plain' + body = "hello" + contenttype = 'text/plain' } - $uri = Get-WebListenerUrl -Test 'Response' -Query $query + $uri = Get-WebListenerUrl -Test 'Response' -Query $query $result = iwr $uri -TimeoutSec 5 $result.StatusCode | Should Be "200" $result.Content | Should Be "hello" @@ -2568,9 +2472,9 @@ Describe "Web cmdlets tests using the cmdlet's aliases" -Tags "CI" { It "Execute Invoke-RestMethod" { $query = @{ contenttype = 'application/json' - body = @{Hello = "world"} | ConvertTo-Json -Compress + body = @{Hello = "world"} | ConvertTo-Json -Compress } - $uri = Get-WebListenerUrl -Test 'Response' -Query $query + $uri = Get-WebListenerUrl -Test 'Response' -Query $query $result = irm $uri -TimeoutSec 5 $result.Hello | Should Be "world" } From f30f0de294773154e1216a36399451a83ab56759 Mon Sep 17 00:00:00 2001 From: Mark Kraus Date: Tue, 9 Jan 2018 13:12:05 -0600 Subject: [PATCH 2/2] [Feature] Address PR Feedback --- .../Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index 4737c244ca29..e44dc0c671be 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -1619,11 +1619,12 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { } # Validate all available user agents for Invoke-RestMethod - $agents = @{InternetExplorer = "MSIE 9.0" - Chrome = "Chrome" - Opera = "Opera" - Safari = "Safari" - FireFox = "Firefox" + $agents = @{ + InternetExplorer = "MSIE 9.0" + Chrome = "Chrome" + Opera = "Opera" + Safari = "Safari" + FireFox = "Firefox" } foreach ($agentName in $agents.Keys) {