Skip to content

Commit b30da37

Browse files
markekrausTravisEz13
authored andcommitted
Replace httpbin.org/delay Tests with WebListener (PowerShell#4905)
1 parent bb3a840 commit b30da37

File tree

6 files changed

+94
-30
lines changed

6 files changed

+94
-30
lines changed

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

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" {
494494

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

497-
$uri = Get-WebListenerUrl -Test 'Redirect/3'
497+
$uri = Get-WebListenerUrl -Test 'Redirect' -TestValue '3'
498498
$command = "Invoke-WebRequest -Uri '$uri' -MaximumRedirection 4"
499499

500500
$result = ExecuteWebCommand -command $command
@@ -508,7 +508,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" {
508508

509509
It "Validate Invoke-WebRequest error for -MaximumRedirection" {
510510

511-
$uri = Get-WebListenerUrl -Test 'Redirect/3'
511+
$uri = Get-WebListenerUrl -Test 'Redirect' -TestValue '3'
512512
$command = "Invoke-WebRequest -Uri '$uri' -MaximumRedirection 2"
513513

514514
$result = ExecuteWebCommand -command $command
@@ -517,7 +517,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" {
517517

518518
It "Invoke-WebRequest supports request that returns page containing UTF-8 data." {
519519

520-
$uri = Get-WebListenerUrl -Test 'Encoding/Utf8'
520+
$uri = Get-WebListenerUrl -Test 'Encoding' -TestValue 'Utf8'
521521
$command = "Invoke-WebRequest -Uri '$uri'"
522522

523523
$result = ExecuteWebCommand -command $command
@@ -529,15 +529,17 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" {
529529

530530
It "Invoke-WebRequest validate timeout option" {
531531

532-
$command = "Invoke-WebRequest -Uri http://httpbin.org/delay/:5 -TimeoutSec 5"
532+
$uri = Get-WebListenerUrl -Test 'Delay' -TestValue '5'
533+
$command = "Invoke-WebRequest -Uri '$uri' -TimeoutSec 5"
533534

534535
$result = ExecuteWebCommand -command $command
535-
$result.Error.FullyQualifiedErrorId | Should Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand"
536+
$result.Error.FullyQualifiedErrorId | Should Be "System.Threading.Tasks.TaskCanceledException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand"
536537
}
537538

538539
It "Validate Invoke-WebRequest error with -Proxy and -NoProxy option" {
539540

540-
$command = "Invoke-WebRequest -Uri http://httpbin.org/delay/:10 -Proxy 'http://localhost:8080' -NoProxy -TimeoutSec 2"
541+
$uri = Get-WebListenerUrl -Test 'Delay' -TestValue '10'
542+
$command = "Invoke-WebRequest -Uri '$uri' -Proxy 'http://localhost:8080' -NoProxy -TimeoutSec 2"
541543

542544
$result = ExecuteWebCommand -command $command
543545
$result.Error.FullyQualifiedErrorId | Should Be "AmbiguousParameterSet,Microsoft.PowerShell.Commands.InvokeWebRequestCommand"
@@ -551,10 +553,11 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" {
551553
It "Validate Invoke-WebRequest error with -Proxy option set - '<name>'" -TestCases $testCase {
552554
param($proxy_address, $name, $protocol)
553555

554-
$command = "Invoke-WebRequest -Uri '${protocol}://httpbin.org/delay/:5' -TimeoutSec 5 -Proxy '${proxy_address}'"
556+
$uri = Get-WebListenerUrl -Test 'Delay' -TestValue '5' -Https:$($protocol -eq 'https')
557+
$command = "Invoke-WebRequest -Uri '$uri' -TimeoutSec 5 -Proxy '${proxy_address}' -SkipCertificateCheck"
555558

556559
$result = ExecuteWebCommand -command $command
557-
$result.Error.FullyQualifiedErrorId | Should Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand"
560+
$result.Error.FullyQualifiedErrorId | Should Be "System.Threading.Tasks.TaskCanceledException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand"
558561
}
559562

560563
It "Validate Invoke-WebRequest error with environment proxy set - '<name>'" -TestCases $testCase {
@@ -563,10 +566,11 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" {
563566
# Configure the environment variable.
564567
New-Item -Name ${name} -Value ${proxy_address} -ItemType Variable -Path Env: -Force
565568

566-
$command = "Invoke-WebRequest -Uri '${protocol}://httpbin.org/delay/:5' -TimeoutSec 5"
569+
$uri = Get-WebListenerUrl -Test 'Delay' -TestValue '5' -Https:$($protocol -eq 'https')
570+
$command = "Invoke-WebRequest -Uri '$uri' -TimeoutSec 5 -SkipCertificateCheck"
567571

568572
$result = ExecuteWebCommand -command $command
569-
$result.Error.FullyQualifiedErrorId | Should Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand"
573+
$result.Error.FullyQualifiedErrorId | Should Be "System.Threading.Tasks.TaskCanceledException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand"
570574
}
571575

572576
It "Validate Invoke-WebRequest returns User-Agent where -NoProxy with envirionment proxy set - '<name>'" -TestCases $testCase {
@@ -587,14 +591,6 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" {
587591
$jsonContent.headers.'User-Agent' | Should Match "WindowsPowerShell"
588592
}
589593

590-
It "Invoke-WebRequest validate timeout option" {
591-
592-
$command = "Invoke-WebRequest -Uri http://httpbin.org/delay/:5 -TimeoutSec 10"
593-
594-
$result = ExecuteWebCommand -command $command
595-
$result.Error.FullyQualifiedErrorId | Should Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand"
596-
}
597-
598594
# Perform the following operation for Invoke-WebRequest
599595
# gzip Returns gzip-encoded data.
600596
# deflate Returns deflate-encoded data.
@@ -1353,7 +1349,7 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" {
13531349

13541350
It "Validate Invoke-RestMethod -MaximumRedirection" {
13551351

1356-
$uri = Get-WebListenerUrl -Test 'Redirect/3'
1352+
$uri = Get-WebListenerUrl -Test 'Redirect' -TestValue '3'
13571353
$command = "Invoke-RestMethod -Uri '$uri' -MaximumRedirection 4"
13581354

13591355
$result = ExecuteWebCommand -command $command
@@ -1365,7 +1361,7 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" {
13651361

13661362
It "Validate Invoke-RestMethod error for -MaximumRedirection" {
13671363

1368-
$uri = Get-WebListenerUrl -Test 'Redirect/3'
1364+
$uri = Get-WebListenerUrl -Test 'Redirect' -TestValue '3'
13691365
$command = "Invoke-RestMethod -Uri '$uri' -MaximumRedirection 2"
13701366

13711367
$result = ExecuteWebCommand -command $command
@@ -1374,7 +1370,7 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" {
13741370

13751371
It "Invoke-RestMethod supports request that returns page containing UTF-8 data." {
13761372

1377-
$uri = Get-WebListenerUrl -Test 'Encoding/Utf8'
1373+
$uri = Get-WebListenerUrl -Test 'Encoding' -TestValue 'Utf8'
13781374
$command = "Invoke-RestMethod -Uri '$uri'"
13791375

13801376
$result = ExecuteWebCommand -command $command
@@ -1383,15 +1379,17 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" {
13831379

13841380
It "Invoke-RestMethod validate timeout option" {
13851381

1386-
$command = "Invoke-RestMethod -Uri http://httpbin.org/delay/:5 -TimeoutSec 2"
1382+
$uri = Get-WebListenerUrl -Test 'Delay' -TestValue '5'
1383+
$command = "Invoke-RestMethod -Uri '$uri' -TimeoutSec 2"
13871384

13881385
$result = ExecuteWebCommand -command $command
1389-
$result.Error.FullyQualifiedErrorId | Should Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand"
1386+
$result.Error.FullyQualifiedErrorId | Should Be "System.Threading.Tasks.TaskCanceledException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand"
13901387
}
13911388

13921389
It "Validate Invoke-RestMethod error with -Proxy and -NoProxy option" {
13931390

1394-
$command = "Invoke-RestMethod -Uri http://httpbin.org/delay/:10 -Proxy 'http://localhost:8080' -NoProxy -TimeoutSec 2"
1391+
$uri = Get-WebListenerUrl -Test 'Delay' -TestValue '10'
1392+
$command = "Invoke-RestMethod -Uri '$uri' -Proxy 'http://localhost:8080' -NoProxy -TimeoutSec 2"
13951393

13961394
$result = ExecuteWebCommand -command $command
13971395
$result.Error.FullyQualifiedErrorId | Should Be "AmbiguousParameterSet,Microsoft.PowerShell.Commands.InvokeRestMethodCommand"
@@ -1417,10 +1415,11 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" {
14171415
# Configure the environment variable.
14181416
New-Item -Name ${name} -Value ${proxy_address} -ItemType Variable -Path Env: -Force
14191417

1420-
$command = "Invoke-RestMethod -Uri '${protocol}://httpbin.org/delay/:5' -TimeoutSec 5"
1418+
$uri = Get-WebListenerUrl -Test 'Delay' -TestValue '5' -Https:$($protocol -eq 'https')
1419+
$command = "Invoke-RestMethod -Uri '$uri' -TimeoutSec 5 -SkipCertificateCheck"
14211420

14221421
$result = ExecuteWebCommand -command $command
1423-
$result.Error.FullyQualifiedErrorId | Should Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand"
1422+
$result.Error.FullyQualifiedErrorId | Should Be "System.Threading.Tasks.TaskCanceledException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand"
14241423
}
14251424

14261425
It "Validate Invoke-RestMethod returns User-Agent with option -NoProxy when environment proxy set - '<name>'" -TestCases $testCase {

test/tools/Modules/WebListener/WebListener.psm1

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,17 @@ function Get-WebListenerUrl {
114114
[switch]$Https,
115115
[ValidateSet(
116116
'Cert',
117-
'Encoding/Utf8',
117+
'Delay',
118+
'Encoding',
118119
'Get',
119120
'Home',
120121
'Multipart',
121-
'Redirect/3',
122+
'Redirect',
122123
'/'
123124
)]
124-
[String]$Test
125+
[String]$Test,
126+
127+
[String]$TestValue
125128
)
126129
process {
127130
$runningListener = Get-WebListener
@@ -138,7 +141,7 @@ function Get-WebListenerUrl {
138141
$Uri.Port = $runningListener.HttpsPort
139142
$Uri.Scheme = 'Https'
140143
}
141-
$Uri.Path = $Test
144+
$Uri.Path = '{0}/{1}' -f $Test, $TestValue
142145
return [Uri]$Uri.ToString()
143146
}
144147
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Diagnostics;
5+
using System.Linq;
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.AspNetCore.Mvc;
9+
using Microsoft.AspNetCore.Http.Extensions;
10+
using mvc.Models;
11+
12+
namespace mvc.Controllers
13+
{
14+
public class DelayController : Controller
15+
{
16+
public JsonResult Index(int seconds)
17+
{
18+
if (seconds > 0){
19+
int milliseconds = seconds * 1000;
20+
Thread.Sleep(milliseconds);
21+
}
22+
var getController = new GetController();
23+
getController.ControllerContext = this.ControllerContext;
24+
return getController.Index();
25+
}
26+
27+
public IActionResult Error()
28+
{
29+
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
30+
}
31+
}
32+
}

test/tools/WebListener/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,30 @@ Response when certificate is not provided in request:
5959
}
6060
```
6161

62+
## /Delay/
63+
64+
Returns the same results as the Get test. If a number is supplied, the server will wait that many seconds before returning a response. This can be used to test timeouts.
65+
66+
```powershell
67+
Invoke-WebRequest -Uri 'http://localhost:8083/Delay/5'
68+
```
69+
70+
After 5 Seconds:
71+
72+
```json
73+
{
74+
"args": {
75+
76+
},
77+
"origin": "127.0.0.1",
78+
"headers": {
79+
"User-Agent": "Mozilla/5.0 (Windows NT; Windows NT 10.0; en-US) WindowsPowerShell/5.1.15063.608",
80+
"Host": "localhost:8083"
81+
},
82+
"url": "http://localhost:8083/Delay/5"
83+
}
84+
```
85+
6286
## /Encoding/Utf8/
6387

6488
Returns page containing UTF-8 data.

test/tools/WebListener/Startup.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
4848
template: "Redirect/{count?}",
4949
defaults: new {controller = "Redirect", action = "Index"}
5050
);
51+
routes.MapRoute(
52+
name: "delay",
53+
template: "Delay/{seconds?}",
54+
defaults: new {controller = "Delay", action = "Index"}
55+
);
5156
});
5257
}
5358
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<ul>
33
<li><a href="/">/</a> - This page</li>
44
<li><a href="/Cert/">/Cert/</a> - Client Certificate Details</li>
5+
<li><a href="/Delay/">/Delay/{seconds}</a> - Delays response for <i>seconds</i> seconds.</li>
56
<li><a href="/Encoding/Utf8/">/Encoding/Utf8/</a> - Returns page containing UTF-8 data.</li>
67
<li><a href="/Get/">/Get/</a> - Emulates functionality of https://httpbin.org/get by returning GET headers, Arguments, and Request URL</li>
78
<li><a href="/Multipart/">/Multipart/</a> - Multipart/form-data submission testing</li>

0 commit comments

Comments
 (0)