From ddb0e07264cdd927d7a9e1618f1c261448b3499c Mon Sep 17 00:00:00 2001 From: markekraus Date: Thu, 31 Aug 2017 17:23:40 -0500 Subject: [PATCH 1/5] Move HTTPS tests to WebListener --- .../WebCmdlets.Tests.ps1 | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index 7074c70cc75..24449b6ac30 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -686,12 +686,14 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { It "Validate Invoke-WebRequest -SkipCertificateCheck" { # validate that exception is thrown for URI with expired certificate - $command = "Invoke-WebRequest -Uri 'https://expired.badssl.com'" + $Uri = Get-WebListenerUrl -Https + $command = "Invoke-WebRequest -Uri '$Uri'" $result = ExecuteWebCommand -command $command $result.Error.FullyQualifiedErrorId | Should Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand" # validate that no exception is thrown for URI with expired certificate when using -SkipCertificateCheck option - $command = "Invoke-WebRequest -Uri 'https://expired.badssl.com' -SkipCertificateCheck" + $Uri = Get-WebListenerUrl -Https + $command = "Invoke-WebRequest -Uri '$Uri' -SkipCertificateCheck" $result = ExecuteWebCommand -command $command $result.Error | Should BeNullOrEmpty } @@ -747,8 +749,8 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { } It "Validate Invoke-WebRequest returns native HTTPS error message in exception" { - - $command = "Invoke-WebRequest -Uri https://incomplete.chain.badssl.com" + $uri = Get-WebListenerUrl -Https + $command = "Invoke-WebRequest -Uri '$uri'" $result = ExecuteWebCommand -command $command # need to check against inner exception since Linux and Windows uses different HTTP client libraries so errors aren't the same @@ -1495,12 +1497,13 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { # HTTP method HEAD must be used to not retrieve an unparsable HTTP body # validate that exception is thrown for URI with expired certificate - $command = "Invoke-RestMethod -Uri 'https://expired.badssl.com' -Method HEAD" + $uri= Get-WebListenerUrl -Https + $command = "Invoke-RestMethod -Uri '$uri' -Method HEAD" $result = ExecuteWebCommand -command $command $result.Error.FullyQualifiedErrorId | Should Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand" # validate that no exception is thrown for URI with expired certificate when using -SkipCertificateCheck option - $command = "Invoke-RestMethod -Uri 'https://expired.badssl.com' -SkipCertificateCheck -Method HEAD" + $command = "Invoke-RestMethod -Uri '$uri' -SkipCertificateCheck -Method HEAD" $result = ExecuteWebCommand -command $command $result.Error | Should BeNullOrEmpty } @@ -1563,7 +1566,8 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { It "Validate Invoke-RestMethod returns native HTTPS error message in exception" { - $command = "Invoke-RestMethod -Uri https://incomplete.chain.badssl.com" + $uri = Get-WebListenerUrl -Https + $command = "Invoke-RestMethod -Uri '$uri'" $result = ExecuteWebCommand -command $command # need to check against inner exception since Linux and Windows uses different HTTP client libraries so errors aren't the same From 4b2a9150070d4684be8cce44e18b0dc4bfe2f069 Mon Sep 17 00:00:00 2001 From: markekraus Date: Thu, 31 Aug 2017 17:41:21 -0500 Subject: [PATCH 2/5] Move Invoke-WebRequest HTTPS Tests to Context --- .../WebCmdlets.Tests.ps1 | 91 +++++++++---------- 1 file changed, 44 insertions(+), 47 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index 24449b6ac30..6d7d2d382c2 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -683,21 +683,6 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { $jsonContent.headers.'User-Agent' | Should Match "WindowsPowerShell" } - 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 - $result.Error.FullyQualifiedErrorId | Should Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand" - - # 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 - $result.Error | Should BeNullOrEmpty - } - It "Validate Invoke-WebRequest handles missing Content-Type in response header" { #Validate that exception is not thrown when response headers are missing Content-Type. @@ -748,16 +733,6 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { $result.Error.FullyQualifiedErrorId | Should Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand" } - It "Validate Invoke-WebRequest returns native HTTPS error message in exception" { - $uri = Get-WebListenerUrl -Https - $command = "Invoke-WebRequest -Uri '$uri'" - $result = ExecuteWebCommand -command $command - - # need to check against inner exception since Linux and Windows uses different HTTP client libraries so errors aren't the same - $result.Error.ErrorDetails.Message | Should Match $result.Error.Exception.InnerException.Message - $result.Error.FullyQualifiedErrorId | Should Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand" - } - It "Validate Invoke-WebRequest returns empty RelationLink property if there is no Link Header" { $command = "Invoke-WebRequest -Uri http://localhost:8080/PowerShell?test=response" @@ -1176,32 +1151,54 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { #endregion Content Header Inclusion - #region Client Certificate Authentication - - It "Verifies Invoke-WebRequest Certificate Authentication Fails without -Certificate" { - $uri = Get-WebListenerUrl -Https -Test 'Cert' - $result = Invoke-WebRequest -Uri $uri -SkipCertificateCheck | - Select-Object -ExpandProperty Content | - ConvertFrom-Json + 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 + $result.Error.FullyQualifiedErrorId | Should Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand" + + # 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 + $result.Error | Should BeNullOrEmpty + } - $result.Status | Should Be 'FAILED' - } - - # Test skipped on macOS pending support for Client Certificate Authentication - # https://github.com/PowerShell/PowerShell/issues/4650 - It "Verifies Invoke-WebRequest Certificate Authentication Successful with -Certificate" -skip:$IsOSX { - $uri = Get-WebListenerUrl -Https -Test 'Cert' - $certificate = Get-WebListenerClientCertificate - $result = Invoke-WebRequest -Uri $uri -Certificate $certificate -SkipCertificateCheck | - Select-Object -ExpandProperty Content | - ConvertFrom-Json + It "Validate Invoke-WebRequest returns native HTTPS error message in exception" { + $uri = Get-WebListenerUrl -Https + $command = "Invoke-WebRequest -Uri '$uri'" + $result = ExecuteWebCommand -command $command + + # need to check against inner exception since Linux and Windows uses different HTTP client libraries so errors aren't the same + $result.Error.ErrorDetails.Message | Should Match $result.Error.Exception.InnerException.Message + $result.Error.FullyQualifiedErrorId | Should Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand" + } - $result.Status | Should Be 'OK' - $result.Thumbprint | Should Be $certificate.Thumbprint + It "Verifies Invoke-WebRequest Certificate Authentication Fails without -Certificate" { + $uri = Get-WebListenerUrl -Https -Test 'Cert' + $result = Invoke-WebRequest -Uri $uri -SkipCertificateCheck | + Select-Object -ExpandProperty Content | + ConvertFrom-Json + + $result.Status | Should Be 'FAILED' + } + + # Test skipped on macOS pending support for Client Certificate Authentication + # https://github.com/PowerShell/PowerShell/issues/4650 + It "Verifies Invoke-WebRequest Certificate Authentication Successful with -Certificate" -skip:$IsOSX { + $uri = Get-WebListenerUrl -Https -Test 'Cert' + $certificate = Get-WebListenerClientCertificate + $result = Invoke-WebRequest -Uri $uri -Certificate $certificate -SkipCertificateCheck | + Select-Object -ExpandProperty Content | + ConvertFrom-Json + + $result.Status | Should Be 'OK' + $result.Thumbprint | Should Be $certificate.Thumbprint + } } - #endregion Client Certificate Authentication - BeforeEach { if ($env:http_proxy) { $savedHttpProxy = $env:http_proxy From 00cae5bcae4ac98d6bacd33b80a73b95e0245590 Mon Sep 17 00:00:00 2001 From: markekraus Date: Thu, 31 Aug 2017 17:52:57 -0500 Subject: [PATCH 3/5] [Feature] Move Invoke-RestMethod HTTPS Tests to Context . --- .../WebCmdlets.Tests.ps1 | 86 +++++++++---------- 1 file changed, 41 insertions(+), 45 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index 6d7d2d382c2..2a50d1d7189 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -1490,21 +1490,6 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { $jsonContent.headers.'User-Agent' | Should Match "WindowsPowerShell" } - It "Validate Invoke-RestMethod -SkipCertificateCheck" { - - # HTTP method HEAD must be used to not retrieve an unparsable HTTP body - # validate that exception is thrown for URI with expired certificate - $uri= Get-WebListenerUrl -Https - $command = "Invoke-RestMethod -Uri '$uri' -Method HEAD" - $result = ExecuteWebCommand -command $command - $result.Error.FullyQualifiedErrorId | Should Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand" - - # 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 - $result.Error | Should BeNullOrEmpty - } - It "Validate Invoke-RestMethod handles missing Content-Type in response header" { #Validate that exception is not thrown when response headers are missing Content-Type. @@ -1561,17 +1546,6 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { $result.Error.FullyQualifiedErrorId | Should Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand" } - It "Validate Invoke-RestMethod returns native HTTPS error message in exception" { - - $uri = Get-WebListenerUrl -Https - $command = "Invoke-RestMethod -Uri '$uri'" - $result = ExecuteWebCommand -command $command - - # need to check against inner exception since Linux and Windows uses different HTTP client libraries so errors aren't the same - $result.Error.ErrorDetails.Message | Should Match $result.Error.Exception.InnerException.Message - $result.Error.FullyQualifiedErrorId | Should Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand" - } - It "Validate Invoke-RestMethod -FollowRelLink doesn't fail if no Link Header is present" { $command = "Invoke-RestMethod -Uri 'http://localhost:8081/PowerShell?test=response&output=foo' -FollowRelLink" @@ -1720,28 +1694,50 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { #endregion SkipHeaderVerification tests - #region Client Certificate Authentication - - It "Verifies Invoke-RestMethod Certificate Authentication Fails without -Certificate" { - $uri = Get-WebListenerUrl -Https -Test 'Cert' - $result = Invoke-RestMethod -Uri $uri -SkipCertificateCheck + 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 + $uri= Get-WebListenerUrl -Https + $command = "Invoke-RestMethod -Uri '$uri' -Method HEAD" + $result = ExecuteWebCommand -command $command + $result.Error.FullyQualifiedErrorId | Should Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand" + + # 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 + $result.Error | Should BeNullOrEmpty + } - $result.Status | Should Be 'FAILED' - } - - # Test skipped on macOS pending support for Client Certificate Authentication - # https://github.com/PowerShell/PowerShell/issues/4650 - It "Verifies Invoke-RestMethod Certificate Authentication Successful with -Certificate" -skip:$IsOSX { - $uri = Get-WebListenerUrl -Https -Test 'Cert' - $certificate = Get-WebListenerClientCertificate - $result = Invoke-RestMethod -uri $uri -Certificate $certificate -SkipCertificateCheck - - $result.Status | Should Be 'OK' - $result.Thumbprint | Should Be $certificate.Thumbprint + It "Validate Invoke-RestMethod returns native HTTPS error message in exception" { + $uri = Get-WebListenerUrl -Https + $command = "Invoke-RestMethod -Uri '$uri'" + $result = ExecuteWebCommand -command $command + + # need to check against inner exception since Linux and Windows uses different HTTP client libraries so errors aren't the same + $result.Error.ErrorDetails.Message | Should Match $result.Error.Exception.InnerException.Message + $result.Error.FullyQualifiedErrorId | Should Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand" + } + + It "Verifies Invoke-RestMethod Certificate Authentication Fails without -Certificate" { + $uri = Get-WebListenerUrl -Https -Test 'Cert' + $result = Invoke-RestMethod -Uri $uri -SkipCertificateCheck + + $result.Status | Should Be 'FAILED' + } + + # Test skipped on macOS pending support for Client Certificate Authentication + # https://github.com/PowerShell/PowerShell/issues/4650 + It "Verifies Invoke-RestMethod Certificate Authentication Successful with -Certificate" -skip:$IsOSX { + $uri = Get-WebListenerUrl -Https -Test 'Cert' + $certificate = Get-WebListenerClientCertificate + $result = Invoke-RestMethod -uri $uri -Certificate $certificate -SkipCertificateCheck + + $result.Status | Should Be 'OK' + $result.Thumbprint | Should Be $certificate.Thumbprint + } } - #endregion Client Certificate Authentication - #region charset encoding tests Context "Invoke-RestMethod Encoding tests with BasicHtmlWebResponseObject response" { From e68c95f73d9ff4365775b1260e9efe0f31372303 Mon Sep 17 00:00:00 2001 From: markekraus Date: Thu, 31 Aug 2017 19:33:21 -0500 Subject: [PATCH 4/5] [feature] Address PR Feedback --- .../Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index 2a50d1d7189..ab9649a52e5 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -1182,7 +1182,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { Select-Object -ExpandProperty Content | ConvertFrom-Json - $result.Status | Should Be 'FAILED' + $result.Status | Should Be 'FAILED' } # Test skipped on macOS pending support for Client Certificate Authentication @@ -1194,7 +1194,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { Select-Object -ExpandProperty Content | ConvertFrom-Json - $result.Status | Should Be 'OK' + $result.Status | Should Be 'OK' $result.Thumbprint | Should Be $certificate.Thumbprint } } @@ -1723,7 +1723,7 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { $uri = Get-WebListenerUrl -Https -Test 'Cert' $result = Invoke-RestMethod -Uri $uri -SkipCertificateCheck - $result.Status | Should Be 'FAILED' + $result.Status | Should Be 'FAILED' } # Test skipped on macOS pending support for Client Certificate Authentication @@ -1733,7 +1733,7 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { $certificate = Get-WebListenerClientCertificate $result = Invoke-RestMethod -uri $uri -Certificate $certificate -SkipCertificateCheck - $result.Status | Should Be 'OK' + $result.Status | Should Be 'OK' $result.Thumbprint | Should Be $certificate.Thumbprint } } From e41d1ddfd69a76960f5d79fce0d788d796a45d9b Mon Sep 17 00:00:00 2001 From: markekraus Date: Fri, 1 Sep 2017 12:36:43 -0500 Subject: [PATCH 5/5] [feature] Mark Cert Auth Tests Pending -skip -> -pending --- .../Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index ab9649a52e5..f3511ea3c03 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -1187,7 +1187,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { # Test skipped on macOS pending support for Client Certificate Authentication # https://github.com/PowerShell/PowerShell/issues/4650 - It "Verifies Invoke-WebRequest Certificate Authentication Successful with -Certificate" -skip:$IsOSX { + It "Verifies Invoke-WebRequest Certificate Authentication Successful with -Certificate" -Pending:$IsOSX { $uri = Get-WebListenerUrl -Https -Test 'Cert' $certificate = Get-WebListenerClientCertificate $result = Invoke-WebRequest -Uri $uri -Certificate $certificate -SkipCertificateCheck | @@ -1728,7 +1728,7 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { # Test skipped on macOS pending support for Client Certificate Authentication # https://github.com/PowerShell/PowerShell/issues/4650 - It "Verifies Invoke-RestMethod Certificate Authentication Successful with -Certificate" -skip:$IsOSX { + It "Verifies Invoke-RestMethod Certificate Authentication Successful with -Certificate" -Pending:$IsOSX { $uri = Get-WebListenerUrl -Https -Test 'Cert' $certificate = Get-WebListenerClientCertificate $result = Invoke-RestMethod -uri $uri -Certificate $certificate -SkipCertificateCheck