Skip to content

Commit

Permalink
URL: avoid double slash at the start of the path (#5187)
Browse files Browse the repository at this point in the history
* URL: avoid double slash at the start of the path

* Remove unnecessary change

* More test coverage

* Changelog

---------

Co-authored-by: Anton Kolesnyk <[email protected]>
  • Loading branch information
antkmsft and antkmsft authored Nov 28, 2023
1 parent a318d37 commit ee4be19
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
2 changes: 2 additions & 0 deletions sdk/core/azure-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion sdk/core/azure-core/src/http/url.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
45 changes: 45 additions & 0 deletions sdk/core/azure-core/test/ut/url_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit ee4be19

Please sign in to comment.