From a0bbea6b9652224f5c5e786d8b5e03a6d3c97e14 Mon Sep 17 00:00:00 2001 From: MihaZupan Date: Mon, 3 May 2021 21:05:16 +0200 Subject: [PATCH] Fix UriHelper length calculation --- src/Http/Http.Extensions/src/UriHelper.cs | 11 +++++++---- src/Http/Http.Extensions/test/UriHelperTests.cs | 1 + 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Http/Http.Extensions/src/UriHelper.cs b/src/Http/Http.Extensions/src/UriHelper.cs index dbcd8dd819f3..1f4b68b482f0 100644 --- a/src/Http/Http.Extensions/src/UriHelper.cs +++ b/src/Http/Http.Extensions/src/UriHelper.cs @@ -77,12 +77,15 @@ public static string BuildAbsolute( queryText.Length + fragmentText.Length; - if (string.IsNullOrEmpty(pathBaseText) && string.IsNullOrEmpty(pathText)) + if (string.IsNullOrEmpty(pathText)) { - pathText = "/"; - length++; + if (string.IsNullOrEmpty(pathBaseText)) + { + pathText = "/"; + length++; + } } - else if (pathBaseText.Length > 0 && pathBaseText[^1] == '/') + else if (pathBaseText.EndsWith('/')) { // If the path string has a trailing slash and the other string has a leading slash, we need // to trim one of them. diff --git a/src/Http/Http.Extensions/test/UriHelperTests.cs b/src/Http/Http.Extensions/test/UriHelperTests.cs index 368feb0fdec2..018123f89f25 100644 --- a/src/Http/Http.Extensions/test/UriHelperTests.cs +++ b/src/Http/Http.Extensions/test/UriHelperTests.cs @@ -51,6 +51,7 @@ public void EncodeFullUrl() [InlineData("http", "example.com", "", "/foo", "?bar=1", "#col=2", "http://example.com/foo?bar=1#col=2")] [InlineData("http", "example.com", "/base", "/foo", "?bar=1", "#col=2", "http://example.com/base/foo?bar=1#col=2")] [InlineData("http", "example.com", "/base/", "/foo", "?bar=1", "#col=2", "http://example.com/base/foo?bar=1#col=2")] + [InlineData("http", "example.com", "/base/", "", "?bar=1", "#col=2", "http://example.com/base/?bar=1#col=2")] [InlineData("http", "example.com", "", "", "?bar=1", "#col=2", "http://example.com/?bar=1#col=2")] [InlineData("http", "example.com", "", "", "", "#frag?stillfrag/stillfrag", "http://example.com/#frag?stillfrag/stillfrag")] [InlineData("http", "example.com", "", "", "?q/stillq", "#frag?stillfrag/stillfrag", "http://example.com/?q/stillq#frag?stillfrag/stillfrag")]