diff --git a/.spelling b/.spelling index 4ab03e94856..e9d2da459eb 100644 --- a/.spelling +++ b/.spelling @@ -56,6 +56,7 @@ hashtable hashtables homebrew hotfix +HttpBin's init Invoke-RestMethod Invoke-WebRequest diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index d362f0ca2e8..96b80517b10 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -41,7 +41,7 @@ function ExecuteRequestWithOutFile [string] $cmdletName, [string] - $uri = "http://httpbin.org/get" + $uri = (Get-WebListenerUrl -Test 'Get') ) $result = [PSObject]@{Output = $null; Error = $null} @@ -82,7 +82,7 @@ function ExecuteRequestWithHeaders [string] $cmdletName, [string] - $uri = "http://httpbin.org/get" + $uri = (Get-WebListenerUrl -Test 'Get') ) $result = [PSObject]@{Output = $null; Error = $null} @@ -442,13 +442,13 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { It "Validate Invoke-WebRequest -DisableKeepAlive" { # Operation options - $uri = "http://httpbin.org/get" + $uri = Get-WebListenerUrl -Test 'Get' $command = "Invoke-WebRequest -Uri $uri -TimeoutSec 5 -DisableKeepAlive" $result = ExecuteWebCommand -command $command ValidateResponse -response $result - $result.Output.Headers["Connection"] | Should Be "Close" + $result.Output.Headers.Connection | Should Be "Close" } It "Validate Invoke-WebRequest -MaximumRedirection" { @@ -641,10 +641,10 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { It "Validate Invoke-WebRequest -Headers --> Set KeepAlive to false via headers" { - $uri = "http://httpbin.org/get" + $uri = Get-WebListenerUrl -Test 'Get' $result = ExecuteRequestWithHeaders -cmdletName Invoke-WebRequest -uri $uri ValidateResponse -response $result - $result.Output.Headers["Connection"] | Should Be "Close" + $result.Output.Headers.Connection | Should Be "Close" } # Validate all available user agents for Invoke-WebRequest @@ -658,7 +658,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { foreach ($agentName in $agents.Keys) { $expectedAgent = $agents[$agentName] - $uri = "http://httpbin.org/get" + $uri = Get-WebListenerUrl -Test 'Get' $userAgent = "[Microsoft.PowerShell.Commands.PSUserAgent]::$agentName" $command = "Invoke-WebRequest -Uri $uri -UserAgent ($userAgent) -TimeoutSec 5" @@ -669,17 +669,17 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { # Validate response content $jsonContent = $result.Output.Content | ConvertFrom-Json - $jsonContent.headers.Host | Should Match "httpbin.org" + $jsonContent.headers.Host | Should Be $uri.Authority $jsonContent.headers.'User-Agent' | Should Match $expectedAgent } } It "Validate Invoke-WebRequest -OutFile" { - $uri = "http://httpbin.org/get" + $uri = Get-WebListenerUrl -Test 'Get' $result = ExecuteRequestWithOutFile -cmdletName "Invoke-WebRequest" -uri $uri $jsonContent = $result.Output | ConvertFrom-Json - $jsonContent.headers.Host | Should Match "httpbin.org" + $jsonContent.headers.Host | Should Be $uri.Authority $jsonContent.headers.'User-Agent' | Should Match "WindowsPowerShell" } @@ -715,7 +715,8 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { It "Validate Invoke-WebRequest body is converted to query params for CustomMethod GET" { - $command = "Invoke-WebRequest -Uri 'http://httpbin.org/get' -CustomMethod GET -Body @{'testparam'='testvalue'}" + $uri = Get-WebListenerUrl -Test 'Get' + $command = "Invoke-WebRequest -Uri '$uri' -CustomMethod GET -Body @{'testparam'='testvalue'}" $result = ExecuteWebCommand -command $command ($result.Output.Content | ConvertFrom-Json).args.testparam | Should Be "testvalue" } @@ -1262,16 +1263,15 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { It "Validate Invoke-RestMethod -DisableKeepAlive" { # Operation options - $command = "Invoke-RestMethod -Uri 'http://httpbin.org/get' -TimeoutSec 5 -DisableKeepAlive" + $uri = Get-WebListenerUrl -Test 'Get' + $command = "Invoke-RestMethod -Uri '$uri' -TimeoutSec 5 -DisableKeepAlive" $result = ExecuteWebCommand -command $command # Validate response - $result.Output.headers.Host | Should Match "httpbin.org" + $result.Output.headers.Host | Should Be $uri.Authority $result.Output.headers.'User-Agent' | Should Match "WindowsPowerShell" - - # Unfortunately, the connection information is not display in the output of Invoke-RestMethod - #$result.Output.Headers["Connection"] | Should Be "Close" + $result.Output.Headers.Connection | Should Be "Close" } It "Validate Invoke-RestMethod -MaximumRedirection" { @@ -1445,15 +1445,13 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { It "Validate Invoke-RestMethod -Headers --> Set KeepAlive to false via headers" { - $uri = "http://httpbin.org/get" + $uri = Get-WebListenerUrl -Test 'Get' $result = ExecuteRequestWithHeaders -cmdletName Invoke-RestMethod -uri $uri # Validate response $result.Output.url | Should Match $uri $result.Output.headers.'User-Agent' | Should Match "WindowsPowerShell" - - # Unfortunately, the connection information is not display in the output of Invoke-RestMethod - #$result.Output.Headers["Connection"] | Should Be "Close" + $result.Output.Headers.Connection | Should Be "Close" } # Validate all available user agents for Invoke-RestMethod @@ -1467,7 +1465,7 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { foreach ($agentName in $agents.Keys) { $expectedAgent = $agents[$agentName] - $uri = "http://httpbin.org/get" + $uri = Get-WebListenerUrl -Test 'Get' $userAgent = "[Microsoft.PowerShell.Commands.PSUserAgent]::$agentName" $command = "Invoke-RestMethod -Uri $uri -UserAgent ($userAgent) -TimeoutSec 5" @@ -1476,17 +1474,17 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { $result = ExecuteWebCommand -command $command # Validate response - $result.Output.headers.Host | Should Match "httpbin.org" + $result.Output.headers.Host | Should Be $uri.Authority $result.Output.headers.'User-Agent' | Should Match $expectedAgent } } It "Validate Invoke-RestMethod -OutFile" { - $uri = "http://httpbin.org/get" + $uri = Get-WebListenerUrl -Test 'Get' $result = ExecuteRequestWithOutFile -cmdletName "Invoke-RestMethod" -uri $uri $jsonContent = $result.Output | ConvertFrom-Json - $jsonContent.headers.Host | Should Match "httpbin.org" + $jsonContent.headers.Host | Should Be $uri.Authority $jsonContent.headers.'User-Agent' | Should Match "WindowsPowerShell" } @@ -1521,7 +1519,8 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { It "Validate Invoke-RestMethod body is converted to query params for CustomMethod GET" { - $command = "Invoke-RestMethod -Uri 'http://httpbin.org/get' -CustomMethod GET -Body @{'testparam'='testvalue'}" + $uri = Get-WebListenerUrl -Test 'Get' + $command = "Invoke-RestMethod -Uri '$uri' -CustomMethod GET -Body @{'testparam'='testvalue'}" $result = ExecuteWebCommand -command $command $result.Output.args.testparam | Should Be "testvalue" } diff --git a/test/tools/Modules/WebListener/WebListener.psm1 b/test/tools/Modules/WebListener/WebListener.psm1 index a9071547a41..09f8b6836a7 100644 --- a/test/tools/Modules/WebListener/WebListener.psm1 +++ b/test/tools/Modules/WebListener/WebListener.psm1 @@ -112,6 +112,12 @@ function Get-WebListenerUrl { [OutputType([Uri])] param ( [switch]$Https, + [ValidateSet( + 'Cert', + 'Get', + 'Home', + '/' + )] [String]$Test ) process { diff --git a/test/tools/WebListener/Constants.cs b/test/tools/WebListener/Constants.cs new file mode 100644 index 00000000000..1eae8c284f2 --- /dev/null +++ b/test/tools/WebListener/Constants.cs @@ -0,0 +1,9 @@ +using System; + +namespace mvc.Controllers +{ + internal static class Constants + { + public const string HeaderSeparator = ", "; + } +} diff --git a/test/tools/WebListener/Controllers/GetController.cs b/test/tools/WebListener/Controllers/GetController.cs new file mode 100644 index 00000000000..ccd082ef13f --- /dev/null +++ b/test/tools/WebListener/Controllers/GetController.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Http.Extensions; +using mvc.Models; + +namespace mvc.Controllers +{ + public class GetController : Controller + { + public JsonResult Index() + { + Hashtable args = new Hashtable(); + foreach (var key in Request.Query.Keys) + { + args.Add(key, String.Join(Constants.HeaderSeparator, Request.Query[key])); + } + Hashtable headers = new Hashtable(); + foreach (var key in Request.Headers.Keys) + { + headers.Add(key, String.Join(Constants.HeaderSeparator, Request.Headers[key])); + } + Hashtable output = new Hashtable + { + {"args" , args}, + {"headers", headers}, + {"origin" , Request.HttpContext.Connection.RemoteIpAddress.ToString()}, + {"url" , UriHelper.GetDisplayUrl(Request)} + }; + return Json(output); + } + 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 e931b2c3a5b..9db5f8dc077 100644 --- a/test/tools/WebListener/README.md +++ b/test/tools/WebListener/README.md @@ -1,6 +1,6 @@ # WebListener App -ASP.NET Core 2.0 app for testing HTTP and HTTPS Requests. The default page will return a list of available tests. +ASP.NET Core 2.0 app for testing HTTP and HTTPS Requests. # Run with `dotnet` @@ -30,6 +30,10 @@ $Listener = Start-WebListener -HttpPort 8083 -HttpsPort 8084 # Tests +## / or /Home/ + +Returns a static HTML page containing links and descriptions of the available tests in WebListener. This can be used as a default or general test where no specific test functionality or return data is required. + ## /Cert/ Returns a JSON object containing the details of the Client Certificate if one is provided in the request. @@ -54,3 +58,27 @@ Response when certificate is not provided in request: "Status": "FAILED" } ``` + + +## /Get/ + +Returns a JSON object containing the Request URL, Request Headers, GET Query Fields and Values, and Origin IP. This emulates the functionality of [HttpBin's get test](https://httpbin.org/get). + +```powershell +Invoke-WebRequest -Uri 'http://localhost:8083/Get/' -Body @{TestField = 'TestValue'} +``` + +```json +{ + "url": "http://localhost:8083/Get/?TestField=TestValue", + "args": { + "TestField": "TestValue" + }, + "headers": { + "Connection": "Keep-Alive", + "User-Agent": "Mozilla/5.0 (Windows NT; Microsoft Windows 10.0.15063 ; en-US) WindowsPowerShell/6.0.0", + "Host": "localhost:8083" + }, + "origin": "127.0.0.1" +} +``` diff --git a/test/tools/WebListener/Views/Home/Index.cshtml b/test/tools/WebListener/Views/Home/Index.cshtml index 0007857f793..667c2512761 100644 --- a/test/tools/WebListener/Views/Home/Index.cshtml +++ b/test/tools/WebListener/Views/Home/Index.cshtml @@ -2,4 +2,5 @@