Add UT for http parser.

This commit is contained in:
Chunting Gu
2019-04-16 11:11:50 +08:00
parent ddfcecccc6
commit b72820c9c5
8 changed files with 235 additions and 48 deletions
+22 -6
View File
@@ -28,9 +28,10 @@ std::ostream& operator<<(std::ostream& os, const HttpMessage& message) {
// -----------------------------------------------------------------------------
bool HttpMessage::IsConnectionKeepAlive() const {
using http::headers::kConnection;
bool existed = false;
const std::string& connection =
GetHeader(http::headers::kConnection, &existed);
const std::string& connection = GetHeader(kConnection, &existed);
if (!existed) {
// Keep-Alive is by default for HTTP/1.1.
@@ -45,7 +46,9 @@ bool HttpMessage::IsConnectionKeepAlive() const {
}
http::ContentEncoding HttpMessage::GetContentEncoding() const {
const std::string& encoding = GetHeader(http::headers::kContentEncoding);
using http::headers::kContentEncoding;
const std::string& encoding = GetHeader(kContentEncoding);
if (encoding == "gzip") {
return http::ContentEncoding::kGzip;
}
@@ -64,11 +67,12 @@ bool HttpMessage::AcceptEncodingGzip() const {
// See: https://tools.ietf.org/html/rfc7231#section-3.1.1.1
void HttpMessage::SetContentType(const std::string& media_type,
const std::string& charset) {
using http::headers::kContentType;
if (charset.empty()) {
SetHeader(http::headers::kContentType, media_type);
SetHeader(kContentType, media_type);
} else {
SetHeader(http::headers::kContentType,
media_type + ";charset=" + charset);
SetHeader(kContentType, media_type + ";charset=" + charset);
}
}
@@ -103,6 +107,18 @@ void HttpMessage::Prepare() {
}
}
void HttpMessage::CopyPayload(std::ostream& os) const {
for (const boost::asio::const_buffer& b : payload_) {
os.write(static_cast<const char*>(b.data()), b.size());
}
}
void HttpMessage::CopyPayload(std::string* str) const {
std::stringstream ss;
CopyPayload(ss);
*str = ss.str();
}
void HttpMessage::Dump(std::ostream& os, std::size_t indent,
const std::string& prefix) const {
std::string indent_str;
+6
View File
@@ -88,6 +88,12 @@ public:
return payload_;
}
// Copy the exact payload to the given output stream.
void CopyPayload(std::ostream& os) const;
// Copy the exact payload to the given string.
void CopyPayload(std::string* str) const;
// Dump to output stream.
void Dump(std::ostream& os, std::size_t indent = 0,
const std::string& prefix = "") const;
+1 -1
View File
@@ -152,8 +152,8 @@ bool HttpParser::ParseHeaderLine(const std::string& line) {
LOG_INFO("Content length: %u.", content_length_);
// Reserve memory to avoid frequent reallocation when append.
try {
// Reserve memory to avoid frequent reallocation when append.
content_.reserve(content_length_);
} catch (const std::exception& e) {
LOG_ERRO("Failed to reserve content memory: %s.", e.what());