blob: c644ea9c055690ac2e1c233b199886f0791e8806 [file] [log] [blame]
#include "boost/beast/core/flat_buffer.hpp"
#include "boost/beast/http/serializer.hpp"
#include "http/http_response.hpp"
#include <filesystem>
#include <fstream>
#include <thread>
#include "gtest/gtest.h"
namespace
{
void addHeaders(crow::Response& res)
{
res.addHeader("myheader", "myvalue");
res.keepAlive(true);
res.result(boost::beast::http::status::ok);
}
void verifyHeaders(crow::Response& res)
{
EXPECT_EQ(res.getHeaderValue("myheader"), "myvalue");
EXPECT_EQ(res.keepAlive(), true);
EXPECT_EQ(res.result(), boost::beast::http::status::ok);
}
std::string makeFile()
{
std::filesystem::path path = std::filesystem::temp_directory_path();
path /= "bmcweb_http_response_test_XXXXXXXXXXX";
std::string stringPath = path.string();
int fd = mkstemp(stringPath.data());
EXPECT_GT(fd, 0);
std::string_view sample = "sample text";
EXPECT_EQ(write(fd, sample.data(), sample.size()), sample.size());
return stringPath;
}
TEST(HttpResponse, Defaults)
{
crow::Response res;
EXPECT_EQ(
boost::variant2::holds_alternative<crow::Response::string_response>(
res.response),
true);
}
TEST(HttpResponse, Headers)
{
crow::Response res;
addHeaders(res);
verifyHeaders(res);
}
TEST(HttpResponse, StringBody)
{
crow::Response res;
addHeaders(res);
std::string_view bodyvalue = "this is my new body";
res.write({bodyvalue.data(), bodyvalue.length()});
EXPECT_EQ(*res.body(), bodyvalue);
verifyHeaders(res);
}
TEST(HttpResponse, FileBody)
{
crow::Response res;
addHeaders(res);
std::string path = makeFile();
res.openFile(path);
verifyHeaders(res);
std::filesystem::remove(path);
}
TEST(HttpResponse, BodyTransitions)
{
crow::Response res;
addHeaders(res);
std::string path = makeFile();
res.openFile(path);
EXPECT_EQ(boost::variant2::holds_alternative<crow::Response::file_response>(
res.response),
true);
verifyHeaders(res);
res.write("body text");
EXPECT_EQ(
boost::variant2::holds_alternative<crow::Response::string_response>(
res.response),
true);
verifyHeaders(res);
std::filesystem::remove(path);
}
} // namespace