From 51d0db256506023ea3deae37a4a400287c6dbc8b Mon Sep 17 00:00:00 2001 From: alandefreitas Date: Wed, 3 Aug 2022 18:57:29 -0300 Subject: [PATCH] static_url operator<< fix #320 --- include/boost/url/static_url.hpp | 30 ++++++++++++++++++++++++++++++ test/unit/static_url.cpp | 14 ++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/include/boost/url/static_url.hpp b/include/boost/url/static_url.hpp index d28639ec..35b53aa8 100644 --- a/include/boost/url/static_url.hpp +++ b/include/boost/url/static_url.hpp @@ -190,8 +190,38 @@ public: { construct(s); } + + // hidden friend + friend + std::ostream& + operator<<(std::ostream& os, static_url const& u) + { + return os << u.string(); + } }; +/** Format the static URL to the output stream + + This function serializes the encoded URL + to the output stream. + + @par Example + @code + static_url u( "http://www.example.com/index.htm" ); + + std::cout << u << std::endl; + @endcode + + @return A reference to the output stream, for chaining + + @param os The output stream to write to. + + @param u The URL to write. +*/ +template +std::ostream& +operator<<(std::ostream& os, static_url const& u); + } // urls } // boost diff --git a/test/unit/static_url.cpp b/test/unit/static_url.cpp index 0421a77e..8feeb3be 100644 --- a/test/unit/static_url.cpp +++ b/test/unit/static_url.cpp @@ -156,11 +156,25 @@ public: BOOST_TEST_EQ(u.encoded_fragment(), "frag"); } + void + testOstream() + { + { + static_url<64> u = parse_uri( + "http://example.com/index.htm?q#f").value(); + std::stringstream ss; + ss << u; + BOOST_TEST(ss.str() == + "http://example.com/index.htm?q#f"); + } + } + void run() { testSpecial(); testParts(); + testOstream(); } };