Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
# This is a Pester test suite which validate the Web cmdlets.
#
# Note: These tests use data from http://httpbin.org/
# Note: These tests use data from WebListener
#

# Invokes the given command via script block invocation.
Expand Down Expand Up @@ -642,51 +642,29 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" {
# patch Returns PATCH data.
# put Returns PUT data.
# delete Returns DELETE data
$testMethods = @("GET", "POST", "PATCH", "PUT", "DELETE")
$testMethods = @("POST", "PATCH", "PUT", "DELETE")
$contentTypes = @("text/plain", "application/xml", "application/json")

foreach ($contentType in $contentTypes)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor comment - I'd prefer TestCases here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have an open issue somewhere about refactoring the web cmdlet tests. There is much room for improvement.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{
foreach ($method in $testMethods)
{
# Operation options
$operation = $method.ToLower()
$uri = "http://httpbin.org/$operation"
$uri = Get-WebListenerUrl -Test $method
$body = GetTestData -contentType $contentType
$command = "Invoke-WebRequest -Uri $uri -Body '$body' -Method $method -ContentType $contentType"

if ($method -eq "GET")
{
$command = "Invoke-WebRequest -Uri $uri"
}
else
{
$command = "Invoke-WebRequest -Uri $uri -Body '$body' -Method $method -ContentType $contentType -TimeoutSec 5"
}

It "$command" {
It "Invoke-WebRequest -Uri $uri -Method $method -ContentType $contentType -Body [body data]" {

$result = ExecuteWebCommand -command $command
ValidateResponse -response $result

# Validate response content
$jsonContent = $result.Output.Content | ConvertFrom-Json
$jsonContent.url | Should Match $uri

# For a GET request, there is no data property to validate.
if ($method -ne "GET")
{
$jsonContent.headers.'Content-Type' | Should Match $contentType

# Validate that the response Content.data field is the same as what we sent.
if ($contentType -eq "application/xml")
{
$jsonContent.data | Should Be $body
}
else
{
$jsonContent.data | Should Match $body
}
}
$jsonContent.headers.'Content-Type' | Should Match $contentType
# Validate that the response Content.data field is the same as what we sent.
$jsonContent.data | Should Be $body
}
}
}
Expand Down Expand Up @@ -760,9 +738,12 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" {

It "Validate Invoke-WebRequest default ContentType for CustomMethod POST" {

$command = "Invoke-WebRequest -Uri 'http://httpbin.org/post' -CustomMethod POST -Body 'testparam=testvalue'"
$uri = Get-WebListenerUrl -Test 'Post'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test header say "default ContentType" but why the test don't check a content type name?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default content type as in -ContentType is not specified. see the line below this.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I misinterpreted the question. The headline says we're checking the default content type, but inside the test we're not doing it. I expect something like

 $jsonContent.headers.'Content-Type' | Should Be "default-content-type-value" 

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will take another look at the surrounding code and try to determine what this test was really looking for.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not 100% sure what this is testing for, so i added the Headers test.
Fixed

$command = "Invoke-WebRequest -Uri '$uri' -CustomMethod POST -Body 'testparam=testvalue'"
$result = ExecuteWebCommand -command $command
($result.Output.Content | ConvertFrom-Json).form.testparam | Should Be "testvalue"
$jsonResult = $result.Output.Content | ConvertFrom-Json
$jsonResult.form.testparam | Should Be "testvalue"
$jsonResult.Headers.'Content-Type' | Should Be "application/x-www-form-urlencoded"
}

