diff options
author | Willy Tu <wltu@google.com> | 2022-09-27 08:36:59 +0300 |
---|---|---|
committer | Ed Tanous <ed@tanous.net> | 2022-10-26 00:24:56 +0300 |
commit | c6bcedc6c82fefa7f4327aae9e57e6464413c4b7 (patch) | |
tree | 24774b58f2e7759e830cb93b2d2f79ccf23b4c1b | |
parent | 513d1ffca957889c765569cb3d7d713c12394073 (diff) | |
download | bmcweb-c6bcedc6c82fefa7f4327aae9e57e6464413c4b7.tar.xz |
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>
-rw-r--r-- | http/utility.hpp | 18 | ||||
-rw-r--r-- | test/http/utility_test.cpp | 13 |
2 files changed, 29 insertions, 2 deletions
diff --git a/http/utility.hpp b/http/utility.hpp index 78570169e4..b1b38da6f7 100644 --- a/http/utility.hpp +++ b/http/utility.hpp @@ -562,15 +562,23 @@ struct ConstantTimeCompare 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 @@ inline boost::urls::url urlFromPieces(const AV... args) 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 8eef93f4a3..58a28654e1 100644 --- a/test/http/utility_test.cpp +++ b/test/http/utility_test.cpp @@ -222,5 +222,18 @@ TEST(URL, JsonEncoding) 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 |