URL: avoid double slash at the start of the path (#5187)

* URL: avoid double slash at the start of the path

* Remove unnecessary change

* More test coverage

* Changelog

---------

Co-authored-by: Anton Kolesnyk <antkmsft@users.noreply.github.com>
This commit is contained in:
Anton Kolesnyk 2023-11-27 23:03:27 -08:00 committed by GitHub
parent a318d3756f
commit ee4be19b3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 1 deletions

View File

@ -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)

View File

@ -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;

View File

@ -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