diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index b226af42113..619022e5c16 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -494,7 +494,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { It "Validate Invoke-WebRequest -MaximumRedirection" { - $uri = Get-WebListenerUrl -Test 'Redirect/3' + $uri = Get-WebListenerUrl -Test 'Redirect' -TestValue '3' $command = "Invoke-WebRequest -Uri '$uri' -MaximumRedirection 4" $result = ExecuteWebCommand -command $command @@ -508,7 +508,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { It "Validate Invoke-WebRequest error for -MaximumRedirection" { - $uri = Get-WebListenerUrl -Test 'Redirect/3' + $uri = Get-WebListenerUrl -Test 'Redirect' -TestValue '3' $command = "Invoke-WebRequest -Uri '$uri' -MaximumRedirection 2" $result = ExecuteWebCommand -command $command @@ -517,7 +517,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { It "Invoke-WebRequest supports request that returns page containing UTF-8 data." { - $uri = Get-WebListenerUrl -Test 'Encoding/Utf8' + $uri = Get-WebListenerUrl -Test 'Encoding' -TestValue 'Utf8' $command = "Invoke-WebRequest -Uri '$uri'" $result = ExecuteWebCommand -command $command @@ -529,15 +529,17 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { It "Invoke-WebRequest validate timeout option" { - $command = "Invoke-WebRequest -Uri http://httpbin.org/delay/:5 -TimeoutSec 5" + $uri = Get-WebListenerUrl -Test 'Delay' -TestValue '5' + $command = "Invoke-WebRequest -Uri '$uri' -TimeoutSec 5" $result = ExecuteWebCommand -command $command - $result.Error.FullyQualifiedErrorId | Should Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand" + $result.Error.FullyQualifiedErrorId | Should Be "System.Threading.Tasks.TaskCanceledException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand" } It "Validate Invoke-WebRequest error with -Proxy and -NoProxy option" { - $command = "Invoke-WebRequest -Uri http://httpbin.org/delay/:10 -Proxy 'http://localhost:8080' -NoProxy -TimeoutSec 2" + $uri = Get-WebListenerUrl -Test 'Delay' -TestValue '10' + $command = "Invoke-WebRequest -Uri '$uri' -Proxy 'http://localhost:8080' -NoProxy -TimeoutSec 2" $result = ExecuteWebCommand -command $command $result.Error.FullyQualifiedErrorId | Should Be "AmbiguousParameterSet,Microsoft.PowerShell.Commands.InvokeWebRequestCommand" @@ -551,10 +553,11 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { It "Validate Invoke-WebRequest error with -Proxy option set - ''" -TestCases $testCase { param($proxy_address, $name, $protocol) - $command = "Invoke-WebRequest -Uri '${protocol}://httpbin.org/delay/:5' -TimeoutSec 5 -Proxy '${proxy_address}'" + $uri = Get-WebListenerUrl -Test 'Delay' -TestValue '5' -Https:$($protocol -eq 'https') + $command = "Invoke-WebRequest -Uri '$uri' -TimeoutSec 5 -Proxy '${proxy_address}' -SkipCertificateCheck" $result = ExecuteWebCommand -command $command - $result.Error.FullyQualifiedErrorId | Should Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand" + $result.Error.FullyQualifiedErrorId | Should Be "System.Threading.Tasks.TaskCanceledException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand" } It "Validate Invoke-WebRequest error with environment proxy set - ''" -TestCases $testCase { @@ -563,10 +566,11 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { # Configure the environment variable. New-Item -Name ${name} -Value ${proxy_address} -ItemType Variable -Path Env: -Force - $command = "Invoke-WebRequest -Uri '${protocol}://httpbin.org/delay/:5' -TimeoutSec 5" + $uri = Get-WebListenerUrl -Test 'Delay' -TestValue '5' -Https:$($protocol -eq 'https') + $command = "Invoke-WebRequest -Uri '$uri' -TimeoutSec 5 -SkipCertificateCheck" $result = ExecuteWebCommand -command $command - $result.Error.FullyQualifiedErrorId | Should Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand" + $result.Error.FullyQualifiedErrorId | Should Be "System.Threading.Tasks.TaskCanceledException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand" } It "Validate Invoke-WebRequest returns User-Agent where -NoProxy with envirionment proxy set - ''" -TestCases $testCase { @@ -587,14 +591,6 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { $jsonContent.headers.'User-Agent' | Should Match "WindowsPowerShell" } - It "Invoke-WebRequest validate timeout option" { - - $command = "Invoke-WebRequest -Uri http://httpbin.org/delay/:5 -TimeoutSec 10" - - $result = ExecuteWebCommand -command $command - $result.Error.FullyQualifiedErrorId | Should Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand" - } - # Perform the following operation for Invoke-WebRequest # gzip Returns gzip-encoded data. # deflate Returns deflate-encoded data. @@ -1353,7 +1349,7 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { It "Validate Invoke-RestMethod -MaximumRedirection" { - $uri = Get-WebListenerUrl -Test 'Redirect/3' + $uri = Get-WebListenerUrl -Test 'Redirect' -TestValue '3' $command = "Invoke-RestMethod -Uri '$uri' -MaximumRedirection 4" $result = ExecuteWebCommand -command $command @@ -1365,7 +1361,7 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { It "Validate Invoke-RestMethod error for -MaximumRedirection" { - $uri = Get-WebListenerUrl -Test 'Redirect/3' + $uri = Get-WebListenerUrl -Test 'Redirect' -TestValue '3' $command = "Invoke-RestMethod -Uri '$uri' -MaximumRedirection 2" $result = ExecuteWebCommand -command $command @@ -1374,7 +1370,7 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { It "Invoke-RestMethod supports request that returns page containing UTF-8 data." { - $uri = Get-WebListenerUrl -Test 'Encoding/Utf8' + $uri = Get-WebListenerUrl -Test 'Encoding' -TestValue 'Utf8' $command = "Invoke-RestMethod -Uri '$uri'" $result = ExecuteWebCommand -command $command @@ -1383,15 +1379,17 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { It "Invoke-RestMethod validate timeout option" { - $command = "Invoke-RestMethod -Uri http://httpbin.org/delay/:5 -TimeoutSec 2" + $uri = Get-WebListenerUrl -Test 'Delay' -TestValue '5' + $command = "Invoke-RestMethod -Uri '$uri' -TimeoutSec 2" $result = ExecuteWebCommand -command $command - $result.Error.FullyQualifiedErrorId | Should Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand" + $result.Error.FullyQualifiedErrorId | Should Be "System.Threading.Tasks.TaskCanceledException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand" } It "Validate Invoke-RestMethod error with -Proxy and -NoProxy option" { - $command = "Invoke-RestMethod -Uri http://httpbin.org/delay/:10 -Proxy 'http://localhost:8080' -NoProxy -TimeoutSec 2" + $uri = Get-WebListenerUrl -Test 'Delay' -TestValue '10' + $command = "Invoke-RestMethod -Uri '$uri' -Proxy 'http://localhost:8080' -NoProxy -TimeoutSec 2" $result = ExecuteWebCommand -command $command $result.Error.FullyQualifiedErrorId | Should Be "AmbiguousParameterSet,Microsoft.PowerShell.Commands.InvokeRestMethodCommand" @@ -1417,10 +1415,11 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { # Configure the environment variable. New-Item -Name ${name} -Value ${proxy_address} -ItemType Variable -Path Env: -Force - $command = "Invoke-RestMethod -Uri '${protocol}://httpbin.org/delay/:5' -TimeoutSec 5" + $uri = Get-WebListenerUrl -Test 'Delay' -TestValue '5' -Https:$($protocol -eq 'https') + $command = "Invoke-RestMethod -Uri '$uri' -TimeoutSec 5 -SkipCertificateCheck" $result = ExecuteWebCommand -command $command - $result.Error.FullyQualifiedErrorId | Should Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand" + $result.Error.FullyQualifiedErrorId | Should Be "System.Threading.Tasks.TaskCanceledException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand" } It "Validate Invoke-RestMethod returns User-Agent with option -NoProxy when environment proxy set - ''" -TestCases $testCase { diff --git a/test/tools/Modules/WebListener/WebListener.psm1 b/test/tools/Modules/WebListener/WebListener.psm1 index fca4e047622..6fe0049cde1 100644 --- a/test/tools/Modules/WebListener/WebListener.psm1 +++ b/test/tools/Modules/WebListener/WebListener.psm1 @@ -114,14 +114,17 @@ function Get-WebListenerUrl { [switch]$Https, [ValidateSet( 'Cert', - 'Encoding/Utf8', + 'Delay', + 'Encoding', 'Get', 'Home', 'Multipart', - 'Redirect/3', + 'Redirect', '/' )] - [String]$Test + [String]$Test, + + [String]$TestValue ) process { $runningListener = Get-WebListener @@ -138,7 +141,7 @@ function Get-WebListenerUrl { $Uri.Port = $runningListener.HttpsPort $Uri.Scheme = 'Https' } - $Uri.Path = $Test + $Uri.Path = '{0}/{1}' -f $Test, $TestValue return [Uri]$Uri.ToString() } } diff --git a/test/tools/WebListener/Controllers/DelayController.cs b/test/tools/WebListener/Controllers/DelayController.cs new file mode 100644 index 00000000000..310276bd15b --- /dev/null +++ b/test/tools/WebListener/Controllers/DelayController.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Http.Extensions; +using mvc.Models; + +namespace mvc.Controllers +{ + public class DelayController : Controller + { + public JsonResult Index(int seconds) + { + if (seconds > 0){ + int milliseconds = seconds * 1000; + Thread.Sleep(milliseconds); + } + var getController = new GetController(); + getController.ControllerContext = this.ControllerContext; + return getController.Index(); + } + + public IActionResult Error() + { + return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); + } + } +} diff --git a/test/tools/WebListener/README.md b/test/tools/WebListener/README.md index 8dc89520d86..802b4cc4fc9 100644 --- a/test/tools/WebListener/README.md +++ b/test/tools/WebListener/README.md @@ -59,6 +59,30 @@ Response when certificate is not provided in request: } ``` +## /Delay/ + +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. + +```powershell +Invoke-WebRequest -Uri 'http://localhost:8083/Delay/5' +``` + +After 5 Seconds: + +```json +{ + "args": { + + }, + "origin": "127.0.0.1", + "headers": { + "User-Agent": "Mozilla/5.0 (Windows NT; Windows NT 10.0; en-US) WindowsPowerShell/5.1.15063.608", + "Host": "localhost:8083" + }, + "url": "http://localhost:8083/Delay/5" +} +``` + ## /Encoding/Utf8/ Returns page containing UTF-8 data. diff --git a/test/tools/WebListener/Startup.cs b/test/tools/WebListener/Startup.cs index 040721ee39e..00553c5e247 100644 --- a/test/tools/WebListener/Startup.cs +++ b/test/tools/WebListener/Startup.cs @@ -48,6 +48,11 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env) template: "Redirect/{count?}", defaults: new {controller = "Redirect", action = "Index"} ); + routes.MapRoute( + name: "delay", + template: "Delay/{seconds?}", + defaults: new {controller = "Delay", action = "Index"} + ); }); } } diff --git a/test/tools/WebListener/Views/Home/Index.cshtml b/test/tools/WebListener/Views/Home/Index.cshtml index ffa4cc474ab..7b4669ce334 100644 --- a/test/tools/WebListener/Views/Home/Index.cshtml +++ b/test/tools/WebListener/Views/Home/Index.cshtml @@ -2,6 +2,7 @@
  • / - This page
  • /Cert/ - Client Certificate Details
  • +
  • /Delay/{seconds} - Delays response for seconds seconds.
  • /Encoding/Utf8/ - Returns page containing UTF-8 data.
  • /Get/ - Emulates functionality of https://httpbin.org/get by returning GET headers, Arguments, and Request URL
  • /Multipart/ - Multipart/form-data submission testing