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) 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..de3d4d833d 100644 --- a/sdk/core/azure-core/test/ut/url_test.cpp +++ b/sdk/core/azure-core/test/ut/url_test.cpp @@ -333,4 +333,49 @@ 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/"); + + EXPECT_EQ(u0.GetAbsoluteUrl(), "https://www.microsoft.com"); + EXPECT_EQ(u1.GetAbsoluteUrl(), "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