This commit is contained in:
yhirose 2020-11-07 09:33:22 -05:00
parent 84661ea6ed
commit eb4b7c70a9
2 changed files with 18 additions and 4 deletions

View file

@ -430,7 +430,7 @@ struct Response {
void set_redirect(const char *url, int status = 302);
void set_redirect(const std::string &url, int status = 302);
void set_content(const char *s, size_t n, const char *content_type);
void set_content(std::string s, const char *content_type);
void set_content(const std::string &s, const char *content_type);
void set_content_provider(
size_t length, const char *content_type, ContentProvider provider,
@ -3643,12 +3643,15 @@ inline void Response::set_redirect(const std::string &url, int stat) {
inline void Response::set_content(const char *s, size_t n,
const char *content_type) {
body.assign(s, n);
auto rng = headers.equal_range("Content-Type");
headers.erase(rng.first, rng.second);
set_header("Content-Type", content_type);
}
inline void Response::set_content(std::string s, const char *content_type) {
body = std::move(s);
set_header("Content-Type", content_type);
inline void Response::set_content(const std::string &s,
const char *content_type) {
set_content(s.data(), s.size(), content_type);
}
inline void

View file

@ -135,6 +135,17 @@ TEST(GetHeaderValueTest, RegularValue) {
EXPECT_STREQ("text/html", val);
}
TEST(GetHeaderValueTest, SetContent) {
Response res;
res.set_content("html", "text/html");
EXPECT_EQ("text/html", res.get_header_value("Content-Type"));
res.set_content("text", "text/plain");
EXPECT_EQ(1, res.get_header_value_count("Content-Type"));
EXPECT_EQ("text/plain", res.get_header_value("Content-Type"));
}
TEST(GetHeaderValueTest, RegularValueInt) {
Headers headers = {{"Content-Length", "100"}, {"Dummy", "Dummy"}};
auto val =