From 7a85e0f6a4c6788a74566e841b7cdefe7068196a Mon Sep 17 00:00:00 2001 From: Vincent Damewood Date: Fri, 30 Aug 2019 06:14:18 -0700 Subject: [PATCH 01/11] Add -SkipHttpErrorCheck to web request cmdlets The -SkipHttpErrorCheck flag causes web request cmdlets (Invoke-WebRequest and Invoke-RestMethod) to ignore HTTP statuses that are error statuses and treat them as successful requests. This allows users to handle the responses using their own error handler and gives them access to the full, unmodified response body and headers. --- .../WebCmdlet/Common/WebRequestPSCmdlet.Common.cs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index 6cdb5ecfbe6..eb0e890f353 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -381,6 +381,12 @@ public virtual string CustomMethod [Parameter] public virtual SwitchParameter Resume { get; set; } + /// + /// Ignores the HTTP status code in the response, and acts as if error statuses are successes. + /// + [Parameter] + public virtual SwitchParameter SkipHttpErrorCheck { get; set; } + #endregion #endregion Virtual Properties @@ -690,6 +696,11 @@ internal bool ShouldWriteToPipeline get { return (!ShouldSaveToOutFile || PassThru); } } + internal bool ShouldCheckHttpStatus + { + get { return !SkipHttpErrorCheck; } + } + /// /// Determines whether writing to a file should Resume and append rather than overwrite. /// @@ -1513,7 +1524,7 @@ protected override void ProcessRecord() OutFile = null; } - if (!_isSuccess) + if (ShouldCheckHttpStatus && !_isSuccess) { string message = string.Format(CultureInfo.CurrentCulture, WebCmdletStrings.ResponseStatusCodeFailure, (int)response.StatusCode, response.ReasonPhrase); From 53ec3c5258450f941e6ff81d26abe9aa1b96c4a5 Mon Sep 17 00:00:00 2001 From: Vincent Damewood Date: Fri, 30 Aug 2019 07:01:09 -0700 Subject: [PATCH 02/11] Add -ResponseStatusVariable to Invoke-RestMethod This allows the user to specify a variable to set to the integer value of the respons's status code, Analogous to using -ResponseHeadersVariable to retrieve the headers of the response. This can be used to distinguish error messages from success messages when used with -SkipHttpErrorCheck. --- .../Common/InvokeRestMethodCommand.Common.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs index 13a2aa4ee40..b82d47952da 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs @@ -75,6 +75,12 @@ public int MaximumFollowRelLink [Alias("RHV")] public string ResponseHeadersVariable { get; set; } + /// + /// Gets or sets the variable name to use for storing the status code from the response. + /// + [Parameter] + public string StatusCodeVariable { get; set; } + #endregion Parameters #region Helper Methods @@ -454,6 +460,12 @@ internal override void ProcessResponse(HttpResponseMessage response) StreamHelper.SaveStreamToFile(responseStream, QualifiedOutFile, this); } + if (!string.IsNullOrEmpty(StatusCodeVariable)) + { + PSVariableIntrinsics vi = SessionState.PSVariable; + vi.Set(StatusCodeVariable, (int)response.StatusCode); + } + if (!string.IsNullOrEmpty(ResponseHeadersVariable)) { PSVariableIntrinsics vi = SessionState.PSVariable; From 21427c19cf21315a5ea18e80a3de417c599dbd40 Mon Sep 17 00:00:00 2001 From: Vincent Damewood Date: Fri, 30 Aug 2019 08:45:17 -0700 Subject: [PATCH 03/11] Fix coding-style error The summary for the SkipHttpErrorCheck property didn't conform to style guidelines. This changes the summary to start with "Gets or sets". --- .../utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index eb0e890f353..d2d4417ab30 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -382,7 +382,7 @@ public virtual string CustomMethod public virtual SwitchParameter Resume { get; set; } /// - /// Ignores the HTTP status code in the response, and acts as if error statuses are successes. + /// Gets or sets whether to skip checking HTTP status for error codes. /// [Parameter] public virtual SwitchParameter SkipHttpErrorCheck { get; set; } @@ -1253,7 +1253,7 @@ internal virtual void FillRequestStream(HttpRequestMessage request) // Add the content headers if (request.Content == null) - { + { request.Content = new StringContent(string.Empty); request.Content.Headers.Clear(); } From 478c0a8a7d1b05d5ade808b1b74c9cf41a7aa442 Mon Sep 17 00:00:00 2001 From: Vincent Damewood Date: Fri, 30 Aug 2019 10:50:12 -0700 Subject: [PATCH 04/11] Add tests for -SkipHttpErrorCheck This flag supresses terminating errors on web cmdlets. The tests are written to check that it properly supressed the errors. --- .../WebCmdlets.Tests.ps1 | 34 ++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index b6d77ad4eff..db923e570cd 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -780,6 +780,23 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { $result.Output.RelationLink["next"] | Should -BeExactly "${baseUri}?maxlinks=3&linknumber=2&type=${type}" } + It "Verify Invoke-WebRequest supresses terminating errors with -SkipHttpErrorCheck" { + $Query = @{ + statuscode = 404 + responsephrase = 'Not found' + contenttype = 'text/plain' + body = 'oops' + headers = "{}" + } + + $Uri = Get-WebListenerUrl -Test 'Response' -Query $Query + $command = "Invoke-WebRequest -SkipHttpErrorCheck -Uri '$uri'" + $result = ExecuteWebCommand -command $command + $result.output.StatusCode | Should -be 404 + $result.output.Content | Should -match "oops" + $result.error | Should -be $null + } + Context "Redirect" { It "Validates Invoke-WebRequest with -PreserveAuthorizationOnRedirect preserves the authorization header on redirect: " -TestCases $redirectTests { param($redirectType, $redirectedMethod) @@ -2259,6 +2276,22 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { 1..3 | ForEach-Object { $result.Output[$_ - 1].linknumber | Should -BeExactly $_ } } + It "Verify Invoke-RestMethod supresses terminating errors with -SkipHttpErrorCheck" { + $Query = @{ + statuscode = 404 + responsephrase = 'Not found' + contenttype = 'application/json' + body = '{"message": "oops"}' + headers = "{}" + } + + $Uri = Get-WebListenerUrl -Test 'Response' -Query $Query + $command = "Invoke-RestMethod -SkipHttpErrorCheck -Uri '$uri'" + $result = ExecuteWebCommand -command $command + $result.output.message | Should -match "oops" + $result.output.error | Should -be $null + } + #region Redirect tests It "Validates Invoke-RestMethod with -PreserveAuthorizationOnRedirect preserves the authorization header on redirect: " -TestCases $redirectTests { @@ -3387,4 +3420,3 @@ Describe "Web cmdlets tests using the cmdlet's aliases" -Tags "CI", "RequireAdmi $result.Hello | Should -Be "world" } } - From b1e43e12a6a64ce1bb10d709bfa400b0a8ab124f Mon Sep 17 00:00:00 2001 From: Vincent Damewood Date: Fri, 30 Aug 2019 10:51:34 -0700 Subject: [PATCH 05/11] Add test for -StatusCodeVariable Th -StatusCodeVariable parameter specifies an output variable for the status code with Invoke-RestMethod. This test makes sure it functions properly. --- .../WebCmdlets.Tests.ps1 | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index db923e570cd..d29ebe1a8a6 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -2292,6 +2292,20 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { $result.output.error | Should -be $null } + It "Verify Invoke-RestMethod assigns status code with -StatusCodeVariable" { + $Query = @{ + statuscode = 200 + responsephrase = 'OK' + contenttype = 'application/json' + body = '{"message": "works"}' + headers = "{}" + } + + $Uri = Get-WebListenerUrl -Test 'Response' -Query $Query + Invoke-RestMethod -SkipHttpErrorCheck -StatusCodeVariable code -Uri "$uri" + $code | Should -be 200 + } + #region Redirect tests It "Validates Invoke-RestMethod with -PreserveAuthorizationOnRedirect preserves the authorization header on redirect: " -TestCases $redirectTests { From 95852bd4498d986b4f0d3724efece1f752d9aa94 Mon Sep 17 00:00:00 2001 From: Vincent Damewood Date: Sat, 31 Aug 2019 10:57:43 -0700 Subject: [PATCH 06/11] Fix typos and style for -SkipHttpErrorCheck tests Variables had different cases from each other and some parameter names were lower case. --- .../WebCmdlets.Tests.ps1 | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index d29ebe1a8a6..42f9fb9fb25 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -781,7 +781,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { } It "Verify Invoke-WebRequest supresses terminating errors with -SkipHttpErrorCheck" { - $Query = @{ + $query = @{ statuscode = 404 responsephrase = 'Not found' contenttype = 'text/plain' @@ -789,12 +789,12 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { headers = "{}" } - $Uri = Get-WebListenerUrl -Test 'Response' -Query $Query + $uri = Get-WebListenerUrl -Test 'Response' -Query $query $command = "Invoke-WebRequest -SkipHttpErrorCheck -Uri '$uri'" - $result = ExecuteWebCommand -command $command - $result.output.StatusCode | Should -be 404 - $result.output.Content | Should -match "oops" - $result.error | Should -be $null + $result = ExecuteWebCommand -Command $command + $result.output.StatusCode | Should -Be 404 + $result.output.Content | Should -Match "oops" + $result.error | Should -Be $null } Context "Redirect" { @@ -2277,7 +2277,7 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { } It "Verify Invoke-RestMethod supresses terminating errors with -SkipHttpErrorCheck" { - $Query = @{ + $query = @{ statuscode = 404 responsephrase = 'Not found' contenttype = 'application/json' @@ -2285,11 +2285,11 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { headers = "{}" } - $Uri = Get-WebListenerUrl -Test 'Response' -Query $Query + $uri = Get-WebListenerUrl -Test 'Response' -Query $query $command = "Invoke-RestMethod -SkipHttpErrorCheck -Uri '$uri'" - $result = ExecuteWebCommand -command $command - $result.output.message | Should -match "oops" - $result.output.error | Should -be $null + $result = ExecuteWebCommand -Command $command + $result.output.message | Should -Match "oops" + $result.output.error | Should -Be $null } It "Verify Invoke-RestMethod assigns status code with -StatusCodeVariable" { From 727e063a14fff4c86729f3d0614f73a92bad6b80 Mon Sep 17 00:00:00 2001 From: Vincent Damewood Date: Sat, 31 Aug 2019 11:06:54 -0700 Subject: [PATCH 07/11] Fix typos and style for -StatusCodeVariable test Variables had different cases from each other and some parameter names were lower case. --- .../Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index 42f9fb9fb25..18858b863e2 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -2293,7 +2293,7 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { } It "Verify Invoke-RestMethod assigns status code with -StatusCodeVariable" { - $Query = @{ + $query = @{ statuscode = 200 responsephrase = 'OK' contenttype = 'application/json' @@ -2301,9 +2301,9 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { headers = "{}" } - $Uri = Get-WebListenerUrl -Test 'Response' -Query $Query + $uri = Get-WebListenerUrl -Test 'Response' -Query $query Invoke-RestMethod -SkipHttpErrorCheck -StatusCodeVariable code -Uri "$uri" - $code | Should -be 200 + $code | Should -Be 200 } #region Redirect tests From 5c63c772d0aa76d8fe194d17e146c58d6f70af94 Mon Sep 17 00:00:00 2001 From: Vincent Damewood Date: Mon, 2 Sep 2019 15:07:45 -0700 Subject: [PATCH 08/11] Add failure tests when missing -SkipHttpErrorCheck These tests ensure that Web Cmdlets fail when -SkipHttpErrorCheck is missing. --- .../WebCmdlets.Tests.ps1 | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index 18858b863e2..9e107530463 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -797,6 +797,22 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { $result.error | Should -Be $null } + It "Verify Invoke-WebRequest terminates without -SkipHttpErrorCheck" { + $query = @{ + statuscode = 404 + responsephrase = 'Not found' + contenttype = 'text/plain' + body = 'oops' + headers = "{}" + } + + $uri = Get-WebListenerUrl -Test 'Response' -Query $query + $command = "Invoke-WebRequest -Uri '$uri'" + $result = ExecuteWebCommand -Command $command + $result.output | Should -Be $null + $result.error | Should -Not -Be $null + } + Context "Redirect" { It "Validates Invoke-WebRequest with -PreserveAuthorizationOnRedirect preserves the authorization header on redirect: " -TestCases $redirectTests { param($redirectType, $redirectedMethod) @@ -2292,6 +2308,22 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { $result.output.error | Should -Be $null } + It "Verify Invoke-RestMethod terminates without -SkipHttpErrorCheck" { + $query = @{ + statuscode = 404 + responsephrase = 'Not found' + contenttype = 'application/json' + body = '{"message": "oops"}' + headers = "{}" + } + + $uri = Get-WebListenerUrl -Test 'Response' -Query $query + $command = "Invoke-RestMethod -Uri '$uri'" + $result = ExecuteWebCommand -Command $command + $result.output | Should -Be $null + $result.error | Should -Not -Be $null + } + It "Verify Invoke-RestMethod assigns status code with -StatusCodeVariable" { $query = @{ statuscode = 200 From 035af8370c84667abe88089311e5917cee967ca4 Mon Sep 17 00:00:00 2001 From: Vincent Damewood Date: Thu, 5 Sep 2019 17:49:14 -0700 Subject: [PATCH 09/11] Clean up tests for -SkipHttpErrorCheck Per discussion on the pull requests. This commit fixes up style problems with the tests for -SkipHttpErrorCheck. --- .../WebCmdlets.Tests.ps1 | 71 +++++++------------ 1 file changed, 27 insertions(+), 44 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index 9e107530463..915637b7c02 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -370,6 +370,13 @@ $redirectTests = @( Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { BeforeAll { $WebListener = Start-WebListener + $NotFoundQuery = @{ + statuscode = 404 + responsephrase = 'Not Found' + contenttype = 'text/plain' + body = 'oops' + headers = "{}" + } } # Validate the output of Invoke-WebRequest @@ -781,36 +788,20 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { } It "Verify Invoke-WebRequest supresses terminating errors with -SkipHttpErrorCheck" { - $query = @{ - statuscode = 404 - responsephrase = 'Not found' - contenttype = 'text/plain' - body = 'oops' - headers = "{}" - } - - $uri = Get-WebListenerUrl -Test 'Response' -Query $query + $uri = Get-WebListenerUrl -Test 'Response' -Query $NotFoundQuery $command = "Invoke-WebRequest -SkipHttpErrorCheck -Uri '$uri'" $result = ExecuteWebCommand -Command $command $result.output.StatusCode | Should -Be 404 - $result.output.Content | Should -Match "oops" - $result.error | Should -Be $null + $result.output.Content | Should -BeExactly "oops" + $result.error | Should -BeNullOrEmpty } It "Verify Invoke-WebRequest terminates without -SkipHttpErrorCheck" { - $query = @{ - statuscode = 404 - responsephrase = 'Not found' - contenttype = 'text/plain' - body = 'oops' - headers = "{}" - } - - $uri = Get-WebListenerUrl -Test 'Response' -Query $query + $uri = Get-WebListenerUrl -Test 'Response' -Query $NotFoundQuery $command = "Invoke-WebRequest -Uri '$uri'" $result = ExecuteWebCommand -Command $command - $result.output | Should -Be $null - $result.error | Should -Not -Be $null + $result.output | Should -BeNullOrEmpty + $result.error | Should -Not -BeNullOrEmpty } Context "Redirect" { @@ -1963,6 +1954,14 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { BeforeAll { $WebListener = Start-WebListener + + $NotFoundQuery = @{ + statuscode = 404 + responsephrase = 'Not Found' + contenttype = 'application/json' + body = '{"message": "oops"}' + headers = "{}" + } } #User-Agent changes on different platforms, so tests should only be run if on the correct platform @@ -2293,35 +2292,19 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { } It "Verify Invoke-RestMethod supresses terminating errors with -SkipHttpErrorCheck" { - $query = @{ - statuscode = 404 - responsephrase = 'Not found' - contenttype = 'application/json' - body = '{"message": "oops"}' - headers = "{}" - } - - $uri = Get-WebListenerUrl -Test 'Response' -Query $query + $uri = Get-WebListenerUrl -Test 'Response' -Query $NotFoundQuery $command = "Invoke-RestMethod -SkipHttpErrorCheck -Uri '$uri'" $result = ExecuteWebCommand -Command $command - $result.output.message | Should -Match "oops" - $result.output.error | Should -Be $null + $result.output.message | Should -BeExactly "oops" + $result.output.error | Should -BeNullOrEmpty } It "Verify Invoke-RestMethod terminates without -SkipHttpErrorCheck" { - $query = @{ - statuscode = 404 - responsephrase = 'Not found' - contenttype = 'application/json' - body = '{"message": "oops"}' - headers = "{}" - } - - $uri = Get-WebListenerUrl -Test 'Response' -Query $query + $uri = Get-WebListenerUrl -Test 'Response' -Query $NotFoundQuery $command = "Invoke-RestMethod -Uri '$uri'" $result = ExecuteWebCommand -Command $command - $result.output | Should -Be $null - $result.error | Should -Not -Be $null + $result.output | Should -BeNullOrEmpty + $result.error | Should -Not -BeNullOrEmpty } It "Verify Invoke-RestMethod assigns status code with -StatusCodeVariable" { From d69cd328403c2daeeda7a36663368cca6e7ee17f Mon Sep 17 00:00:00 2001 From: Vincent Damewood Date: Thu, 5 Sep 2019 18:17:23 -0700 Subject: [PATCH 10/11] Add more status tests for -StatusCodeVariable Previously, the -StatusCodeVariable flag in Invoke-RestMethod only had tests for 200 status. This commit adds tests for 404 and 500 statuses and removes -SkipHttpErrorCheck from the 200 check. --- .../WebCmdlets.Tests.ps1 | 32 +++++++++++++++++-- 1 file changed, 30 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 915637b7c02..20e8500c943 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -2307,7 +2307,7 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { $result.error | Should -Not -BeNullOrEmpty } - It "Verify Invoke-RestMethod assigns status code with -StatusCodeVariable" { + It "Verify Invoke-RestMethod assigns 200 status code with -StatusCodeVariable" { $query = @{ statuscode = 200 responsephrase = 'OK' @@ -2317,10 +2317,38 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { } $uri = Get-WebListenerUrl -Test 'Response' -Query $query - Invoke-RestMethod -SkipHttpErrorCheck -StatusCodeVariable code -Uri "$uri" + Invoke-RestMethod -StatusCodeVariable code -Uri "$uri" $code | Should -Be 200 } + It "Verify Invoke-RestMethod assigns 404 status code with -StatusCodeVariable" { + $query = @{ + statuscode = 404 + responsephrase = 'Not Found' + contenttype = 'application/json' + body = '{"message": "oops"}' + headers = "{}" + } + + $uri = Get-WebListenerUrl -Test 'Response' -Query $query + Invoke-RestMethod -SkipHttpErrorCheck -StatusCodeVariable code -Uri "$uri" + $code | Should -Be 404 + } + + It "Verify Invoke-RestMethod assigns 500 status code with -StatusCodeVariable" { + $query = @{ + statuscode = 500 + responsephrase = 'Internal Server Error' + contenttype = 'application/json' + body = '{"message": "works"}' + headers = "{}" + } + + $uri = Get-WebListenerUrl -Test 'Response' -Query $query + Invoke-RestMethod -SkipHttpErrorCheck -StatusCodeVariable code -Uri "$uri" + $code | Should -Be 500 + } + #region Redirect tests It "Validates Invoke-RestMethod with -PreserveAuthorizationOnRedirect preserves the authorization header on redirect: " -TestCases $redirectTests { From aa15c0ccd640a918a919ce1e4335ac00e6d64849 Mon Sep 17 00:00:00 2001 From: Vincent Damewood Date: Thu, 5 Sep 2019 18:27:14 -0700 Subject: [PATCH 11/11] Fix response body in -StatusCodeVariable test The test was copy/pastes from the 200 status test. The body indicated success. This commit fixes that so it is also an error indicator. --- .../Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index 20e8500c943..d4ab2ff5009 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -2340,7 +2340,7 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { statuscode = 500 responsephrase = 'Internal Server Error' contenttype = 'application/json' - body = '{"message": "works"}' + body = '{"message": "oops"}' headers = "{}" }