From bd7f2d4f06d4fa0e2ff0cebf2a230fa2bed0b896 Mon Sep 17 00:00:00 2001 From: markekraus Date: Fri, 1 Sep 2017 08:53:41 -0500 Subject: [PATCH 1/8] Add "Get" test to WebListener --- .../WebListener/Controllers/GetController.cs | 41 +++++++++++++++++++ test/tools/WebListener/README.md | 24 +++++++++++ .../tools/WebListener/Views/Home/Index.cshtml | 1 + 3 files changed, 66 insertions(+) create mode 100644 test/tools/WebListener/Controllers/GetController.cs diff --git a/test/tools/WebListener/Controllers/GetController.cs b/test/tools/WebListener/Controllers/GetController.cs new file mode 100644 index 00000000000..9e53f3f45ef --- /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(" ,", Request.Query[key])); + } + Hashtable headers = new Hashtable(); + foreach (var key in Request.Headers.Keys) + { + headers.Add(key, String.Join(" ,", 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..e2ecd5cf4aa 100644 --- a/test/tools/WebListener/README.md +++ b/test/tools/WebListener/README.md @@ -54,3 +54,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 @@ From fbec6f89829d5e8237236e4d6fe51a74046a3ed7 Mon Sep 17 00:00:00 2001 From: markekraus Date: Fri, 1 Sep 2017 13:52:27 -0500 Subject: [PATCH 2/8] [Feature] Move HttpBin/Get Tests to WebListener --- .../WebCmdlets.Tests.ps1 | 49 +++++++++---------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index d362f0ca2e8..984ee3bab45 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 Match "localhost" $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 Match "localhost" $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 Match "localhost" $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 Match "localhost" $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 Match "localhost" $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" } From 2118551a29cf39b70eaf87baf94d04faf8906231 Mon Sep 17 00:00:00 2001 From: markekraus Date: Fri, 1 Sep 2017 15:46:57 -0500 Subject: [PATCH 3/8] [Feature] update .spelling --- .spelling | 1 + 1 file changed, 1 insertion(+) 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 From 5c9ab0bac4e5319e8d43d274f6f71916c95f2275 Mon Sep 17 00:00:00 2001 From: markekraus Date: Mon, 4 Sep 2017 06:28:39 -0500 Subject: [PATCH 4/8] [Feature] Address PR Feedback --- test/tools/Modules/WebListener/WebListener.psm1 | 5 +++++ test/tools/WebListener/Constants.cs | 9 +++++++++ test/tools/WebListener/Controllers/GetController.cs | 4 ++-- 3 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 test/tools/WebListener/Constants.cs diff --git a/test/tools/Modules/WebListener/WebListener.psm1 b/test/tools/Modules/WebListener/WebListener.psm1 index a9071547a41..42947f2a224 100644 --- a/test/tools/Modules/WebListener/WebListener.psm1 +++ b/test/tools/Modules/WebListener/WebListener.psm1 @@ -112,6 +112,11 @@ function Get-WebListenerUrl { [OutputType([Uri])] param ( [switch]$Https, + [ValidateSet( + 'Cert', + 'Get', + '/' + )] [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 index 9e53f3f45ef..ccd082ef13f 100644 --- a/test/tools/WebListener/Controllers/GetController.cs +++ b/test/tools/WebListener/Controllers/GetController.cs @@ -17,12 +17,12 @@ public JsonResult Index() Hashtable args = new Hashtable(); foreach (var key in Request.Query.Keys) { - args.Add(key, String.Join(" ,", Request.Query[key])); + 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(" ,", Request.Headers[key])); + headers.Add(key, String.Join(Constants.HeaderSeparator, Request.Headers[key])); } Hashtable output = new Hashtable { From 4f7f7960878b2e663d4860b95869eb8f340a9588 Mon Sep 17 00:00:00 2001 From: markekraus Date: Mon, 4 Sep 2017 08:31:04 -0500 Subject: [PATCH 5/8] [Feature] Add and document Home & / --- test/tools/Modules/WebListener/WebListener.psm1 | 1 + test/tools/WebListener/README.md | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/test/tools/Modules/WebListener/WebListener.psm1 b/test/tools/Modules/WebListener/WebListener.psm1 index 42947f2a224..09f8b6836a7 100644 --- a/test/tools/Modules/WebListener/WebListener.psm1 +++ b/test/tools/Modules/WebListener/WebListener.psm1 @@ -115,6 +115,7 @@ function Get-WebListenerUrl { [ValidateSet( 'Cert', 'Get', + 'Home', '/' )] [String]$Test diff --git a/test/tools/WebListener/README.md b/test/tools/WebListener/README.md index e2ecd5cf4aa..b16dcbecf23 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. + ## /Cert/ Returns a JSON object containing the details of the Client Certificate if one is provided in the request. From 2aa18fd78016f7b5f287ce59060c2e1e8deb1625 Mon Sep 17 00:00:00 2001 From: markekraus Date: Mon, 4 Sep 2017 08:54:09 -0500 Subject: [PATCH 6/8] Readme Update --- test/tools/WebListener/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/tools/WebListener/README.md b/test/tools/WebListener/README.md index b16dcbecf23..9db5f8dc077 100644 --- a/test/tools/WebListener/README.md +++ b/test/tools/WebListener/README.md @@ -32,7 +32,7 @@ $Listener = Start-WebListener -HttpPort 8083 -HttpsPort 8084 ## / or /Home/ -Returns a static HTML page containing links and descriptions of the available tests in WebListener. +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/ From 02265ca28076200733ac48813886791e87c8cf12 Mon Sep 17 00:00:00 2001 From: markekraus Date: Wed, 6 Sep 2017 03:38:07 -0500 Subject: [PATCH 7/8] [Feature] Should Match -> Should Be Rerun CI --- .../Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index 984ee3bab45..96b80517b10 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -669,7 +669,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { # Validate response content $jsonContent = $result.Output.Content | ConvertFrom-Json - $jsonContent.headers.Host | Should Match "localhost" + $jsonContent.headers.Host | Should Be $uri.Authority $jsonContent.headers.'User-Agent' | Should Match $expectedAgent } } @@ -679,7 +679,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { $uri = Get-WebListenerUrl -Test 'Get' $result = ExecuteRequestWithOutFile -cmdletName "Invoke-WebRequest" -uri $uri $jsonContent = $result.Output | ConvertFrom-Json - $jsonContent.headers.Host | Should Match "localhost" + $jsonContent.headers.Host | Should Be $uri.Authority $jsonContent.headers.'User-Agent' | Should Match "WindowsPowerShell" } @@ -1269,7 +1269,7 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { $result = ExecuteWebCommand -command $command # Validate response - $result.Output.headers.Host | Should Match "localhost" + $result.Output.headers.Host | Should Be $uri.Authority $result.Output.headers.'User-Agent' | Should Match "WindowsPowerShell" $result.Output.Headers.Connection | Should Be "Close" } @@ -1474,7 +1474,7 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { $result = ExecuteWebCommand -command $command # Validate response - $result.Output.headers.Host | Should Match "localhost" + $result.Output.headers.Host | Should Be $uri.Authority $result.Output.headers.'User-Agent' | Should Match $expectedAgent } } @@ -1484,7 +1484,7 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { $uri = Get-WebListenerUrl -Test 'Get' $result = ExecuteRequestWithOutFile -cmdletName "Invoke-RestMethod" -uri $uri $jsonContent = $result.Output | ConvertFrom-Json - $jsonContent.headers.Host | Should Match "localhost" + $jsonContent.headers.Host | Should Be $uri.Authority $jsonContent.headers.'User-Agent' | Should Match "WindowsPowerShell" } From a35adde880b69983641b9e5159cc0b35679e750e Mon Sep 17 00:00:00 2001 From: markekraus Date: Thu, 7 Sep 2017 03:28:22 -0500 Subject: [PATCH 8/8] [Feature] Rebase and Rerun CI