-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Replace HttpListener Link Header Tests with WebListener #5806
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -712,33 +712,31 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { | |
| } | ||
|
|
||
| It "Validate Invoke-WebRequest returns valid RelationLink property with absolute uris if Link Header is present" { | ||
| $command = "Invoke-WebRequest -Uri 'http://localhost:8080/PowerShell?test=linkheader&maxlinks=5'" | ||
| $uri = Get-WebListenerUrl -Test 'Link' -Query @{maxlinks = 5; linknumber = 2} | ||
| $command = "Invoke-WebRequest -Uri '$uri'" | ||
| $result = ExecuteWebCommand -command $command | ||
| $result.Output.RelationLink.Count | Should BeExactly 2 | ||
| $result.Output.RelationLink["next"] | Should BeExactly "http://localhost:8080/PowerShell?test=linkheader&maxlinks=5&linknumber=2" | ||
| $result.Output.RelationLink["last"] | Should BeExactly "http://localhost:8080/PowerShell?test=linkheader&maxlinks=5&linknumber=5" | ||
|
|
||
| $result.Output.RelationLink.Count | Should BeExactly 5 | ||
| $baseUri = Get-WebListenerUrl -Test 'Link' | ||
| $result.Output.RelationLink["next"] | Should BeExactly "${baseUri}?maxlinks=5&linknumber=3&type=default" | ||
| $result.Output.RelationLink["last"] | Should BeExactly "${baseUri}?maxlinks=5&linknumber=5&type=default" | ||
| $result.Output.RelationLink["prev"] | Should BeExactly "${baseUri}?maxlinks=5&linknumber=1&type=default" | ||
| $result.Output.RelationLink["first"] | Should BeExactly "${baseUri}?maxlinks=5&linknumber=1&type=default" | ||
| $result.Output.RelationLink["self"] | Should BeExactly "${baseUri}?maxlinks=5&linknumber=2&type=default" | ||
| } | ||
|
|
||
| # Test pending support for multiple header capable server on Linux/macOS see issue #4639 | ||
| It "Validate Invoke-WebRequest returns valid RelationLink property with absolute uris if Multiple Link Headers are present" -Pending:$(!$IsWindows) { | ||
| $Query = @{ | ||
| body = "ok" | ||
| contenttype = 'text/plain' | ||
| headers = @{ | ||
| Link = @( | ||
| '<http://localhost:8080/PowerShell?test=linkheader&maxlinks=5&linknumber=1>; rel="self"' | ||
| '<http://localhost:8080/PowerShell?test=linkheader&maxlinks=5&linknumber=2>; rel="next"' | ||
| '<http://localhost:8080/PowerShell?test=linkheader&maxlinks=5&linknumber=5>; rel="last"' | ||
| ) | ||
| } | ConvertTo-Json -Compress | ||
| } | ||
| $Uri = Get-WebListenerUrl -Test 'Response' -Query $Query | ||
| It "Validate Invoke-WebRequest returns valid RelationLink property with absolute uris if Multiple Link Headers are present" { | ||
| $uri = Get-WebListenerUrl -Test 'Link' -Query @{maxlinks = 5; linknumber = 2; type = 'multiple'} | ||
| $command = "Invoke-WebRequest -Uri '$uri'" | ||
| $result = ExecuteWebCommand -command $command | ||
| $result.Output.RelationLink.Count | Should BeExactly 3 | ||
| $result.Output.RelationLink["self"] | Should BeExactly "http://localhost:8080/PowerShell?test=linkheader&maxlinks=5&linknumber=1" | ||
| $result.Output.RelationLink["next"] | Should BeExactly "http://localhost:8080/PowerShell?test=linkheader&maxlinks=5&linknumber=2" | ||
| $result.Output.RelationLink["last"] | Should BeExactly "http://localhost:8080/PowerShell?test=linkheader&maxlinks=5&linknumber=5" | ||
|
|
||
| $result.Output.RelationLink.Count | Should BeExactly 5 | ||
| $baseUri = Get-WebListenerUrl -Test 'Link' | ||
| $result.Output.RelationLink["next"] | Should BeExactly "${baseUri}?maxlinks=5&linknumber=3&type=multiple" | ||
| $result.Output.RelationLink["last"] | Should BeExactly "${baseUri}?maxlinks=5&linknumber=5&type=multiple" | ||
| $result.Output.RelationLink["prev"] | Should BeExactly "${baseUri}?maxlinks=5&linknumber=1&type=multiple" | ||
| $result.Output.RelationLink["first"] | Should BeExactly "${baseUri}?maxlinks=5&linknumber=1&type=multiple" | ||
| $result.Output.RelationLink["self"] | Should BeExactly "${baseUri}?maxlinks=5&linknumber=2&type=multiple" | ||
| } | ||
|
|
||
| It "Validate Invoke-WebRequest quietly ignores invalid Link Headers in RelationLink property: <type>" -TestCases @( | ||
|
|
@@ -747,10 +745,15 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { | |
| @{ type = "noRel" } | ||
| ) { | ||
| param($type) | ||
| $command = "Invoke-WebRequest -Uri 'http://localhost:8080/PowerShell?test=linkheader&type=$type'" | ||
| $uri = Get-WebListenerUrl -Test 'Link' -Query @{type = $type} | ||
| $command = "Invoke-WebRequest -Uri '$uri'" | ||
| $result = ExecuteWebCommand -command $command | ||
| $result.Output.RelationLink.Count | Should BeExactly 1 | ||
| $result.Output.RelationLink["last"] | Should BeExactly "http://localhost:8080/PowerShell?test=linkheader&maxlinks=3&linknumber=3" | ||
|
|
||
| $result.Output.RelationLink.Count | Should BeExactly 3 | ||
| $baseUri = Get-WebListenerUrl -Test 'Link' | ||
| $result.Output.RelationLink["last"] | Should BeExactly "${baseUri}?maxlinks=3&linknumber=3&type=${type}" | ||
| $result.Output.RelationLink["first"] | Should BeExactly "${baseUri}?maxlinks=3&linknumber=1&type=${type}" | ||
| $result.Output.RelationLink["self"] | Should BeExactly "${baseUri}?maxlinks=3&linknumber=1&type=${type}" | ||
| } | ||
|
|
||
| #region Redirect tests | ||
|
|
@@ -1026,9 +1029,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { | |
| $result.Output.RawContent | Should Match ([regex]::Escape('Content-Length: 2')) | ||
| } | ||
|
|
||
| # Test pending due to limitation on Linux/macOS | ||
| # https://github.com/PowerShell/PowerShell/pull/4640 | ||
| It "Verifies Invoke-WebRequest Supports Multiple response headers with same name" -Pending:$(!$IsWindows) { | ||
| It "Verifies Invoke-WebRequest Supports Multiple response headers with same name" { | ||
| $query = @{ | ||
| contenttype = 'text/plain' | ||
| body = 'OK' | ||
|
|
@@ -1719,23 +1720,23 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { | |
|
|
||
| It "Validate Invoke-RestMethod -FollowRelLink correctly follows all the available relation links" { | ||
| $maxLinks = 5 | ||
|
|
||
| $command = "Invoke-RestMethod -Uri 'http://localhost:8081/PowerShell?test=linkheader&maxlinks=$maxlinks' -FollowRelLink" | ||
| $uri = Get-WebListenerUrl -Test 'Link' -Query @{maxlinks = $maxLinks} | ||
| $command = "Invoke-RestMethod -Uri '$uri' -FollowRelLink" | ||
| $result = ExecuteWebCommand -command $command | ||
|
|
||
| $result.Output.output.Count | Should BeExactly $maxLinks | ||
| 1..$maxLinks | ForEach-Object { $result.Output.output[$_ - 1] | Should BeExactly $_ } | ||
| $result.Output.Count | Should BeExactly $maxLinks | ||
| 1..$maxLinks | ForEach-Object { $result.Output[$_ - 1].linknumber | Should BeExactly $_ } | ||
| } | ||
|
|
||
| It "Validate Invoke-RestMethod -FollowRelLink correctly limits to -MaximumRelLink" { | ||
| $maxLinks = 10 | ||
| $maxLinksToFollow = 6 | ||
|
|
||
| $command = "Invoke-RestMethod -Uri 'http://localhost:8081/PowerShell?test=linkheader&maxlinks=$maxlinks' -FollowRelLink -MaximumFollowRelLink $maxLinksToFollow" | ||
| $uri = Get-WebListenerUrl -Test 'Link' -Query @{maxlinks = $maxLinks} | ||
| $command = "Invoke-RestMethod -Uri '$uri' -FollowRelLink -MaximumFollowRelLink $maxLinksToFollow" | ||
| $result = ExecuteWebCommand -command $command | ||
|
|
||
| $result.Output.output.Count | Should BeExactly $maxLinksToFollow | ||
| 1..$maxLinksToFollow | ForEach-Object { $result.Output.output[$_ - 1] | Should BeExactly $_ } | ||
| $result.Output.Count | Should BeExactly $maxLinksToFollow | ||
| 1..$maxLinksToFollow | ForEach-Object { $result.Output[$_ - 1].linknumber | Should BeExactly $_ } | ||
| } | ||
|
|
||
| It "Validate Invoke-RestMethod quietly ignores invalid Link Headers if -FollowRelLink is specified: <type>" -TestCases @( | ||
|
|
@@ -1744,9 +1745,10 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { | |
| @{ type = "noRel" } | ||
| ) { | ||
| param($type) | ||
| $command = "Invoke-RestMethod -Uri 'http://localhost:8081/PowerShell?test=linkheader&type=$type' -FollowRelLink" | ||
| $uri = Get-WebListenerUrl -Test 'Link' -Query @{type = $type} | ||
| $command = "Invoke-RestMethod -Uri '$uri' -FollowRelLink" | ||
| $result = ExecuteWebCommand -command $command | ||
| $result.Output.output | Should BeExactly 1 | ||
| $result.Output.linknumber | Should BeExactly 1 | ||
|
||
| } | ||
|
|
||
| #region Redirect tests | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -135,6 +135,7 @@ function Get-WebListenerUrl { | |
| 'Encoding', | ||
| 'Get', | ||
| 'Home', | ||
| 'Link', | ||
| 'Multipart', | ||
| 'Patch', | ||
| 'Post', | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| using System; | ||
| using System.Collections; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using System.Linq; | ||
| using System.Text.RegularExpressions; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Microsoft.AspNetCore.Http.Extensions; | ||
| using Microsoft.Extensions.Primitives; | ||
| using mvc.Models; | ||
|
|
||
| namespace mvc.Controllers | ||
| { | ||
| public class LinkController : Controller | ||
| { | ||
| public JsonResult Index() | ||
| { | ||
| if (!Request.Query.TryGetValue("maxlinks", out StringValues maxLinksSV) || !Int32.TryParse(maxLinksSV.FirstOrDefault(), out int maxLinks) || maxLinks < 1) | ||
| { | ||
| maxLinks = 3; | ||
| } | ||
|
|
||
| if (!Request.Query.TryGetValue("linknumber", out StringValues linkNumberSV) || !Int32.TryParse(linkNumberSV.FirstOrDefault(), out int linkNumber) || linkNumber < 1) | ||
| { | ||
| linkNumber = 1; | ||
| } | ||
|
|
||
| string baseUri = Regex.Replace(UriHelper.GetDisplayUrl(Request), "\\?.*", String.Empty); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So simple? 👍
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Something wrong with it?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, I expected a url parsing 😄
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. heh. So far as I can tell, the only way to get the request url is |
||
|
|
||
| string type = Request.Query.TryGetValue("type", out StringValues typeSV) ? typeSV.FirstOrDefault() : "default"; | ||
|
|
||
| var linkList = new List<String>(); | ||
| if (maxLinks > 1 && linkNumber > 1) | ||
| { | ||
| linkList.Add(GetLink(baseUri: baseUri, maxLinks: maxLinks, linkNumber: linkNumber - 1, type: type, rel: "prev")); | ||
| } | ||
| linkList.Add(GetLink(baseUri: baseUri, maxLinks: maxLinks, linkNumber: maxLinks, type: type, rel: "last")); | ||
| linkList.Add(GetLink(baseUri: baseUri, maxLinks: maxLinks, linkNumber: 1, type: type, rel: "first")); | ||
| linkList.Add(GetLink(baseUri: baseUri, maxLinks: maxLinks, linkNumber: linkNumber, type: type, rel: "self")); | ||
|
|
||
| bool sendMultipleHeaders = false; | ||
| bool skipNextLink = false; | ||
| switch (type.ToUpper()) | ||
| { | ||
| case "NOURL": | ||
| linkList.Add(Constants.NoUrlLinkHeader); | ||
| skipNextLink = true; | ||
| break; | ||
| case "MALFORMED": | ||
| linkList.Add(Constants.MalformedUrlLinkHeader); | ||
| skipNextLink = true; | ||
| break; | ||
| case "NOREL": | ||
| linkList.Add(Constants.NoRelLinkHeader); | ||
| skipNextLink = true; | ||
| break; | ||
| case "MULTIPLE": | ||
| sendMultipleHeaders = true; | ||
| break; | ||
| default: | ||
| break; | ||
| } | ||
|
|
||
| if (!skipNextLink && maxLinks > 1 && linkNumber < maxLinks) | ||
| { | ||
| linkList.Add(GetLink(baseUri: baseUri, maxLinks: maxLinks, linkNumber: linkNumber + 1, type: type, rel: "next")); | ||
| } | ||
|
|
||
| StringValues linkHeader; | ||
| if (sendMultipleHeaders) | ||
| { | ||
| linkHeader = linkList.ToArray(); | ||
| } | ||
| else | ||
| { | ||
| linkHeader = String.Join(",", linkList); | ||
| } | ||
| Response.Headers.Add("Link", linkHeader); | ||
|
|
||
| // Generate /Get/ result and append linknumber, maxlinks, and type | ||
| var getController = new GetController(); | ||
| getController.ControllerContext = this.ControllerContext; | ||
| var result = getController.Index(); | ||
| var output = result.Value as Hashtable; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Never null?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It will always return a HashTable unless some uncaught exception prevents it from even getting to this point.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks. Closed. |
||
| output.Add("linknumber", linkNumber); | ||
| output.Add("maxlinks", maxLinks); | ||
| output.Add("type", type.FirstOrDefault()); | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| public IActionResult Error() | ||
| { | ||
| return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); | ||
| } | ||
|
|
||
| private string GetLink(string baseUri, int maxLinks, int linkNumber, string type, string rel) | ||
| { | ||
| return String.Format(Constants.LinkUriTemplate, baseUri, maxLinks, linkNumber, type, rel); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why we use
BeExactlyin the whole file forCount?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it was like that before I didn't change them. Does it really matter though?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer
Should BebecauseBeExactlyis used for strings and my first thought was that hereCountis a method inRelationLinkwhich return a string.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@iSazonov This appears to happen in more place. Lets leave it as is here and I will address them all as part of #5669 which I have updated.