From 57df0c295faa2731431c5d0219e6d74cd6d5c10f Mon Sep 17 00:00:00 2001 From: Mark Kraus Date: Sat, 28 Oct 2017 09:33:43 -0500 Subject: [PATCH 1/2] [Feature] Add multiple link header support --- .../CoreCLR/WebRequestPSCmdlet.CoreClr.cs | 19 +++++++++++-------- .../WebCmdlets.Tests.ps1 | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+), 8 deletions(-) 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..7b7d5c4e6ef 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 ses 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" } From 5c052d94c89c8c5de3a7853caf31a409cc3b31c4 Mon Sep 17 00:00:00 2001 From: Mark Kraus Date: Mon, 30 Oct 2017 17:00:44 -0500 Subject: [PATCH 2/2] fix typo --- .../Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index 7b7d5c4e6ef..8dc1b1aaaef 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -803,7 +803,7 @@ 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 ses issue #4639 + # 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 =