drogon/unittest/HttpDateUnittest.cpp
VayuDev d4d5adf88b
Added additional formats for getHttpDate function and fixed undefined behavior upon error (#453) (#456)
This patch adds support for the RFC 850 and asctime format. If an error
occurs, we now return a date with the epoch value of -1 and warn instead of
triggering undefined behavior. This is checked by a new set of tests.

Co-authored-by: VayuDev <vayudev@protonmail.com>
Co-authored-by: antao <antao2002@gmail.com>
2020-06-05 20:57:36 +08:00

35 lines
965 B
C++

#include <drogon/utils/Utilities.h>
#include <gtest/gtest.h>
using namespace drogon;
TEST(HttpDate, rfc850)
{
auto date = utils::getHttpDate("Fri, 05-Jun-20 09:19:38 GMT");
EXPECT_EQ(date.microSecondsSinceEpoch() / MICRO_SECONDS_PRE_SEC, 1591348778);
}
TEST(HttpDate, redditFormat)
{
auto date = utils::getHttpDate("Fri, 05-Jun-2020 09:19:38 GMT");
EXPECT_EQ(date.microSecondsSinceEpoch() / MICRO_SECONDS_PRE_SEC, 1591348778);
}
TEST(HttpDate, invalidFormat)
{
auto date = utils::getHttpDate("Fri, this format is invalid");
EXPECT_EQ(date.microSecondsSinceEpoch(), (std::numeric_limits<int64_t>::max)());
}
TEST(HttpDate, asctimeFormat)
{
auto epoch = time(nullptr);
auto str = asctime(gmtime(&epoch));
auto date = utils::getHttpDate(str);
EXPECT_EQ(date.microSecondsSinceEpoch() / MICRO_SECONDS_PRE_SEC, epoch);
}
int main(int argc, char **argv)
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}