Commit 4147d67b authored by knowledge4igor's avatar knowledge4igor

Add overload for out operator in Cookie

parent a95c97af
...@@ -20,6 +20,8 @@ namespace Pistache { ...@@ -20,6 +20,8 @@ namespace Pistache {
namespace Http { namespace Http {
struct Cookie { struct Cookie {
friend std::ostream& operator<<(std::ostream& os, const Cookie& cookie);
Cookie(std::string name, std::string value); Cookie(std::string name, std::string value);
std::string name; std::string name;
...@@ -38,9 +40,12 @@ struct Cookie { ...@@ -38,9 +40,12 @@ struct Cookie {
static Cookie fromRaw(const char* str, size_t len); static Cookie fromRaw(const char* str, size_t len);
static Cookie fromString(const std::string& str); static Cookie fromString(const std::string& str);
private:
void write(std::ostream& os) const; void write(std::ostream& os) const;
}; };
std::ostream& operator<<(std::ostream& os, const Cookie& cookie);
class CookieJar { class CookieJar {
public: public:
using HashMapCookies = std::unordered_map<std::string, Cookie>; // "value" -> Cookie using HashMapCookies = std::unordered_map<std::string, Cookie>; // "value" -> Cookie
......
...@@ -207,6 +207,12 @@ Cookie::write(std::ostream& os) const { ...@@ -207,6 +207,12 @@ Cookie::write(std::ostream& os) const {
} }
std::ostream& operator<<(std::ostream& os, const Cookie& cookie)
{
cookie.write(os);
return os;
}
CookieJar::CookieJar() CookieJar::CookieJar()
: cookies() : cookies()
{ } { }
......
...@@ -90,7 +90,7 @@ namespace { ...@@ -90,7 +90,7 @@ namespace {
std::ostream os(&buf); std::ostream os(&buf);
for (const auto& cookie: cookies) { for (const auto& cookie: cookies) {
OUT(os << "Set-Cookie: "); OUT(os << "Set-Cookie: ");
OUT(cookie.write(os)); OUT(os << cookie);
OUT(os << crlf); OUT(os << crlf);
} }
......
...@@ -97,7 +97,7 @@ TEST(cookie_test, write_test) { ...@@ -97,7 +97,7 @@ TEST(cookie_test, write_test) {
c1.domain = Some(std::string("example.com")); c1.domain = Some(std::string("example.com"));
std::ostringstream oss; std::ostringstream oss;
c1.write(oss); oss << c1;
ASSERT_EQ(oss.str(), "lang=fr-FR; Path=/; Domain=example.com"); ASSERT_EQ(oss.str(), "lang=fr-FR; Path=/; Domain=example.com");
...@@ -110,13 +110,13 @@ TEST(cookie_test, write_test) { ...@@ -110,13 +110,13 @@ TEST(cookie_test, write_test) {
c2.expires = Some(FullDate(expires)); c2.expires = Some(FullDate(expires));
oss.str(""); oss.str("");
c2.write(oss); oss << c2;
Cookie c3("lang", "en-US"); Cookie c3("lang", "en-US");
c3.secure = true; c3.secure = true;
c3.ext.insert(std::make_pair("Scope", "Private")); c3.ext.insert(std::make_pair("Scope", "Private"));
oss.str(""); oss.str("");
c3.write(oss); oss << c3;
ASSERT_EQ(oss.str(), "lang=en-US; Secure; Scope=Private"); ASSERT_EQ(oss.str(), "lang=en-US; Secure; Scope=Private");
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment