utility: Append Url Pieces to existing Url
Add helper function to append pieces to existing url to allow more
flexible control over the url. This allows us to avoid have each
resource append the pieces outside of the utility functions and help
maintain all url modifications in a central place for easy management.
Tested: Does not affect Redfish Tree. Unit Test passed.
Change-Id: I751f3c120cbadb465915b12aa253edd53ef32123
Signed-off-by: Willy Tu <wltu@google.com>
diff --git a/http/utility.hpp b/http/utility.hpp
index 7857016..b1b38da 100644
--- a/http/utility.hpp
+++ b/http/utility.hpp
@@ -562,15 +562,23 @@
namespace details
{
inline boost::urls::url
- urlFromPiecesDetail(const std::initializer_list<std::string_view> args)
+ appendUrlPieces(boost::urls::url& url,
+ const std::initializer_list<std::string_view> args)
{
- boost::urls::url url("/");
for (const std::string_view& arg : args)
{
url.segments().push_back(arg);
}
return url;
}
+
+inline boost::urls::url
+ urlFromPiecesDetail(const std::initializer_list<std::string_view> args)
+{
+ boost::urls::url url("/");
+ appendUrlPieces(url, args);
+ return url;
+}
} // namespace details
class OrMorePaths
@@ -582,6 +590,12 @@
return details::urlFromPiecesDetail({args...});
}
+template <typename... AV>
+inline void appendUrlPieces(boost::urls::url& url, const AV... args)
+{
+ details::appendUrlPieces(url, {args...});
+}
+
namespace details
{
diff --git a/test/http/utility_test.cpp b/test/http/utility_test.cpp
index 8eef93f..58a2865 100644
--- a/test/http/utility_test.cpp
+++ b/test/http/utility_test.cpp
@@ -222,5 +222,18 @@
EXPECT_EQ(nlohmann::json(boost::urls::url_view(urlString)), urlString);
}
+TEST(AppendUrlFromPieces, PiecesAreAppendedViaDelimiters)
+{
+ boost::urls::url url = urlFromPieces("redfish", "v1", "foo");
+ EXPECT_EQ(std::string_view(url.data(), url.size()), "/redfish/v1/foo");
+
+ appendUrlPieces(url, "bar");
+ EXPECT_EQ(std::string_view(url.data(), url.size()), "/redfish/v1/foo/bar");
+
+ appendUrlPieces(url, "/", "bad&tring");
+ EXPECT_EQ(std::string_view(url.data(), url.size()),
+ "/redfish/v1/foo/bar/%2f/bad&tring");
+}
+
} // namespace
} // namespace crow::utility