diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index a4a924079fb..3a3c4386293 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -609,7 +609,7 @@ protected override void ProcessRecord() if (_parseRelLink || _followRelLink) { - ParseLinkHeader(response, uri); + ParseLinkHeader(response); } ProcessResponse(response); @@ -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) @@ -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 = "<(?.*?)>;\\s*rel=(?\")?(?(?(quoted).*?|[^,;]*))(?(quoted)\")"; + const string Pattern = "<(?.*?)>;\\s*rel=(?\")?(?(?(quoted).*?|[^,;]*))(?(quoted)\")"; if (response.Headers.TryGetValues("Link", out IEnumerable 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) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index 7ff5867130e..2c3c3fb671a 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -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 " -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 " -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