From 56f72907a071dc8942e058b653376f8005ca5e40 Mon Sep 17 00:00:00 2001 From: Anton Kolesnyk Date: Sat, 25 Nov 2023 09:09:32 -0800 Subject: [PATCH 1/4] URL: avoid double slash at the start of the path --- sdk/core/azure-core/src/http/url.cpp | 5 ++- sdk/core/azure-core/test/ut/url_test.cpp | 45 ++++++++++++++++++++++-- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/sdk/core/azure-core/src/http/url.cpp b/sdk/core/azure-core/src/http/url.cpp index a46c013c3b..e9c3228f14 100644 --- a/sdk/core/azure-core/src/http/url.cpp +++ b/sdk/core/azure-core/src/http/url.cpp @@ -219,7 +219,10 @@ std::string Url::GetUrlWithoutQuery(bool relative) const { if (!relative) { - url += "/"; + if (m_encodedPath.empty() || m_encodedPath[0] != '/') + { + url += "/"; + } } url += m_encodedPath; diff --git a/sdk/core/azure-core/test/ut/url_test.cpp b/sdk/core/azure-core/test/ut/url_test.cpp index 64218ecab4..c5a5a8a0e3 100644 --- a/sdk/core/azure-core/test/ut/url_test.cpp +++ b/sdk/core/azure-core/test/ut/url_test.cpp @@ -7,8 +7,7 @@ namespace Azure { namespace Core { namespace Test { - class TestURL : public ::testing::Test { - }; + class TestURL : public ::testing::Test {}; }}} // namespace Azure::Core::Test @@ -333,4 +332,46 @@ namespace Azure { namespace Core { namespace Test { EXPECT_NE(params.find("param"), params.end()); EXPECT_EQ(params["param"], "value"); } + + TEST(URL, LeadingSlashInPath) + { + Core::Url const u0("https://www.microsoft.com"); + Core::Url const u1("https://www.microsoft.com/"); + + { + auto url0 = u0; + auto url1 = u1; + url0.AppendPath("path"); + url1.AppendPath("path"); + EXPECT_EQ(url0.GetAbsoluteUrl(), "https://www.microsoft.com/path"); + EXPECT_EQ(url1.GetAbsoluteUrl(), "https://www.microsoft.com/path"); + } + + { + auto url0 = u0; + auto url1 = u1; + url0.AppendPath("/path"); + url1.AppendPath("/path"); + EXPECT_EQ(url0.GetAbsoluteUrl(), "https://www.microsoft.com/path"); + EXPECT_EQ(url1.GetAbsoluteUrl(), "https://www.microsoft.com/path"); + } + + { + auto url0 = u0; + auto url1 = u1; + url0.SetPath("path"); + url1.SetPath("path"); + EXPECT_EQ(url0.GetAbsoluteUrl(), "https://www.microsoft.com/path"); + EXPECT_EQ(url1.GetAbsoluteUrl(), "https://www.microsoft.com/path"); + } + + { + auto url0 = u0; + auto url1 = u1; + url0.SetPath("/path"); + url1.SetPath("/path"); + EXPECT_EQ(url0.GetAbsoluteUrl(), "https://www.microsoft.com/path"); + EXPECT_EQ(url1.GetAbsoluteUrl(), "https://www.microsoft.com/path"); + } + } }}} // namespace Azure::Core::Test From 2358b8fa7fcd2659b14d90fa2d42db4bd4cf7ab8 Mon Sep 17 00:00:00 2001 From: Anton Kolesnyk Date: Sat, 25 Nov 2023 09:11:45 -0800 Subject: [PATCH 2/4] Remove unnecessary change --- sdk/core/azure-core/test/ut/url_test.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sdk/core/azure-core/test/ut/url_test.cpp b/sdk/core/azure-core/test/ut/url_test.cpp index c5a5a8a0e3..52ce73a557 100644 --- a/sdk/core/azure-core/test/ut/url_test.cpp +++ b/sdk/core/azure-core/test/ut/url_test.cpp @@ -7,7 +7,8 @@ namespace Azure { namespace Core { namespace Test { - class TestURL : public ::testing::Test {}; + class TestURL : public ::testing::Test { + }; }}} // namespace Azure::Core::Test From 766fec2467f0144627c6e44d9f06a3f5d3fe4bf3 Mon Sep 17 00:00:00 2001 From: Anton Kolesnyk Date: Sat, 25 Nov 2023 20:55:22 -0800 Subject: [PATCH 3/4] More test coverage --- sdk/core/azure-core/test/ut/url_test.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sdk/core/azure-core/test/ut/url_test.cpp b/sdk/core/azure-core/test/ut/url_test.cpp index 52ce73a557..de3d4d833d 100644 --- a/sdk/core/azure-core/test/ut/url_test.cpp +++ b/sdk/core/azure-core/test/ut/url_test.cpp @@ -339,6 +339,9 @@ namespace Azure { namespace Core { namespace Test { Core::Url const u0("https://www.microsoft.com"); Core::Url const u1("https://www.microsoft.com/"); + EXPECT_EQ(u0.GetAbsoluteUrl(), "https://www.microsoft.com"); + EXPECT_EQ(u1.GetAbsoluteUrl(), "https://www.microsoft.com"); + { auto url0 = u0; auto url1 = u1; From 0ed19576eed2b0ce9c87d411ccee691489357268 Mon Sep 17 00:00:00 2001 From: Anton Kolesnyk Date: Sat, 25 Nov 2023 22:49:08 -0800 Subject: [PATCH 4/4] Changelog --- sdk/core/azure-core/CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sdk/core/azure-core/CHANGELOG.md b/sdk/core/azure-core/CHANGELOG.md index 373ea4ab64..1fb18874e5 100644 --- a/sdk/core/azure-core/CHANGELOG.md +++ b/sdk/core/azure-core/CHANGELOG.md @@ -8,6 +8,8 @@ ### Bugs Fixed +- [[#5130]](https://github.com/Azure/azure-sdk-for-cpp/issues/5130) `Url::AppendPath()` and `Url::SetPath()` may end up with a double slash at the beginning of a path. + ### Other Changes ## 1.11.0-beta.2 (2023-11-02)