Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Accept header serialization #1235

Merged
merged 4 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
} while (!cursor.eof());
}

void Accept::write(std::ostream& /*os*/) const { }
void Accept::write(std::ostream& os) const
{
if (mediaRange_.empty())
{
return;

Check warning on line 667 in src/common/http_header.cc

View check run for this annotation

Codecov / codecov/patch

src/common/http_header.cc#L667

Added line #L667 was not covered by tests
}
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
Loading