It "Validate Invoke-WebRequest body is converted to query params for CustomMethod GET" {
Expand All @@ -775,14 +756,20 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" {

It "Validate Invoke-WebRequest returns HTTP errors in exception" {

$command = "Invoke-WebRequest -Uri http://httpbin.org/status/418"
$query = @{
body = "I am a teapot!!!"
statuscode = 418
responsephrase = "I am a teapot"
}
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
$command = "Invoke-WebRequest -Uri '$uri'"
$result = ExecuteWebCommand -command $command

$result.Error.ErrorDetails.Message | Should Match "\-=\[ teapot \]"
$result.Error.Exception | Should BeOfType Microsoft.PowerShell.Commands.HttpResponseException
$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 "I'm a teapot"
$result.Error.Exception.Message | Should Match ": 418 \(I'm a teapot\)\."
$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"
}

Expand Down Expand Up @@ -1622,7 +1609,9 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" {
It "Validate Invoke-RestMethod error with -Proxy option - '<name>'" -TestCases $testCase {
param($proxy_address, $name, $protocol)

$command = "Invoke-RestMethod -Uri '${protocol}://httpbin.org/' -Proxy '${proxy_address}'"
# A non-loopback URI is required but the external resource will not be accessed
$uri = 'http://httpbin.org'
$command = "Invoke-RestMethod -Uri '$uri' -Proxy '${proxy_address}'"

$result = ExecuteWebCommand -command $command
$result.Error.FullyQualifiedErrorId | Should Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand"
Expand Down Expand Up @@ -1679,49 +1668,28 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" {
# patch Returns PATCH data.
# put Returns PUT data.
# delete Returns DELETE data
$testMethods = @("GET", "POST", "PATCH", "PUT", "DELETE")
$testMethods = @("POST", "PATCH", "PUT", "DELETE")
$contentTypes = @("text/plain", "application/xml", "application/json")

foreach ($contentType in $contentTypes)
{
foreach ($method in $testMethods)
{
# Operation options
$operation = $method.ToLower()
$uri = "http://httpbin.org/$operation"
$uri = Get-WebListenerUrl -Test $method
$body = GetTestData -contentType $contentType
$command = "Invoke-RestMethod -Uri $uri -Body '$body' -Method $method -ContentType $contentType"

if ($method -eq "GET")
{
$command = "Invoke-RestMethod -Uri $uri"
}
else
{
$command = "Invoke-RestMethod -Uri $uri -Body '$body' -Method $method -ContentType $contentType -TimeoutSec 5"
}

It "$command" {
It "Invoke-RestMethod -Uri $uri -Method $method -ContentType $contentType -Body [body data]" {

$result = ExecuteWebCommand -command $command

# Validate response
$result.Output.url | Should Match $uri
$result.Output.headers.'Content-Type' | Should Match $contentType

# For a GET request, there is no data property to validate.
if ($method -ne "GET")
{
$result.Output.headers.'Content-Type' | Should Match $contentType

# Validate that the response Content.data field is the same as what we sent.
if ($contentType -eq "application/xml")
{
$result.Output.data | Should Be $body
}
else
{
$result.Output.data | Should Match $body
}
}
# Validate that the response Content.data field is the same as what we sent.
$result.Output.data | Should Be $body
}
}
}
Expand Down Expand Up @@ -1794,9 +1762,11 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" {

It "Validate Invoke-RestMethod default ContentType for CustomMethod POST" {

$command = "Invoke-RestMethod -Uri 'http://httpbin.org/post' -CustomMethod POST -Body 'testparam=testvalue'"
$uri = Get-WebListenerUrl -Test 'Post'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same about default ContentType.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

$command = "Invoke-RestMethod -Uri '$uri' -CustomMethod POST -Body 'testparam=testvalue'"
$result = ExecuteWebCommand -command $command
$result.Output.form.testparam | Should Be "testvalue"
$result.Output.Headers.'Content-Type' | Should Be "application/x-www-form-urlencoded"
}

It "Validate Invoke-RestMethod body is converted to query params for CustomMethod GET" {
Expand All @@ -1809,14 +1779,20 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" {

It "Validate Invoke-RestMethod returns HTTP errors in exception" {

$command = "Invoke-RestMethod -Uri http://httpbin.org/status/418"
$query = @{
body = "I am a teapot!!!"
statuscode = 418
responsephrase = "I am a teapot"
}
$uri = Get-WebListenerUrl -Test 'Response' -Query $query
$command = "Invoke-RestMethod -Uri '$uri'"
$result = ExecuteWebCommand -command $command

$result.Error.ErrorDetails.Message | Should Match "\-=\[ teapot \]"
$result.Error.Exception | Should BeOfType Microsoft.PowerShell.Commands.HttpResponseException
$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 "I'm a teapot"
$result.Error.Exception.Message | Should Match ": 418 \(I'm a teapot\)\."
$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"
}

Expand Down Expand Up @@ -2489,48 +2465,50 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" {
Describe "Validate Invoke-WebRequest and Invoke-RestMethod -InFile" -Tags "Feature" {

Context "InFile parameter negative tests" {

$testCases = @(
BeforeAll {
$uri = Get-WebListenerUrl -Test 'Post'
$testCases = @(
#region INVOKE-WEBREQUEST
@{
Name = 'Validate error for Invoke-WebRequest -InFile ""'
ScriptBlock = {Invoke-WebRequest -Uri http://httpbin.org/post -Method Post -InFile ""}
ExpectedFullyQualifiedErrorId = 'WebCmdletInFileNotFilePathException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand'
}
@{
Name = 'Validate error for Invoke-WebRequest -InFile ""'
ScriptBlock = {Invoke-WebRequest -Uri $uri -Method Post -InFile ""}
ExpectedFullyQualifiedErrorId = 'WebCmdletInFileNotFilePathException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand'
}

@{
Name = 'Validate error for Invoke-WebRequest -InFile'
ScriptBlock = {Invoke-WebRequest -Uri http://httpbin.org/post -Method Post -InFile}
ExpectedFullyQualifiedErrorId = 'MissingArgument,Microsoft.PowerShell.Commands.InvokeWebRequestCommand'
}
@{
Name = 'Validate error for Invoke-WebRequest -InFile'
ScriptBlock = {Invoke-WebRequest -Uri $uri -Method Post -InFile}
ExpectedFullyQualifiedErrorId = 'MissingArgument,Microsoft.PowerShell.Commands.InvokeWebRequestCommand'
}

@{
Name = "Validate error for Invoke-WebRequest -InFile $TestDrive\content.txt"
ScriptBlock = {Invoke-WebRequest -Uri http://httpbin.org/post -Method Post -InFile $TestDrive\content.txt}
ExpectedFullyQualifiedErrorId = 'PathNotFound,Microsoft.PowerShell.Commands.InvokeWebRequestCommand'
}
@{
Name = "Validate error for Invoke-WebRequest -InFile $TestDrive\content.txt"
ScriptBlock = {Invoke-WebRequest -Uri $uri -Method Post -InFile $TestDrive\content.txt}
ExpectedFullyQualifiedErrorId = 'PathNotFound,Microsoft.PowerShell.Commands.InvokeWebRequestCommand'
}
#endregion

#region INVOKE-RESTMETHOD
@{
Name = "Validate error for Invoke-RestMethod -InFile ''"
ScriptBlock = {Invoke-RestMethod -Uri http://httpbin.org/post -Method Post -InFile ''}
ExpectedFullyQualifiedErrorId = 'WebCmdletInFileNotFilePathException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand'
}
@{
Name = "Validate error for Invoke-RestMethod -InFile ''"
ScriptBlock = {Invoke-RestMethod -Uri $uri -Method Post -InFile ''}
ExpectedFullyQualifiedErrorId = 'WebCmdletInFileNotFilePathException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand'
}

@{
Name = "Validate error for Invoke-RestMethod -InFile <null>"
ScriptBlock = {Invoke-RestMethod -Uri http://httpbin.org/post -Method Post -InFile}
ExpectedFullyQualifiedErrorId = 'MissingArgument,Microsoft.PowerShell.Commands.InvokeRestMethodCommand'
}
@{
Name = "Validate error for Invoke-RestMethod -InFile <null>"
ScriptBlock = {Invoke-RestMethod -Uri $uri -Method Post -InFile}
ExpectedFullyQualifiedErrorId = 'MissingArgument,Microsoft.PowerShell.Commands.InvokeRestMethodCommand'
}

@{
Name = "Validate error for Invoke-RestMethod -InFile $TestDrive\content.txt"
ScriptBlock = {Invoke-RestMethod -Uri http://httpbin.org/post -Method Post -InFile $TestDrive\content.txt}
ExpectedFullyQualifiedErrorId = 'PathNotFound,Microsoft.PowerShell.Commands.InvokeRestMethodCommand'
}
@{
Name = "Validate error for Invoke-RestMethod -InFile $TestDrive\content.txt"
ScriptBlock = {Invoke-RestMethod -Uri $uri -Method Post -InFile $TestDrive\content.txt}
ExpectedFullyQualifiedErrorId = 'PathNotFound,Microsoft.PowerShell.Commands.InvokeRestMethodCommand'
}
#endregion
)
)
}

It "<Name>" -TestCases $testCases {
param ($scriptblock, $expectedFullyQualifiedErrorId)
Expand All @@ -2551,18 +2529,21 @@ Describe "Validate Invoke-WebRequest and Invoke-RestMethod -InFile" -Tags "Featu

BeforeAll {
$filePath = Join-Path $TestDrive test.txt
New-Item -Path $filePath -Value "hello" -ItemType File -Force
New-Item -Path $filePath -Value "hello=world" -ItemType File -Force
$uri = Get-WebListenerUrl -Test 'Post'
}

It "Invoke-WebRequest -InFile" {
$result = Invoke-WebRequest -InFile $filePath -Uri http://httpbin.org/post -Method Post
$result = Invoke-WebRequest -InFile $filePath -Uri $uri -Method Post
$content = $result.Content | ConvertFrom-Json
$content.form | Should Match "hello"
$content.form.hello.Count | Should Be 1
$content.form.hello[0] | Should Match "world"
}

It "Invoke-RestMethod -InFile" {
$result = Invoke-RestMethod -InFile $filePath -Uri http://httpbin.org/post -Method Post
$result.form | Should Match "hello"
$result = Invoke-RestMethod -InFile $filePath -Uri $uri -Method Post
$result.form.hello.Count | Should Be 1
$result.form.hello[0] | Should Match "world"
}
}
}
Expand Down
Loading