Skip to content

Commit

Permalink
Merge pull request #1235 from EmilienKia/write-accept-header
Browse files Browse the repository at this point in the history
Add Accept header serialization
  • Loading branch information
kiplingw authored Aug 29, 2024
2 parents 5fa25ae + 9c0b0e4 commit eddff16
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 8 deletions.
8 changes: 8 additions & 0 deletions include/pistache/http_header.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,14 @@ namespace Pistache::Http::Header
: mediaRange_()
{ }

explicit Accept(const std::vector<Mime::MediaType>& mediaRange)
: mediaRange_(mediaRange)
{ }

explicit Accept(std::initializer_list<Mime::MediaType> mediaRange)
: mediaRange_(mediaRange)
{ }

void parseRaw(const char* str, size_t len) override;
void write(std::ostream& os) const override;

Expand Down
13 changes: 12 additions & 1 deletion src/common/http_header.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand Down
22 changes: 16 additions & 6 deletions tests/headers_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <algorithm>
#include <chrono>
#include <iostream>
#include <sstream>

using testing::ElementsAre;
using testing::SizeIs;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.4.1.20240822
0.4.1.20240828

0 comments on commit eddff16

Please sign in to comment.