Skip to content

Commit 211ee63

Browse files
markekrausiSazonov
authored andcommitted
[Feature] Replace HttpListener Redirect Tests with WebListener (#5872)
- adds type query to Redirect listener and adjusts logic to accommodate - Replaces HttpListener Redirect Tests with WebListener
1 parent 3e9a530 commit 211ee63

File tree

3 files changed

+52
-37
lines changed

3 files changed

+52
-37
lines changed

test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -757,51 +757,49 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" {
757757

758758
It "Validates Invoke-WebRequest with -PreserveAuthorizationOnRedirect preserves the authorization header on redirect: <redirectType> <redirectedMethod>" -TestCases $redirectTests {
759759
param($redirectType, $redirectedMethod)
760-
761-
$response = ExecuteRedirectRequest -Uri "http://localhost:8080/PowerShell?test=redirect&type=$redirectType" -PreserveAuthorizationOnRedirect
760+
$uri = Get-WebListenerUrl -Test 'Redirect' -Query @{type = $redirectType}
761+
$response = ExecuteRedirectRequest -Uri $uri -PreserveAuthorizationOnRedirect
762762

763763
$response.Error | Should BeNullOrEmpty
764-
# ensure Authorization header has been preserved.
765-
$response.Content.Headers -contains "Authorization" | Should Be $true
764+
$response.Content.Headers."Authorization" | Should BeExactly "test"
766765
}
767766

768767

769768
It "Validates Invoke-WebRequest preserves the authorization header on multiple redirects: <redirectType>" -TestCases $redirectTests {
770769
param($redirectType)
771-
772-
$response = ExecuteRedirectRequest -Uri "http://localhost:8080/PowerShell?test=redirect&type=$redirectType&multiredirect=true" -PreserveAuthorizationOnRedirect
770+
$uri = Get-WebListenerUrl -Test 'Redirect' -TestValue 3 -Query @{type = $redirectType}
771+
$response = ExecuteRedirectRequest -Uri $uri -PreserveAuthorizationOnRedirect
773772

774773
$response.Error | Should BeNullOrEmpty
775-
# ensure Authorization header was stripped
776-
$response.Content.Headers -contains "Authorization" | Should Be $true
774+
$response.Content.Headers."Authorization" | Should BeExactly "test"
777775
}
778776

779777
It "Validates Invoke-WebRequest strips the authorization header on various redirects: <redirectType>" -TestCases $redirectTests {
780778
param($redirectType)
781-
782-
$response = ExecuteRedirectRequest -Uri "http://localhost:8080/PowerShell?test=redirect&type=$redirectType"
779+
$uri = Get-WebListenerUrl -Test 'Redirect' -Query @{type = $redirectType}
780+
$response = ExecuteRedirectRequest -Uri $uri
783781

784782
$response.Error | Should BeNullOrEmpty
785783
# ensure user-agent is present (i.e., no false positives )
786-
$response.Content.Headers -contains "User-Agent" | Should Be $true
784+
$response.Content.Headers."User-Agent" | Should Not BeNullOrEmpty
787785
# ensure Authorization header has been removed.
788-
$response.Content.Headers -contains "Authorization" | Should Be $false
786+
$response.Content.Headers."Authorization" | Should BeNullOrEmpty
789787
}
790788

791789
# NOTE: Only testing redirection of POST -> GET for unique underlying values of HttpStatusCode.
792790
# Some names overlap in underlying value.
793791
It "Validates Invoke-WebRequest strips the authorization header redirects and switches from POST to GET when it handles the redirect: <redirectType> <redirectedMethod>" -TestCases $redirectTests {
794792
param($redirectType, $redirectedMethod)
795-
796-
$response = ExecuteRedirectRequest -Uri "http://localhost:8080/PowerShell?test=redirect&type=$redirectType" -Method 'POST'
793+
$uri = Get-WebListenerUrl -Test 'Redirect' -Query @{type = $redirectType}
794+
$response = ExecuteRedirectRequest -Uri $uri -Method 'POST'
797795

798796
$response.Error | Should BeNullOrEmpty
799797
# ensure user-agent is present (i.e., no false positives )
800-
$response.Content.Headers -contains "User-Agent" | Should Be $true
798+
$response.Content.Headers."User-Agent" | Should Not BeNullOrEmpty
801799
# ensure Authorization header has been removed.
802-
$response.Content.Headers -contains "Authorization" | Should Be $false
800+
$response.Content.Headers."Authorization" | Should BeNullOrEmpty
803801
# ensure POST was changed to GET for selected redirections and remains as POST for others.
804-
$response.Content.HttpMethod | Should Be $redirectedMethod
802+
$response.Content.Method | Should Be $redirectedMethod
805803
}
806804

807805
#endregion Redirect tests
@@ -1753,50 +1751,50 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" {
17531751

17541752
It "Validates Invoke-RestMethod with -PreserveAuthorizationOnRedirect preserves the authorization header on redirect: <redirectType> <redirectedMethod>" -TestCases $redirectTests {
17551753
param($redirectType, $redirectedMethod)
1756-
1757-
$response = ExecuteRedirectRequest -Cmdlet 'Invoke-RestMethod' -Uri "http://localhost:8081/PowerShell?test=redirect&type=$redirectType" -PreserveAuthorizationOnRedirect
1754+
$uri = Get-WebListenerUrl -Test 'Redirect' -Query @{type = $redirectType}
1755+
$response = ExecuteRedirectRequest -Cmdlet 'Invoke-RestMethod' -Uri $uri -PreserveAuthorizationOnRedirect
17581756

17591757
$response.Error | Should BeNullOrEmpty
17601758
# ensure Authorization header has been preserved.
1761-
$response.Content.Headers -contains "Authorization" | Should Be $true
1759+
$response.Content.Headers."Authorization" | Should BeExactly "test"
17621760
}
17631761

17641762
It "Validates Invoke-RestMethod preserves the authorization header on multiple redirects: <redirectType>" -TestCases $redirectTests {
17651763
param($redirectType)
1766-
1767-
$response = ExecuteRedirectRequest -Cmdlet 'Invoke-RestMethod' -Uri "http://localhost:8081/PowerShell?test=redirect&type=$redirectType&multiredirect=true" -PreserveAuthorizationOnRedirect
1764+
$uri = Get-WebListenerUrl -Test 'Redirect' -TestValue 3 -Query @{type = $redirectType}
1765+
$response = ExecuteRedirectRequest -Cmdlet 'Invoke-RestMethod' -Uri $uri -PreserveAuthorizationOnRedirect
17681766

17691767
$response.Error | Should BeNullOrEmpty
17701768
# ensure Authorization header was stripped
1771-
$response.Content.Headers -contains "Authorization" | Should Be $true
1769+
$response.Content.Headers."Authorization" | Should BeExactly "test"
17721770
}
17731771

17741772
It "Validates Invoke-RestMethod strips the authorization header on various redirects: <redirectType>" -TestCases $redirectTests {
17751773
param($redirectType)
1776-
1777-
$response = ExecuteRedirectRequest -Cmdlet 'Invoke-RestMethod' -Uri "http://localhost:8081/PowerShell?test=redirect&type=$redirectType"
1774+
$uri = Get-WebListenerUrl -Test 'Redirect' -Query @{type = $redirectType}
1775+
$response = ExecuteRedirectRequest -Cmdlet 'Invoke-RestMethod' -Uri $uri
17781776

17791777
$response.Error | Should BeNullOrEmpty
17801778
# ensure user-agent is present (i.e., no false positives )
1781-
$response.Output.Headers -contains "User-Agent" | Should Be $true
1779+
$response.Content.Headers."User-Agent" | Should Not BeNullOrEmpty
17821780
# ensure Authorization header has been removed.
1783-
$response.Content.Headers -contains "Authorization" | Should Be $false
1781+
$response.Content.Headers."Authorization" | Should BeNullOrEmpty
17841782
}
17851783

17861784
# NOTE: Only testing redirection of POST -> GET for unique underlying values of HttpStatusCode.
17871785
# Some names overlap in underlying value.
17881786
It "Validates Invoke-RestMethod strips the authorization header redirects and switches from POST to GET when it handles the redirect: <redirectType> <redirectedMethod>" -TestCases $redirectTests {
17891787
param($redirectType, $redirectedMethod)
1790-
1791-
$response = ExecuteRedirectRequest -Cmdlet 'Invoke-RestMethod' -Uri "http://localhost:8081/PowerShell?test=redirect&type=$redirectType" -Method 'POST'
1788+
$uri = Get-WebListenerUrl -Test 'Redirect' -Query @{type = $redirectType}
1789+
$response = ExecuteRedirectRequest -Cmdlet 'Invoke-RestMethod' -Uri $uri -Method 'POST'
17921790

17931791
$response.Error | Should BeNullOrEmpty
17941792
# ensure user-agent is present (i.e., no false positives )
1795-
$response.Content.Headers -contains "User-Agent" | Should Be $true
1793+
$response.Content.Headers."User-Agent" | Should Not BeNullOrEmpty
17961794
# ensure Authorization header has been removed.
1797-
$response.Content.Headers -contains "Authorization" | Should Be $false
1795+
$response.Content."Authorization" | Should BeNullOrEmpty
17981796
# ensure POST was changed to GET for selected redirections and remains as POST for others.
1799-
$response.Content.HttpMethod | Should Be $redirectedMethod
1797+
$response.Content.Method | Should Be $redirectedMethod
18001798
}
18011799

18021800
#endregion Redirect tests

test/tools/WebListener/Controllers/RedirectController.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,45 @@
33
using System.Collections.Generic;
44
using System.Diagnostics;
55
using System.Linq;
6+
using System.Net;
7+
using System.Text.RegularExpressions;
68
using System.Threading.Tasks;
79
using Microsoft.AspNetCore.Mvc;
810
using Microsoft.AspNetCore.Http.Extensions;
11+
using Microsoft.Extensions.Primitives;
912
using mvc.Models;
1013

14+
1115
namespace mvc.Controllers
1216
{
1317
public class RedirectController : Controller
1418
{
1519
public IActionResult Index(int count)
1620
{
17-
string url;
21+
string url = Regex.Replace(input: Request.GetDisplayUrl(), pattern: "\\/Redirect.*", replacement: "", options: RegexOptions.IgnoreCase);
1822
if (count <= 1)
1923
{
20-
url = "/Get/";
24+
url = $"{url}/Get/";
2125
}
2226
else
2327
{
2428
int nextHop = count - 1;
25-
url = String.Format("/Redirect/{0}", nextHop);
29+
url = $"{url}/Redirect/{nextHop}";
2630
}
27-
Response.Redirect(url, false);
31+
32+
if (Request.Query.TryGetValue("type", out StringValues type) && Enum.TryParse(type.FirstOrDefault(), out HttpStatusCode status))
33+
{
34+
Response.StatusCode = (int)status;
35+
url = $"{url}?type={type.FirstOrDefault()}";
36+
Response.Headers.Add("Location", url);
37+
}
38+
else
39+
{
40+
Response.Redirect(url, false);
41+
}
42+
2843
ViewData["Url"] = url;
44+
2945
return View();
3046
}
3147
public IActionResult Error()

test/tools/WebListener/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,8 @@ Invoke-RestMethod -Uri $uri -Body $body -Method 'Put'
426426

427427
### /Redirect/
428428

429-
Will 302 redirect to `/Get/`. If a number is supplied, redirect will occur that many times. Can be used to test maximum redirects.
429+
Will `302` redirect to `/Get/`. If a number is supplied, redirect will occur that many times. Can be used to test maximum redirects.
430+
If the `type` query field is supplied the corresponding `System.Net.HttpStatusCode` will be returned instead of `302`.
430431

431432
```powershell
432433
$uri = Get-WebListenerUrl -Test 'Redirect' -TestValue '2'

0 commit comments

Comments
 (0)