Build the relative URI for links from the response in Invoke-WebRequest (#19092)

This commit is contained in:
CarloToso
2023-02-10 21:50:01 +05:00
committed by GitHub
parent ed68c861d4
commit 2f43013df3
2 changed files with 17 additions and 8 deletions
@@ -609,7 +609,7 @@ namespace Microsoft.PowerShell.Commands
if (_parseRelLink || _followRelLink)
{
ParseLinkHeader(response, uri);
ParseLinkHeader(response);
}
ProcessResponse(response);
@@ -1596,8 +1596,9 @@ namespace Microsoft.PowerShell.Commands
SetRequestContent(request, body);
}
internal void ParseLinkHeader(HttpResponseMessage response, System.Uri requestUri)
internal void ParseLinkHeader(HttpResponseMessage response)
{
Uri requestUri = response.RequestMessage.RequestUri;
if (_relationLink is null)
{
// Must ignore the case of relation links. See RFC 8288 (https://tools.ietf.org/html/rfc8288)
@@ -1610,12 +1611,12 @@ namespace Microsoft.PowerShell.Commands
// We only support the URL in angle brackets and `rel`, other attributes are ignored
// user can still parse it themselves via the Headers property
const string pattern = "<(?<url>.*?)>;\\s*rel=(?<quoted>\")?(?<rel>(?(quoted).*?|[^,;]*))(?(quoted)\")";
const string Pattern = "<(?<url>.*?)>;\\s*rel=(?<quoted>\")?(?<rel>(?(quoted).*?|[^,;]*))(?(quoted)\")";
if (response.Headers.TryGetValues("Link", out IEnumerable<string> links))
{
foreach (string linkHeader in links)
{
MatchCollection matchCollection = Regex.Matches(linkHeader, pattern);
MatchCollection matchCollection = Regex.Matches(linkHeader, Pattern);
foreach (Match match in matchCollection)
{
if (match.Success)
@@ -834,8 +834,12 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" {
$result.Output.RelationLink.Count | Should -Be 0
}
It "Validate Invoke-WebRequest returns valid RelationLink property with absolute uris if Link Header is present" {
$uri = Get-WebListenerUrl -Test 'Link' -Query @{maxlinks = 5; linknumber = 2}
It "Validate Invoke-WebRequest returns valid RelationLink property with absolute uris if Link Header is present <name>" -TestCases @(
$originalUri = Get-WebListenerUrl -Test 'Link' -Query @{maxlinks = 5; linknumber = 2}
@{name = '(URI with scheme)'; uri = $originalUri}
@{name = '(URI without scheme)'; uri = $originalUri.OriginalString.Split("//")[1]}
) {
param($uri)
$command = "Invoke-WebRequest -Uri '$uri'"
$result = ExecuteWebCommand -command $command
@@ -2547,9 +2551,13 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" {
$result.Output | Should -BeExactly "foo"
}
It "Validate Invoke-RestMethod -FollowRelLink correctly follows all the available relation links" {
It "Validate Invoke-RestMethod -FollowRelLink correctly follows all the available relation links <name>" -TestCases @(
$maxLinks = 5
$uri = Get-WebListenerUrl -Test 'Link' -Query @{maxlinks = $maxLinks}
$originalUri = Get-WebListenerUrl -Test 'Link' -Query @{maxlinks = $maxLinks}
@{name = '(URI with scheme)'; uri = $originalUri}
@{name = '(URI without scheme)'; uri = $originalUri.OriginalString.Split("//")[1]}
) {
param($uri)
$command = "Invoke-RestMethod -Uri '$uri' -FollowRelLink"
$result = ExecuteWebCommand -command $command