Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ protected override void ProcessRecord()

if (_parseRelLink || _followRelLink)
{
ParseLinkHeader(response, uri);
ParseLinkHeader(response);
}

ProcessResponse(response);
Expand Down Expand Up @@ -1596,8 +1596,9 @@ internal void SetRequestContent(HttpRequestMessage request, IDictionary content)
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)
Expand All @@ -1610,12 +1611,12 @@ internal void ParseLinkHeader(HttpResponseMessage response, System.Uri requestUr

// 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down