2018-03-01 23:00:00 -06:00
# Copyright (c) Microsoft Corporation. All rights reserved.
2018-02-13 09:23:53 -08:00
# Licensed under the MIT License.
2016-07-22 15:09:03 -07:00
# This is a Pester test suite which validate the Web cmdlets.
#
2017-12-13 09:28:05 -06:00
# Note: These tests use data from WebListener
2016-07-22 15:09:03 -07:00
#
# Invokes the given command via script block invocation.
#
2018-01-09 15:43:25 -06:00
function ExecuteWebCommand {
2016-07-22 15:09:03 -07:00
param (
[ ValidateNotNullOrEmpty ()]
[ string ]
$command
)
2017-01-16 13:31:14 -08:00
2016-07-22 15:09:03 -07:00
$result = [ PSObject ] @ { Output = $null ; Error = $null }
2018-01-09 15:43:25 -06:00
try {
2016-07-22 15:09:03 -07:00
$scriptBlock = [ scriptblock ]:: Create ( $command )
2018-01-09 15:43:25 -06:00
$result . Output = & $scriptBlock
} catch {
2016-07-22 15:09:03 -07:00
$result . Error = $_
}
return $result
}
# 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.
#
2018-01-09 15:43:25 -06:00
function ExecuteRequestWithOutFile {
2016-07-22 15:09:03 -07:00
param (
2017-01-16 13:31:14 -08:00
[ ValidateSet ( "Invoke-RestMethod" , "Invoke-WebRequest" )]
2016-07-22 15:09:03 -07:00
[ string ]
$cmdletName ,
2018-01-09 15:43:25 -06:00
2016-07-22 15:09:03 -07:00
[ string ]
2017-09-07 22:53:04 -05:00
$uri = ( Get-WebListenerUrl -Test 'Get' )
2016-07-22 15:09:03 -07:00
)
2017-01-16 13:31:14 -08:00
2016-07-22 15:09:03 -07:00
$result = [ PSObject ] @ { Output = $null ; Error = $null }
$filePath = Join-Path $TestDrive (( Get-Random ). ToString () + ".txt" )
2018-01-09 15:43:25 -06:00
try {
if ( $cmdletName -eq "Invoke-WebRequest" ) {
2016-07-22 15:09:03 -07:00
Invoke-WebRequest -Uri $uri -OutFile $filePath
2018-01-09 15:43:25 -06:00
} else {
2016-07-22 15:09:03 -07:00
Invoke-RestMethod -Uri $uri -OutFile $filePath
}
2018-05-17 14:42:04 -07:00
$result . Output = Get-Content $filePath -Raw -ErrorAction SilentlyContinue
2018-01-09 15:43:25 -06:00
} catch {
2016-07-22 15:09:03 -07:00
$result . Error = $_
2018-01-09 15:43:25 -06:00
} finally {
if ( Test-Path $filePath ) {
2018-05-17 14:42:04 -07:00
Remove-Item $filePath -Force -ErrorAction SilentlyContinue
2016-07-22 15:09:03 -07:00
}
}
return $result
}
2017-01-16 13:31:14 -08:00
# This function calls either Invoke-WebRequest or Invoke-RestMethod with the given uri
2016-07-22 15:09:03 -07:00
# using the Headers parameter to disable keep-alive.
#
2018-01-09 15:43:25 -06:00
function ExecuteRequestWithHeaders {
2016-07-22 15:09:03 -07:00
param (
2017-01-16 13:31:14 -08:00
[ ValidateSet ( "Invoke-RestMethod" , "Invoke-WebRequest" )]
2016-07-22 15:09:03 -07:00
[ string ]
$cmdletName ,
2018-01-09 15:43:25 -06:00
2016-07-22 15:09:03 -07:00
[ string ]
2017-09-07 22:53:04 -05:00
$uri = ( Get-WebListenerUrl -Test 'Get' )
2016-07-22 15:09:03 -07:00
)
2017-01-16 13:31:14 -08:00
2016-07-22 15:09:03 -07:00
$result = [ PSObject ] @ { Output = $null ; Error = $null }
2018-01-09 15:43:25 -06:00
try {
2016-07-22 15:09:03 -07:00
$headers = @ { Connection = 'close' }
2018-01-09 15:43:25 -06:00
if ( $cmdletName -eq "Invoke-WebRequest" ) {
2018-02-02 13:56:06 -06:00
$result . Output = Invoke-WebRequest -Uri $uri -Headers $headers
2018-01-09 15:43:25 -06:00
} else {
2018-02-02 13:56:06 -06:00
$result . Output = Invoke-RestMethod -Uri $uri -Headers $headers
2016-07-22 15:09:03 -07:00
}
2018-01-09 15:43:25 -06:00
} catch {
2016-07-22 15:09:03 -07:00
$result . Error = $_
}
return $result
}
# Returns test data for the given content type.
#
2018-01-09 15:43:25 -06:00
function GetTestData {
param (
2016-07-22 15:09:03 -07:00
[ ValidateSet ( "text/plain" , "application/xml" , "application/json" )]
[ String ]
$contentType
)
$testData = @ { ItemID = 987123 ; Name = 'TestData' }
2018-01-09 15:43:25 -06:00
if ( $contentType -eq "text/plain" ) {
2016-07-22 15:09:03 -07:00
$body = $testData | Out-String
2018-01-09 15:43:25 -06:00
} elseif ( $contentType -eq "application/xml" ) {
2016-07-22 15:09:03 -07:00
$body = '
<?xml version="1.0" encoding="utf-8"?>
<Objects>
<Object>
<ItemID>987123</ItemID>
<Name>TestData</Name>
</Object>
</Objects>
'
2018-01-09 15:43:25 -06:00
} else {
# "application/json"
2016-07-22 15:09:03 -07:00
$body = $testData | ConvertTo-Json -Compress
}
return $body
}
2018-01-09 15:43:25 -06:00
function ExecuteRedirectRequest {
2017-06-15 15:52:08 -07:00
param (
[ Parameter ( Mandatory )]
[ string ]
$uri ,
[ ValidateSet ( 'Invoke-WebRequest' , 'Invoke-RestMethod' )]
2018-01-09 15:43:25 -06:00
[ string ]
$Cmdlet = 'Invoke-WebRequest' ,
2017-06-15 15:52:08 -07:00
[ ValidateSet ( 'POST' , 'GET' )]
2018-01-09 15:43:25 -06:00
[ string ]
$Method = 'GET' ,
2017-06-15 15:52:08 -07:00
2018-01-09 15:43:25 -06:00
[ switch ]
$PreserveAuthorizationOnRedirect
2017-06-15 15:52:08 -07:00
)
$result = [ PSObject ] @ { Output = $null ; Error = $null ; Content = $null }
2018-01-09 15:43:25 -06:00
try {
2017-06-15 15:52:08 -07:00
$headers = @ { "Authorization" = "test" }
2018-01-09 15:43:25 -06:00
if ( $Cmdlet -eq 'Invoke-WebRequest' ) {
2018-02-02 13:56:06 -06:00
$result . Output = Invoke-WebRequest -Uri $uri -Headers $headers -PreserveAuthorizationOnRedirect: $PreserveAuthorizationOnRedirect . IsPresent -Method $Method
2017-06-15 15:52:08 -07:00
$result . Content = $result . Output . Content | ConvertFrom-Json
2018-01-09 15:43:25 -06:00
} else {
2018-02-02 13:56:06 -06:00
$result . Output = Invoke-RestMethod -Uri $uri -Headers $headers -PreserveAuthorizationOnRedirect: $PreserveAuthorizationOnRedirect . IsPresent -Method $Method
2017-06-15 15:52:08 -07:00
# NOTE: $result.Output should already be a PSObject (Invoke-RestMethod converts the returned json automatically)
# so simply reference $result.Output
$result . Content = $result . Output
2017-07-07 09:25:46 -07:00
}
2018-01-09 15:43:25 -06:00
} catch {
2017-06-15 15:52:08 -07:00
$result . Error = $_
}
return $result
}
2017-07-14 10:24:20 -07:00
# This function calls either Invoke-WebRequest or Invoke-RestMethod with the given uri
2017-09-04 19:45:51 -07:00
# using the custum headers and the optional SkipHeaderValidation switch.
2018-01-09 15:43:25 -06:00
function ExecuteRequestWithCustomHeaders {
2017-07-14 10:24:20 -07:00
param (
[ Parameter ( Mandatory )]
[ string ]
$Uri ,
[ ValidateSet ( 'Invoke-WebRequest' , 'Invoke-RestMethod' )]
2018-01-09 15:43:25 -06:00
[ string ]
$Cmdlet = 'Invoke-WebRequest' ,
2017-07-14 10:24:20 -07:00
[ Parameter ( Mandatory )]
[ ValidateNotNull ()]
2018-01-09 15:43:25 -06:00
[ Hashtable ]
$Headers ,
2017-07-14 10:24:20 -07:00
2018-01-09 15:43:25 -06:00
[ switch ]
$SkipHeaderValidation
2017-07-14 10:24:20 -07:00
)
$result = [ PSObject ] @ { Output = $null ; Error = $null ; Content = $null }
2018-01-09 15:43:25 -06:00
try {
if ( $Cmdlet -eq 'Invoke-WebRequest' ) {
2018-02-02 13:56:06 -06:00
$result . Output = Invoke-WebRequest -Uri $Uri -Headers $Headers -SkipHeaderValidation: $SkipHeaderValidation . IsPresent
2017-07-14 10:24:20 -07:00
$result . Content = $result . Output . Content | ConvertFrom-Json
2018-01-09 15:43:25 -06:00
} else {
2018-02-02 13:56:06 -06:00
$result . Output = Invoke-RestMethod -Uri $Uri -Headers $Headers -SkipHeaderValidation: $SkipHeaderValidation . IsPresent
2017-07-14 10:24:20 -07:00
# NOTE: $result.Output should already be a PSObject (Invoke-RestMethod converts the returned json automatically)
# so simply reference $result.Output
$result . Content = $result . Output
}
2018-01-09 15:43:25 -06:00
} catch {
2017-07-14 10:24:20 -07:00
$result . Error = $_
}
return $result
}
2017-08-04 11:10:24 -05:00
# This function calls either Invoke-WebRequest or Invoke-RestMethod with the given uri
2017-09-04 19:45:51 -07:00
# using the custom UserAgent and the optional SkipHeaderValidation switch.
2017-08-04 11:10:24 -05:00
function ExecuteRequestWithCustomUserAgent {
param (
[ Parameter ( Mandatory )]
[ string ]
$Uri ,
[ ValidateSet ( 'Invoke-WebRequest' , 'Invoke-RestMethod' )]
2018-01-09 15:43:25 -06:00
[ string ]
$Cmdlet = 'Invoke-WebRequest' ,
2017-08-04 11:10:24 -05:00
[ Parameter ( Mandatory )]
[ ValidateNotNull ()]
2018-01-09 15:43:25 -06:00
[ string ]
$UserAgent ,
2017-08-04 11:10:24 -05:00
2018-01-09 15:43:25 -06:00
[ switch ]
$SkipHeaderValidation
2017-08-04 11:10:24 -05:00
)
$result = [ PSObject ] @ { Output = $null ; Error = $null ; Content = $null }
try {
$Params = @ {
2017-08-16 16:25:16 -07:00
Uri = $Uri
TimeoutSec = 5
UserAgent = $UserAgent
2017-08-04 11:10:24 -05:00
SkipHeaderValidation = $SkipHeaderValidation . IsPresent
}
if ( $Cmdlet -eq 'Invoke-WebRequest' ) {
$result . Output = Invoke-WebRequest @Params
$result . Content = $result . Output . Content | ConvertFrom-Json
2018-01-09 15:43:25 -06:00
} else {
2017-08-04 11:10:24 -05:00
$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
}
2018-01-09 15:43:25 -06:00
} catch {
2017-08-04 11:10:24 -05:00
$result . Error = $_
}
return $result
}
2017-08-16 16:25:16 -07:00
# This function calls Invoke-WebRequest with the given uri
2018-01-09 15:43:25 -06:00
function ExecuteWebRequest {
2017-08-16 16:25:16 -07:00
param (
[ Parameter ( Mandatory )]
[ string ]
$Uri ,
2018-01-09 15:43:25 -06:00
[ switch ]
$UseBasicParsing
2017-08-16 16:25:16 -07:00
)
2018-01-09 15:43:25 -06:00
$result = [ PSObject ] @ { Output = $null ; Error = $null ; Content = $null }
2017-08-16 16:25:16 -07:00
2018-01-09 15:43:25 -06:00
try {
2018-02-02 13:56:06 -06:00
$result . Output = Invoke-WebRequest -Uri $Uri -UseBasicParsing: $UseBasicParsing . IsPresent
2017-08-16 16:25:16 -07:00
$result . Content = $result . Output . Content
2018-01-09 15:43:25 -06:00
} catch {
2017-08-16 16:25:16 -07:00
$result . Error = $_
}
return $result
}
2017-08-31 10:47:54 -07:00
[ 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.
2018-01-09 15:43:25 -06:00
function ExecuteRestMethod {
param (
2017-08-31 10:47:54 -07:00
[ Parameter ( Mandatory )]
[ string ]
$Uri ,
2018-01-09 15:43:25 -06:00
[ switch ]
$UseBasicParsing
2017-08-31 10:47:54 -07:00
)
$result = @ { Output = $null ; Error = $null ; Encoding = $null ; Content = $null }
$verbosePreferenceSave = $VerbosePreference
$VerbosePreference = 'Continue'
2018-01-09 15:43:25 -06:00
try {
2017-08-31 10:47:54 -07:00
$verboseFile = Join-Path $TestDrive -ChildPath ExecuteRestMethod . verbose . txt
2018-02-02 13:56:06 -06:00
$result . Output = Invoke-RestMethod -Uri $Uri -UseBasicParsing: $UseBasicParsing . IsPresent -Verbose 4 > $verboseFile
2017-08-31 10:47:54 -07:00
$result . Content = $result . Output
2018-01-09 15:43:25 -06:00
if ( Test-Path -Path $verboseFile ) {
2017-08-31 10:47:54 -07:00
$result . Verbose = Get-Content -Path $verboseFile
2018-01-09 15:43:25 -06:00
foreach ( $item in $result . Verbose ) {
2017-08-31 10:47:54 -07:00
$line = $item . Trim ()
2018-01-09 15:43:25 -06:00
if ( $line . StartsWith ( $verboseEncodingPrefix )) {
2017-08-31 10:47:54 -07:00
$encodingName = $item . SubString ( $verboseEncodingPrefix . Length ). Trim ()
$result . Encoding = [ System.Text.Encoding ]:: GetEncoding ( $encodingName )
break
}
}
2018-01-09 15:43:25 -06:00
if ( $result . Encoding -eq $null ) {
2017-08-31 10:47:54 -07:00
throw "Encoding not found in verbose output. Lines: $( $result . Verbose . Count ) Content: $( $result . Verbose ) "
}
}
2018-01-09 15:43:25 -06:00
if ( $result . Verbose -eq $null ) {
2017-08-31 10:47:54 -07:00
throw "No verbose output was found"
}
2018-01-09 15:43:25 -06:00
} catch {
2017-08-31 10:47:54 -07:00
$result . Error = $_ | select-object * | Out-String
2018-01-09 15:43:25 -06:00
} finally {
2017-08-31 10:47:54 -07:00
$VerbosePreference = $verbosePreferenceSave
2018-01-09 15:43:25 -06:00
if ( Test-Path -Path $verboseFile ) {
2017-08-31 10:47:54 -07:00
Remove-Item -Path $verboseFile -ErrorAction SilentlyContinue
}
}
return $result
}
2018-01-09 15:43:25 -06:00
function GetMultipartBody {
param (
[ Switch ]
$String ,
[ Switch ]
$File
2017-09-12 11:41:36 -05:00
)
$multipartContent = [ System.Net.Http.MultipartFormDataContent ]:: new ()
2018-01-09 15:43:25 -06:00
if ( $String . IsPresent ) {
2017-09-12 11:41:36 -05:00
$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 )
}
2018-01-09 15:43:25 -06:00
if ( $File . IsPresent ) {
2017-09-12 11:41:36 -05:00
$multipartFile = Join-Path $TestDrive 'multipart.txt'
"TestContent" | Set-Content $multipartFile
$FileStream = [ System.IO.FileStream ]:: new ( $multipartFile , [ System.IO.FileMode ]:: Open )
$fileHeader = [ System.Net.Http.Headers.ContentDispositionHeaderValue ]:: new ( "form-data" )
$fileHeader . Name = "TestFile"
$fileHeader . FileName = 'multipart.txt'
$fileContent = [ System.Net.Http.StreamContent ]:: new ( $FileStream )
$fileContent . Headers . ContentDisposition = $fileHeader
$fileContent . Headers . ContentType = [ System.Net.Http.Headers.MediaTypeHeaderValue ]:: Parse ( "text/plain" )
$multipartContent . Add ( $fileContent )
}
# unary comma required to prevent $multipartContent from being unwrapped/enumerated
2018-01-09 15:43:25 -06:00
return , $multipartContent
2017-09-12 11:41:36 -05:00
}
2017-06-15 15:52:08 -07:00
<#
Defines the list of redirect codes to test as well as the
expected Method when the redirection is handled.
2018-06-08 05:49:03 +00:00
See https://docs.microsoft.com/previous-versions/windows/apps/f92ssyy1(v=vs.105)
2017-06-15 15:52:08 -07:00
for additonal details.
#>
$redirectTests = @ (
2018-05-14 13:37:30 -05:00
@ { redirectType = 'MultipleChoices' ; redirectedMethod = 'GET' }
@ { redirectType = 'Ambiguous' ; redirectedMethod = 'GET' } # Synonym for MultipleChoices
2017-06-15 15:52:08 -07:00
2018-01-09 15:43:25 -06:00
@ { redirectType = 'Moved' ; redirectedMethod = 'GET' }
@ { redirectType = 'MovedPermanently' ; redirectedMethod = 'GET' } # Synonym for Moved
2017-06-15 15:52:08 -07:00
2018-01-09 15:43:25 -06:00
@ { redirectType = 'Found' ; redirectedMethod = 'GET' }
@ { redirectType = 'Redirect' ; redirectedMethod = 'GET' } # Synonym for Found
2017-06-15 15:52:08 -07:00
2018-01-09 15:43:25 -06:00
@ { redirectType = 'redirectMethod' ; redirectedMethod = 'GET' }
@ { redirectType = 'SeeOther' ; redirectedMethod = 'GET' } # Synonym for RedirectMethod
2017-06-15 15:52:08 -07:00
2018-05-14 13:37:30 -05:00
@ { redirectType = 'TemporaryRedirect' ; redirectedMethod = 'POST' }
@ { redirectType = 'RedirectKeepVerb' ; redirectedMethod = 'POST' } # Synonym for TemporaryRedirect
2018-03-09 08:28:20 -08:00
@ { redirectType = 'relative' ; redirectedMethod = 'GET' }
2017-06-15 15:52:08 -07:00
)
2016-07-25 14:01:41 -07:00
Describe "Invoke-WebRequest tests" -Tags "Feature" {
2017-05-24 15:54:02 -07:00
BeforeAll {
2017-08-31 04:30:35 -05:00
$WebListener = Start-WebListener
2017-05-24 15:54:02 -07:00
}
2016-07-22 15:09:03 -07:00
# Validate the output of Invoke-WebRequest
#
2018-01-09 15:43:25 -06:00
function ValidateResponse {
2016-07-22 15:09:03 -07:00
param ( $response )
2018-03-01 23:00:00 -06:00
$response . Error | Should -Be $null
2016-07-22 15:09:03 -07:00
2016-08-25 17:49:32 +00:00
# A successful call returns: Status = 200, and StatusDescription = "OK"
2018-03-01 23:00:00 -06:00
$response . Output . StatusDescription | Should -Match "OK"
$response . Output . StatusCode | Should -Be 200
2016-07-22 15:09:03 -07:00
# Make sure the response contains the following properties:
2018-03-01 23:00:00 -06:00
$response . Output . RawContent | Should -Not -Be $null
$response . Output . Headers | Should -Not -Be $null
$response . Output . RawContent | Should -Not -Be $null
$response . Output . RawContentLength | Should -Not -Be $null
$response . Output . Content | Should -Not -Be $null
2016-07-22 15:09:03 -07:00
}
2017-10-06 22:59:31 +02:00
#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'
2018-02-02 13:56:06 -06:00
$command = "Invoke-WebRequest -Uri ' $uri '"
2017-10-06 22:59:31 +02:00
$result = ExecuteWebCommand -command $command
ValidateResponse -response $result
# Validate response content
$jsonContent = $result . Output . Content | ConvertFrom-Json
2018-03-01 23:00:00 -06:00
$jsonContent . headers . 'User-Agent' | Should -MatchExactly '.*\(Macintosh;.*\) PowerShell\/\d+\.\d+\.\d+.*'
2017-10-06 22:59:31 +02:00
}
It "Invoke-WebRequest returns Correct User-Agent on Linux" -Skip: (! $IsLinux ) {
$uri = Get-WebListenerUrl -Test 'Get'
2018-02-02 13:56:06 -06:00
$command = "Invoke-WebRequest -Uri ' $uri '"
2017-10-06 22:59:31 +02:00
$result = ExecuteWebCommand -command $command
ValidateResponse -response $result
# Validate response content
$jsonContent = $result . Output . Content | ConvertFrom-Json
2018-03-01 23:00:00 -06:00
$jsonContent . headers . 'User-Agent' | Should -MatchExactly '.*\(Linux;.*\) PowerShell\/\d+\.\d+\.\d+.*'
2017-10-06 22:59:31 +02:00
}
It "Invoke-WebRequest returns Correct User-Agent on Windows" -Skip: (! $IsWindows ) {
2017-09-14 00:23:29 -05:00
$uri = Get-WebListenerUrl -Test 'Get'
2018-02-02 13:56:06 -06:00
$command = "Invoke-WebRequest -Uri ' $uri '"
2017-01-16 13:31:14 -08:00
2016-07-22 15:09:03 -07:00
$result = ExecuteWebCommand -command $command
ValidateResponse -response $result
# Validate response content
$jsonContent = $result . Output . Content | ConvertFrom-Json
2018-03-01 23:00:00 -06:00
$jsonContent . headers . 'User-Agent' | Should -MatchExactly '.*\(Windows NT \d+\.\d*;.*\) PowerShell\/\d+\.\d+\.\d+.*'
2016-07-22 15:09:03 -07:00
}
It "Invoke-WebRequest returns headers dictionary" {
2017-09-14 00:22:52 -05:00
$uri = Get-WebListenerUrl -Test 'Get'
2018-02-02 13:56:06 -06:00
$command = "Invoke-WebRequest -Uri ' $uri '"
2017-01-16 13:31:14 -08:00
2016-07-22 15:09:03 -07:00
$result = ExecuteWebCommand -command $command
ValidateResponse -response $result
# Validate response content
$jsonContent = $result . Output . Content | ConvertFrom-Json
2018-03-01 23:00:00 -06:00
$jsonContent . headers . Host | Should -Be $Uri . Authority
2016-07-22 15:09:03 -07:00
}
It "Validate Invoke-WebRequest -DisableKeepAlive" {
# Operation options
2017-09-07 22:53:04 -05:00
$uri = Get-WebListenerUrl -Test 'Get'
2018-02-02 13:56:06 -06:00
$command = "Invoke-WebRequest -Uri $uri -DisableKeepAlive"
2016-07-22 15:09:03 -07:00
$result = ExecuteWebCommand -command $command
ValidateResponse -response $result
2018-03-01 23:00:00 -06:00
$result . Output . Headers . Connection | Should -Be "Close"
2016-07-22 15:09:03 -07:00
}
It "Validate Invoke-WebRequest -MaximumRedirection" {
2017-09-25 14:50:15 -05:00
$uri = Get-WebListenerUrl -Test 'Redirect' -TestValue '3'
2017-09-18 11:26:01 -05:00
$command = "Invoke-WebRequest -Uri ' $uri ' -MaximumRedirection 4"
2017-01-16 13:31:14 -08:00
2016-07-22 15:09:03 -07:00
$result = ExecuteWebCommand -command $command
ValidateResponse -response $result
# Validate response content
$jsonContent = $result . Output . Content | ConvertFrom-Json
2018-03-01 23:00:00 -06:00
$jsonContent . headers . Host | Should -Match $uri . Authority
2016-07-22 15:09:03 -07:00
}
2017-07-07 09:25:46 -07:00
2016-07-22 15:09:03 -07:00
It "Validate Invoke-WebRequest error for -MaximumRedirection" {
2017-09-25 14:50:15 -05:00
$uri = Get-WebListenerUrl -Test 'Redirect' -TestValue '3'
2017-09-18 11:26:01 -05:00
$command = "Invoke-WebRequest -Uri ' $uri ' -MaximumRedirection 2"
2017-01-16 13:31:14 -08:00
2016-07-22 15:09:03 -07:00
$result = ExecuteWebCommand -command $command
2018-03-01 23:00:00 -06:00
$result . Error . FullyQualifiedErrorId | Should -Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand"
2016-07-22 15:09:03 -07:00
}
2017-07-07 09:25:46 -07:00
2016-07-22 15:09:03 -07:00
It "Invoke-WebRequest supports request that returns page containing UTF-8 data." {
2017-09-25 14:50:15 -05:00
$uri = Get-WebListenerUrl -Test 'Encoding' -TestValue 'Utf8'
2017-09-22 12:09:49 -05:00
$command = "Invoke-WebRequest -Uri ' $uri '"
2017-01-16 13:31:14 -08:00
2016-07-22 15:09:03 -07:00
$result = ExecuteWebCommand -command $command
ValidateResponse -response $result
2018-03-01 23:00:00 -06:00
$Result . Output . Encoding . BodyName | Should -Be 'utf-8'
$Result . Output . Content | Should -Match '⡌⠁⠧⠑ ⠼⠁⠒ ⡍⠜⠇⠑⠹⠰⠎ ⡣⠕⠌'
2016-07-22 15:09:03 -07:00
}
It "Invoke-WebRequest validate timeout option" {
2017-10-06 13:17:52 -05:00
$uri = Get-WebListenerUrl -Test 'Delay' -TestValue '5'
$command = "Invoke-WebRequest -Uri ' $uri ' -TimeoutSec 2"
2017-01-16 13:31:14 -08:00
2016-07-22 15:09:03 -07:00
$result = ExecuteWebCommand -command $command
2018-03-01 23:00:00 -06:00
$result . Error . FullyQualifiedErrorId | Should -Be "System.Threading.Tasks.TaskCanceledException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand"
2017-03-28 21:25:13 +02:00
}
2016-07-22 15:09:03 -07:00
2017-03-28 21:25:13 +02:00
It "Validate Invoke-WebRequest error with -Proxy and -NoProxy option" {
2017-09-25 14:50:15 -05:00
$uri = Get-WebListenerUrl -Test 'Delay' -TestValue '10'
2018-01-31 22:26:05 -06:00
$command = "Invoke-WebRequest -Uri ' $uri ' -Proxy 'http://127.0.0.1:8080' -NoProxy -TimeoutSec 2"
2017-03-28 21:25:13 +02:00
$result = ExecuteWebCommand -command $command
2018-03-01 23:00:00 -06:00
$result . Error . FullyQualifiedErrorId | Should -Be "AmbiguousParameterSet,Microsoft.PowerShell.Commands.InvokeWebRequestCommand"
2017-03-28 21:25:13 +02:00
}
$testCase = @ (
2018-05-08 09:15:49 +05:00
@ { proxy_address = ( Get-WebListenerUrl ). Authority ; name = 'HTTP proxy' ; protocol = 'http' }
@ { proxy_address = ( Get-WebListenerUrl -https ). Authority ; name = 'HTTPS proxy' ; protocol = 'https' }
2017-03-28 21:25:13 +02:00
)
2018-05-03 04:58:39 +05:00
It "Validate Invoke-WebRequest with -Proxy option set - '<name>'" -TestCases $testCase {
2017-03-28 21:25:13 +02:00
param ( $proxy_address , $name , $protocol )
2018-05-03 04:58:39 +05:00
# use external url, but with proxy the external url should not actually be called
$command = "Invoke-WebRequest -Uri ${protocol} ://httpbin.org -Proxy ' ${protocol} :// ${proxy_address} ' -SkipCertificateCheck"
2017-03-28 21:25:13 +02:00
$result = ExecuteWebCommand -command $command
2018-05-03 04:58:39 +05:00
$command = "Invoke-WebRequest -Uri ' ${protocol} :// ${proxy_address} ' -NoProxy"
$expectedResult = ExecuteWebCommand -command $command
$result . Output . Content | Should -BeExactly $expectedResult . Output . Content
2017-03-28 21:25:13 +02:00
}
2016-07-22 15:09:03 -07:00
# Perform the following operation for Invoke-WebRequest
# gzip Returns gzip-encoded data.
# deflate Returns deflate-encoded data.
2017-01-16 13:31:14 -08:00
# $dataEncodings = @("Chunked", "Compress", "Deflate", "GZip", "Identity")
2017-10-02 15:46:53 -05:00
# Note: These are the supported options, but we do not have a web service to test them all.
It "Invoke-WebRequest supports request that returns <DataEncoding>-encoded data." -TestCases @ (
@ { DataEncoding = "gzip" }
@ { DataEncoding = "deflate" }
) {
param ( $dataEncoding )
$uri = Get-WebListenerUrl -Test 'Compression' -TestValue $dataEncoding
$command = "Invoke-WebRequest -Uri ' $uri '"
2016-07-22 15:09:03 -07:00
2017-10-02 15:46:53 -05:00
$result = ExecuteWebCommand -command $command
ValidateResponse -response $result
2017-01-16 13:31:14 -08:00
2017-10-02 15:46:53 -05:00
# Validate response content
2018-03-01 23:00:00 -06:00
$result . Output . Headers . 'Content-Encoding' [ 0 ] | Should -BeExactly $dataEncoding
2017-10-27 05:12:19 +08:00
$jsonContent = $result . Output . Content | ConvertFrom-Json
2018-03-01 23:00:00 -06:00
$jsonContent . Headers . Host | Should -BeExactly $uri . Authority
2016-07-22 15:09:03 -07:00
}
# Perform the following operation for Invoke-WebRequest using the following content types: "text/plain", "application/xml", "application/xml"
# post Returns POST data.
# patch Returns PATCH data.
# put Returns PUT data.
# delete Returns DELETE data
2017-12-13 09:28:05 -06:00
$testMethods = @ ( "POST" , "PATCH" , "PUT" , "DELETE" )
2016-07-22 15:09:03 -07:00
$contentTypes = @ ( "text/plain" , "application/xml" , "application/json" )
2018-01-09 15:43:25 -06:00
foreach ( $contentType in $contentTypes ) {
foreach ( $method in $testMethods ) {
2016-07-22 15:09:03 -07:00
# Operation options
2017-12-13 09:28:05 -06:00
$uri = Get-WebListenerUrl -Test $method
2016-07-22 15:09:03 -07:00
$body = GetTestData -contentType $contentType
2017-12-13 09:28:05 -06:00
$command = "Invoke-WebRequest -Uri $uri -Body ' $body ' -Method $method -ContentType $contentType "
2016-07-22 15:09:03 -07:00
2017-12-13 09:28:05 -06:00
It "Invoke-WebRequest -Uri $uri -Method $method -ContentType $contentType -Body [body data]" {
2016-07-22 15:09:03 -07:00
$result = ExecuteWebCommand -command $command
ValidateResponse -response $result
# Validate response content
$jsonContent = $result . Output . Content | ConvertFrom-Json
2018-03-01 23:00:00 -06:00
$jsonContent . url | Should -Match $uri
$jsonContent . headers . 'Content-Type' | Should -Match $contentType
2017-12-13 09:28:05 -06:00
# Validate that the response Content.data field is the same as what we sent.
2018-03-01 23:00:00 -06:00
$jsonContent . data | Should -Be $body
2017-01-16 13:31:14 -08:00
}
2016-07-22 15:09:03 -07:00
}
}
It "Validate Invoke-WebRequest -Headers --> Set KeepAlive to false via headers" {
2017-09-07 22:53:04 -05:00
$uri = Get-WebListenerUrl -Test 'Get'
2016-07-22 15:09:03 -07:00
$result = ExecuteRequestWithHeaders -cmdletName Invoke-WebRequest -uri $uri
ValidateResponse -response $result
2018-03-01 23:00:00 -06:00
$result . Output . Headers . Connection | Should -Be "Close"
2016-07-22 15:09:03 -07:00
}
# Validate all available user agents for Invoke-WebRequest
2018-01-09 15:43:25 -06:00
$agents = @ {
InternetExplorer = "MSIE 9.0"
Chrome = "Chrome"
Opera = "Opera"
Safari = "Safari"
FireFox = "Firefox"
}
2016-07-22 15:09:03 -07:00
2018-01-09 15:43:25 -06:00
foreach ( $agentName in $agents . Keys ) {
2016-07-22 15:09:03 -07:00
$expectedAgent = $agents [ $agentName ]
2017-09-07 22:53:04 -05:00
$uri = Get-WebListenerUrl -Test 'Get'
2016-07-22 15:09:03 -07:00
$userAgent = "[Microsoft.PowerShell.Commands.PSUserAgent]:: $agentName "
2018-02-02 13:56:06 -06:00
$command = "Invoke-WebRequest -Uri $uri -UserAgent ( $userAgent ) "
2016-07-22 15:09:03 -07:00
It "Validate Invoke-WebRequest UserAgent. Execute--> $command " {
$result = ExecuteWebCommand -command $command
ValidateResponse -response $result
# Validate response content
$jsonContent = $result . Output . Content | ConvertFrom-Json
2018-03-01 23:00:00 -06:00
$jsonContent . headers . Host | Should -Be $uri . Authority
$jsonContent . headers . 'User-Agent' | Should -Match $expectedAgent
2016-07-22 15:09:03 -07:00
}
}
It "Validate Invoke-WebRequest -OutFile" {
2017-09-07 22:53:04 -05:00
$uri = Get-WebListenerUrl -Test 'Get'
2016-07-22 15:09:03 -07:00
$result = ExecuteRequestWithOutFile -cmdletName "Invoke-WebRequest" -uri $uri
$jsonContent = $result . Output | ConvertFrom-Json
2018-03-01 23:00:00 -06:00
$jsonContent . headers . Host | Should -Be $uri . Authority
2016-07-22 15:09:03 -07:00
}
2016-11-15 08:35:07 +11:00
It "Validate Invoke-WebRequest handles missing Content-Type in response header" {
#Validate that exception is not thrown when response headers are missing Content-Type.
2017-10-18 14:24:02 -05:00
$uri = Get-WebListenerUrl -Test 'ResponseHeaders' -Query @ { 'Content-Type' = '' }
$command = "Invoke-WebRequest -Uri ' $uri '"
2016-11-15 08:35:07 +11:00
$result = ExecuteWebCommand -command $command
2018-03-01 23:00:00 -06:00
$result . Error | Should -BeNullOrEmpty
2016-11-15 08:35:07 +11:00
}
2017-02-24 22:29:11 -08:00
2017-03-06 19:41:14 +00:00
It "Validate Invoke-WebRequest StandardMethod and CustomMethod parameter sets" {
2017-12-18 22:29:46 -06:00
$uri = Get-WebListenerUrl -Test 'Get'
2017-03-06 19:41:14 +00:00
#Validate that parameter sets are functioning correctly
$errorId = "AmbiguousParameterSet,Microsoft.PowerShell.Commands.InvokeWebRequestCommand"
2018-05-17 14:42:04 -07:00
{ Invoke-WebRequest -Uri $uri -Method GET -CustomMethod TEST } | Should -Throw -ErrorId $errorId
2017-03-06 19:41:14 +00:00
}
It "Validate Invoke-WebRequest CustomMethod method is used" {
2017-12-18 22:29:46 -06:00
$uri = Get-WebListenerUrl -Test 'Get'
$command = "Invoke-WebRequest -Uri ' $uri ' -CustomMethod TEST"
2017-03-06 19:41:14 +00:00
$result = ExecuteWebCommand -command $command
2018-03-01 23:00:00 -06:00
$result . Error | Should -BeNullOrEmpty
( $result . Output . Content | ConvertFrom-Json ). method | Should -Be "TEST"
2017-03-06 19:41:14 +00:00
}
It "Validate Invoke-WebRequest default ContentType for CustomMethod POST" {
2017-12-13 09:28:05 -06:00
$uri = Get-WebListenerUrl -Test 'Post'
$command = "Invoke-WebRequest -Uri ' $uri ' -CustomMethod POST -Body 'testparam=testvalue'"
2017-03-06 19:41:14 +00:00
$result = ExecuteWebCommand -command $command
2017-12-13 09:28:05 -06:00
$jsonResult = $result . Output . Content | ConvertFrom-Json
2018-03-01 23:00:00 -06:00
$jsonResult . form . testparam | Should -Be "testvalue"
$jsonResult . Headers . 'Content-Type' | Should -Be "application/x-www-form-urlencoded"
2017-03-06 19:41:14 +00:00
}
It "Validate Invoke-WebRequest body is converted to query params for CustomMethod GET" {
2017-09-07 22:53:04 -05:00
$uri = Get-WebListenerUrl -Test 'Get'
$command = "Invoke-WebRequest -Uri ' $uri ' -CustomMethod GET -Body @{'testparam'='testvalue'}"
2017-03-06 19:41:14 +00:00
$result = ExecuteWebCommand -command $command
2018-03-01 23:00:00 -06:00
( $result . Output . Content | ConvertFrom-Json ). args . testparam | Should -Be "testvalue"
2017-03-06 19:41:14 +00:00
}
2017-07-07 09:25:46 -07:00
2017-02-24 22:29:11 -08:00
It "Validate Invoke-WebRequest returns HTTP errors in exception" {
2017-12-13 09:28:05 -06:00
$query = @ {
2018-01-09 15:43:25 -06:00
body = "I am a teapot!!!"
statuscode = 418
2017-12-13 09:28:05 -06:00
responsephrase = "I am a teapot"
}
2018-01-09 15:43:25 -06:00
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
2017-12-13 09:28:05 -06:00
$command = "Invoke-WebRequest -Uri ' $uri '"
2017-02-24 22:29:11 -08:00
$result = ExecuteWebCommand -command $command
2018-03-01 23:00:00 -06:00
$result . Error . ErrorDetails . Message | Should -Be $query . body
$result . Error . Exception | Should -BeOfType 'Microsoft.PowerShell.Commands.HttpResponseException'
$result . Error . Exception . Response . StatusCode | Should -Be 418
$result . Error . Exception . Response . ReasonPhrase | Should -Be $query . responsephrase
$result . Error . Exception . Message | Should -Match ": 418 \( $( $query . responsephrase ) \)\."
$result . Error . FullyQualifiedErrorId | Should -Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand"
2017-02-24 22:29:11 -08:00
}
2017-03-16 17:59:15 -07:00
2017-05-24 15:54:02 -07:00
It "Validate Invoke-WebRequest returns empty RelationLink property if there is no Link Header" {
2017-12-04 11:52:14 -06:00
$uri = $uri = Get-WebListenerUrl -Test 'Get'
$command = "Invoke-WebRequest -Uri ' $uri '"
2017-05-24 15:54:02 -07:00
$result = ExecuteWebCommand -command $command
2018-03-01 23:00:00 -06:00
$result . Output . RelationLink . Count | Should -Be 0
2017-05-24 15:54:02 -07:00
}
It "Validate Invoke-WebRequest returns valid RelationLink property with absolute uris if Link Header is present" {
2018-01-16 11:23:22 -06:00
$uri = Get-WebListenerUrl -Test 'Link' -Query @ { maxlinks = 5 ; linknumber = 2 }
2017-11-01 16:06:30 -05:00
$command = "Invoke-WebRequest -Uri ' $uri '"
$result = ExecuteWebCommand -command $command
2018-01-16 11:23:22 -06:00
2018-03-01 23:00:00 -06:00
$result . Output . RelationLink . Count | Should -BeExactly 5
2018-01-16 11:23:22 -06:00
$baseUri = Get-WebListenerUrl -Test 'Link'
2018-03-01 23:00:00 -06:00
$result . Output . RelationLink [ "next" ] | Should -BeExactly " ${baseUri} ?maxlinks=5&linknumber=3&type=default"
$result . Output . RelationLink [ "last" ] | Should -BeExactly " ${baseUri} ?maxlinks=5&linknumber=5&type=default"
$result . Output . RelationLink [ "prev" ] | Should -BeExactly " ${baseUri} ?maxlinks=5&linknumber=1&type=default"
$result . Output . RelationLink [ "first" ] | Should -BeExactly " ${baseUri} ?maxlinks=5&linknumber=1&type=default"
$result . Output . RelationLink [ "self" ] | Should -BeExactly " ${baseUri} ?maxlinks=5&linknumber=2&type=default"
2018-01-16 11:23:22 -06:00
}
It "Validate Invoke-WebRequest returns valid RelationLink property with absolute uris if Multiple Link Headers are present" {
$uri = Get-WebListenerUrl -Test 'Link' -Query @ { maxlinks = 5 ; linknumber = 2 ; type = 'multiple' }
$command = "Invoke-WebRequest -Uri ' $uri '"
$result = ExecuteWebCommand -command $command
2018-03-01 23:00:00 -06:00
$result . Output . RelationLink . Count | Should -BeExactly 5
2018-01-16 11:23:22 -06:00
$baseUri = Get-WebListenerUrl -Test 'Link'
2018-03-01 23:00:00 -06:00
$result . Output . RelationLink [ "next" ] | Should -BeExactly " ${baseUri} ?maxlinks=5&linknumber=3&type=multiple"
$result . Output . RelationLink [ "last" ] | Should -BeExactly " ${baseUri} ?maxlinks=5&linknumber=5&type=multiple"
$result . Output . RelationLink [ "prev" ] | Should -BeExactly " ${baseUri} ?maxlinks=5&linknumber=1&type=multiple"
$result . Output . RelationLink [ "first" ] | Should -BeExactly " ${baseUri} ?maxlinks=5&linknumber=1&type=multiple"
$result . Output . RelationLink [ "self" ] | Should -BeExactly " ${baseUri} ?maxlinks=5&linknumber=2&type=multiple"
2017-05-24 15:54:02 -07:00
}
2018-03-08 15:54:42 -08:00
It "Validate Invoke-WebRequest RelationLink keys are treated case-insensitively" {
$uri = Get-WebListenerUrl -Test 'Link' -Query @ { maxlinks = 5 ; linknumber = 2 ; type = 'multiple' }
$command = "Invoke-WebRequest -Uri ' $uri '"
$result = ExecuteWebCommand -command $command
$result . Output . RelationLink [ "next" ] | Should -Not -BeNullOrEmpty
$result . Output . RelationLink [ "next" ] | Should -BeExactly $result . Output . RelationLink [ "Next" ]
$result . Output . RelationLink [ "last" ] | Should -Not -BeNullOrEmpty
$result . Output . RelationLink [ "last" ] | Should -BeExactly $result . Output . RelationLink [ "LAST" ]
$result . Output . RelationLink [ "prev" ] | Should -Not -BeNullOrEmpty
$result . Output . RelationLink [ "prev" ] | Should -BeExactly $result . Output . RelationLink [ "preV" ]
$result . Output . RelationLink [ "first" ] | Should -Not -BeNullOrEmpty
$result . Output . RelationLink [ "first" ] | Should -BeExactly $result . Output . RelationLink [ "FiRsT" ]
$result . Output . RelationLink [ "self" ] | Should -Not -BeNullOrEmpty
$result . Output . RelationLink [ "self" ] | Should -BeExactly $result . Output . RelationLink [ "self" ]
}
2017-07-07 09:25:46 -07:00
It "Validate Invoke-WebRequest quietly ignores invalid Link Headers in RelationLink property: <type>" -TestCases @ (
2017-05-24 15:54:02 -07:00
@ { type = "noUrl" }
@ { type = "malformed" }
2017-07-07 09:25:46 -07:00
@ { type = "noRel" }
2017-05-24 15:54:02 -07:00
) {
param ( $type )
2018-01-16 11:23:22 -06:00
$uri = Get-WebListenerUrl -Test 'Link' -Query @ { type = $type }
$command = "Invoke-WebRequest -Uri ' $uri '"
2017-05-24 15:54:02 -07:00
$result = ExecuteWebCommand -command $command
2018-01-16 11:23:22 -06:00
2018-03-01 23:00:00 -06:00
$result . Output . RelationLink . Count | Should -BeExactly 3
2018-01-16 11:23:22 -06:00
$baseUri = Get-WebListenerUrl -Test 'Link'
2018-03-01 23:00:00 -06:00
$result . Output . RelationLink [ "last" ] | Should -BeExactly " ${baseUri} ?maxlinks=3&linknumber=3&type= ${type} "
$result . Output . RelationLink [ "first" ] | Should -BeExactly " ${baseUri} ?maxlinks=3&linknumber=1&type= ${type} "
$result . Output . RelationLink [ "self" ] | Should -BeExactly " ${baseUri} ?maxlinks=3&linknumber=1&type= ${type} "
2017-05-24 15:54:02 -07:00
}
2017-06-15 15:52:08 -07:00
#region Redirect tests
2018-05-14 13:37:30 -05:00
It "Validates Invoke-WebRequest with -PreserveAuthorizationOnRedirect preserves the authorization header on redirect: <redirectType> <redirectedMethod>" -TestCases $redirectTests {
2017-06-15 15:52:08 -07:00
param ( $redirectType , $redirectedMethod )
2018-01-13 01:51:04 -06:00
$uri = Get-WebListenerUrl -Test 'Redirect' -Query @ { type = $redirectType }
$response = ExecuteRedirectRequest -Uri $uri -PreserveAuthorizationOnRedirect
2017-07-07 09:25:46 -07:00
2018-03-01 23:00:00 -06:00
$response . Error | Should -BeNullOrEmpty
$response . Content . Headers . "Authorization" | Should -BeExactly "test"
2017-06-15 15:52:08 -07:00
}
2018-05-14 13:37:30 -05:00
It "Validates Invoke-WebRequest preserves the authorization header on multiple redirects: <redirectType>" -TestCases $redirectTests {
2017-06-15 15:52:08 -07:00
param ( $redirectType )
2018-01-13 01:51:04 -06:00
$uri = Get-WebListenerUrl -Test 'Redirect' -TestValue 3 -Query @ { type = $redirectType }
$response = ExecuteRedirectRequest -Uri $uri -PreserveAuthorizationOnRedirect
2017-06-15 15:52:08 -07:00
2018-03-01 23:00:00 -06:00
$response . Error | Should -BeNullOrEmpty
$response . Content . Headers . "Authorization" | Should -BeExactly "test"
2017-06-15 15:52:08 -07:00
}
2018-05-14 13:37:30 -05:00
It "Validates Invoke-WebRequest strips the authorization header on various redirects: <redirectType>" -TestCases $redirectTests {
2017-06-15 15:52:08 -07:00
param ( $redirectType )
2018-01-13 01:51:04 -06:00
$uri = Get-WebListenerUrl -Test 'Redirect' -Query @ { type = $redirectType }
$response = ExecuteRedirectRequest -Uri $uri
2017-06-15 15:52:08 -07:00
2018-03-01 23:00:00 -06:00
$response . Error | Should -BeNullOrEmpty
2017-06-15 15:52:08 -07:00
# ensure user-agent is present (i.e., no false positives )
2018-03-01 23:00:00 -06:00
$response . Content . Headers . "User-Agent" | Should -Not -BeNullOrEmpty
2017-06-15 15:52:08 -07:00
# ensure Authorization header has been removed.
2018-03-01 23:00:00 -06:00
$response . Content . Headers . "Authorization" | Should -BeNullOrEmpty
2017-06-15 15:52:08 -07:00
}
# NOTE: Only testing redirection of POST -> GET for unique underlying values of HttpStatusCode.
# Some names overlap in underlying value.
2018-05-14 13:37:30 -05:00
It "Validates Invoke-WebRequest strips the authorization header redirects and switches from POST to GET when it handles the redirect: <redirectType> <redirectedMethod>" -TestCases $redirectTests {
2017-06-15 15:52:08 -07:00
param ( $redirectType , $redirectedMethod )
2018-01-13 01:51:04 -06:00
$uri = Get-WebListenerUrl -Test 'Redirect' -Query @ { type = $redirectType }
$response = ExecuteRedirectRequest -Uri $uri -Method 'POST'
2017-07-07 09:25:46 -07:00
2018-03-01 23:00:00 -06:00
$response . Error | Should -BeNullOrEmpty
2017-06-15 15:52:08 -07:00
# ensure user-agent is present (i.e., no false positives )
2018-03-01 23:00:00 -06:00
$response . Content . Headers . "User-Agent" | Should -Not -BeNullOrEmpty
2017-06-15 15:52:08 -07:00
# ensure Authorization header has been removed.
2018-03-01 23:00:00 -06:00
$response . Content . Headers . "Authorization" | Should -BeNullOrEmpty
2017-06-15 15:52:08 -07:00
# ensure POST was changed to GET for selected redirections and remains as POST for others.
2018-03-01 23:00:00 -06:00
$response . Content . Method | Should -Be $redirectedMethod
2017-07-07 09:25:46 -07:00
}
2017-06-15 15:52:08 -07:00
2018-05-14 13:37:30 -05:00
It "Validates Invoke-WebRequest -PreserveAuthorizationOnRedirect keeps the authorization header redirects and switches from POST to GET when it handles the redirect: <redirectType> <redirectedMethod>" -TestCases $redirectTests {
param ( $redirectType , $redirectedMethod )
$uri = Get-WebListenerUrl -Test 'Redirect' -Query @ { type = $redirectType }
$response = ExecuteRedirectRequest -PreserveAuthorizationOnRedirect -Uri $uri -Method 'POST'
$response . Error | Should -BeNullOrEmpty
# ensure user-agent is present (i.e., no false positives )
$response . Content . Headers . "User-Agent" | Should -Not -BeNullOrEmpty
# ensure Authorization header has been removed.
$response . Content . Headers . "Authorization" | Should -BeExactly 'test'
# ensure POST was changed to GET for selected redirections and remains as POST for others.
$response . Content . Method | Should -Be $redirectedMethod
}
2018-04-08 14:51:17 +02:00
It "Validates Invoke-WebRequest handles responses without Location header for requests with Authorization header and redirect: <redirectType>" -TestCases $redirectTests {
param ( $redirectType , $redirectedMethod )
# Skip relative test as it is not a valid response type.
if ( $redirectType -eq 'relative' ) { return }
# When an Authorization request header is present,
# and -PreserveAuthorizationOnRedirect is not present,
# PowerShell should throw an HTTP Response Exception
# for a redirect response which does not contain a Location response header.
# The correct redirect status code should be included in the exception.
$StatusCode = [ int][System.Net.HttpStatusCode ] $redirectType
$uri = Get-WebListenerUrl -Test Response -Query @ { statuscode = $StatusCode }
$command = "Invoke-WebRequest -Uri ' $uri ' -Headers @{Authorization = 'foo'}"
$response = ExecuteWebCommand -command $command
$response . Error . Exception | Should -BeOfType 'Microsoft.PowerShell.Commands.HttpResponseException'
$response . Error . Exception . Response . StatusCode | Should -Be $StatusCode
$response . Error . Exception . Response . Headers . Location | Should -BeNullOrEmpty
}
2017-07-07 09:25:46 -07:00
#endregion Redirect tests
2017-06-15 15:52:08 -07:00
2018-02-22 13:15:05 -06:00
Context "Invoke-WebRequest SkipHeaderVerification Tests" {
BeforeAll {
$Testfile = Join-Path $testdrive 'SkipHeaderVerification.txt'
'bar' | Set-Content $Testfile
}
2017-07-14 10:24:20 -07:00
2018-02-22 13:15:05 -06:00
It "Verifies Invoke-WebRequest default header handling with no errors" {
$uri = Get-WebListenerUrl -Test 'Get'
$headers = @ { "If-Match" = "*" }
$response = ExecuteRequestWithCustomHeaders -Uri $uri -headers $headers
2017-07-14 10:24:20 -07:00
2018-03-01 23:00:00 -06:00
$response . Error | Should -BeNullOrEmpty
$response . Content . Headers . "If-Match" | Should -BeExactly "*"
2018-02-22 13:15:05 -06:00
}
It "Verifies Invoke-WebRequest default header handling reports an error is returned for an invalid If-Match header value" {
$uri = Get-WebListenerUrl -Test 'Get'
$headers = @ { "If-Match" = "12345" }
$response = ExecuteRequestWithCustomHeaders -Uri $uri -headers $headers
2018-03-01 23:00:00 -06:00
$response . Error | Should -Not -BeNullOrEmpty
$response . Error . FullyQualifiedErrorId | Should -Be "System.FormatException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand"
$response . Error . Exception . Message | Should -Be "The format of value '12345' is invalid."
2018-02-22 13:15:05 -06:00
}
It "Verifies Invoke-WebRequest header handling does not report an error when using -SkipHeaderValidation" {
$uri = Get-WebListenerUrl -Test 'Get'
$headers = @ { "If-Match" = "12345" }
$response = ExecuteRequestWithCustomHeaders -Uri $uri -headers $headers -SkipHeaderValidation
2018-03-01 23:00:00 -06:00
$response . Error | Should -BeNullOrEmpty
$response . Content . Headers . "If-Match" | Should -BeExactly "12345"
2018-02-22 13:15:05 -06:00
}
It "Verifies Invoke-WebRequest default UserAgent handling with no errors" {
$uri = Get-WebListenerUrl -Test 'Get'
$UserAgent = [ Microsoft.PowerShell.Commands.PSUserAgent ]:: InternetExplorer
$response = ExecuteRequestWithCustomUserAgent -Uri $uri -UserAgent $UserAgent -Cmdlet "Invoke-WebRequest"
2018-03-01 23:00:00 -06:00
$response . Error | Should -BeNullOrEmpty
2018-02-22 13:15:05 -06:00
$Pattern = [ regex ]:: Escape ( $UserAgent )
2018-03-01 23:00:00 -06:00
$response . Content . Headers . "User-Agent" | Should -Match $Pattern
2018-02-22 13:15:05 -06:00
}
It "Verifies Invoke-WebRequest default UserAgent handling reports an error is returned for an invalid UserAgent value" {
$uri = Get-WebListenerUrl -Test 'Get'
$UserAgent = 'Invalid:Agent'
$response = ExecuteRequestWithCustomUserAgent -Uri $uri -UserAgent $UserAgent -Cmdlet "Invoke-WebRequest"
2018-03-01 23:00:00 -06:00
$response . Error | Should -Not -BeNullOrEmpty
$response . Error . FullyQualifiedErrorId | Should -Be "System.FormatException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand"
$response . Error . Exception . Message | Should -Be "The format of value 'Invalid:Agent' is invalid."
2018-02-22 13:15:05 -06:00
}
It "Verifies Invoke-WebRequest UserAgent handling does not report an error when using -SkipHeaderValidation" {
$uri = Get-WebListenerUrl -Test 'Get'
$UserAgent = 'Invalid:Agent'
$response = ExecuteRequestWithCustomUserAgent -Uri $uri -UserAgent $UserAgent -SkipHeaderValidation -Cmdlet "Invoke-WebRequest"
2018-03-01 23:00:00 -06:00
$response . Error | Should -BeNullOrEmpty
2018-02-22 13:15:05 -06:00
$Pattern = [ regex ]:: Escape ( $UserAgent )
2018-03-01 23:00:00 -06:00
$response . Content . Headers . "User-Agent" | Should -Match $Pattern
2018-02-22 13:15:05 -06:00
}
It "Verifies Invoke-WebRequest default ContentType handling reports no error is returned for a valid Content-Type header value and -Body" {
$contentType = 'text/plain'
$body = "bar"
$uri = Get-WebListenerUrl -Test 'Post'
$response = Invoke-WebRequest -Uri $uri -Method 'Post' -ContentType $contentType -Body $body
$result = $response . Content | ConvertFrom-Json
2018-03-01 23:00:00 -06:00
$result . data | Should -BeExactly $body
$result . headers . 'Content-Type' | Should -BeExactly $contentType
2018-02-22 13:15:05 -06:00
}
It "Verifies Invoke-WebRequest default ContentType handling reports an error is returned for an invalid Content-Type header value and -Body" {
$contentType = 'foo'
$body = "bar"
$uri = Get-WebListenerUrl -Test 'Post'
{ Invoke-WebRequest -Uri $uri -Method 'Post' -ContentType $contentType -Body $body -ErrorAction 'Stop' } |
2018-05-17 14:42:04 -07:00
Should -Throw -ErrorId "WebCmdletContentTypeException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand"
2018-02-22 13:15:05 -06:00
}
It "Verifies Invoke-WebRequest ContentType handling reports no error is returned for an invalid Content-Type header value, -Body, and -SkipHeaderValidation" {
$contentType = 'foo'
$body = "bar"
$uri = Get-WebListenerUrl -Test 'Post'
$response = Invoke-WebRequest -Uri $uri -Method 'Post' -ContentType $contentType -Body $body -SkipHeaderValidation
$result = $response . Content | ConvertFrom-Json
2018-03-01 23:00:00 -06:00
$result . data | Should -BeExactly 'bar'
$result . headers . 'Content-Type' | Should -BeExactly $contentType
2018-02-22 13:15:05 -06:00
}
It "Verifies Invoke-WebRequest default ContentType handling reports no error is returned for a valid Content-Type header value and -InFile" {
$contentType = 'text/plain'
$uri = Get-WebListenerUrl -Test 'Post'
$response = Invoke-WebRequest -Uri $uri -Method 'Post' -ContentType $contentType -InFile $Testfile
$result = $response . Content | ConvertFrom-Json
# Match used due to inconsistent newline rendering
2018-03-01 23:00:00 -06:00
$result . data | Should -Match 'bar'
$result . headers . 'Content-Type' | Should -BeExactly $contentType
2018-02-22 13:15:05 -06:00
}
It "Verifies Invoke-WebRequest default ContentType handling reports an error is returned for an invalid Content-Type header value and -InFile" {
$contentType = 'foo'
$uri = Get-WebListenerUrl -Test 'Post'
{ Invoke-WebRequest -Uri $uri -Method 'Post' -ContentType $contentType -InFile $Testfile -ErrorAction 'Stop' } |
2018-05-17 14:42:04 -07:00
Should -Throw -ErrorId "WebCmdletContentTypeException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand"
2018-02-22 13:15:05 -06:00
}
It "Verifies Invoke-WebRequest default ContentType handling reports no error is returned for an invalid Content-Type header value, -Infile, and -SkipHeaderValidation" {
$contentType = 'foo'
$uri = Get-WebListenerUrl -Test 'Post'
$response = Invoke-WebRequest -Uri $uri -Method 'Post' -ContentType $contentType -InFile $Testfile -SkipHeaderValidation
$result = $response . Content | ConvertFrom-Json
# Match used due to inconsistent newline rendering
2018-03-01 23:00:00 -06:00
$result . data | Should -Match 'bar'
$result . headers . 'Content-Type' | Should -BeExactly $contentType
2018-02-22 13:15:05 -06:00
}
2017-07-14 10:24:20 -07:00
}
2017-08-16 16:25:16 -07:00
#region charset encoding tests
Context "BasicHtmlWebResponseObject Encoding tests" {
It "Verifies Invoke-WebRequest detects charset meta value when the ContentType header does not define it." {
2017-12-04 11:52:14 -06:00
$query = @ {
contenttype = 'text/html'
2018-01-09 15:43:25 -06:00
body = '<html><head><meta charset="Unicode"></head></html>'
2017-12-04 11:52:14 -06:00
}
2018-01-09 15:43:25 -06:00
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
2017-08-16 16:25:16 -07:00
$expectedEncoding = [ System.Text.Encoding ]:: GetEncoding ( 'Unicode' )
2017-12-04 11:52:14 -06:00
$response = ExecuteWebRequest -Uri $uri -UseBasicParsing
2017-08-16 16:25:16 -07:00
2018-03-01 23:00:00 -06:00
$response . Error | Should -BeNullOrEmpty
$response . Output . Encoding . EncodingName | Should -Be $expectedEncoding . EncodingName
$response . Output | Should -BeOfType 'Microsoft.PowerShell.Commands.BasicHtmlWebResponseObject'
2017-08-16 16:25:16 -07:00
}
It "Verifies Invoke-WebRequest detects charset meta value when newlines are encountered in the element." {
2017-12-04 11:52:14 -06:00
$query = @ {
contenttype = 'text/html'
2018-01-09 15:43:25 -06:00
body = "<html> `n <head> `n <meta `n charset= `" Unicode `"`n > `n </head> `n </html>"
2017-12-04 11:52:14 -06:00
}
2018-01-09 15:43:25 -06:00
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
2017-08-16 16:25:16 -07:00
$expectedEncoding = [ System.Text.Encoding ]:: GetEncoding ( 'Unicode' )
2017-12-04 11:52:14 -06:00
$response = ExecuteWebRequest -Uri $uri -UseBasicParsing
2017-08-16 16:25:16 -07:00
2018-03-01 23:00:00 -06:00
$response . Error | Should -BeNullOrEmpty
$response . Output . Encoding . EncodingName | Should -Be $expectedEncoding . EncodingName
$response . Output | Should -BeOfType 'Microsoft.PowerShell.Commands.BasicHtmlWebResponseObject'
2017-08-16 16:25:16 -07:00
}
It "Verifies Invoke-WebRequest detects charset meta value when the attribute value is unquoted." {
2017-12-04 11:52:14 -06:00
$query = @ {
contenttype = 'text/html'
2018-01-09 15:43:25 -06:00
body = '<html><head><meta charset = Unicode></head></html>'
2017-12-04 11:52:14 -06:00
}
2018-01-09 15:43:25 -06:00
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
2017-08-16 16:25:16 -07:00
$expectedEncoding = [ System.Text.Encoding ]:: GetEncoding ( 'Unicode' )
2017-12-04 11:52:14 -06:00
$response = ExecuteWebRequest -Uri $uri -UseBasicParsing
2017-08-16 16:25:16 -07:00
2018-03-01 23:00:00 -06:00
$response . Error | Should -BeNullOrEmpty
$response . Output . Encoding . EncodingName | Should -Be $expectedEncoding . EncodingName
$response . Output | Should -BeOfType 'Microsoft.PowerShell.Commands.BasicHtmlWebResponseObject'
2017-08-16 16:25:16 -07:00
}
It "Verifies Invoke-WebRequest detects http-equiv charset meta value when the ContentType header does not define it." {
2017-12-04 11:52:14 -06:00
$query = @ {
contenttype = 'text/html'
2018-01-09 15:43:25 -06:00
body = "<html><head> `n <meta http-equiv= `" content-type `" content= `" text/html; charset=Unicode `" > `n </head> `n </html>"
2017-12-04 11:52:14 -06:00
}
2018-01-09 15:43:25 -06:00
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
2017-08-16 16:25:16 -07:00
$expectedEncoding = [ System.Text.Encoding ]:: GetEncoding ( 'Unicode' )
2017-12-04 11:52:14 -06:00
$response = ExecuteWebRequest -Uri $uri -UseBasicParsing
2017-08-16 16:25:16 -07:00
2018-03-01 23:00:00 -06:00
$response . Error | Should -BeNullOrEmpty
$response . Output . Encoding . EncodingName | Should -Be $expectedEncoding . EncodingName
$response . Output | Should -BeOfType 'Microsoft.PowerShell.Commands.BasicHtmlWebResponseObject'
2017-08-16 16:25:16 -07:00
}
It "Verifies Invoke-WebRequest detects http-equiv charset meta value newlines are encountered in the element." {
2017-12-04 11:52:14 -06:00
$query = @ {
contenttype = 'text/html'
2018-01-09 15:43:25 -06:00
body = "<html><head> `n <meta `n http-equiv= `" content-type `"`n content= `" text/html; charset=Unicode `" > `n </head> `n </html>"
2017-12-04 11:52:14 -06:00
}
2018-01-09 15:43:25 -06:00
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
2017-08-16 16:25:16 -07:00
$expectedEncoding = [ System.Text.Encoding ]:: GetEncoding ( 'Unicode' )
2017-12-04 11:52:14 -06:00
$response = ExecuteWebRequest -Uri $uri -UseBasicParsing
2017-08-16 16:25:16 -07:00
2018-03-01 23:00:00 -06:00
$response . Error | Should -BeNullOrEmpty
$response . Output . Encoding . EncodingName | Should -Be $expectedEncoding . EncodingName
$response . Output | Should -BeOfType 'Microsoft.PowerShell.Commands.BasicHtmlWebResponseObject'
2017-08-16 16:25:16 -07:00
}
It "Verifies Invoke-WebRequest ignores meta charset value when Content-Type header defines it." {
2017-12-04 11:52:14 -06:00
$query = @ {
contenttype = 'text/html; charset=utf-8'
2018-01-09 15:43:25 -06:00
body = '<html><head><meta charset="utf-32"></head></html>'
2017-12-04 11:52:14 -06:00
}
2018-01-09 15:43:25 -06:00
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
2017-08-16 16:25:16 -07:00
# NOTE: meta charset should be ignored
$expectedEncoding = [ System.Text.Encoding ]:: UTF8
2017-12-04 11:52:14 -06:00
$response = ExecuteWebRequest -Uri $uri -UseBasicParsing
2017-08-16 16:25:16 -07:00
2018-03-01 23:00:00 -06:00
$response . Error | Should -BeNullOrEmpty
$response . Output . Encoding . EncodingName | Should -Be $expectedEncoding . EncodingName
$response . Output | Should -BeOfType 'Microsoft.PowerShell.Commands.BasicHtmlWebResponseObject'
2017-08-16 16:25:16 -07:00
}
It "Verifies Invoke-WebRequest honors non-utf8 charsets in the Content-Type header" {
2017-12-04 11:52:14 -06:00
$query = @ {
contenttype = 'text/html; charset=utf-16'
2018-01-09 15:43:25 -06:00
body = '<html><head><meta charset="utf-32"></head></html>'
2017-12-04 11:52:14 -06:00
}
2018-01-09 15:43:25 -06:00
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
2017-08-16 16:25:16 -07:00
# NOTE: meta charset should be ignored
$expectedEncoding = [ System.Text.Encoding ]:: GetEncoding ( 'utf-16' )
2017-12-04 11:52:14 -06:00
$response = ExecuteWebRequest -Uri $uri -UseBasicParsing
2017-08-16 16:25:16 -07:00
2018-03-01 23:00:00 -06:00
$response . Error | Should -BeNullOrEmpty
$response . Output . Encoding . EncodingName | Should -Be $expectedEncoding . EncodingName
$response . Output | Should -BeOfType 'Microsoft.PowerShell.Commands.BasicHtmlWebResponseObject'
2017-08-16 16:25:16 -07:00
}
It "Verifies Invoke-WebRequest defaults to iso-8859-1 when an unsupported/invalid charset is declared" {
2017-12-04 11:52:14 -06:00
$query = @ {
contenttype = 'text/html'
2018-01-09 15:43:25 -06:00
body = '<html><head><meta charset="invalid"></head></html>'
2017-12-04 11:52:14 -06:00
}
2018-01-09 15:43:25 -06:00
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
2017-08-16 16:25:16 -07:00
$expectedEncoding = [ System.Text.Encoding ]:: GetEncoding ( 'iso-8859-1' )
2017-12-04 11:52:14 -06:00
$response = ExecuteWebRequest -Uri $uri -UseBasicParsing
2017-08-16 16:25:16 -07:00
2018-03-01 23:00:00 -06:00
$response . Error | Should -BeNullOrEmpty
$response . Output . Encoding . EncodingName | Should -Be $expectedEncoding . EncodingName
$response . Output | Should -BeOfType 'Microsoft.PowerShell.Commands.BasicHtmlWebResponseObject'
2017-08-16 16:25:16 -07:00
}
It "Verifies Invoke-WebRequest defaults to iso-8859-1 when an unsupported/invalid charset is declared using http-equiv" {
2017-12-04 11:52:14 -06:00
$query = @ {
contenttype = 'text/html'
2018-01-09 15:43:25 -06:00
body = "<html><head> `n <meta http-equiv= `" content-type `" content= `" text/html; charset=Invalid `" > `n </head> `n </html>"
2017-12-04 11:52:14 -06:00
}
2018-01-09 15:43:25 -06:00
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
2017-08-16 16:25:16 -07:00
$expectedEncoding = [ System.Text.Encoding ]:: GetEncoding ( 'iso-8859-1' )
2017-12-04 11:52:14 -06:00
$response = ExecuteWebRequest -Uri $uri -UseBasicParsing
2017-08-16 16:25:16 -07:00
2018-03-01 23:00:00 -06:00
$response . Error | Should -BeNullOrEmpty
$response . Output . Encoding . EncodingName | Should -Be $expectedEncoding . EncodingName
$response . Output | Should -BeOfType 'Microsoft.PowerShell.Commands.BasicHtmlWebResponseObject'
2017-08-16 16:25:16 -07:00
}
2018-02-21 23:31:00 -06:00
It "Verifies Invoke-WebRequest defaults to UTF8 on application/json when no charset is present" {
# when contenttype is set, WebListener suppresses charset unless it is included in the query
$query = @ {
contenttype = 'application/json'
body = '{"Test": "Test"}'
}
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
$expectedEncoding = [ System.Text.Encoding ]:: UTF8
$response = ExecuteWebRequest -Uri $uri -UseBasicParsing
2018-03-01 23:00:00 -06:00
$response . Error | Should -BeNullOrEmpty
$response . Output . Encoding . EncodingName | Should -Be $expectedEncoding . EncodingName
$response . Output | Should -BeOfType 'Microsoft.PowerShell.Commands.BasicHtmlWebResponseObject'
$response . Output . Content | Should -BeExactly $query . body
2018-02-21 23:31:00 -06:00
}
2017-08-16 16:25:16 -07:00
}
#endregion charset encoding tests
2017-08-21 12:48:25 -05:00
#region Content Header Inclusion
2017-12-04 11:52:14 -06:00
2017-08-21 12:48:25 -05:00
It "Verifies Invoke-WebRequest includes Content headers in Headers property" {
2017-12-04 11:52:14 -06:00
$query = @ {
contenttype = 'text/plain'
2018-01-09 15:43:25 -06:00
body = 'OK'
2017-12-04 11:52:14 -06:00
}
2018-01-09 15:43:25 -06:00
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
2017-08-21 12:48:25 -05:00
$command = "Invoke-WebRequest -Uri ' $uri '"
$result = ExecuteWebCommand -command $command
ValidateResponse $result
2018-03-01 23:00:00 -06:00
$result . Output . Headers . 'Content-Type' | Should -Be 'text/plain'
$result . Output . Headers . 'Content-Length' | Should -Be 2
2017-08-21 12:48:25 -05:00
}
It "Verifies Invoke-WebRequest includes Content headers in RawContent property" {
2017-12-04 11:52:14 -06:00
$query = @ {
contenttype = 'text/plain'
2018-01-09 15:43:25 -06:00
body = 'OK'
2017-12-04 11:52:14 -06:00
}
2018-01-09 15:43:25 -06:00
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
2017-08-21 12:48:25 -05:00
$command = "Invoke-WebRequest -Uri ' $uri '"
$result = ExecuteWebCommand -command $command
ValidateResponse $result
2018-03-01 23:00:00 -06:00
$result . Output . RawContent | Should -Match ([ regex ]:: Escape ( 'Content-Type: text/plain' ))
$result . Output . RawContent | Should -Match ([ regex ]:: Escape ( 'Content-Length: 2' ))
2017-08-21 12:48:25 -05:00
}
2018-01-16 11:23:22 -06:00
It "Verifies Invoke-WebRequest Supports Multiple response headers with same name" {
2017-12-04 11:52:14 -06:00
$query = @ {
contenttype = 'text/plain'
2018-01-09 15:43:25 -06:00
body = 'OK'
headers = @ {
'X-Fake-Header' = @ ( 'testvalue01' , 'testvalue02' )
2017-12-04 11:52:14 -06:00
} | ConvertTo-Json -Compress
}
2018-01-09 15:43:25 -06:00
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
2017-08-21 12:48:25 -05:00
$command = "Invoke-WebRequest -Uri ' $uri '"
$result = ExecuteWebCommand -command $command
ValidateResponse $result
2018-03-01 23:00:00 -06:00
$result . Output . Headers . 'X-Fake-Header' . Count | Should -Be 2
$result . Output . Headers . 'X-Fake-Header' . Contains ( 'testvalue01' ) | Should -BeTrue
$result . Output . Headers . 'X-Fake-Header' . Contains ( 'testvalue02' ) | Should -BeTrue
$result . Output . RawContent | Should -Match ([ regex ]:: Escape ( 'X-Fake-Header: testvalue01' ))
$result . Output . RawContent | Should -Match ([ regex ]:: Escape ( 'X-Fake-Header: testvalue02' ))
2017-08-21 12:48:25 -05:00
}
#endregion Content Header Inclusion
2017-09-01 13:31:15 -05:00
Context "HTTPS Tests" {
It "Validate Invoke-WebRequest -SkipCertificateCheck" {
# validate that exception is thrown for URI with expired certificate
$Uri = Get-WebListenerUrl -Https
$command = "Invoke-WebRequest -Uri ' $Uri '"
$result = ExecuteWebCommand -command $command
2018-03-01 23:00:00 -06:00
$result . Error . FullyQualifiedErrorId | Should -Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand"
2017-10-05 09:46:33 -07:00
2017-09-01 13:31:15 -05:00
# validate that no exception is thrown for URI with expired certificate when using -SkipCertificateCheck option
$Uri = Get-WebListenerUrl -Https
$command = "Invoke-WebRequest -Uri ' $Uri ' -SkipCertificateCheck"
$result = ExecuteWebCommand -command $command
2018-03-01 23:00:00 -06:00
$result . Error | Should -BeNullOrEmpty
2017-09-01 13:31:15 -05:00
}
2017-10-05 09:46:33 -07:00
2017-09-01 13:31:15 -05:00
It "Validate Invoke-WebRequest returns native HTTPS error message in exception" {
$uri = Get-WebListenerUrl -Https
$command = "Invoke-WebRequest -Uri ' $uri '"
$result = ExecuteWebCommand -command $command
2017-10-05 09:46:33 -07:00
2017-09-01 13:31:15 -05:00
# need to check against inner exception since Linux and Windows uses different HTTP client libraries so errors aren't the same
2018-03-01 23:00:00 -06:00
$result . Error . ErrorDetails . Message | Should -Match $result . Error . Exception . InnerException . Message
$result . Error . FullyQualifiedErrorId | Should -Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand"
2017-09-01 13:31:15 -05:00
}
2017-10-05 09:46:33 -07:00
2018-01-09 15:43:25 -06:00
It "Verifies Invoke-WebRequest Certificate Authentication Fails without -Certificate" {
2017-09-01 13:31:15 -05:00
$uri = Get-WebListenerUrl -Https -Test 'Cert'
2017-10-05 09:46:33 -07:00
$result = Invoke-WebRequest -Uri $uri -SkipCertificateCheck |
2017-09-01 13:31:15 -05:00
Select-Object -ExpandProperty Content |
ConvertFrom-Json
2017-10-05 09:46:33 -07:00
2018-03-01 23:00:00 -06:00
$result . Status | Should -Be 'FAILED'
2017-09-01 13:31:15 -05:00
}
2017-10-05 09:46:33 -07:00
2018-05-09 06:35:52 -05:00
It "Verifies Invoke-WebRequest Certificate Authentication Successful with -Certificate" {
2017-09-01 13:31:15 -05:00
$uri = Get-WebListenerUrl -Https -Test 'Cert'
$certificate = Get-WebListenerClientCertificate
2017-10-05 09:46:33 -07:00
$result = Invoke-WebRequest -Uri $uri -Certificate $certificate -SkipCertificateCheck |
2017-09-01 13:31:15 -05:00
Select-Object -ExpandProperty Content |
ConvertFrom-Json
2017-10-05 09:46:33 -07:00
2018-03-01 23:00:00 -06:00
$result . Status | Should -Be 'OK'
$result . Thumbprint | Should -Be $certificate . Thumbprint
2017-09-01 13:31:15 -05:00
}
2017-08-31 04:30:35 -05:00
}
2017-09-12 11:41:36 -05:00
Context "Multipart/form-data Tests" {
2018-01-24 23:17:55 -06:00
<#
Content-Type request headers for multipart/form-data appear as:
multipart/form-data; boundary="0ab0cb90-f01b-4c15-bd4d-53d073efcc1d"
MultipartFormDataContent sets a random GUID for the boundary before submitting the request
to the remote endpoint. Tests in this context for Content-Type match 'multipart/form-data'
as we do not have access to the random GUID.
#>
<#
Kestrel/ASP.NET inconsistently renders the new line for uploaded text files.
File content tests in this context use match as a workaround.
#>
BeforeAll {
$file1Name = "testfile1.txt"
$file1Path = Join-Path $testdrive $file1Name
$file1Contents = "Test123"
$file1Contents | Set-Content $file1Path -Force
$file2Name = "testfile2.txt"
$file2Path = Join-Path $testdrive $file2Name
$file2Contents = "Test456"
$file2Contents | Set-Content $file2Path -Force
}
2017-09-12 11:41:36 -05:00
It "Verifies Invoke-WebRequest Supports Multipart String Values" {
$body = GetMultipartBody -String
$uri = Get-WebListenerUrl -Test 'Multipart'
$response = Invoke-WebRequest -Uri $uri -Body $body -Method 'POST'
$result = $response . Content | ConvertFrom-Json
2018-03-01 23:00:00 -06:00
$result . Headers . 'Content-Type' | Should -Match 'multipart/form-data'
$result . Items . TestString [ 0 ] | Should -Be 'TestValue'
2017-09-12 11:41:36 -05:00
}
2018-01-24 23:17:55 -06:00
2017-09-12 11:41:36 -05:00
It "Verifies Invoke-WebRequest Supports Multipart File Values" {
$body = GetMultipartBody -File
$uri = Get-WebListenerUrl -Test 'Multipart'
$response = Invoke-WebRequest -Uri $uri -Body $body -Method 'POST'
$result = $response . Content | ConvertFrom-Json
2018-03-01 23:00:00 -06:00
$result . Headers . 'Content-Type' | Should -Match 'multipart/form-data'
$result . Files [ 0 ]. FileName | Should -Be 'multipart.txt'
$result . Files [ 0 ]. ContentType | Should -Be 'text/plain'
$result . Files [ 0 ]. Content | Should -Match 'TestContent'
2017-09-12 11:41:36 -05:00
}
2018-01-24 23:17:55 -06:00
2017-09-12 11:41:36 -05:00
It "Verifies Invoke-WebRequest Supports Mixed Multipart String and File Values" {
$body = GetMultipartBody -String -File
$uri = Get-WebListenerUrl -Test 'Multipart'
$response = Invoke-WebRequest -Uri $uri -Body $body -Method 'POST'
$result = $response . Content | ConvertFrom-Json
2018-03-01 23:00:00 -06:00
$result . Headers . 'Content-Type' | Should -Match 'multipart/form-data'
$result . Items . TestString [ 0 ] | Should -Be 'TestValue'
$result . Files [ 0 ]. FileName | Should -Be 'multipart.txt'
$result . Files [ 0 ]. ContentType | Should -Be 'text/plain'
$result . Files [ 0 ]. Content | Should -Match 'TestContent'
2017-09-12 11:41:36 -05:00
}
2018-01-24 23:17:55 -06:00
It "Verifies Invoke-WebRequest -Form supports string values" {
$form = @ { TestString = "TestValue" }
$uri = Get-WebListenerUrl -Test 'Multipart'
$response = Invoke-WebRequest -Uri $uri -Form $form -Method 'POST'
$result = $response . Content | ConvertFrom-Json
2018-03-01 23:00:00 -06:00
$result . Headers . 'Content-Type' | Should -Match 'multipart/form-data'
$result . Items . TestString . Count | Should -Be 1
$result . Items . TestString [ 0 ] | Should -BeExactly 'TestValue'
2018-01-24 23:17:55 -06:00
}
It "Verifies Invoke-WebRequest -Form supports a collection of string values" {
$form = @ { TestStrings = "TestValue" , "TestValue2" }
$uri = Get-WebListenerUrl -Test 'Multipart'
$response = Invoke-WebRequest -Uri $uri -Form $form -Method 'POST'
$result = $response . Content | ConvertFrom-Json
2018-03-01 23:00:00 -06:00
$result . Headers . 'Content-Type' | Should -Match 'multipart/form-data'
$result . Items . TestStrings . Count | Should -Be 2
$result . Items . TestStrings [ 0 ] | Should -BeExactly 'TestValue'
$result . Items . TestStrings [ 1 ] | Should -BeExactly 'TestValue2'
2018-01-24 23:17:55 -06:00
}
It "Verifies Invoke-WebRequest -Form supports file values" {
$form = @ { TestFile = [ System.IO.FileInfo ] $file1Path }
$uri = Get-WebListenerUrl -Test 'Multipart'
$response = Invoke-WebRequest -Uri $uri -Form $form -Method 'POST'
$result = $response . Content | ConvertFrom-Json
2018-03-01 23:00:00 -06:00
$result . Headers . 'Content-Type' | Should -Match 'multipart/form-data'
$result . Files . Count | Should -Be 1
2018-01-24 23:17:55 -06:00
2018-03-01 23:00:00 -06:00
$result . Files [ 0 ]. Name | Should -BeExactly "TestFile"
$result . Files [ 0 ]. FileName | Should -BeExactly $file1Name
$result . Files [ 0 ]. ContentType | Should -BeExactly 'application/octet-stream'
$result . Files [ 0 ]. Content | Should -Match $file1Contents
2018-01-24 23:17:55 -06:00
}
It "Verifies Invoke-WebRequest -Form supports a collection of file values" {
$form = @ { TestFiles = [ System.IO.FileInfo ] $file1Path , [ System.IO.FileInfo ] $file2Path }
$uri = Get-WebListenerUrl -Test 'Multipart'
$response = Invoke-WebRequest -Uri $uri -Form $form -Method 'POST'
$result = $response . Content | ConvertFrom-Json
2018-03-01 23:00:00 -06:00
$result . Headers . 'Content-Type' | Should -Match 'multipart/form-data'
$result . Files . Count | Should -Be 2
2018-01-24 23:17:55 -06:00
2018-03-01 23:00:00 -06:00
$result . Files [ 0 ]. Name | Should -BeExactly "TestFiles"
$result . Files [ 0 ]. FileName | Should -BeExactly $file1Name
$result . Files [ 0 ]. ContentType | Should -BeExactly 'application/octet-stream'
$result . Files [ 0 ]. Content | Should -Match $file1Contents
2018-01-24 23:17:55 -06:00
2018-03-01 23:00:00 -06:00
$result . Files [ 1 ]. Name | Should -BeExactly "TestFiles"
$result . Files [ 1 ]. FileName | Should -BeExactly $file2Name
$result . Files [ 1 ]. ContentType | Should -BeExactly 'application/octet-stream'
$result . Files [ 1 ]. Content | Should -Match $file2Contents
2018-01-24 23:17:55 -06:00
}
It "Verifies Invoke-WebRequest -Form supports combinations of strings and files" {
$form = @ {
TestStrings = "TestValue" , "TestValue2"
TestFiles = [ System.IO.FileInfo ] $file1Path , [ System.IO.FileInfo ] $file2Path
}
$uri = Get-WebListenerUrl -Test 'Multipart'
$response = Invoke-WebRequest -Uri $uri -Form $form -Method 'POST'
$result = $response . Content | ConvertFrom-Json
2018-03-01 23:00:00 -06:00
$result . Headers . 'Content-Type' | Should -Match 'multipart/form-data'
$result . Items . TestStrings . Count | Should -Be 2
$result . Files . Count | Should -Be 2
2018-01-24 23:17:55 -06:00
2018-03-01 23:00:00 -06:00
$result . Items . TestStrings [ 0 ] | Should -BeExactly 'TestValue'
$result . Items . TestStrings [ 1 ] | Should -BeExactly 'TestValue2'
2018-01-24 23:17:55 -06:00
2018-03-01 23:00:00 -06:00
$result . Files [ 0 ]. Name | Should -Be "TestFiles"
$result . Files [ 0 ]. FileName | Should -BeExactly $file1Name
$result . Files [ 0 ]. ContentType | Should -BeExactly 'application/octet-stream'
$result . Files [ 0 ]. Content | Should -Match $file1Contents
2018-01-24 23:17:55 -06:00
2018-03-01 23:00:00 -06:00
$result . Files [ 1 ]. Name | Should -BeExactly "TestFiles"
$result . Files [ 1 ]. FileName | Should -BeExactly $file2Name
$result . Files [ 1 ]. ContentType | Should -BeExactly 'application/octet-stream'
$result . Files [ 1 ]. Content | Should -Match $file2Contents
2018-01-24 23:17:55 -06:00
}
It "Verifies Invoke-WebRequest -Form is mutually exclusive with -Body" {
$form = @ { TestString = "TestValue" }
$body = "test"
$uri = Get-WebListenerUrl -Test 'Multipart'
{ Invoke-WebRequest -Uri $uri -Form $form -Body $Body -ErrorAction 'Stop' } |
2018-05-17 14:42:04 -07:00
Should -Throw -ErrorId 'WebCmdletBodyFormConflictException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand'
2018-01-24 23:17:55 -06:00
}
It "Verifies Invoke-WebRequest -Form is mutually exclusive with -InFile" {
$form = @ { TestString = "TestValue" }
$uri = Get-WebListenerUrl -Test 'Multipart'
{ Invoke-WebRequest -Uri $uri -Form $form -InFile $file1Path -ErrorAction 'Stop' } |
2018-05-17 14:42:04 -07:00
Should -Throw -ErrorId 'WebCmdletFormInFileConflictException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand'
2018-01-24 23:17:55 -06:00
}
2017-09-12 11:41:36 -05:00
}
2017-10-17 21:08:06 -05:00
Context "Invoke-WebRequest -Authentication tests" {
BeforeAll {
#[SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Demo/doc/test secret.")]
$token = "testpassword" | ConvertTo-SecureString -AsPlainText -Force
2018-01-09 15:43:25 -06:00
$credential = [ pscredential ]:: new ( "testuser" , $token )
2017-10-17 21:08:06 -05:00
$httpUri = Get-WebListenerUrl -Test 'Get'
$httpsUri = Get-WebListenerUrl -Test 'Get' -Https
2017-11-10 10:11:57 -06:00
$httpBasicUri = Get-WebListenerUrl -Test 'Auth' -TestValue 'Basic'
$httpsBasicUri = Get-WebListenerUrl -Test 'Auth' -TestValue 'Basic' -Https
2017-10-17 21:08:06 -05:00
$testCases = @ (
@ { Authentication = "bearer" }
@ { Authentication = "OAuth" }
)
}
It "Verifies Invoke-WebRequest -Authentication Basic" {
$params = @ {
2018-01-09 15:43:25 -06:00
Uri = $httpsUri
Authentication = "Basic"
Credential = $credential
2017-10-17 21:08:06 -05:00
SkipCertificateCheck = $true
}
$Response = Invoke-WebRequest @params
$result = $response . Content | ConvertFrom-Json
2018-03-01 23:00:00 -06:00
$result . Headers . Authorization | Should -BeExactly "Basic dGVzdHVzZXI6dGVzdHBhc3N3b3Jk"
2017-10-17 21:08:06 -05:00
}
It "Verifies Invoke-WebRequest -Authentication <Authentication>" -TestCases $testCases {
param ( $Authentication )
$params = @ {
2018-01-09 15:43:25 -06:00
Uri = $httpsUri
Authentication = $Authentication
Token = $token
2017-10-17 21:08:06 -05:00
SkipCertificateCheck = $true
}
$Response = Invoke-WebRequest @params
$result = $response . Content | ConvertFrom-Json
2018-03-01 23:00:00 -06:00
$result . Headers . Authorization | Should -BeExactly "Bearer testpassword"
2017-10-17 21:08:06 -05:00
}
It "Verifies Invoke-WebRequest -Authentication does not support -UseDefaultCredentials" {
$params = @ {
2018-01-09 15:43:25 -06:00
Uri = $httpsUri
Token = $token
Authentication = "OAuth"
2017-10-17 21:08:06 -05:00
UseDefaultCredentials = $true
2018-01-09 15:43:25 -06:00
ErrorAction = 'Stop'
SkipCertificateCheck = $true
2017-10-17 21:08:06 -05:00
}
2018-05-17 14:42:04 -07:00
{ Invoke-WebRequest @params } | Should -Throw -ErrorId "WebCmdletAuthenticationConflictException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand"
2017-10-17 21:08:06 -05:00
}
It "Verifies Invoke-WebRequest -Authentication does not support Both -Credential and -Token" {
$params = @ {
2018-01-09 15:43:25 -06:00
Uri = $httpsUri
Token = $token
Credential = $credential
Authentication = "OAuth"
ErrorAction = 'Stop'
2017-10-17 21:08:06 -05:00
SkipCertificateCheck = $true
}
2018-05-17 14:42:04 -07:00
{ Invoke-WebRequest @params } | Should -Throw -ErrorId "WebCmdletAuthenticationTokenConflictException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand"
2017-10-17 21:08:06 -05:00
}
It "Verifies Invoke-WebRequest -Authentication <Authentication> requires -Token" -TestCases $testCases {
param ( $Authentication )
$params = @ {
2018-01-09 15:43:25 -06:00
Uri = $httpsUri
Authentication = $Authentication
ErrorAction = 'Stop'
2017-10-17 21:08:06 -05:00
SkipCertificateCheck = $true
}
2018-05-17 14:42:04 -07:00
{ Invoke-WebRequest @params } | Should -Throw -ErrorId "WebCmdletAuthenticationTokenNotSuppliedException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand"
2017-10-17 21:08:06 -05:00
}
It "Verifies Invoke-WebRequest -Authentication Basic requires -Credential" {
$params = @ {
2018-01-09 15:43:25 -06:00
Uri = $httpsUri
Authentication = "Basic"
ErrorAction = 'Stop'
2017-10-17 21:08:06 -05:00
SkipCertificateCheck = $true
}
2018-05-17 14:42:04 -07:00
{ Invoke-WebRequest @params } | Should -Throw -ErrorId "WebCmdletAuthenticationCredentialNotSuppliedException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand"
2017-10-17 21:08:06 -05:00
}
It "Verifies Invoke-WebRequest -Authentication Requires HTTPS" {
$params = @ {
2018-01-09 15:43:25 -06:00
Uri = $httpUri
Token = $token
2017-10-17 21:08:06 -05:00
Authentication = "OAuth"
2018-01-09 15:43:25 -06:00
ErrorAction = 'Stop'
2017-10-17 21:08:06 -05:00
}
2018-05-17 14:42:04 -07:00
{ Invoke-WebRequest @params } | Should -Throw -ErrorId "WebCmdletAllowUnencryptedAuthenticationRequiredException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand"
2017-10-17 21:08:06 -05:00
}
It "Verifies Invoke-WebRequest -Authentication Can use HTTP with -AllowUnencryptedAuthentication" {
$params = @ {
2018-01-09 15:43:25 -06:00
Uri = $httpUri
Token = $token
Authentication = "OAuth"
2017-10-17 21:08:06 -05:00
AllowUnencryptedAuthentication = $true
}
$Response = Invoke-WebRequest @params
$result = $response . Content | ConvertFrom-Json
2018-03-01 23:00:00 -06:00
$result . Headers . Authorization | Should -BeExactly "Bearer testpassword"
2017-10-17 21:08:06 -05:00
}
2017-11-10 10:11:57 -06:00
It "Verifies Invoke-WebRequest Negotiated -Credential over HTTPS" {
$params = @ {
2018-01-09 15:43:25 -06:00
Uri = $httpsBasicUri
Credential = $credential
2017-11-10 10:11:57 -06:00
SkipCertificateCheck = $true
}
$Response = Invoke-WebRequest @params
$result = $response . Content | ConvertFrom-Json
2018-03-01 23:00:00 -06:00
$result . Headers . Authorization | Should -BeExactly "Basic dGVzdHVzZXI6dGVzdHBhc3N3b3Jk"
2017-11-10 10:11:57 -06:00
}
It "Verifies Invoke-WebRequest Negotiated -Credential Requires HTTPS" {
$params = @ {
2018-01-09 15:43:25 -06:00
Uri = $httpBasicUri
Credential = $credential
2017-11-10 10:11:57 -06:00
ErrorAction = 'Stop'
}
2018-05-17 14:42:04 -07:00
{ Invoke-WebRequest @params } | Should -Throw -ErrorId "WebCmdletAllowUnencryptedAuthenticationRequiredException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand"
2017-11-10 10:11:57 -06:00
}
It "Verifies Invoke-WebRequest Negotiated -Credential Can use HTTP with -AllowUnencryptedAuthentication" {
$params = @ {
2018-01-09 15:43:25 -06:00
Uri = $httpBasicUri
Credential = $credential
2017-11-10 10:11:57 -06:00
AllowUnencryptedAuthentication = $true
}
$Response = Invoke-WebRequest @params
$result = $response . Content | ConvertFrom-Json
2018-03-01 23:00:00 -06:00
$result . Headers . Authorization | Should -BeExactly "Basic dGVzdHVzZXI6dGVzdHBhc3N3b3Jk"
2017-11-10 10:11:57 -06:00
}
# UseDefaultCredentials is only reliably testable on Windows
It "Verifies Invoke-WebRequest Negotiated -UseDefaultCredentials with '<AuthType>' over HTTPS" -Skip: $ (! $IsWindows ) -TestCases @ (
@ { AuthType = 'NTLM' }
@ { AuthType = 'Negotiate' }
) {
param ( $AuthType )
$params = @ {
2018-01-09 15:43:25 -06:00
Uri = Get-WebListenerUrl -Test 'Auth' -TestValue $AuthType -Https
2017-11-10 10:11:57 -06:00
UseDefaultCredentials = $true
2018-01-09 15:43:25 -06:00
SkipCertificateCheck = $true
2017-11-10 10:11:57 -06:00
}
$Response = Invoke-WebRequest @params
$result = $response . Content | ConvertFrom-Json
2018-03-01 23:00:00 -06:00
$result . Headers . Authorization | Should -Match "^ $AuthType "
2017-11-10 10:11:57 -06:00
}
# The error condition can at least be tested on all platforms.
It "Verifies Invoke-WebRequest Negotiated -UseDefaultCredentials Requires HTTPS" {
$params = @ {
2018-01-09 15:43:25 -06:00
Uri = $httpUri
2017-11-10 10:11:57 -06:00
UseDefaultCredentials = $true
2018-01-09 15:43:25 -06:00
ErrorAction = 'Stop'
2017-11-10 10:11:57 -06:00
}
2018-05-17 14:42:04 -07:00
{ Invoke-WebRequest @params } | Should -Throw -ErrorId "WebCmdletAllowUnencryptedAuthenticationRequiredException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand"
2017-11-10 10:11:57 -06:00
}
# UseDefaultCredentials is only reliably testable on Windows
It "Verifies Invoke-WebRequest Negotiated -UseDefaultCredentials with '<AuthType>' Can use HTTP with -AllowUnencryptedAuthentication" -Skip: $ (! $IsWindows ) -TestCases @ (
@ { AuthType = 'NTLM' }
@ { AuthType = 'Negotiate' }
) {
param ( $AuthType )
$params = @ {
2018-01-09 15:43:25 -06:00
Uri = Get-WebListenerUrl -Test 'Auth' -TestValue $AuthType
UseDefaultCredentials = $true
2017-11-10 10:11:57 -06:00
AllowUnencryptedAuthentication = $true
}
$Response = Invoke-WebRequest @params
$result = $response . Content | ConvertFrom-Json
2018-03-01 23:00:00 -06:00
$result . Headers . Authorization | Should -Match "^ $AuthType "
2017-11-10 10:11:57 -06:00
}
2017-10-17 21:08:06 -05:00
}
2017-11-13 12:45:46 -06:00
Context "Invoke-WebRequest -SslProtocol Test" {
2017-12-04 11:38:30 -08:00
BeforeAll {
## Test cases for the 1st 'It'
$testCases1 = @ (
@ { Test = @ { SslProtocol = 'Default' ; ActualProtocol = 'Default' }; Pending = $false }
@ { Test = @ { SslProtocol = 'Tls' ; ActualProtocol = 'Tls' }; Pending = $false }
2017-12-06 14:20:44 -06:00
@ { Test = @ { SslProtocol = 'Tls11' ; ActualProtocol = 'Tls11' }; Pending = $false }
@ { Test = @ { SslProtocol = 'Tls12' ; ActualProtocol = 'Tls12' }; Pending = $false }
2018-05-09 06:35:52 -05:00
@ { Test = @ { SslProtocol = 'Tls, Tls11, Tls12' ; ActualProtocol = 'Tls12' }; Pending = $false }
@ { Test = @ { SslProtocol = 'Tls11, Tls12' ; ActualProtocol = 'Tls12' }; Pending = $false }
@ { Test = @ { SslProtocol = 'Tls, Tls11, Tls12' ; ActualProtocol = 'Tls11' }; Pending = $false }
@ { Test = @ { SslProtocol = 'Tls11, Tls12' ; ActualProtocol = 'Tls11' }; Pending = $false }
@ { Test = @ { SslProtocol = 'Tls, Tls11' ; ActualProtocol = 'Tls11' }; Pending = $false }
@ { Test = @ { SslProtocol = 'Tls, Tls11, Tls12' ; ActualProtocol = 'Tls' }; Pending = $false }
@ { Test = @ { SslProtocol = 'Tls, Tls11' ; ActualProtocol = 'Tls' }; Pending = $false }
# Skipping intermediary protocols is not supported on all platforms
@ { Test = @ { SslProtocol = 'Tls, Tls12' ; ActualProtocol = 'Tls' }; Pending = -not $IsWindows }
2017-12-04 11:38:30 -08:00
@ { Test = @ { SslProtocol = 'Tls, Tls12' ; ActualProtocol = 'Tls12' }; Pending = -not $IsWindows }
)
2017-12-04 11:52:14 -06:00
2017-12-04 11:38:30 -08:00
$testCases2 = @ (
2018-01-09 15:43:25 -06:00
@ { Test = @ { IntendedProtocol = 'Tls' ; ActualProtocol = 'Tls12' }; Pending = $false }
@ { Test = @ { IntendedProtocol = 'Tls' ; ActualProtocol = 'Tls11' }; Pending = $false }
2017-12-04 11:38:30 -08:00
@ { Test = @ { IntendedProtocol = 'Tls11' ; ActualProtocol = 'Tls12' }; Pending = $false }
@ { Test = @ { IntendedProtocol = 'Tls12' ; ActualProtocol = 'Tls11' }; Pending = $false }
2017-12-06 14:20:44 -06:00
@ { Test = @ { IntendedProtocol = 'Tls11' ; ActualProtocol = 'Tls' }; Pending = $false }
@ { Test = @ { IntendedProtocol = 'Tls12' ; ActualProtocol = 'Tls' }; Pending = $false }
2018-05-09 06:35:52 -05:00
@ { Test = @ { IntendedProtocol = 'Tls11, Tls12' ; ActualProtocol = 'Tls' }; Pending = $false }
@ { Test = @ { IntendedProtocol = 'Tls, Tls12' ; ActualProtocol = 'Tls11' }; Pending = $false }
@ { Test = @ { IntendedProtocol = 'Tls, Tls11' ; ActualProtocol = 'Tls12' }; Pending = $false }
2017-12-04 11:38:30 -08:00
)
2017-11-13 12:45:46 -06:00
}
2017-12-04 11:38:30 -08:00
foreach ( $entry in $testCases1 ) {
It "Verifies Invoke-WebRequest -SslProtocol <SslProtocol> works on <ActualProtocol>" -TestCases ( $entry . Test ) -Pending: ( $entry . Pending ) {
param ( $SslProtocol , $ActualProtocol )
$params = @ {
2018-01-09 15:43:25 -06:00
Uri = Get-WebListenerUrl -Test 'Get' -Https -SslProtocol $ActualProtocol
SslProtocol = $SslProtocol
2017-12-04 11:38:30 -08:00
SkipCertificateCheck = $true
}
$response = Invoke-WebRequest @params
$result = $Response . Content | ConvertFrom-Json
2018-03-01 23:00:00 -06:00
$result . headers . Host | Should -Be $params . Uri . Authority
2017-11-13 12:45:46 -06:00
}
}
2017-12-04 11:38:30 -08:00
foreach ( $entry in $testCases2 ) {
It "Verifies Invoke-WebRequest -SslProtocol -SslProtocol <IntendedProtocol> fails on a <ActualProtocol> only connection" -TestCases ( $entry . Test ) -Pending: ( $entry . Pending ) {
param ( $IntendedProtocol , $ActualProtocol )
$params = @ {
2018-01-09 15:43:25 -06:00
Uri = Get-WebListenerUrl -Test 'Get' -Https -SslProtocol $ActualProtocol
SslProtocol = $IntendedProtocol
2017-12-04 11:38:30 -08:00
SkipCertificateCheck = $true
2018-01-09 15:43:25 -06:00
ErrorAction = 'Stop'
2017-12-04 11:38:30 -08:00
}
2018-05-17 14:42:04 -07:00
{ Invoke-WebRequest @params } | Should -Throw -ErrorId 'WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand'
2017-12-04 11:38:30 -08:00
}
}
2017-11-13 12:45:46 -06:00
}
2018-03-26 12:23:24 -07:00
Context "Invoke-WebRequest File Resume Feature" {
BeforeAll {
$outFile = Join-Path $TestDrive "resume.txt"
# 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
$referenceFileHash = Get-FileHash -Algorithm SHA256 -Path $referenceFile
$referenceFileSize = Get-Item $referenceFile | Select-Object -ExpandProperty Length
}
AfterEach {
Remove-Item -Force -ErrorAction 'SilentlyContinue' -Path $outFile
}
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 -OutFile $outFile -Resume -PassThru
$outFileHash = Get-FileHash -Algorithm SHA256 -Path $outFile
$outFileHash . Hash | Should -BeExactly $referenceFileHash . Hash
Get-Item $outFile | Select-Object -ExpandProperty Length | Should -Be $referenceFileSize
$response . Headers . 'X-WebListener-Has-Range' [ 0 ] | Should -BeExactly 'true'
$response . Headers . 'X-WebListener-Request-Range' [ 0 ] | Should -BeExactly 'bytes=0-'
$response . StatusCode | Should -Be 206
$response . Headers . 'Content-Range' [ 0 ] | Should -BeExactly "bytes 0- $( $referenceFileSize - 1 ) / $referenceFileSize "
}
It "Invoke-WebRequest -Resume overwrites an existing file that is larger than the remote file" {
# Create a file larger than the download file
$largerFileSize = $referenceFileSize + 20
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
$outFileHash = Get-FileHash -Algorithm SHA256 -Path $outFile
$outFileHash . Hash | Should -BeExactly $referenceFileHash . Hash
Get-Item $outFile | Select-Object -ExpandProperty Length | Should -Be $referenceFileSize
Get-Item $outFile | Select-Object -ExpandProperty Length | Should -BeLessThan $largerFileSize
$response . Headers . 'X-WebListener-Has-Range' [ 0 ] | Should -BeExactly 'false'
$response . StatusCode | Should -Be 200
$response . Headers . ContainsKey ( 'Content-Range' ) | Should -BeFalse
}
It "Invoke-WebRequest -Resume overwrites existing file when remote server does not support resume" {
# Create a file larger than the download file
$largerFileSize = $referenceFileSize + 20
1 .. $largerFileSize | ForEach-Object { [ Byte ] $_ } | Set-Content -AsByteStream $outFile
$largerFileSize = Get-Item $outFile | Select-Object -ExpandProperty Length
$uri = Get-WebListenerUrl -Test 'Resume' -TestValue 'NoResume'
$response = Invoke-WebRequest -Uri $uri -OutFile $outFile -Resume -PassThru
$outFileHash = Get-FileHash -Algorithm SHA256 -Path $outFile
$outFileHash . Hash | Should -BeExactly $referenceFileHash . Hash
Get-Item $outFile | Select-Object -ExpandProperty Length | Should -Be $referenceFileSize
$response . Headers . 'X-WebListener-Has-Range' [ 0 ] | Should -BeExactly 'true'
$response . Headers . 'X-WebListener-Request-Range' [ 0 ] | Should -BeExactly "bytes= $largerFileSize -"
$response . StatusCode | Should -Be 200
$response . Headers . ContainsKey ( 'Content-Range' ) | Should -BeFalse
}
It "Invoke-WebRequest -Resume resumes downloading from <bytes> bytes" -TestCases @ (
@ { bytes = 4 }
@ { bytes = 8 }
@ { bytes = 12 }
@ { bytes = 16 }
) {
param ( $bytes , $statuscode )
# Simulate partial download
$uri = Get-WebListenerUrl -Test 'Resume' -TestValue "Bytes/ $bytes "
$null = Invoke-WebRequest -uri $uri -OutFile $outFile
Get-Item $outFile | Select-Object -ExpandProperty Length | Should -Be $bytes
$response = Invoke-WebRequest -uri $resumeUri -OutFile $outFile -Resume -PassThru
$outFileHash = Get-FileHash -Algorithm SHA256 -Path $outFile
$outFileHash . Hash | Should -BeExactly $referenceFileHash . Hash
Get-Item $outFile | Select-Object -ExpandProperty Length | Should -Be $referenceFileSize
$response . Headers . 'X-WebListener-Has-Range' [ 0 ] | Should -BeExactly 'true'
$response . Headers . 'X-WebListener-Request-Range' [ 0 ] | Should -BeExactly "bytes= $bytes -"
$response . StatusCode | Should -Be 206
$response . Headers . 'Content-Range' [ 0 ] | Should -BeExactly "bytes $bytes - $( $referenceFileSize - 1 ) / $referenceFileSize "
}
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
$fileSize = Get-Item $outFile | Select-Object -ExpandProperty Length
$response = Invoke-WebRequest -uri $resumeUri -OutFile $outFile -Resume -PassThru
$outFileHash = Get-FileHash -Algorithm SHA256 -Path $outFile
$outFileHash . Hash | Should -BeExactly $referenceFileHash . Hash
Get-Item $outFile | Select-Object -ExpandProperty Length | Should -Be $referenceFileSize
$response . Headers . 'X-WebListener-Has-Range' [ 0 ] | Should -BeExactly 'true'
$response . Headers . 'X-WebListener-Request-Range' [ 0 ] | Should -BeExactly "bytes= $fileSize -"
# The web cmdlets special case 416 as a success code when the local file and remote file are the same size
$response . StatusCode | Should -Be 416
$response . Headers . 'Content-Range' [ 0 ] | Should -BeExactly "bytes */ $referenceFileSize "
}
}
2016-07-22 15:09:03 -07:00
}
2016-07-25 14:01:41 -07:00
Describe "Invoke-RestMethod tests" -Tags "Feature" {
2017-05-24 15:54:02 -07:00
BeforeAll {
2017-08-31 04:30:35 -05:00
$WebListener = Start-WebListener
2017-05-24 15:54:02 -07:00
}
2017-10-06 22:59:31 +02:00
#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'
2018-02-02 13:56:06 -06:00
$command = "Invoke-RestMethod -Uri ' $uri '"
2017-10-06 22:59:31 +02:00
$result = ExecuteWebCommand -command $command
# Validate response
2018-03-01 23:00:00 -06:00
$result . Output . headers . 'User-Agent' | Should -MatchExactly '.*\(Macintosh;.*\) PowerShell\/\d+\.\d+\.\d+.*'
2017-10-06 22:59:31 +02:00
}
It "Invoke-RestMethod returns Correct User-Agent on Linux" -Skip: (! $IsLinux ) {
$uri = Get-WebListenerUrl -Test 'Get'
2018-02-02 13:56:06 -06:00
$command = "Invoke-RestMethod -Uri ' $uri '"
2017-10-06 22:59:31 +02:00
$result = ExecuteWebCommand -command $command
# Validate response
2018-03-01 23:00:00 -06:00
$result . Output . headers . 'User-Agent' | Should -MatchExactly '.*\(Linux;.*\) PowerShell\/\d+\.\d+\.\d+.*'
2017-10-06 22:59:31 +02:00
}
It "Invoke-RestMethod returns Correct User-Agent on Windows" -Skip: (! $IsWindows ) {
2017-09-14 00:23:29 -05:00
$uri = Get-WebListenerUrl -Test 'Get'
2018-02-02 13:56:06 -06:00
$command = "Invoke-RestMethod -Uri ' $uri '"
2016-07-22 15:09:03 -07:00
$result = ExecuteWebCommand -command $command
# Validate response
2018-03-01 23:00:00 -06:00
$result . Output . headers . 'User-Agent' | Should -MatchExactly '.*\(Windows NT \d+\.\d*;.*\) PowerShell\/\d+\.\d+\.\d+.*'
2016-07-22 15:09:03 -07:00
}
It "Invoke-RestMethod returns headers dictionary" {
2017-09-14 00:22:52 -05:00
$uri = Get-WebListenerUrl -Test 'Get'
2018-02-02 13:56:06 -06:00
$command = "Invoke-RestMethod -Uri ' $uri '"
2017-01-16 13:31:14 -08:00
2016-07-22 15:09:03 -07:00
$result = ExecuteWebCommand -command $command
# Validate response
2018-03-01 23:00:00 -06:00
$result . Output . headers . Host | Should -Be $Uri . Authority
2016-07-22 15:09:03 -07:00
}
It "Validate Invoke-RestMethod -DisableKeepAlive" {
# Operation options
2017-09-07 22:53:04 -05:00
$uri = Get-WebListenerUrl -Test 'Get'
2018-02-02 13:56:06 -06:00
$command = "Invoke-RestMethod -Uri ' $uri ' -DisableKeepAlive"
2016-07-22 15:09:03 -07:00
$result = ExecuteWebCommand -command $command
# Validate response
2018-03-01 23:00:00 -06:00
$result . Output . headers . Host | Should -Be $uri . Authority
$result . Output . Headers . Connection | Should -Be "Close"
2016-07-22 15:09:03 -07:00
}
It "Validate Invoke-RestMethod -MaximumRedirection" {
2017-09-25 14:50:15 -05:00
$uri = Get-WebListenerUrl -Test 'Redirect' -TestValue '3'
2017-09-18 11:26:01 -05:00
$command = "Invoke-RestMethod -Uri ' $uri ' -MaximumRedirection 4"
2017-01-16 13:31:14 -08:00
2016-07-22 15:09:03 -07:00
$result = ExecuteWebCommand -command $command
# Validate response
2018-03-01 23:00:00 -06:00
$result . Output . headers . Host | Should -Match $uri . Authority
2016-07-22 15:09:03 -07:00
}
It "Validate Invoke-RestMethod error for -MaximumRedirection" {
2017-09-25 14:50:15 -05:00
$uri = Get-WebListenerUrl -Test 'Redirect' -TestValue '3'
2017-09-18 11:26:01 -05:00
$command = "Invoke-RestMethod -Uri ' $uri ' -MaximumRedirection 2"
2017-01-16 13:31:14 -08:00
2016-07-22 15:09:03 -07:00
$result = ExecuteWebCommand -command $command
2018-03-01 23:00:00 -06:00
$result . Error . FullyQualifiedErrorId | Should -Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand"
2016-07-22 15:09:03 -07:00
}
It "Invoke-RestMethod supports request that returns page containing UTF-8 data." {
2017-09-25 14:50:15 -05:00
$uri = Get-WebListenerUrl -Test 'Encoding' -TestValue 'Utf8'
2017-09-22 12:09:49 -05:00
$command = "Invoke-RestMethod -Uri ' $uri '"
2017-01-16 13:31:14 -08:00
2017-10-05 09:46:33 -07:00
$result = ExecuteWebCommand -command $command
2018-03-01 23:00:00 -06:00
$Result . Output | Should -Match '⡌⠁⠧⠑ ⠼⠁⠒ ⡍⠜⠇⠑⠹⠰⠎ ⡣⠕⠌'
2016-07-22 15:09:03 -07:00
}
It "Invoke-RestMethod validate timeout option" {
2017-09-25 14:50:15 -05:00
$uri = Get-WebListenerUrl -Test 'Delay' -TestValue '5'
$command = "Invoke-RestMethod -Uri ' $uri ' -TimeoutSec 2"
2017-01-16 13:31:14 -08:00
2016-07-22 15:09:03 -07:00
$result = ExecuteWebCommand -command $command
2018-03-01 23:00:00 -06:00
$result . Error . FullyQualifiedErrorId | Should -Be "System.Threading.Tasks.TaskCanceledException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand"
2017-03-28 21:25:13 +02:00
}
2016-07-22 15:09:03 -07:00
2017-03-28 21:25:13 +02:00
It "Validate Invoke-RestMethod error with -Proxy and -NoProxy option" {
2017-09-25 14:50:15 -05:00
$uri = Get-WebListenerUrl -Test 'Delay' -TestValue '10'
2018-01-31 22:26:05 -06:00
$command = "Invoke-RestMethod -Uri ' $uri ' -Proxy 'http://127.0.0.1:8080' -NoProxy -TimeoutSec 2"
2017-03-28 21:25:13 +02:00
$result = ExecuteWebCommand -command $command
2018-03-01 23:00:00 -06:00
$result . Error . FullyQualifiedErrorId | Should -Be "AmbiguousParameterSet,Microsoft.PowerShell.Commands.InvokeRestMethodCommand"
2017-03-28 21:25:13 +02:00
}
$testCase = @ (
2018-05-08 09:15:49 +05:00
@ { proxy_address = ( Get-WebListenerUrl ). Authority ; name = 'HTTP proxy' ; protocol = 'http' }
@ { proxy_address = ( Get-WebListenerUrl -https ). Authority ; name = 'HTTPS proxy' ; protocol = 'https' }
2017-03-28 21:25:13 +02:00
)
2018-05-03 04:58:39 +05:00
It "Validate Invoke-RestMethod with -Proxy option - '<name>'" -TestCases $testCase {
2017-03-28 21:25:13 +02:00
param ( $proxy_address , $name , $protocol )
2018-05-03 04:58:39 +05:00
# use external url, but with proxy the external url should not actually be called
$command = "Invoke-RestMethod -Uri ${protocol} ://httpbin.org -Proxy ' ${protocol} :// ${proxy_address} '"
2017-03-28 21:25:13 +02:00
$result = ExecuteWebCommand -command $command
2018-05-03 04:58:39 +05:00
$command = "Invoke-RestMethod -Uri ' ${protocol} :// ${proxy_address} ' -NoProxy"
$expectedResult = ExecuteWebCommand -command $command
$result . Output | Should -BeExactly $expectedResult . Output
2017-03-28 21:25:13 +02:00
}
2016-07-22 15:09:03 -07:00
# Perform the following operation for Invoke-RestMethod
# gzip Returns gzip-encoded data.
# deflate Returns deflate-encoded data.
2017-01-16 13:31:14 -08:00
# $dataEncodings = @("Chunked", "Compress", "Deflate", "GZip", "Identity")
2017-10-02 15:46:53 -05:00
# Note: These are the supported options, but we do not have a web service to test them all.
It "Invoke-RestMethod supports request that returns <DataEncoding>-encoded data." -TestCases @ (
@ { DataEncoding = "gzip" }
@ { DataEncoding = "deflate" }
) {
param ( $dataEncoding )
$uri = Get-WebListenerUrl -Test 'Compression' -TestValue $dataEncoding
$result = Invoke-RestMethod -Uri $uri -ResponseHeadersVariable 'headers'
2016-07-22 15:09:03 -07:00
2017-10-02 15:46:53 -05:00
# Validate response content
2018-03-01 23:00:00 -06:00
$headers . 'Content-Encoding' [ 0 ] | Should -BeExactly $dataEncoding
$result . Headers . Host | Should -BeExactly $uri . Authority
2016-07-22 15:09:03 -07:00
}
# Perform the following operation for Invoke-RestMethod using the following content types: "text/plain", "application/xml", "application/xml"
# post Returns POST data.
# patch Returns PATCH data.
# put Returns PUT data.
# delete Returns DELETE data
2017-12-13 09:28:05 -06:00
$testMethods = @ ( "POST" , "PATCH" , "PUT" , "DELETE" )
2016-07-22 15:09:03 -07:00
$contentTypes = @ ( "text/plain" , "application/xml" , "application/json" )
2018-01-09 15:43:25 -06:00
foreach ( $contentType in $contentTypes ) {
foreach ( $method in $testMethods ) {
2016-07-22 15:09:03 -07:00
# Operation options
2017-12-13 09:28:05 -06:00
$uri = Get-WebListenerUrl -Test $method
2016-07-22 15:09:03 -07:00
$body = GetTestData -contentType $contentType
2017-12-13 09:28:05 -06:00
$command = "Invoke-RestMethod -Uri $uri -Body ' $body ' -Method $method -ContentType $contentType "
2016-07-22 15:09:03 -07:00
2017-12-13 09:28:05 -06:00
It "Invoke-RestMethod -Uri $uri -Method $method -ContentType $contentType -Body [body data]" {
2016-07-22 15:09:03 -07:00
$result = ExecuteWebCommand -command $command
# Validate response
2018-03-01 23:00:00 -06:00
$result . Output . url | Should -Match $uri
$result . Output . headers . 'Content-Type' | Should -Match $contentType
2016-07-22 15:09:03 -07:00
2017-12-13 09:28:05 -06:00
# Validate that the response Content.data field is the same as what we sent.
2018-03-01 23:00:00 -06:00
$result . Output . data | Should -Be $body
2016-07-22 15:09:03 -07:00
}
}
}
It "Validate Invoke-RestMethod -Headers --> Set KeepAlive to false via headers" {
2017-09-07 22:53:04 -05:00
$uri = Get-WebListenerUrl -Test 'Get'
2016-07-22 15:09:03 -07:00
$result = ExecuteRequestWithHeaders -cmdletName Invoke-RestMethod -uri $uri
# Validate response
2018-03-01 23:00:00 -06:00
$result . Output . url | Should -Match $uri
$result . Output . Headers . Connection | Should -Be "Close"
2016-07-22 15:09:03 -07:00
}
# Validate all available user agents for Invoke-RestMethod
2018-01-09 15:43:25 -06:00
$agents = @ {
InternetExplorer = "MSIE 9.0"
Chrome = "Chrome"
Opera = "Opera"
Safari = "Safari"
FireFox = "Firefox"
}
2016-07-22 15:09:03 -07:00
2018-01-09 15:43:25 -06:00
foreach ( $agentName in $agents . Keys ) {
2016-07-22 15:09:03 -07:00
$expectedAgent = $agents [ $agentName ]
2017-09-07 22:53:04 -05:00
$uri = Get-WebListenerUrl -Test 'Get'
2016-07-22 15:09:03 -07:00
$userAgent = "[Microsoft.PowerShell.Commands.PSUserAgent]:: $agentName "
2018-02-02 13:56:06 -06:00
$command = "Invoke-RestMethod -Uri $uri -UserAgent ( $userAgent ) "
2016-07-22 15:09:03 -07:00
It "Validate Invoke-RestMethod UserAgent. Execute--> $command " {
$result = ExecuteWebCommand -command $command
# Validate response
2018-03-01 23:00:00 -06:00
$result . Output . headers . Host | Should -Be $uri . Authority
$result . Output . headers . 'User-Agent' | Should -Match $expectedAgent
2016-07-22 15:09:03 -07:00
}
}
It "Validate Invoke-RestMethod -OutFile" {
2017-09-07 22:53:04 -05:00
$uri = Get-WebListenerUrl -Test 'Get'
2016-07-22 15:09:03 -07:00
$result = ExecuteRequestWithOutFile -cmdletName "Invoke-RestMethod" -uri $uri
$jsonContent = $result . Output | ConvertFrom-Json
2018-03-01 23:00:00 -06:00
$jsonContent . headers . Host | Should -Be $uri . Authority
2016-07-22 15:09:03 -07:00
}
2016-11-03 22:29:17 +01:00
2016-11-15 08:35:07 +11:00
It "Validate Invoke-RestMethod handles missing Content-Type in response header" {
#Validate that exception is not thrown when response headers are missing Content-Type.
2017-10-18 14:24:02 -05:00
$uri = Get-WebListenerUrl -Test 'ResponseHeaders' -Query @ { 'Content-Type' = '' }
$command = "Invoke-RestMethod -Uri ' $uri '"
2016-11-15 08:35:07 +11:00
$result = ExecuteWebCommand -command $command
2018-03-01 23:00:00 -06:00
$result . Error | Should -BeNullOrEmpty
2016-11-15 08:35:07 +11:00
}
2016-12-15 11:06:26 +03:00
2017-03-06 19:41:14 +00:00
It "Validate Invoke-RestMethod StandardMethod and CustomMethod parameter sets" {
2017-12-18 22:29:46 -06:00
$uri = Get-WebListenerUrl -Test 'Get'
2017-03-06 19:41:14 +00:00
$errorId = "AmbiguousParameterSet,Microsoft.PowerShell.Commands.InvokeRestMethodCommand"
2018-05-17 14:42:04 -07:00
{ Invoke-RestMethod -Uri $uri -Method GET -CustomMethod TEST } | Should -Throw -ErrorId $errorId
2017-03-06 19:41:14 +00:00
}
It "Validate CustomMethod method is used" {
2017-12-18 22:29:46 -06:00
$uri = Get-WebListenerUrl -Test 'Get'
$command = "Invoke-RestMethod -Uri ' $uri ' -CustomMethod TEST"
2017-03-06 19:41:14 +00:00
$result = ExecuteWebCommand -command $command
2018-03-01 23:00:00 -06:00
$result . Error | Should -BeNullOrEmpty
$result . Output . method | Should -Be "TEST"
2017-03-06 19:41:14 +00:00
}
It "Validate Invoke-RestMethod default ContentType for CustomMethod POST" {
2017-12-13 09:28:05 -06:00
$uri = Get-WebListenerUrl -Test 'Post'
$command = "Invoke-RestMethod -Uri ' $uri ' -CustomMethod POST -Body 'testparam=testvalue'"
2017-03-06 19:41:14 +00:00
$result = ExecuteWebCommand -command $command
2018-03-01 23:00:00 -06:00
$result . Output . form . testparam | Should -Be "testvalue"
$result . Output . Headers . 'Content-Type' | Should -Be "application/x-www-form-urlencoded"
2017-03-06 19:41:14 +00:00
}
It "Validate Invoke-RestMethod body is converted to query params for CustomMethod GET" {
2017-09-07 22:53:04 -05:00
$uri = Get-WebListenerUrl -Test 'Get'
$command = "Invoke-RestMethod -Uri ' $uri ' -CustomMethod GET -Body @{'testparam'='testvalue'}"
2017-03-06 19:41:14 +00:00
$result = ExecuteWebCommand -command $command
2018-03-01 23:00:00 -06:00
$result . Output . args . testparam | Should -Be "testvalue"
2017-03-06 19:41:14 +00:00
}
2017-02-24 22:29:11 -08:00
It "Validate Invoke-RestMethod returns HTTP errors in exception" {
2017-12-13 09:28:05 -06:00
$query = @ {
2018-01-09 15:43:25 -06:00
body = "I am a teapot!!!"
statuscode = 418
2017-12-13 09:28:05 -06:00
responsephrase = "I am a teapot"
}
2018-01-09 15:43:25 -06:00
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
2017-12-13 09:28:05 -06:00
$command = "Invoke-RestMethod -Uri ' $uri '"
2017-02-24 22:29:11 -08:00
$result = ExecuteWebCommand -command $command
2018-03-01 23:00:00 -06:00
$result . Error . ErrorDetails . Message | Should -Be $query . body
$result . Error . Exception | Should -BeOfType 'Microsoft.PowerShell.Commands.HttpResponseException'
$result . Error . Exception . Response . StatusCode | Should -Be 418
$result . Error . Exception . Response . ReasonPhrase | Should -Be $query . responsephrase
$result . Error . Exception . Message | Should -Match ": 418 \( $( $query . responsephrase ) \)\."
$result . Error . FullyQualifiedErrorId | Should -Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand"
2017-02-24 22:29:11 -08:00
}
2017-03-16 17:59:15 -07:00
2017-05-24 15:54:02 -07:00
It "Validate Invoke-RestMethod -FollowRelLink doesn't fail if no Link Header is present" {
2017-12-04 11:52:14 -06:00
$query = @ {
contenttype = 'application/json'
2018-01-09 15:43:25 -06:00
body = '"foo"'
2017-12-04 11:52:14 -06:00
}
2018-01-09 15:43:25 -06:00
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
2017-12-04 11:52:14 -06:00
$command = "Invoke-RestMethod -Uri ' $uri ' -FollowRelLink"
2017-05-24 15:54:02 -07:00
$result = ExecuteWebCommand -command $command
2018-03-01 23:00:00 -06:00
$result . Output | Should -BeExactly "foo"
2017-05-24 15:54:02 -07:00
}
It "Validate Invoke-RestMethod -FollowRelLink correctly follows all the available relation links" {
$maxLinks = 5
2018-01-16 11:23:22 -06:00
$uri = Get-WebListenerUrl -Test 'Link' -Query @ { maxlinks = $maxLinks }
$command = "Invoke-RestMethod -Uri ' $uri ' -FollowRelLink"
2017-05-24 15:54:02 -07:00
$result = ExecuteWebCommand -command $command
2018-03-01 23:00:00 -06:00
$result . Output . Count | Should -BeExactly $maxLinks
1 .. $maxLinks | ForEach-Object { $result . Output [ $_ - 1 ]. linknumber | Should -BeExactly $_ }
2017-05-24 15:54:02 -07:00
}
It "Validate Invoke-RestMethod -FollowRelLink correctly limits to -MaximumRelLink" {
$maxLinks = 10
$maxLinksToFollow = 6
2018-01-16 11:23:22 -06:00
$uri = Get-WebListenerUrl -Test 'Link' -Query @ { maxlinks = $maxLinks }
$command = "Invoke-RestMethod -Uri ' $uri ' -FollowRelLink -MaximumFollowRelLink $maxLinksToFollow "
2017-05-24 15:54:02 -07:00
$result = ExecuteWebCommand -command $command
2018-03-01 23:00:00 -06:00
$result . Output . Count | Should -BeExactly $maxLinksToFollow
1 .. $maxLinksToFollow | ForEach-Object { $result . Output [ $_ - 1 ]. linknumber | Should -BeExactly $_ }
2017-05-24 15:54:02 -07:00
}
2017-07-07 09:25:46 -07:00
It "Validate Invoke-RestMethod quietly ignores invalid Link Headers if -FollowRelLink is specified: <type>" -TestCases @ (
2017-05-24 15:54:02 -07:00
@ { type = "noUrl" }
@ { type = "malformed" }
2017-07-07 09:25:46 -07:00
@ { type = "noRel" }
2017-05-24 15:54:02 -07:00
) {
param ( $type )
2018-01-16 11:23:22 -06:00
$uri = Get-WebListenerUrl -Test 'Link' -Query @ { type = $type }
$command = "Invoke-RestMethod -Uri ' $uri ' -FollowRelLink"
2017-05-24 15:54:02 -07:00
$result = ExecuteWebCommand -command $command
2018-03-01 23:00:00 -06:00
$result . Output . linknumber | Should -BeExactly 1
2017-05-24 15:54:02 -07:00
}
2017-06-15 15:52:08 -07:00
#region Redirect tests
2018-05-14 13:37:30 -05:00
It "Validates Invoke-RestMethod with -PreserveAuthorizationOnRedirect preserves the authorization header on redirect: <redirectType> <redirectedMethod>" -TestCases $redirectTests {
2017-06-15 15:52:08 -07:00
param ( $redirectType , $redirectedMethod )
2018-01-13 01:51:04 -06:00
$uri = Get-WebListenerUrl -Test 'Redirect' -Query @ { type = $redirectType }
$response = ExecuteRedirectRequest -Cmdlet 'Invoke-RestMethod' -Uri $uri -PreserveAuthorizationOnRedirect
2017-07-07 09:25:46 -07:00
2018-03-01 23:00:00 -06:00
$response . Error | Should -BeNullOrEmpty
2017-06-15 15:52:08 -07:00
# ensure Authorization header has been preserved.
2018-03-01 23:00:00 -06:00
$response . Content . Headers . "Authorization" | Should -BeExactly "test"
2017-06-15 15:52:08 -07:00
}
2018-05-14 13:37:30 -05:00
It "Validates Invoke-RestMethod preserves the authorization header on multiple redirects: <redirectType>" -TestCases $redirectTests {
2017-06-15 15:52:08 -07:00
param ( $redirectType )
2018-01-13 01:51:04 -06:00
$uri = Get-WebListenerUrl -Test 'Redirect' -TestValue 3 -Query @ { type = $redirectType }
$response = ExecuteRedirectRequest -Cmdlet 'Invoke-RestMethod' -Uri $uri -PreserveAuthorizationOnRedirect
2017-06-15 15:52:08 -07:00
2018-03-01 23:00:00 -06:00
$response . Error | Should -BeNullOrEmpty
2017-06-15 15:52:08 -07:00
# ensure Authorization header was stripped
2018-03-01 23:00:00 -06:00
$response . Content . Headers . "Authorization" | Should -BeExactly "test"
2017-06-15 15:52:08 -07:00
}
2018-05-14 13:37:30 -05:00
It "Validates Invoke-RestMethod strips the authorization header on various redirects: <redirectType>" -TestCases $redirectTests {
2017-06-15 15:52:08 -07:00
param ( $redirectType )
2018-01-13 01:51:04 -06:00
$uri = Get-WebListenerUrl -Test 'Redirect' -Query @ { type = $redirectType }
$response = ExecuteRedirectRequest -Cmdlet 'Invoke-RestMethod' -Uri $uri
2017-06-15 15:52:08 -07:00
2018-03-01 23:00:00 -06:00
$response . Error | Should -BeNullOrEmpty
2017-06-15 15:52:08 -07:00
# ensure user-agent is present (i.e., no false positives )
2018-03-01 23:00:00 -06:00
$response . Content . Headers . "User-Agent" | Should -Not -BeNullOrEmpty
2017-06-15 15:52:08 -07:00
# ensure Authorization header has been removed.
2018-03-01 23:00:00 -06:00
$response . Content . Headers . "Authorization" | Should -BeNullOrEmpty
2017-06-15 15:52:08 -07:00
}
# NOTE: Only testing redirection of POST -> GET for unique underlying values of HttpStatusCode.
# Some names overlap in underlying value.
2018-05-14 13:37:30 -05:00
It "Validates Invoke-RestMethod strips the authorization header redirects and switches from POST to GET when it handles the redirect: <redirectType> <redirectedMethod>" -TestCases $redirectTests {
2017-06-15 15:52:08 -07:00
param ( $redirectType , $redirectedMethod )
2018-01-13 01:51:04 -06:00
$uri = Get-WebListenerUrl -Test 'Redirect' -Query @ { type = $redirectType }
$response = ExecuteRedirectRequest -Cmdlet 'Invoke-RestMethod' -Uri $uri -Method 'POST'
2017-07-07 09:25:46 -07:00
2018-03-01 23:00:00 -06:00
$response . Error | Should -BeNullOrEmpty
2017-06-15 15:52:08 -07:00
# ensure user-agent is present (i.e., no false positives )
2018-03-01 23:00:00 -06:00
$response . Content . Headers . "User-Agent" | Should -Not -BeNullOrEmpty
2017-06-15 15:52:08 -07:00
# ensure Authorization header has been removed.
2018-05-14 13:37:30 -05:00
$response . Content . Headers . "Authorization" | Should -BeNullOrEmpty
# ensure POST was changed to GET for selected redirections and remains as POST for others.
$response . Content . Method | Should -Be $redirectedMethod
}
It "Validates Invoke-RestMethod -PreserveAuthorizationOnRedirect keeps the authorization header redirects and switches from POST to GET when it handles the redirect: <redirectType> <redirectedMethod>" -TestCases $redirectTests {
param ( $redirectType , $redirectedMethod )
$uri = Get-WebListenerUrl -Test 'Redirect' -Query @ { type = $redirectType }
$response = ExecuteRedirectRequest -PreserveAuthorizationOnRedirect -Cmdlet 'Invoke-RestMethod' -Uri $uri -Method 'POST'
$response . Error | Should -BeNullOrEmpty
# ensure user-agent is present (i.e., no false positives )
$response . Content . Headers . "User-Agent" | Should -Not -BeNullOrEmpty
# ensure Authorization header has been removed.
$response . Content . Headers . "Authorization" | Should -BeExactly 'test'
2017-06-15 15:52:08 -07:00
# ensure POST was changed to GET for selected redirections and remains as POST for others.
2018-03-01 23:00:00 -06:00
$response . Content . Method | Should -Be $redirectedMethod
2017-07-07 09:25:46 -07:00
}
2017-06-15 15:52:08 -07:00
2018-04-08 14:51:17 +02:00
It "Validates Invoke-RestMethod handles responses without Location header for requests with Authorization header and redirect: <redirectType>" -TestCases $redirectTests {
param ( $redirectType , $redirectedMethod )
# Skip relative test as it is not a valid response type.
if ( $redirectType -eq 'relative' ) { return }
# When an Authorization request header is present,
# and -PreserveAuthorizationOnRedirect is not present,
# PowerShell should throw an HTTP Response Exception
# for a redirect response which does not contain a Location response header.
# The correct redirect status code should be included in the exception.
$StatusCode = [ int][System.Net.HttpStatusCode ] $redirectType
$uri = Get-WebListenerUrl -Test Response -Query @ { statuscode = $StatusCode }
$command = "Invoke-RestMethod -Uri ' $uri ' -Headers @{Authorization = 'foo'}"
$response = ExecuteWebCommand -command $command
$response . Error . Exception | Should -BeOfType 'Microsoft.PowerShell.Commands.HttpResponseException'
$response . Error . Exception . Response . StatusCode | Should -Be $StatusCode
$response . Error . Exception . Response . Headers . Location | Should -BeNullOrEmpty
}
2017-07-07 09:25:46 -07:00
#endregion Redirect tests
2017-06-15 15:52:08 -07:00
2018-02-22 13:15:05 -06:00
Context "Invoke-RestMethod SkipHeaderVerification Tests" {
BeforeAll {
$Testfile = Join-Path $testdrive 'SkipHeaderVerification.txt'
'bar' | Set-Content $Testfile
}
2017-07-14 10:24:20 -07:00
2018-02-22 13:15:05 -06:00
It "Verifies Invoke-RestMethod default header handling with no errors" {
$uri = Get-WebListenerUrl -Test 'Get'
$headers = @ { "If-Match" = "*" }
$response = ExecuteRequestWithCustomHeaders -Uri $uri -headers $headers -Cmdlet "Invoke-RestMethod"
2017-07-14 10:24:20 -07:00
2018-03-01 23:00:00 -06:00
$response . Error | Should -BeNullOrEmpty
$response . Content . Headers . "If-Match" | Should -BeExactly "*"
2018-02-22 13:15:05 -06:00
}
It "Verifies Invoke-RestMethod default header handling reports an error is returned for an invalid If-Match header value" {
$uri = Get-WebListenerUrl -Test 'Get'
$headers = @ { "If-Match" = "12345" }
$response = ExecuteRequestWithCustomHeaders -Uri $uri -headers $headers -Cmdlet "Invoke-RestMethod"
2018-03-01 23:00:00 -06:00
$response . Error | Should -Not -BeNullOrEmpty
$response . Error . FullyQualifiedErrorId | Should -Be "System.FormatException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand"
$response . Error . Exception . Message | Should -Be "The format of value '12345' is invalid."
2018-02-22 13:15:05 -06:00
}
It "Verifies Invoke-RestMethod header handling does not report an error when using -SkipHeaderValidation" {
$uri = Get-WebListenerUrl -Test 'Get'
$headers = @ { "If-Match" = "12345" }
$response = ExecuteRequestWithCustomHeaders -Uri $uri -headers $headers -SkipHeaderValidation -Cmdlet "Invoke-RestMethod"
2018-03-01 23:00:00 -06:00
$response . Error | Should -BeNullOrEmpty
$response . Content . Headers . "If-Match" | Should -BeExactly "12345"
2018-02-22 13:15:05 -06:00
}
It "Verifies Invoke-RestMethod default UserAgent handling with no errors" {
$uri = Get-WebListenerUrl -Test 'Get'
$UserAgent = [ Microsoft.PowerShell.Commands.PSUserAgent ]:: InternetExplorer
$response = ExecuteRequestWithCustomUserAgent -Uri $uri -UserAgent $UserAgent -Cmdlet "Invoke-RestMethod"
2018-03-01 23:00:00 -06:00
$response . Error | Should -BeNullOrEmpty
2018-02-22 13:15:05 -06:00
$Pattern = [ regex ]:: Escape ( $UserAgent )
2018-03-01 23:00:00 -06:00
$response . Content . Headers . "User-Agent" | Should -Match $Pattern
2018-02-22 13:15:05 -06:00
}
It "Verifies Invoke-RestMethod default UserAgent handling reports an error is returned for an invalid UserAgent value" {
$uri = Get-WebListenerUrl -Test 'Get'
$UserAgent = 'Invalid:Agent'
$response = ExecuteRequestWithCustomUserAgent -Uri $uri -UserAgent $UserAgent -Cmdlet "Invoke-RestMethod"
2018-03-01 23:00:00 -06:00
$response . Error | Should -Not -BeNullOrEmpty
$response . Error . FullyQualifiedErrorId | Should -Be "System.FormatException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand"
$response . Error . Exception . Message | Should -Be "The format of value 'Invalid:Agent' is invalid."
2018-02-22 13:15:05 -06:00
}
It "Verifies Invoke-RestMethod UserAgent handling does not report an error when using -SkipHeaderValidation" {
$uri = Get-WebListenerUrl -Test 'Get'
$UserAgent = 'Invalid:Agent'
$response = ExecuteRequestWithCustomUserAgent -Uri $uri -UserAgent $UserAgent -SkipHeaderValidation -Cmdlet "Invoke-RestMethod"
2018-03-01 23:00:00 -06:00
$response . Error | Should -BeNullOrEmpty
2018-02-22 13:15:05 -06:00
$Pattern = [ regex ]:: Escape ( $UserAgent )
2018-03-01 23:00:00 -06:00
$response . Content . Headers . "User-Agent" | Should -Match $Pattern
2018-02-22 13:15:05 -06:00
}
It "Verifies Invoke-RestMethod default ContentType handling reports no error is returned for a valid Content-Type header value and -Body" {
$contentType = 'text/plain'
$body = "bar"
$uri = Get-WebListenerUrl -Test 'Post'
$result = Invoke-RestMethod -Uri $uri -Method 'Post' -ContentType $contentType -Body $body
2018-03-01 23:00:00 -06:00
$result . data | Should -BeExactly $body
$result . headers . 'Content-Type' | Should -BeExactly $contentType
2018-02-22 13:15:05 -06:00
}
It "Verifies Invoke-RestMethod default ContentType handling reports an error is returned for an invalid Content-Type header value and -Body" {
$contentType = 'foo'
$body = "bar"
$uri = Get-WebListenerUrl -Test 'Post'
{ Invoke-RestMethod -Uri $uri -Method 'Post' -ContentType $contentType -Body $body -ErrorAction 'Stop' } |
2018-05-17 14:42:04 -07:00
Should -Throw -ErrorId "WebCmdletContentTypeException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand"
2018-02-22 13:15:05 -06:00
}
It "Verifies Invoke-RestMethod ContentType handling reports no error is returned for an invalid Content-Type header value, -Body, and -SkipHeaderValidation" {
$contentType = 'foo'
$body = "bar"
$uri = Get-WebListenerUrl -Test 'Post'
$result = Invoke-RestMethod -Uri $uri -Method 'Post' -ContentType $contentType -Body $body -SkipHeaderValidation
2018-03-01 23:00:00 -06:00
$result . data | Should -BeExactly $body
$result . headers . 'Content-Type' | Should -BeExactly $contentType
2018-02-22 13:15:05 -06:00
}
It "Verifies Invoke-RestMethod default ContentType handling reports no error is returned for a valid Content-Type header value and -InFile" {
$contentType = 'text/plain'
$uri = Get-WebListenerUrl -Test 'Post'
$result = Invoke-RestMethod -Uri $uri -Method 'Post' -ContentType $contentType -InFile $Testfile
# Match used due to inconsistent newline rendering
2018-03-01 23:00:00 -06:00
$result . data | Should -Match 'bar'
$result . headers . 'Content-Type' | Should -BeExactly $contentType
2018-02-22 13:15:05 -06:00
}
It "Verifies Invoke-RestMethod default ContentType handling reports an error is returned for an invalid Content-Type header value and -InFile" {
$contentType = 'foo'
$uri = Get-WebListenerUrl -Test 'Post'
{ Invoke-RestMethod -Uri $uri -Method 'Post' -ContentType $contentType -InFile $Testfile -ErrorAction 'Stop' } |
2018-05-17 14:42:04 -07:00
Should -Throw -ErrorId "WebCmdletContentTypeException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand"
2018-02-22 13:15:05 -06:00
}
It "Verifies Invoke-RestMethod default ContentType handling reports no error is returned for an invalid Content-Type header value, -Infile, and -SkipHeaderValidation" {
$contentType = 'foo'
$uri = Get-WebListenerUrl -Test 'Post'
$result = Invoke-RestMethod -Uri $uri -Method 'Post' -ContentType $contentType -InFile $Testfile -SkipHeaderValidation
# Match used due to inconsistent newline rendering
2018-03-01 23:00:00 -06:00
$result . data | Should -Match 'bar'
$result . headers . 'Content-Type' | Should -BeExactly $contentType
2018-02-22 13:15:05 -06:00
}
2017-07-14 10:24:20 -07:00
}
2017-09-01 13:31:15 -05:00
Context "HTTPS Tests" {
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
2018-01-09 15:43:25 -06:00
$uri = Get-WebListenerUrl -Https
2017-09-01 13:31:15 -05:00
$command = "Invoke-RestMethod -Uri ' $uri ' -Method HEAD"
$result = ExecuteWebCommand -command $command
2018-03-01 23:00:00 -06:00
$result . Error . FullyQualifiedErrorId | Should -Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand"
2017-10-05 09:46:33 -07:00
2017-09-01 13:31:15 -05:00
# validate that no exception is thrown for URI with expired certificate when using -SkipCertificateCheck option
$command = "Invoke-RestMethod -Uri ' $uri ' -SkipCertificateCheck -Method HEAD"
$result = ExecuteWebCommand -command $command
2018-03-01 23:00:00 -06:00
$result . Error | Should -BeNullOrEmpty
2017-09-01 13:31:15 -05:00
}
2017-10-05 09:46:33 -07:00
2017-09-01 13:31:15 -05:00
It "Validate Invoke-RestMethod returns native HTTPS error message in exception" {
$uri = Get-WebListenerUrl -Https
$command = "Invoke-RestMethod -Uri ' $uri '"
$result = ExecuteWebCommand -command $command
2017-10-05 09:46:33 -07:00
2017-09-01 13:31:15 -05:00
# need to check against inner exception since Linux and Windows uses different HTTP client libraries so errors aren't the same
2018-03-01 23:00:00 -06:00
$result . Error . ErrorDetails . Message | Should -Match $result . Error . Exception . InnerException . Message
$result . Error . FullyQualifiedErrorId | Should -Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand"
2017-09-01 13:31:15 -05:00
}
2017-10-05 09:46:33 -07:00
2017-09-01 13:31:15 -05:00
It "Verifies Invoke-RestMethod Certificate Authentication Fails without -Certificate" {
$uri = Get-WebListenerUrl -Https -Test 'Cert'
$result = Invoke-RestMethod -Uri $uri -SkipCertificateCheck
2017-10-05 09:46:33 -07:00
2018-03-01 23:00:00 -06:00
$result . Status | Should -Be 'FAILED'
2017-09-01 13:31:15 -05:00
}
2017-10-05 09:46:33 -07:00
2018-05-09 06:35:52 -05:00
It "Verifies Invoke-RestMethod Certificate Authentication Successful with -Certificate" {
2017-09-01 13:31:15 -05:00
$uri = Get-WebListenerUrl -Https -Test 'Cert'
$certificate = Get-WebListenerClientCertificate
$result = Invoke-RestMethod -uri $uri -Certificate $certificate -SkipCertificateCheck
2017-10-05 09:46:33 -07:00
2018-03-01 23:00:00 -06:00
$result . Status | Should -Be 'OK'
$result . Thumbprint | Should -Be $certificate . Thumbprint
2017-09-01 13:31:15 -05:00
}
2017-08-18 15:56:31 -05:00
}
2017-09-12 11:41:36 -05:00
Context "Multipart/form-data Tests" {
2018-01-24 23:17:55 -06:00
<#
Content-Type request headers for multipart/form-data appear as:
multipart/form-data; boundary="0ab0cb90-f01b-4c15-bd4d-53d073efcc1d"
MultipartFormDataContent sets a random GUID for the boundary before submitting the request
to the remote endpoint. Tests in this context for Content-Type match 'multipart/form-data'
as we do not have access to the random GUID.
#>
<#
Kestrel/ASP.NET inconsistently renders the new line for uploaded text files.
File content tests in this context use match as a workaround.
#>
BeforeAll {
$file1Name = "testfile1.txt"
$file1Path = Join-Path $testdrive $file1Name
$file1Contents = "Test123"
$file1Contents | Set-Content $file1Path -Force
$file2Name = "testfile2.txt"
$file2Path = Join-Path $testdrive $file2Name
$file2Contents = "Test456"
$file2Contents | Set-Content $file2Path -Force
}
2017-09-12 11:41:36 -05:00
It "Verifies Invoke-RestMethod Supports Multipart String Values" {
$body = GetMultipartBody -String
$uri = Get-WebListenerUrl -Test 'Multipart'
$result = Invoke-RestMethod -Uri $uri -Body $body -Method 'POST'
2018-03-01 23:00:00 -06:00
$result . Headers . 'Content-Type' | Should -Match 'multipart/form-data'
$result . Items . TestString [ 0 ] | Should -Be 'TestValue'
2017-09-12 11:41:36 -05:00
}
2018-01-09 15:43:25 -06:00
2017-09-12 11:41:36 -05:00
It "Verifies Invoke-RestMethod Supports Multipart File Values" {
$body = GetMultipartBody -File
$uri = Get-WebListenerUrl -Test 'Multipart'
$result = Invoke-RestMethod -Uri $uri -Body $body -Method 'POST'
2018-03-01 23:00:00 -06:00
$result . Headers . 'Content-Type' | Should -Match 'multipart/form-data'
$result . Files [ 0 ]. FileName | Should -Be 'multipart.txt'
$result . Files [ 0 ]. ContentType | Should -Be 'text/plain'
$result . Files [ 0 ]. Content | Should -Match 'TestContent'
2017-09-12 11:41:36 -05:00
}
2018-01-09 15:43:25 -06:00
2017-09-12 11:41:36 -05:00
It "Verifies Invoke-RestMethod Supports Mixed Multipart String and File Values" {
$body = GetMultipartBody -String -File
$uri = Get-WebListenerUrl -Test 'Multipart'
$result = Invoke-RestMethod -Uri $uri -Body $body -Method 'POST'
2018-03-01 23:00:00 -06:00
$result . Headers . 'Content-Type' | Should -Match 'multipart/form-data'
$result . Items . TestString [ 0 ] | Should -Be 'TestValue'
$result . Files [ 0 ]. FileName | Should -Be 'multipart.txt'
$result . Files [ 0 ]. ContentType | Should -Be 'text/plain'
$result . Files [ 0 ]. Content | Should -Match 'TestContent'
2017-09-12 11:41:36 -05:00
}
2018-01-24 23:17:55 -06:00
It "Verifies Invoke-RestMethod -Form supports string values" {
$form = @ { TestString = "TestValue" }
$uri = Get-WebListenerUrl -Test 'Multipart'
$result = Invoke-RestMethod -Uri $uri -Form $form -Method 'POST'
2018-03-01 23:00:00 -06:00
$result . Headers . 'Content-Type' | Should -Match 'multipart/form-data'
$result . Items . TestString . Count | Should -Be 1
$result . Items . TestString [ 0 ] | Should -Be 'TestValue'
2018-01-24 23:17:55 -06:00
}
It "Verifies Invoke-RestMethod -Form supports a collection of string values" {
$form = @ { TestStrings = "TestValue" , "TestValue2" }
$uri = Get-WebListenerUrl -Test 'Multipart'
$result = Invoke-RestMethod -Uri $uri -Form $form -Method 'POST'
2018-03-01 23:00:00 -06:00
$result . Headers . 'Content-Type' | Should -Match 'multipart/form-data'
$result . Items . TestStrings . Count | Should -Be 2
$result . Items . TestStrings [ 0 ] | Should -Be 'TestValue'
$result . Items . TestStrings [ 1 ] | Should -Be 'TestValue2'
2018-01-24 23:17:55 -06:00
}
It "Verifies Invoke-RestMethod -Form supports file values" {
$form = @ { TestFile = [ System.IO.FileInfo ] $file1Path }
$uri = Get-WebListenerUrl -Test 'Multipart'
$result = Invoke-RestMethod -Uri $uri -Form $form -Method 'POST'
2018-03-01 23:00:00 -06:00
$result . Headers . 'Content-Type' | Should -Match 'multipart/form-data'
$result . Files . Count | Should -Be 1
2018-01-24 23:17:55 -06:00
2018-03-01 23:00:00 -06:00
$result . Files [ 0 ]. Name | Should -Be "TestFile"
$result . Files [ 0 ]. FileName | Should -Be $file1Name
$result . Files [ 0 ]. ContentType | Should -Be 'application/octet-stream'
$result . Files [ 0 ]. Content | Should -Match $file1Contents
2018-01-24 23:17:55 -06:00
}
It "Verifies Invoke-RestMethod -Form supports a collection of file values" {
$form = @ { TestFiles = [ System.IO.FileInfo ] $file1Path , [ System.IO.FileInfo ] $file2Path }
$uri = Get-WebListenerUrl -Test 'Multipart'
$result = Invoke-RestMethod -Uri $uri -Form $form -Method 'POST'
2018-03-01 23:00:00 -06:00
$result . Headers . 'Content-Type' | Should -Match 'multipart/form-data'
$result . Files . Count | Should -Be 2
2018-01-24 23:17:55 -06:00
2018-03-01 23:00:00 -06:00
$result . Files [ 0 ]. Name | Should -Be "TestFiles"
$result . Files [ 0 ]. FileName | Should -Be $file1Name
$result . Files [ 0 ]. ContentType | Should -Be 'application/octet-stream'
$result . Files [ 0 ]. Content | Should -Match $file1Contents
2018-01-24 23:17:55 -06:00
2018-03-01 23:00:00 -06:00
$result . Files [ 1 ]. Name | Should -Be "TestFiles"
$result . Files [ 1 ]. FileName | Should -Be $file2Name
$result . Files [ 1 ]. ContentType | Should -Be 'application/octet-stream'
$result . Files [ 1 ]. Content | Should -Match $file2Contents
2018-01-24 23:17:55 -06:00
}
It "Verifies Invoke-RestMethod -Form supports combinations of strings and files" {
$form = @ {
TestStrings = "TestValue" , "TestValue2"
TestFiles = [ System.IO.FileInfo ] $file1Path , [ System.IO.FileInfo ] $file2Path
}
$uri = Get-WebListenerUrl -Test 'Multipart'
$result = Invoke-RestMethod -Uri $uri -Form $form -Method 'POST'
2018-03-01 23:00:00 -06:00
$result . Headers . 'Content-Type' | Should -Match 'multipart/form-data'
$result . Items . TestStrings . Count | Should -Be 2
$result . Files . Count | Should -Be 2
2018-01-24 23:17:55 -06:00
2018-03-01 23:00:00 -06:00
$result . Items . TestStrings [ 0 ] | Should -Be 'TestValue'
$result . Items . TestStrings [ 1 ] | Should -Be 'TestValue2'
2018-01-24 23:17:55 -06:00
2018-03-01 23:00:00 -06:00
$result . Files [ 0 ]. Name | Should -Be "TestFiles"
$result . Files [ 0 ]. FileName | Should -Be $file1Name
$result . Files [ 0 ]. ContentType | Should -Be 'application/octet-stream'
$result . Files [ 0 ]. Content | Should -Match $file1Contents
2018-01-24 23:17:55 -06:00
2018-03-01 23:00:00 -06:00
$result . Files [ 1 ]. Name | Should -Be "TestFiles"
$result . Files [ 1 ]. FileName | Should -Be $file2Name
$result . Files [ 1 ]. ContentType | Should -Be 'application/octet-stream'
$result . Files [ 1 ]. Content | Should -Match $file2Contents
2018-01-24 23:17:55 -06:00
}
It "Verifies Invoke-RestMethod -Form is mutually exclusive with -Body" {
$form = @ { TestString = "TestValue" }
$body = "test"
$uri = Get-WebListenerUrl -Test 'Multipart'
{ Invoke-RestMethod -Uri $uri -Form $form -Body $Body -ErrorAction 'Stop' } |
2018-05-17 14:42:04 -07:00
Should -Throw -ErrorId 'WebCmdletBodyFormConflictException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand'
2018-01-24 23:17:55 -06:00
}
It "Verifies Invoke-RestMethod -Form is mutually exclusive with -InFile" {
$form = @ { TestString = "TestValue" }
$uri = Get-WebListenerUrl -Test 'Multipart'
{ Invoke-RestMethod -Uri $uri -Form $form -InFile $file1Path -ErrorAction 'Stop' } |
2018-05-17 14:42:04 -07:00
Should -Throw -ErrorId 'WebCmdletFormInFileConflictException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand'
2018-01-24 23:17:55 -06:00
}
2017-09-12 11:41:36 -05:00
}
2017-08-31 10:47:54 -07:00
#region charset encoding tests
Context "Invoke-RestMethod Encoding tests with BasicHtmlWebResponseObject response" {
It "Verifies Invoke-RestMethod detects charset meta value when the ContentType header does not define it." {
2017-12-04 11:52:14 -06:00
$query = @ {
contenttype = 'text/html'
2018-01-09 15:43:25 -06:00
body = '<html><head><meta charset="Unicode"></head></html>'
2017-12-04 11:52:14 -06:00
}
2018-01-09 15:43:25 -06:00
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
2017-08-31 10:47:54 -07:00
$expectedEncoding = [ System.Text.Encoding ]:: GetEncoding ( 'Unicode' )
2017-12-04 11:52:14 -06:00
$response = ExecuteRestMethod -Uri $uri -UseBasicParsing
2017-08-31 10:47:54 -07:00
2018-03-01 23:00:00 -06:00
$response . Error | Should -BeNullOrEmpty
$response . Encoding . EncodingName | Should -Be $expectedEncoding . EncodingName
2017-08-31 10:47:54 -07:00
}
It "Verifies Invoke-WebRequest detects charset meta value when newlines are encountered in the element." {
2017-12-04 11:52:14 -06:00
$query = @ {
contenttype = 'text/html'
2018-01-09 15:43:25 -06:00
body = "<html> `n <head> `n <meta `n charset= `" Unicode `"`n > `n </head> `n </html>"
2017-12-04 11:52:14 -06:00
}
2018-01-09 15:43:25 -06:00
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
2017-08-31 10:47:54 -07:00
$expectedEncoding = [ System.Text.Encoding ]:: GetEncoding ( 'Unicode' )
2017-12-04 11:52:14 -06:00
$response = ExecuteRestMethod -Uri $uri -UseBasicParsing
2017-08-31 10:47:54 -07:00
2018-03-01 23:00:00 -06:00
$response . Error | Should -BeNullOrEmpty
$response . Encoding . EncodingName | Should -Be $expectedEncoding . EncodingName
2017-08-31 10:47:54 -07:00
}
It "Verifies Invoke-RestMethod detects charset meta value when the attribute value is unquoted." {
2017-12-04 11:52:14 -06:00
$query = @ {
contenttype = 'text/html'
2018-01-09 15:43:25 -06:00
body = '<html><head><meta charset = Unicode></head></html>'
2017-12-04 11:52:14 -06:00
}
2018-01-09 15:43:25 -06:00
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
2017-08-31 10:47:54 -07:00
$expectedEncoding = [ System.Text.Encoding ]:: GetEncoding ( 'Unicode' )
2017-12-04 11:52:14 -06:00
$response = ExecuteRestMethod -Uri $uri -UseBasicParsing
2017-08-31 10:47:54 -07:00
2018-03-01 23:00:00 -06:00
$response . Error | Should -BeNullOrEmpty
$response . Encoding . EncodingName | Should -Be $expectedEncoding . EncodingName
2017-08-31 10:47:54 -07:00
}
It "Verifies Invoke-RestMethod detects http-equiv charset meta value when the ContentType header does not define it." {
2017-12-04 11:52:14 -06:00
$query = @ {
contenttype = 'text/html'
2018-01-09 15:43:25 -06:00
body = "<html><head> `n <meta http-equiv= `" content-type `" content= `" text/html; charset=Unicode `" > `n </head> `n </html>"
2017-12-04 11:52:14 -06:00
}
2018-01-09 15:43:25 -06:00
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
2017-08-31 10:47:54 -07:00
$expectedEncoding = [ System.Text.Encoding ]:: GetEncoding ( 'Unicode' )
2017-12-04 11:52:14 -06:00
$response = ExecuteRestMethod -Uri $uri -UseBasicParsing
2017-08-31 10:47:54 -07:00
2018-03-01 23:00:00 -06:00
$response . Error | Should -BeNullOrEmpty
$response . Encoding . EncodingName | Should -Be $expectedEncoding . EncodingName
2017-08-31 10:47:54 -07:00
}
It "Verifies Invoke-RestMethod detects http-equiv charset meta value newlines are encountered in the element." {
2017-12-04 11:52:14 -06:00
$query = @ {
contenttype = 'text/html'
2018-01-09 15:43:25 -06:00
body = "<html><head> `n <meta `n http-equiv= `" content-type `"`n content= `" text/html; charset=Unicode `" > `n </head> `n </html> `n "
2017-12-04 11:52:14 -06:00
}
2018-01-09 15:43:25 -06:00
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
2017-08-31 10:47:54 -07:00
$expectedEncoding = [ System.Text.Encoding ]:: GetEncoding ( 'Unicode' )
2017-12-04 11:52:14 -06:00
$response = ExecuteRestMethod -Uri $uri -UseBasicParsing
2017-08-31 10:47:54 -07:00
2018-03-01 23:00:00 -06:00
$response . Error | Should -BeNullOrEmpty
$response . Encoding . EncodingName | Should -Be $expectedEncoding . EncodingName
2017-08-31 10:47:54 -07:00
}
It "Verifies Invoke-RestMethod ignores meta charset value when Content-Type header defines it." {
2017-12-04 11:52:14 -06:00
$query = @ {
contenttype = 'text/html; charset=utf-8'
2018-01-09 15:43:25 -06:00
body = '<html><head><meta charset="utf-32"></head></html>'
2017-12-04 11:52:14 -06:00
}
2018-01-09 15:43:25 -06:00
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
2017-08-31 10:47:54 -07:00
# NOTE: meta charset should be ignored
$expectedEncoding = [ System.Text.Encoding ]:: UTF8
2017-12-04 11:52:14 -06:00
$response = ExecuteRestMethod -Uri $uri -UseBasicParsing
2017-08-31 10:47:54 -07:00
2018-03-01 23:00:00 -06:00
$response . Error | Should -BeNullOrEmpty
$response . Encoding . EncodingName | Should -Be $expectedEncoding . EncodingName
2017-08-31 10:47:54 -07:00
}
It "Verifies Invoke-RestMethod honors non-utf8 charsets in the Content-Type header" {
2017-12-04 11:52:14 -06:00
$query = @ {
contenttype = 'text/html; charset=utf-16'
2018-01-09 15:43:25 -06:00
body = '<html><head><meta charset="utf-32"></head></html>'
2017-12-04 11:52:14 -06:00
}
2018-01-09 15:43:25 -06:00
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
2017-08-31 10:47:54 -07:00
# NOTE: meta charset should be ignored
$expectedEncoding = [ System.Text.Encoding ]:: GetEncoding ( 'utf-16' )
2017-12-04 11:52:14 -06:00
$response = ExecuteRestMethod -Uri $uri -UseBasicParsing
2017-08-31 10:47:54 -07:00
2018-03-01 23:00:00 -06:00
$response . Error | Should -BeNullOrEmpty
$response . Encoding . EncodingName | Should -Be $expectedEncoding . EncodingName
2017-08-31 10:47:54 -07:00
}
It "Verifies Invoke-RestMethod defaults to iso-8859-1 when an unsupported/invalid charset is declared" {
2017-12-04 11:52:14 -06:00
$query = @ {
contenttype = 'text/html'
2018-01-09 15:43:25 -06:00
body = '<html><head><meta charset="invalid"></head></html>'
2017-12-04 11:52:14 -06:00
}
2018-01-09 15:43:25 -06:00
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
2017-08-31 10:47:54 -07:00
$expectedEncoding = [ System.Text.Encoding ]:: GetEncoding ( 'iso-8859-1' )
2017-12-04 11:52:14 -06:00
$response = ExecuteRestMethod -Uri $uri -UseBasicParsing
2017-08-31 10:47:54 -07:00
2018-03-01 23:00:00 -06:00
$response . Error | Should -BeNullOrEmpty
$response . Encoding . EncodingName | Should -Be $expectedEncoding . EncodingName
2017-08-31 10:47:54 -07:00
}
It "Verifies Invoke-RestMethod defaults to iso-8859-1 when an unsupported/invalid charset is declared using http-equiv" {
2017-12-04 11:52:14 -06:00
$query = @ {
contenttype = 'text/html'
2018-01-09 15:43:25 -06:00
body = "<html><head> `n <meta http-equiv= `" content-type `" content= `" text/html; charset=Invalid `" > `n </head> `n </html>"
2017-12-04 11:52:14 -06:00
}
2018-01-09 15:43:25 -06:00
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
2017-08-31 10:47:54 -07:00
$expectedEncoding = [ System.Text.Encoding ]:: GetEncoding ( 'iso-8859-1' )
2017-12-04 11:52:14 -06:00
$response = ExecuteRestMethod -Uri $uri -UseBasicParsing
2017-08-31 10:47:54 -07:00
2018-03-01 23:00:00 -06:00
$response . Error | Should -BeNullOrEmpty
$response . Encoding . EncodingName | Should -Be $expectedEncoding . EncodingName
2017-08-31 10:47:54 -07:00
}
2018-02-21 23:31:00 -06:00
It "Verifies Invoke-RestMethod defaults to UTF8 on application/json when no charset is present" {
# when contenttype is set, WebListener suppresses charset unless it is included in the query
$query = @ {
contenttype = 'application/json'
body = '{"Test": "Test"}'
}
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
$expectedEncoding = [ System.Text.Encoding ]:: UTF8
$response = ExecuteRestMethod -Uri $uri -UseBasicParsing
2018-03-01 23:00:00 -06:00
$response . Error | Should -BeNullOrEmpty
$response . Encoding . EncodingName | Should -Be $expectedEncoding . EncodingName
$response . output . Test | Should -BeExactly 'Test'
2018-02-21 23:31:00 -06:00
}
2017-08-31 10:47:54 -07:00
}
#endregion charset encoding tests
2017-09-25 12:11:12 -05:00
Context 'Invoke-RestMethod ResponseHeadersVariable Tests' {
It "Verifies Invoke-RestMethod supports -ResponseHeadersVariable" {
$uri = Get-WebListenerUrl -Test '/'
$response = Invoke-RestMethod -Uri $uri -ResponseHeadersVariable 'headers'
2018-03-01 23:00:00 -06:00
$headers . 'Content-Type' | Should -Be 'text/html; charset=utf-8'
$headers . Server | Should -Be 'Kestrel'
2017-09-25 12:11:12 -05:00
}
2017-10-05 09:46:33 -07:00
2017-09-25 12:11:12 -05:00
It "Verifies Invoke-RestMethod supports -ResponseHeadersVariable overwriting existing variable" {
$uri = Get-WebListenerUrl -Test '/'
$headers = 'prexisting'
$response = Invoke-RestMethod -Uri $uri -ResponseHeadersVariable 'headers'
2018-03-01 23:00:00 -06:00
$headers | Should -Not -Be 'prexisting'
$headers . 'Content-Type' | Should -Be 'text/html; charset=utf-8'
$headers . Server | Should -Be 'Kestrel'
2017-09-25 12:11:12 -05:00
}
}
2017-10-17 21:08:06 -05:00
Context "Invoke-RestMethod -Authentication tests" {
BeforeAll {
#[SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Demo/doc/test secret.")]
$token = "testpassword" | ConvertTo-SecureString -AsPlainText -Force
2018-01-09 15:43:25 -06:00
$credential = [ pscredential ]:: new ( "testuser" , $token )
2017-10-17 21:08:06 -05:00
$httpUri = Get-WebListenerUrl -Test 'Get'
$httpsUri = Get-WebListenerUrl -Test 'Get' -Https
2017-11-10 10:11:57 -06:00
$httpBasicUri = Get-WebListenerUrl -Test 'Auth' -TestValue 'Basic'
$httpsBasicUri = Get-WebListenerUrl -Test 'Auth' -TestValue 'Basic' -Https
2017-10-17 21:08:06 -05:00
$testCases = @ (
@ { Authentication = "bearer" }
@ { Authentication = "OAuth" }
)
}
It "Verifies Invoke-RestMethod -Authentication Basic" {
$params = @ {
2018-01-09 15:43:25 -06:00
Uri = $httpsUri
Authentication = "Basic"
Credential = $credential
2017-10-17 21:08:06 -05:00
SkipCertificateCheck = $true
}
$result = Invoke-RestMethod @params
2018-03-01 23:00:00 -06:00
$result . Headers . Authorization | Should -BeExactly "Basic dGVzdHVzZXI6dGVzdHBhc3N3b3Jk"
2017-10-17 21:08:06 -05:00
}
It "Verifies Invoke-RestMethod -Authentication <Authentication>" -TestCases $testCases {
param ( $Authentication )
$params = @ {
2018-01-09 15:43:25 -06:00
Uri = $httpsUri
Authentication = $Authentication
Token = $token
2017-10-17 21:08:06 -05:00
SkipCertificateCheck = $true
}
$result = Invoke-RestMethod @params
2018-03-01 23:00:00 -06:00
$result . Headers . Authorization | Should -BeExactly "Bearer testpassword"
2017-10-17 21:08:06 -05:00
}
It "Verifies Invoke-RestMethod -Authentication does not support -UseDefaultCredentials" {
$params = @ {
2018-01-09 15:43:25 -06:00
Uri = $httpsUri
Token = $token
Authentication = "OAuth"
2017-10-17 21:08:06 -05:00
UseDefaultCredentials = $true
2018-01-09 15:43:25 -06:00
ErrorAction = 'Stop'
SkipCertificateCheck = $true
2017-10-17 21:08:06 -05:00
}
2018-05-17 14:42:04 -07:00
{ Invoke-RestMethod @params } | Should -Throw -ErrorId "WebCmdletAuthenticationConflictException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand"
2017-10-17 21:08:06 -05:00
}
It "Verifies Invoke-RestMethod -Authentication does not support Both -Credential and -Token" {
$params = @ {
2018-01-09 15:43:25 -06:00
Uri = $httpsUri
Token = $token
Credential = $credential
Authentication = "OAuth"
ErrorAction = 'Stop'
2017-10-17 21:08:06 -05:00
SkipCertificateCheck = $true
}
2018-05-17 14:42:04 -07:00
{ Invoke-RestMethod @params } | Should -Throw -ErrorId "WebCmdletAuthenticationTokenConflictException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand"
2017-10-17 21:08:06 -05:00
}
It "Verifies Invoke-RestMethod -Authentication <Authentication> requires -Token" -TestCases $testCases {
param ( $Authentication )
$params = @ {
2018-01-09 15:43:25 -06:00
Uri = $httpsUri
Authentication = $Authentication
ErrorAction = 'Stop'
2017-10-17 21:08:06 -05:00
SkipCertificateCheck = $true
}
2018-05-17 14:42:04 -07:00
{ Invoke-RestMethod @params } | Should -Throw -ErrorId "WebCmdletAuthenticationTokenNotSuppliedException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand"
2017-10-17 21:08:06 -05:00
}
It "Verifies Invoke-RestMethod -Authentication Basic requires -Credential" {
$params = @ {
2018-01-09 15:43:25 -06:00
Uri = $httpsUri
Authentication = "Basic"
ErrorAction = 'Stop'
2017-10-17 21:08:06 -05:00
SkipCertificateCheck = $true
}
2018-05-17 14:42:04 -07:00
{ Invoke-RestMethod @params } | Should -Throw -ErrorId "WebCmdletAuthenticationCredentialNotSuppliedException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand"
2017-10-17 21:08:06 -05:00
}
It "Verifies Invoke-RestMethod -Authentication Requires HTTPS" {
$params = @ {
2018-01-09 15:43:25 -06:00
Uri = $httpUri
Token = $token
2017-10-17 21:08:06 -05:00
Authentication = "OAuth"
2018-01-09 15:43:25 -06:00
ErrorAction = 'Stop'
2017-10-17 21:08:06 -05:00
}
2018-05-17 14:42:04 -07:00
{ Invoke-RestMethod @params } | Should -Throw -ErrorId "WebCmdletAllowUnencryptedAuthenticationRequiredException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand"
2017-10-17 21:08:06 -05:00
}
It "Verifies Invoke-RestMethod -Authentication Can use HTTP with -AllowUnencryptedAuthentication" {
$params = @ {
2018-01-09 15:43:25 -06:00
Uri = $httpUri
Token = $token
Authentication = "OAuth"
2017-10-17 21:08:06 -05:00
AllowUnencryptedAuthentication = $true
}
$result = Invoke-RestMethod @params
2018-03-01 23:00:00 -06:00
$result . Headers . Authorization | Should -BeExactly "Bearer testpassword"
2017-10-17 21:08:06 -05:00
}
2017-11-10 10:11:57 -06:00
It "Verifies Invoke-RestMethod Negotiated -Credential over HTTPS" {
$params = @ {
2018-01-09 15:43:25 -06:00
Uri = $httpsBasicUri
Credential = $credential
2017-11-10 10:11:57 -06:00
SkipCertificateCheck = $true
}
$result = Invoke-RestMethod @params
2018-03-01 23:00:00 -06:00
$result . Headers . Authorization | Should -BeExactly "Basic dGVzdHVzZXI6dGVzdHBhc3N3b3Jk"
2017-11-10 10:11:57 -06:00
}
It "Verifies Invoke-RestMethod Negotiated -Credential Requires HTTPS" {
$params = @ {
2018-01-09 15:43:25 -06:00
Uri = $httpBasicUri
Credential = $credential
2017-11-10 10:11:57 -06:00
ErrorAction = 'Stop'
}
2018-05-17 14:42:04 -07:00
{ Invoke-RestMethod @params } | Should -Throw -ErrorId "WebCmdletAllowUnencryptedAuthenticationRequiredException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand"
2017-11-10 10:11:57 -06:00
}
It "Verifies Invoke-RestMethod Negotiated -Credential Can use HTTP with -AllowUnencryptedAuthentication" {
$params = @ {
2018-01-09 15:43:25 -06:00
Uri = $httpBasicUri
Credential = $credential
2017-11-10 10:11:57 -06:00
AllowUnencryptedAuthentication = $true
}
$result = Invoke-RestMethod @params
2018-03-01 23:00:00 -06:00
$result . Headers . Authorization | Should -BeExactly "Basic dGVzdHVzZXI6dGVzdHBhc3N3b3Jk"
2017-11-10 10:11:57 -06:00
}
# UseDefaultCredentials is only reliably testable on Windows
It "Verifies Invoke-RestMethod Negotiated -UseDefaultCredentials with '<AuthType>' over HTTPS" -Skip: $ (! $IsWindows ) -TestCases @ (
@ { AuthType = 'NTLM' }
@ { AuthType = 'Negotiate' }
) {
param ( $AuthType )
$params = @ {
2018-01-09 15:43:25 -06:00
Uri = Get-WebListenerUrl -Test 'Auth' -TestValue $AuthType -Https
2017-11-10 10:11:57 -06:00
UseDefaultCredentials = $true
2018-01-09 15:43:25 -06:00
SkipCertificateCheck = $true
2017-11-10 10:11:57 -06:00
}
$result = Invoke-RestMethod @params
2018-03-01 23:00:00 -06:00
$result . Headers . Authorization | Should -Match "^ $AuthType "
2017-11-10 10:11:57 -06:00
}
# The error condition can at least be tested on all platforms.
It "Verifies Invoke-RestMethod Negotiated -UseDefaultCredentials Requires HTTPS" {
$params = @ {
2018-01-09 15:43:25 -06:00
Uri = $httpUri
2017-11-10 10:11:57 -06:00
UseDefaultCredentials = $true
2018-01-09 15:43:25 -06:00
ErrorAction = 'Stop'
2017-11-10 10:11:57 -06:00
}
2018-05-17 14:42:04 -07:00
{ Invoke-RestMethod @params } | Should -Throw -ErrorId "WebCmdletAllowUnencryptedAuthenticationRequiredException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand"
2017-11-10 10:11:57 -06:00
}
# UseDefaultCredentials is only reliably testable on Windows
It "Verifies Invoke-RestMethod Negotiated -UseDefaultCredentials with '<AuthType>' Can use HTTP with -AllowUnencryptedAuthentication" -Skip: $ (! $IsWindows ) -TestCases @ (
@ { AuthType = 'NTLM' }
@ { AuthType = 'Negotiate' }
) {
param ( $AuthType )
$params = @ {
2018-01-09 15:43:25 -06:00
Uri = Get-WebListenerUrl -Test 'Auth' -TestValue $AuthType
UseDefaultCredentials = $true
2017-11-10 10:11:57 -06:00
AllowUnencryptedAuthentication = $true
}
$result = Invoke-RestMethod @params
2018-03-01 23:00:00 -06:00
$result . Headers . Authorization | Should -Match "^ $AuthType "
2017-11-10 10:11:57 -06:00
}
2017-10-17 21:08:06 -05:00
}
2017-11-13 12:45:46 -06:00
Context "Invoke-RestMethod -SslProtocol Test" {
2017-12-04 11:38:30 -08:00
BeforeAll {
$testCases1 = @ (
@ { Test = @ { SslProtocol = 'Default' ; ActualProtocol = 'Default' }; Pending = $false }
@ { Test = @ { SslProtocol = 'Tls' ; ActualProtocol = 'Tls' }; Pending = $false }
2017-12-06 14:20:44 -06:00
@ { Test = @ { SslProtocol = 'Tls11' ; ActualProtocol = 'Tls11' }; Pending = $false }
@ { Test = @ { SslProtocol = 'Tls12' ; ActualProtocol = 'Tls12' }; Pending = $false }
2018-05-09 06:35:52 -05:00
@ { Test = @ { SslProtocol = 'Tls, Tls11, Tls12' ; ActualProtocol = 'Tls12' }; Pending = $false }
@ { Test = @ { SslProtocol = 'Tls11, Tls12' ; ActualProtocol = 'Tls12' }; Pending = $false }
@ { Test = @ { SslProtocol = 'Tls, Tls11, Tls12' ; ActualProtocol = 'Tls11' }; Pending = $false }
@ { Test = @ { SslProtocol = 'Tls11, Tls12' ; ActualProtocol = 'Tls11' }; Pending = $false }
@ { Test = @ { SslProtocol = 'Tls, Tls11' ; ActualProtocol = 'Tls11' }; Pending = $false }
@ { Test = @ { SslProtocol = 'Tls, Tls11, Tls12' ; ActualProtocol = 'Tls' }; Pending = $false }
@ { Test = @ { SslProtocol = 'Tls, Tls11' ; ActualProtocol = 'Tls' }; Pending = $false }
# Skipping intermediary protocols is not supported on all platforms
@ { Test = @ { SslProtocol = 'Tls, Tls12' ; ActualProtocol = 'Tls' }; Pending = -not $IsWindows }
2017-12-04 11:38:30 -08:00
@ { Test = @ { SslProtocol = 'Tls, Tls12' ; ActualProtocol = 'Tls12' }; Pending = -not $IsWindows }
)
2017-11-13 12:45:46 -06:00
2017-12-04 11:38:30 -08:00
$testCases2 = @ (
2018-01-09 15:43:25 -06:00
@ { Test = @ { IntendedProtocol = 'Tls' ; ActualProtocol = 'Tls12' }; Pending = $false }
@ { Test = @ { IntendedProtocol = 'Tls' ; ActualProtocol = 'Tls11' }; Pending = $false }
2017-12-04 11:38:30 -08:00
@ { Test = @ { IntendedProtocol = 'Tls11' ; ActualProtocol = 'Tls12' }; Pending = $false }
@ { Test = @ { IntendedProtocol = 'Tls12' ; ActualProtocol = 'Tls11' }; Pending = $false }
2017-12-06 14:20:44 -06:00
@ { Test = @ { IntendedProtocol = 'Tls11' ; ActualProtocol = 'Tls' }; Pending = $false }
@ { Test = @ { IntendedProtocol = 'Tls12' ; ActualProtocol = 'Tls' }; Pending = $false }
2018-05-09 06:35:52 -05:00
@ { Test = @ { IntendedProtocol = 'Tls11, Tls12' ; ActualProtocol = 'Tls' }; Pending = $false }
@ { Test = @ { IntendedProtocol = 'Tls, Tls12' ; ActualProtocol = 'Tls11' }; Pending = $false }
@ { Test = @ { IntendedProtocol = 'Tls, Tls11' ; ActualProtocol = 'Tls12' }; Pending = $false }
2017-12-04 11:38:30 -08:00
)
2017-11-13 12:45:46 -06:00
}
2017-12-04 11:38:30 -08:00
foreach ( $entry in $testCases1 ) {
It "Verifies Invoke-RestMethod -SslProtocol <SslProtocol> works on <ActualProtocol>" -TestCases ( $entry . Test ) -Pending: ( $entry . Pending ) {
param ( $SslProtocol , $ActualProtocol )
$params = @ {
2018-01-09 15:43:25 -06:00
Uri = Get-WebListenerUrl -Test 'Get' -Https -SslProtocol $ActualProtocol
SslProtocol = $SslProtocol
2017-12-04 11:38:30 -08:00
SkipCertificateCheck = $true
}
$result = Invoke-RestMethod @params
2018-03-01 23:00:00 -06:00
$result . headers . Host | Should -Be $params . Uri . Authority
2017-11-13 12:45:46 -06:00
}
}
2017-12-04 11:38:30 -08:00
foreach ( $entry in $testCases2 ) {
It "Verifies Invoke-RestMethod -SslProtocol <IntendedProtocol> fails on a <ActualProtocol> only connection" -TestCases ( $entry . Test ) -Pending: ( $entry . Pending ) {
param ( $IntendedProtocol , $ActualProtocol )
$params = @ {
2018-01-09 15:43:25 -06:00
Uri = Get-WebListenerUrl -Test 'Get' -Https -SslProtocol $ActualProtocol
SslProtocol = $IntendedProtocol
2017-12-04 11:38:30 -08:00
SkipCertificateCheck = $true
2018-01-09 15:43:25 -06:00
ErrorAction = 'Stop'
2017-12-04 11:38:30 -08:00
}
2018-05-17 14:42:04 -07:00
{ Invoke-RestMethod @params } | Should -Throw -ErrorId 'WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand'
2017-12-04 11:38:30 -08:00
}
}
2017-11-13 12:45:46 -06:00
}
2017-11-13 21:53:43 -06:00
Context "Invoke-RestMethod Single Value JSON null support" {
It "Invoke-RestMethod Supports a Single Value JSON null" {
2017-12-04 11:52:14 -06:00
$query = @ {
contenttype = 'application/json'
2018-01-09 15:43:25 -06:00
body = 'null'
2017-12-04 11:52:14 -06:00
}
2018-01-09 15:43:25 -06:00
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
2018-03-01 23:00:00 -06:00
Invoke-RestMethod -Uri $uri | Should -Be $null
2017-11-13 21:53:43 -06:00
}
2018-01-09 15:43:25 -06:00
2017-11-13 21:53:43 -06:00
It "Invoke-RestMethod Supports a Single Value JSON null and ignores whitespace" {
2017-12-04 11:52:14 -06:00
$query = @ {
contenttype = 'application/json'
2018-01-09 15:43:25 -06:00
body = " null "
2017-12-04 11:52:14 -06:00
}
2018-01-09 15:43:25 -06:00
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
2018-03-01 23:00:00 -06:00
Invoke-RestMethod -Uri $uri | Should -Be $null
2017-12-04 11:52:14 -06:00
$query [ 'body' ] = " null `n "
2018-01-09 15:43:25 -06:00
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
2018-03-01 23:00:00 -06:00
Invoke-RestMethod -Uri $uri | Should -Be $null
2017-11-13 21:53:43 -06:00
}
}
2018-03-26 12:23:24 -07:00
Context "Invoke-RestMethod File Resume Feature" {
BeforeAll {
$outFile = Join-Path $TestDrive "resume.txt"
# 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
$referenceFileHash = Get-FileHash -Algorithm SHA256 -Path $referenceFile
$referenceFileSize = Get-Item $referenceFile | Select-Object -ExpandProperty Length
}
AfterEach {
Remove-Item -Force -ErrorAction 'SilentlyContinue' -Path $outFile
}
It "Invoke-RestMethod -Resume requires -OutFile" {
{ Invoke-RestMethod -Resume -Uri $resumeUri -ErrorAction Stop } |
Should -Throw -ErrorId 'WebCmdletOutFileMissingException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand'
}
It "Invoke-RestMethod -Resume Downloads the whole file when the file does not exist" {
# ensure the file does not exist
Remove-Item -Force -ErrorAction 'SilentlyContinue' -Path $outFile
Invoke-RestMethod -uri $resumeUri -OutFile $outFile -ResponseHeadersVariable 'Headers' -Resume
$outFileHash = Get-FileHash -Algorithm SHA256 -Path $outFile
$outFileHash . Hash | Should -BeExactly $referenceFileHash . Hash
Get-Item $outFile | Select-Object -ExpandProperty Length | Should -Be $referenceFileSize
$Headers . 'X-WebListener-Has-Range' [ 0 ] | Should -BeExactly 'true'
$Headers . 'X-WebListener-Request-Range' [ 0 ] | Should -BeExactly 'bytes=0-'
$Headers . 'Content-Range' [ 0 ] | Should -BeExactly "bytes 0- $( $referenceFileSize - 1 ) / $referenceFileSize "
}
It "Invoke-RestMethod -Resume overwrites an existing file that is larger than the remote file" {
# Create a file larger than the download file
$largerFileSize = $referenceFileSize + 20
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
$outFileHash = Get-FileHash -Algorithm SHA256 -Path $outFile
$outFileHash . Hash | Should -BeExactly $referenceFileHash . Hash
Get-Item $outFile | Select-Object -ExpandProperty Length | Should -Be $referenceFileSize
Get-Item $outFile | Select-Object -ExpandProperty Length | Should -BeLessThan $largerFileSize
$Headers . 'X-WebListener-Has-Range' [ 0 ] | Should -BeExactly 'false'
$Headers . ContainsKey ( 'Content-Range' ) | Should -BeFalse
}
It "Invoke-RestMethod -Resume overwrites existing file when remote server does not support resume" {
# Create a file larger than the download file
$largerFileSize = $referenceFileSize + 20
1 .. $largerFileSize | ForEach-Object { [ Byte ] $_ } | Set-Content -AsByteStream $outFile
$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
$outFileHash = Get-FileHash -Algorithm SHA256 -Path $outFile
$outFileHash . Hash | Should -BeExactly $referenceFileHash . Hash
Get-Item $outFile | Select-Object -ExpandProperty Length | Should -Be $referenceFileSize
Get-Item $outFile | Select-Object -ExpandProperty Length | Should -BeLessThan $largerFileSize
$Headers . 'X-WebListener-Has-Range' [ 0 ] | Should -BeExactly 'true'
$Headers . 'X-WebListener-Request-Range' [ 0 ] | Should -BeExactly "bytes= $largerFileSize -"
$Headers . ContainsKey ( 'Content-Range' ) | Should -BeFalse
}
It "Invoke-RestMethod -Resume resumes downloading from <bytes> bytes" -TestCases @ (
@ { bytes = 4 }
@ { bytes = 8 }
@ { bytes = 12 }
@ { bytes = 16 }
) {
param ( $bytes )
# Simulate partial download
$uri = Get-WebListenerUrl -Test 'Resume' -TestValue "Bytes/ $bytes "
$null = Invoke-RestMethod -uri $uri -OutFile $outFile
Get-Item $outFile | Select-Object -ExpandProperty Length | Should -Be $bytes
$response = Invoke-RestMethod -uri $resumeUri -OutFile $outFile -ResponseHeadersVariable 'Headers' -Resume
$outFileHash = Get-FileHash -Algorithm SHA256 -Path $outFile
$outFileHash . Hash | Should BeExactly $referenceFileHash . Hash
Get-Item $outFile | Select-Object -ExpandProperty Length | Should -Be $referenceFileSize
$Headers . 'X-WebListener-Has-Range' [ 0 ] | Should -BeExactly 'true'
$Headers . 'X-WebListener-Request-Range' [ 0 ] | Should -BeExactly "bytes= $bytes -"
$Headers . 'Content-Range' [ 0 ] | Should -BeExactly "bytes $bytes - $( $referenceFileSize - 1 ) / $referenceFileSize "
}
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
$fileSize = Get-Item $outFile | Select-Object -ExpandProperty Length
$response = Invoke-RestMethod -uri $resumeUri -OutFile $outFile -ResponseHeadersVariable 'Headers' -Resume
$outFileHash = Get-FileHash -Algorithm SHA256 -Path $outFile
$outFileHash . Hash | Should -BeExactly $referenceFileHash . Hash
Get-Item $outFile | Select-Object -ExpandProperty Length | Should -Be $referenceFileSize
$Headers . 'X-WebListener-Has-Range' [ 0 ] | Should -BeExactly 'true'
$Headers . 'X-WebListener-Request-Range' [ 0 ] | Should -BeExactly "bytes= $fileSize -"
$Headers . 'Content-Range' [ 0 ] | Should -BeExactly "bytes */ $referenceFileSize "
}
}
2016-07-22 15:09:03 -07:00
}
2017-02-01 10:14:24 -08:00
Describe "Validate Invoke-WebRequest and Invoke-RestMethod -InFile" -Tags "Feature" {
2018-04-09 23:12:45 -07:00
BeforeAll {
$WebListener = Start-WebListener
}
2017-02-01 10:14:24 -08:00
Context "InFile parameter negative tests" {
2017-12-13 09:28:05 -06:00
BeforeAll {
$uri = Get-WebListenerUrl -Test 'Post'
$testCases = @ (
2018-01-09 15:43:25 -06:00
#region INVOKE-WEBREQUEST
2017-12-13 09:28:05 -06:00
@ {
2018-01-09 15:43:25 -06:00
Name = 'Validate error for Invoke-WebRequest -InFile ""'
ScriptBlock = { Invoke-WebRequest -Uri $uri -Method Post -InFile "" }
2017-12-13 09:28:05 -06:00
ExpectedFullyQualifiedErrorId = 'WebCmdletInFileNotFilePathException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand'
}
2017-02-01 10:14:24 -08:00
2017-12-13 09:28:05 -06:00
@ {
2018-01-09 15:43:25 -06:00
Name = 'Validate error for Invoke-WebRequest -InFile'
ScriptBlock = { Invoke-WebRequest -Uri $uri -Method Post -InFile }
2017-12-13 09:28:05 -06:00
ExpectedFullyQualifiedErrorId = 'MissingArgument,Microsoft.PowerShell.Commands.InvokeWebRequestCommand'
}
2017-02-01 10:14:24 -08:00
2017-12-13 09:28:05 -06:00
@ {
2018-01-09 15:43:25 -06:00
Name = "Validate error for Invoke-WebRequest -InFile $TestDrive \content.txt"
ScriptBlock = { Invoke-WebRequest -Uri $uri -Method Post -InFile $TestDrive \ content . txt }
2017-12-13 09:28:05 -06:00
ExpectedFullyQualifiedErrorId = 'PathNotFound,Microsoft.PowerShell.Commands.InvokeWebRequestCommand'
}
2018-01-09 15:43:25 -06:00
#endregion
2017-02-01 10:14:24 -08:00
2018-01-09 15:43:25 -06:00
#region INVOKE-RESTMETHOD
2017-12-13 09:28:05 -06:00
@ {
2018-01-09 15:43:25 -06:00
Name = "Validate error for Invoke-RestMethod -InFile ''"
ScriptBlock = { Invoke-RestMethod -Uri $uri -Method Post -InFile '' }
2017-12-13 09:28:05 -06:00
ExpectedFullyQualifiedErrorId = 'WebCmdletInFileNotFilePathException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand'
}
2017-02-01 10:14:24 -08:00
2017-12-13 09:28:05 -06:00
@ {
2018-01-09 15:43:25 -06:00
Name = "Validate error for Invoke-RestMethod -InFile <null>"
ScriptBlock = { Invoke-RestMethod -Uri $uri -Method Post -InFile }
2017-12-13 09:28:05 -06:00
ExpectedFullyQualifiedErrorId = 'MissingArgument,Microsoft.PowerShell.Commands.InvokeRestMethodCommand'
}
2017-02-01 10:14:24 -08:00
2017-12-13 09:28:05 -06:00
@ {
2018-01-09 15:43:25 -06:00
Name = "Validate error for Invoke-RestMethod -InFile $TestDrive \content.txt"
ScriptBlock = { Invoke-RestMethod -Uri $uri -Method Post -InFile $TestDrive \ content . txt }
2017-12-13 09:28:05 -06:00
ExpectedFullyQualifiedErrorId = 'PathNotFound,Microsoft.PowerShell.Commands.InvokeRestMethodCommand'
}
2018-01-09 15:43:25 -06:00
#endregion
2017-12-13 09:28:05 -06:00
)
}
2017-02-01 10:14:24 -08:00
It "<Name>" -TestCases $testCases {
param ( $scriptblock , $expectedFullyQualifiedErrorId )
2018-05-17 14:42:04 -07:00
{ & $scriptblock } | Should -Throw -ErrorId $ExpectedFullyQualifiedErrorId
2017-02-01 10:14:24 -08:00
}
}
Context "InFile parameter positive tests" {
BeforeAll {
$filePath = Join-Path $TestDrive test . txt
2017-12-13 09:28:05 -06:00
New-Item -Path $filePath -Value "hello=world" -ItemType File -Force
$uri = Get-WebListenerUrl -Test 'Post'
2017-02-01 10:14:24 -08:00
}
It "Invoke-WebRequest -InFile" {
2017-12-13 09:28:05 -06:00
$result = Invoke-WebRequest -InFile $filePath -Uri $uri -Method Post
2017-02-01 10:14:24 -08:00
$content = $result . Content | ConvertFrom-Json
2018-03-01 23:00:00 -06:00
$content . form . hello . Count | Should -Be 1
$content . form . hello [ 0 ] | Should -Match "world"
2017-02-01 10:14:24 -08:00
}
It "Invoke-RestMethod -InFile" {
2017-12-13 09:28:05 -06:00
$result = Invoke-RestMethod -InFile $filePath -Uri $uri -Method Post
2018-03-01 23:00:00 -06:00
$result . form . hello . Count | Should -Be 1
$result . form . hello [ 0 ] | Should -Match "world"
2017-02-01 10:14:24 -08:00
}
}
}
Describe "Web cmdlets tests using the cmdlet's aliases" -Tags "CI" {
2017-07-18 12:33:40 -07:00
BeforeAll {
2017-12-04 11:52:14 -06:00
$WebListener = Start-WebListener
2017-02-01 10:14:24 -08:00
}
2017-07-18 12:33:40 -07:00
It "Execute Invoke-WebRequest" {
2017-12-04 11:52:14 -06:00
$query = @ {
2018-01-09 15:43:25 -06:00
body = "hello"
contenttype = 'text/plain'
2017-12-04 11:52:14 -06:00
}
2018-01-09 15:43:25 -06:00
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
2018-02-02 13:56:06 -06:00
$result = iwr $uri
2018-03-01 23:00:00 -06:00
$result . StatusCode | Should -Be "200"
$result . Content | Should -Be "hello"
2017-02-01 10:14:24 -08:00
}
2017-07-18 12:33:40 -07:00
It "Execute Invoke-RestMethod" {
2017-12-04 11:52:14 -06:00
$query = @ {
contenttype = 'application/json'
2018-01-09 15:43:25 -06:00
body = @ { Hello = "world" } | ConvertTo-Json -Compress
2017-12-04 11:52:14 -06:00
}
2018-01-09 15:43:25 -06:00
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
2018-02-02 13:56:06 -06:00
$result = irm $uri
2018-03-01 23:00:00 -06:00
$result . Hello | Should -Be "world"
2017-02-01 10:14:24 -08:00
}
}