diff --git a/include/pistache/http_header.h b/include/pistache/http_header.h index 1d52fc1c5..08315aec6 100644 --- a/include/pistache/http_header.h +++ b/include/pistache/http_header.h @@ -174,6 +174,14 @@ namespace Pistache::Http::Header : mediaRange_() { } + explicit Accept(const std::vector& mediaRange) + : mediaRange_(mediaRange) + { } + + explicit Accept(std::initializer_list mediaRange) + : mediaRange_(mediaRange) + { } + void parseRaw(const char* str, size_t len) override; void write(std::ostream& os) const override; diff --git a/src/common/http_header.cc b/src/common/http_header.cc index 535d1183a..6282eb627 100644 --- a/src/common/http_header.cc +++ b/src/common/http_header.cc @@ -660,7 +660,18 @@ namespace Pistache::Http::Header } while (!cursor.eof()); } - void Accept::write(std::ostream& /*os*/) const { } + void Accept::write(std::ostream& os) const + { + if (mediaRange_.empty()) + { + return; + } + for (size_t i = 0; i < mediaRange_.size() - 1; i++) + { + os << mediaRange_[i].toString() << ", "; + } + os << mediaRange_[mediaRange_.size() - 1].toString(); + } void AccessControlAllowOrigin::parse(const std::string& data) { uri_ = data; } diff --git a/tests/headers_test.cc b/tests/headers_test.cc index 434029951..a0849da5d 100644 --- a/tests/headers_test.cc +++ b/tests/headers_test.cc @@ -13,6 +13,7 @@ #include #include #include +#include using testing::ElementsAre; using testing::SizeIs; @@ -30,6 +31,10 @@ TEST(headers_test, accept) const auto& mime = media[0]; ASSERT_EQ(mime, MIME(Audio, Star)); ASSERT_EQ(mime.q().value_or(Pistache::Http::Mime::Q(0)), Pistache::Http::Mime::Q(20)); + + std::ostringstream oss; + a1.write(oss); + ASSERT_EQ(oss.str(), "audio/*; q=0.2"); } Pistache::Http::Header::Accept a2; @@ -49,6 +54,10 @@ TEST(headers_test, accept) ASSERT_EQ(level.value_or(""), "1"); const auto& m4 = media[3]; ASSERT_EQ(m4, MIME(Star, Star)); + + std::ostringstream oss; + a2.write(oss); + ASSERT_EQ(oss.str(), "text/*, text/html, text/html;level=1, */*"); } Pistache::Http::Header::Accept a3; @@ -67,6 +76,10 @@ TEST(headers_test, accept) ASSERT_EQ(media[3], MIME(Text, Html)); ASSERT_EQ(media[4], MIME(Star, Star)); ASSERT_EQ(media[4].q().value_or(Pistache::Http::Mime::Q(0)), Pistache::Http::Mime::Q::fromFloat(0.5)); + + std::ostringstream oss; + a3.write(oss); + ASSERT_EQ(oss.str(), "text/*;q=0.3, text/html;q=0.7, text/html;level=1, text/html;level=2;q=0.4, */*;q=0.5"); } Pistache::Http::Header::Accept a4; @@ -750,12 +763,9 @@ TEST(headers_test, last_modified_test) // As of July/2024, it seems that in macOS, Linux and OpenBSD this produces // an OSS ending "GMT", while in FreeBSD it ends "UTC". Of course, they // mean the same thing, and we allow either. - const bool oss_ends_utc = - ((oss.str().length() >= 3) && - (oss.str().compare(oss.str().length() - 3, 3, "UTC") == 0)); - const std::string ref(std::string("Sun, 06 Nov 1994 08:49:37 ") + - (oss_ends_utc ? "UTC" : "GMT")); - + const bool oss_ends_utc = ((oss.str().length() >= 3) && (oss.str().compare(oss.str().length() - 3, 3, "UTC") == 0)); + const std::string ref(std::string("Sun, 06 Nov 1994 08:49:37 ") + (oss_ends_utc ? "UTC" : "GMT")); + ASSERT_EQ(ref, oss.str()); Pistache::Http::Header::LastModified l1; l1.parse(ref); diff --git a/version.txt b/version.txt index 600bab75a..36f87cbe5 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -0.4.1.20240822 +0.4.1.20240828