Skip to content

Commit a2dd0ca

Browse files
markekrausTravisEz13
authored andcommitted
Replace httpbin.org/redirect Tests with WebListener (PowerShell#4852)
1 parent a3e129c commit a2dd0ca

7 files changed

Lines changed: 110 additions & 6 deletions

File tree

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -494,20 +494,22 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" {
494494

495495
It "Validate Invoke-WebRequest -MaximumRedirection" {
496496

497-
$command = "Invoke-WebRequest -Uri 'http://httpbin.org/redirect/3' -MaximumRedirection 4 -TimeoutSec 5"
497+
$uri = Get-WebListenerUrl -Test 'Redirect/3'
498+
$command = "Invoke-WebRequest -Uri '$uri' -MaximumRedirection 4"
498499

499500
$result = ExecuteWebCommand -command $command
500501
ValidateResponse -response $result
501502

502503
# Validate response content
503504
$jsonContent = $result.Output.Content | ConvertFrom-Json
504-
$jsonContent.headers.Host | Should Match "httpbin.org"
505+
$jsonContent.headers.Host | Should Match $uri.Authority
505506
$jsonContent.headers.'User-Agent' | Should Match "WindowsPowerShell"
506507
}
507508

508509
It "Validate Invoke-WebRequest error for -MaximumRedirection" {
509510

510-
$command = "Invoke-WebRequest -Uri 'http://httpbin.org/redirect/3' -MaximumRedirection 2 -TimeoutSec 5"
511+
$uri = Get-WebListenerUrl -Test 'Redirect/3'
512+
$command = "Invoke-WebRequest -Uri '$uri' -MaximumRedirection 2"
511513

512514
$result = ExecuteWebCommand -command $command
513515
$result.Error.FullyQualifiedErrorId | Should Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand"
@@ -1355,18 +1357,20 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" {
13551357

13561358
It "Validate Invoke-RestMethod -MaximumRedirection" {
13571359

1358-
$command = "Invoke-RestMethod -Uri 'http://httpbin.org/redirect/3' -MaximumRedirection 4 -TimeoutSec 5"
1360+
$uri = Get-WebListenerUrl -Test 'Redirect/3'
1361+
$command = "Invoke-RestMethod -Uri '$uri' -MaximumRedirection 4"
13591362

13601363
$result = ExecuteWebCommand -command $command
13611364

13621365
# Validate response
1363-
$result.Output.headers.Host | Should Match "httpbin.org"
1366+
$result.Output.headers.Host | Should Match $uri.Authority
13641367
$result.Output.headers.'User-Agent' | Should Match "WindowsPowerShell"
13651368
}
13661369

13671370
It "Validate Invoke-RestMethod error for -MaximumRedirection" {
13681371

1369-
$command = "Invoke-RestMethod -Uri 'http://httpbin.org/redirect/3' -MaximumRedirection 2 -TimeoutSec 5"
1372+
$uri = Get-WebListenerUrl -Test 'Redirect/3'
1373+
$command = "Invoke-RestMethod -Uri '$uri' -MaximumRedirection 2"
13701374

13711375
$result = ExecuteWebCommand -command $command
13721376
$result.Error.FullyQualifiedErrorId | Should Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand"

test/tools/Modules/WebListener/WebListener.psm1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ function Get-WebListenerUrl {
117117
'Get',
118118
'Home',
119119
'Multipart',
120+
'Redirect/3',
120121
'/'
121122
)]
122123
[String]$Test
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Diagnostics;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
using Microsoft.AspNetCore.Mvc;
8+
using Microsoft.AspNetCore.Http.Extensions;
9+
using mvc.Models;
10+
11+
namespace mvc.Controllers
12+
{
13+
public class RedirectController : Controller
14+
{
15+
public IActionResult Index(int count)
16+
{
17+
string url;
18+
if (count <= 1)
19+
{
20+
url = "/Get/";
21+
}
22+
else
23+
{
24+
int nextHop = count - 1;
25+
url = String.Format("/Redirect/{0}", nextHop);
26+
}
27+
Response.Redirect(url, false);
28+
ViewData["Url"] = url;
29+
return View();
30+
}
31+
public IActionResult Error()
32+
{
33+
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
34+
}
35+
}
36+
}

test/tools/WebListener/README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,53 @@ Accepts a `multipart/form-data` submission and returns a JSON object containing
126126
}
127127
}
128128
```
129+
130+
## /Redirect/
131+
132+
Will 302 redirect to `/Get/`. If a number is supplied, redirect will occur that many times. Can be used to test maximum redirects.
133+
134+
Request 1:
135+
```none
136+
GET http://localhost:8083/Redirect/2 HTTP/1.1
137+
Connection: Keep-Alive
138+
User-Agent: Mozilla/5.0 (Windows NT; Microsoft Windows 10.0.15063 ; en-US) WindowsPowerShell/6.0.0
139+
Host: localhost:8083
140+
```
141+
142+
Response 1:
143+
```none
144+
HTTP/1.1 302 Found
145+
Date: Fri, 15 Sep 2017 10:46:41 GMT
146+
Content-Type: text/html; charset=utf-8
147+
Server: Kestrel
148+
Transfer-Encoding: chunked
149+
Location: /Redirect/1
150+
151+
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
152+
<title>Redirecting...</title>
153+
<h1>Redirecting...</h1>
154+
<p>You should be redirected automatically to target URL: <a href="/Redirect/1">/Redirect/1</a>. If not click the link.
155+
```
156+
157+
Request 2:
158+
```none
159+
GET http://localhost:8083/Redirect/1 HTTP/1.1
160+
Connection: Keep-Alive
161+
User-Agent: Mozilla/5.0 (Windows NT; Microsoft Windows 10.0.15063 ; en-US) WindowsPowerShell/6.0.0
162+
Host: localhost:8083
163+
```
164+
165+
Response 2:
166+
```none
167+
HTTP/1.1 302 Found
168+
Date: Fri, 15 Sep 2017 10:46:41 GMT
169+
Content-Type: text/html; charset=utf-8
170+
Server: Kestrel
171+
Transfer-Encoding: chunked
172+
Location: /Get/
173+
174+
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
175+
<title>Redirecting...</title>
176+
<h1>Redirecting...</h1>
177+
<p>You should be redirected automatically to target URL: <a href="/Get/">/Get/</a>. If not click the link.
178+
```

test/tools/WebListener/Startup.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
4343
routes.MapRoute(
4444
name: "default",
4545
template: "{controller=Home}/{action=Index}/{id?}");
46+
routes.MapRoute(
47+
name: "redirect",
48+
template: "Redirect/{count?}",
49+
defaults: new {controller = "Redirect", action = "Index"}
50+
);
4651
});
4752
}
4853
}

test/tools/WebListener/Views/Home/Index.cshtml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
<li><a href="/Cert/">/Cert/</a> - Client Certificate Details</li>
55
<li><a href="/Get/">/Get/</a> - Emulates functionality of https://httpbin.org/get by returning GET headers, Arguments, and Request URL</li>
66
<li><a href="/Multipart/">/Multipart/</a> - Multipart/form-data submission testing</li>
7+
<li><a href="/Redirect/">/Redirect/{count}</a> - 302 redirect <i>count</i> times.</li>
78
</ul>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@{
2+
string url = (string)@ViewData["Url"];
3+
}
4+
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
5+
<title>Redirecting...</title>
6+
<h1>Redirecting...</h1>
7+
<p>You should be redirected automatically to target URL: <a href="@url">@url</a>. If not click the link.

0 commit comments

Comments
 (0)