diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebRequestPSCmdlet.CoreClr.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebRequestPSCmdlet.CoreClr.cs index 7dbe3146166..f5322ddc129 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebRequestPSCmdlet.CoreClr.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebRequestPSCmdlet.CoreClr.cs @@ -847,17 +847,20 @@ internal void ParseLinkHeader(HttpResponseMessage response, System.Uri requestUr IEnumerable links; if (response.Headers.TryGetValues("Link", out links)) { - foreach(string link in links.FirstOrDefault().Split(",")) + foreach (string linkHeader in links) { - Match match = Regex.Match(link, pattern); - if (match.Success) + foreach (string link in linkHeader.Split(",")) { - string url = match.Groups["url"].Value; - string rel = match.Groups["rel"].Value; - if (url != String.Empty && rel != String.Empty && !_relationLink.ContainsKey(rel)) + Match match = Regex.Match(link, pattern); + if (match.Success) { - Uri absoluteUri = new Uri(requestUri, url); - _relationLink.Add(rel, absoluteUri.AbsoluteUri.ToString()); + string url = match.Groups["url"].Value; + string rel = match.Groups["rel"].Value; + if (url != String.Empty && rel != String.Empty && !_relationLink.ContainsKey(rel)) + { + Uri absoluteUri = new Uri(requestUri, url); + _relationLink.Add(rel, absoluteUri.AbsoluteUri.ToString()); + } } } } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index 43e072bd25d..8dc1b1aaaef 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -803,6 +803,24 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { $result.Output.RelationLink["last"] | Should BeExactly "http://localhost:8080/PowerShell?test=linkheader&maxlinks=5&linknumber=5" } + # 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){ + $headers = @{ + Link = + '; rel="self"', + '; rel="next"', + '; rel="last"' + } | ConvertTo-Json -Compress + $headers = [uri]::EscapeDataString($headers) + $uri = "http://localhost:8080/PowerShell?test=response&contenttype=text/plain&output=OK&headers=$headers" + $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" + } + It "Validate Invoke-WebRequest quietly ignores invalid Link Headers in RelationLink property: " -TestCases @( @{ type = "noUrl" } @{ type = "malformed" }