diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index c2b3f110655..b930c27d7d9 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -514,6 +514,26 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { $result.Error.FullyQualifiedErrorId | Should -Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand" } + It "Validate Invoke-WebRequest redirect with -Query destination Http" { + $httpUri = Get-WebListenerUrl -Test 'Get' + $uri = Get-WebListenerUrl -Test 'Redirect' -Query @{destination = $httpUri} + $command = "Invoke-WebRequest -Uri '$uri'" + + $result = ExecuteWebCommand -command $command + $jsonContent = $result.Output.Content | ConvertFrom-Json + $jsonContent.headers.Host | Should -Match $httpUri.Authority + } + + It "Validate Invoke-WebRequest redirect with -Query destination Https" { + $httpsUri = Get-WebListenerUrl -Test 'Get' -Https + $uri = Get-WebListenerUrl -Test 'Redirect' -Https -Query @{destination = $httpsUri} + $command = "Invoke-WebRequest -Uri '$uri' -SkipCertificateCheck" + + $result = ExecuteWebCommand -command $command + $jsonContent = $result.Output.Content | ConvertFrom-Json + $jsonContent.headers.Host | Should -Match $httpsUri.Authority + } + It "Invoke-WebRequest supports request that returns page containing UTF-8 data." { $uri = Get-WebListenerUrl -Test 'Encoding' -TestValue 'Utf8' $command = "Invoke-WebRequest -Uri '$uri'" @@ -937,6 +957,25 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { $response.Error.Exception.Response.StatusCode | Should -Be $StatusCode $response.Error.Exception.Response.Headers.Location | Should -BeNullOrEmpty } + + It "Validate Invoke-WebRequest Https to Http redirect with -AllowInsecureRedirect" { + $httpUri = Get-WebListenerUrl -Test 'Get' + $uri = Get-WebListenerUrl -Test 'Redirect' -Https -Query @{destination = $httpUri} + $command = "Invoke-WebRequest -Uri '$uri' -SkipCertificateCheck -AllowInsecureRedirect" + + $result = ExecuteWebCommand -command $command + $jsonContent = $result.Output.Content | ConvertFrom-Json + $jsonContent.headers.Host | Should -Match $httpUri.Authority + } + + It "Validate Invoke-WebRequest Https to Http redirect without -AllowInsecureRedirect" { + $httpUri = Get-WebListenerUrl -Test 'Get' + $uri = Get-WebListenerUrl -Test 'Redirect' -Https -Query @{destination = $httpUri} + $command = "Invoke-WebRequest -Uri '$uri' -SkipCertificateCheck" + + $result = ExecuteWebCommand -command $command + $result.Error.FullyQualifiedErrorId | Should -Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand" + } } @@ -2170,6 +2209,24 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { $result.Error.FullyQualifiedErrorId | Should -Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand" } + It "Validate Invoke-RestMethod redirect with -Query destination Http" { + $httpUri = Get-WebListenerUrl -Test 'Get' + $uri = Get-WebListenerUrl -Test 'Redirect' -Query @{destination = $httpUri} + $command = "Invoke-RestMethod -Uri '$uri'" + + $result = ExecuteWebCommand -command $command + $result.Output.Headers.Host | Should -Be $httpUri.Authority + } + + It "Validate Invoke-RestMethod redirect with -Query destination Https" { + $httpsUri = Get-WebListenerUrl -Test 'Get' -Https + $uri = Get-WebListenerUrl -Test 'Redirect' -Https -Query @{destination = $httpsUri} + $command = "Invoke-RestMethod -Uri '$uri' -SkipCertificateCheck" + + $result = ExecuteWebCommand -command $command + $result.Output.Headers.Host | Should -Be $httpsUri.Authority + } + It "Invoke-RestMethod supports request that returns page containing UTF-8 data." { $uri = Get-WebListenerUrl -Test 'Encoding' -TestValue 'Utf8' $command = "Invoke-RestMethod -Uri '$uri'" @@ -2595,6 +2652,24 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { $response.Error.Exception.Response.Headers.Location | Should -BeNullOrEmpty } + It "Validate Invoke-RestMethod Https to Http redirect with -AllowInsecureRedirect" { + $httpUri = Get-WebListenerUrl -Test 'Get' + $uri = Get-WebListenerUrl -Test 'Redirect' -Https -Query @{destination = $httpUri} + $command = "Invoke-RestMethod -Uri '$uri' -SkipCertificateCheck -AllowInsecureRedirect" + + $result = ExecuteWebCommand -command $command + $result.Output.Headers.Host | Should -Be $httpUri.Authority + } + + It "Validate Invoke-RestMethod Https to Http redirect without -AllowInsecureRedirect" { + $httpUri = Get-WebListenerUrl -Test 'Get' + $uri = Get-WebListenerUrl -Test 'Redirect' -Https -Query @{destination = $httpUri} + $command = "Invoke-RestMethod -Uri '$uri' -SkipCertificateCheck" + + $result = ExecuteWebCommand -command $command + $result.Error.FullyQualifiedErrorId | Should -Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand" + } + #endregion Redirect tests Context "Invoke-RestMethod SkipHeaderVerification Tests" { diff --git a/test/tools/WebListener/Controllers/RedirectController.cs b/test/tools/WebListener/Controllers/RedirectController.cs index 1b7df04f3b3..551db38055d 100644 --- a/test/tools/WebListener/Controllers/RedirectController.cs +++ b/test/tools/WebListener/Controllers/RedirectController.cs @@ -21,9 +21,10 @@ public class RedirectController : Controller public IActionResult Index(int count) { string url = Regex.Replace(input: Request.GetDisplayUrl(), pattern: "\\/Redirect.*", replacement: string.Empty, options: RegexOptions.IgnoreCase); + var destinationIsPresent = Request.Query.TryGetValue("destination", out StringValues destination); if (count <= 1) { - url = $"{url}/Get/"; + url = destinationIsPresent ? destination.FirstOrDefault() : $"{url}/Get/"; } else { @@ -44,6 +45,11 @@ public IActionResult Index(int count) url = new Uri($"{url}?type={type.FirstOrDefault()}").PathAndQuery; Response.Redirect(url, false); } + else if (destinationIsPresent) + { + Response.StatusCode = 302; + Response.Headers.Add("Location", url); + } else { Response.Redirect(url, false);