1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
|
#include <gtest/gtest.h>
#include <string>
#include "cpr/cprtypes.h"
#include "cpr/util.h"
using namespace cpr;
TEST(UtilParseCookiesTests, BasicParseTest) {
Cookies expectedCookies{{Cookie("status", "on", "127.0.0.1", false, "/", false, std::chrono::system_clock::from_time_t(1656908640)), Cookie("name", "debug", "127.0.0.1", false, "/", false, std::chrono::system_clock::from_time_t(0))}};
curl_slist* raw_cookies = new curl_slist{
(char*) "127.0.0.1\tFALSE\t/\tFALSE\t1656908640\tstatus\ton",
new curl_slist{
(char*) "127.0.0.1\tFALSE\t/\tFALSE\t0\tname\tdebug",
nullptr,
},
};
Cookies cookies = util::parseCookies(raw_cookies);
for (auto cookie = cookies.begin(), expectedCookie = expectedCookies.begin(); cookie != cookies.end() && expectedCookie != expectedCookies.end(); cookie++, expectedCookie++) {
EXPECT_EQ(expectedCookie->GetName(), cookie->GetName());
EXPECT_EQ(expectedCookie->GetValue(), cookie->GetValue());
EXPECT_EQ(expectedCookie->GetDomain(), cookie->GetDomain());
EXPECT_EQ(expectedCookie->IsIncludingSubdomains(), cookie->IsIncludingSubdomains());
EXPECT_EQ(expectedCookie->GetPath(), cookie->GetPath());
EXPECT_EQ(expectedCookie->IsHttpsOnly(), cookie->IsHttpsOnly());
EXPECT_EQ(expectedCookie->GetExpires(), cookie->GetExpires());
}
delete raw_cookies->next;
delete raw_cookies;
}
TEST(UtilParseHeaderTests, BasicParseTest) {
std::string header_string{
"HTTP/1.1 200 OK\r\n"
"Server: nginx\r\n"
"Date: Sun, 05 Mar 2017 00:34:54 GMT\r\n"
"Content-Type: application/json\r\n"
"Content-Length: 351\r\n"
"Connection: keep-alive\r\n"
"Access-Control-Allow-Origin: *\r\n"
"Access-Control-Allow-Credentials: true\r\n"
"\r\n"};
Header header = util::parseHeader(header_string);
EXPECT_EQ(std::string{"nginx"}, header["Server"]);
EXPECT_EQ(std::string{"Sun, 05 Mar 2017 00:34:54 GMT"}, header["Date"]);
EXPECT_EQ(std::string{"application/json"}, header["Content-Type"]);
EXPECT_EQ(std::string{"351"}, header["Content-Length"]);
EXPECT_EQ(std::string{"keep-alive"}, header["Connection"]);
EXPECT_EQ(std::string{"*"}, header["Access-Control-Allow-Origin"]);
EXPECT_EQ(std::string{"true"}, header["Access-Control-Allow-Credentials"]);
}
TEST(UtilParseHeaderTests, NewlineTest) {
std::string header_string{
"HTTP/1.1 200 OK\r\n"
"Auth:\n"
"Access-Control-Allow-Credentials: true\r\n"
"\r\n"};
Header header = util::parseHeader(header_string);
EXPECT_EQ(std::string{""}, header["Server"]);
EXPECT_EQ(std::string{"true"}, header["Access-Control-Allow-Credentials"]);
}
TEST(UtilParseHeaderTests, SpaceNewlineTest) {
std::string header_string{
"HTTP/1.1 200 OK\r\n"
"Auth: \n"
"Access-Control-Allow-Credentials: true\r\n"
"\r\n"};
Header header = util::parseHeader(header_string);
EXPECT_EQ(std::string{""}, header["Server"]);
EXPECT_EQ(std::string{"true"}, header["Access-Control-Allow-Credentials"]);
}
TEST(UtilParseHeaderTests, CarriageReturnNewlineTest) {
std::string header_string{
"HTTP/1.1 200 OK\n"
"Auth:\r\n"
"Access-Control-Allow-Credentials: true\r\n"
"\r\n"};
Header header = util::parseHeader(header_string);
EXPECT_EQ(std::string{""}, header["Server"]);
EXPECT_EQ(std::string{"true"}, header["Access-Control-Allow-Credentials"]);
}
TEST(UtilParseHeaderTests, SpaceCarriageReturnNewlineTest) {
std::string header_string{
"HTTP/1.1 200 OK\n"
"Auth: \r\n"
"Access-Control-Allow-Credentials: true\r\n"
"\r\n"};
Header header = util::parseHeader(header_string);
EXPECT_EQ(std::string{""}, header["Server"]);
EXPECT_EQ(std::string{"true"}, header["Access-Control-Allow-Credentials"]);
}
TEST(UtilParseHeaderTests, BasicStatusLineTest) {
std::string header_string{
"HTTP/1.1 200 OK\r\n"
"Server: nginx\r\n"
"Content-Type: application/json\r\n"
"\r\n"};
std::string status_line;
std::string reason;
Header header = util::parseHeader(header_string, &status_line, &reason);
EXPECT_EQ(std::string{"HTTP/1.1 200 OK"}, status_line);
EXPECT_EQ(std::string{"OK"}, reason);
EXPECT_EQ(std::string{"nginx"}, header["Server"]);
EXPECT_EQ(std::string{"application/json"}, header["Content-Type"]);
}
TEST(UtilParseHeaderTests, NewlineStatusLineTest) {
std::string header_string{
"HTTP/1.1 407 Proxy Authentication Required\n"
"Server: nginx\r\n"
"Content-Type: application/json\r\n"
"\r\n"};
std::string status_line;
std::string reason;
Header header = util::parseHeader(header_string, &status_line, &reason);
EXPECT_EQ(std::string{"HTTP/1.1 407 Proxy Authentication Required"}, status_line);
EXPECT_EQ(std::string{"Proxy Authentication Required"}, reason);
EXPECT_EQ(std::string{"nginx"}, header["Server"]);
EXPECT_EQ(std::string{"application/json"}, header["Content-Type"]);
}
TEST(UtilParseHeaderTests, NoReasonSpaceTest) {
std::string header_string{
"HTTP/1.1 200 \n"
"Server: nginx\r\n"
"Content-Type: application/json\r\n"
"\r\n"};
std::string status_line;
std::string reason;
Header header = util::parseHeader(header_string, &status_line, &reason);
EXPECT_EQ(std::string{"HTTP/1.1 200"}, status_line);
EXPECT_EQ(std::string{""}, reason);
EXPECT_EQ(std::string{"nginx"}, header["Server"]);
EXPECT_EQ(std::string{"application/json"}, header["Content-Type"]);
}
TEST(UtilParseHeaderTests, NoReasonTest) {
std::string header_string{
"HTTP/1.1 200\n"
"Server: nginx\r\n"
"Content-Type: application/json\r\n"
"\r\n"};
std::string status_line;
std::string reason;
Header header = util::parseHeader(header_string, &status_line, &reason);
EXPECT_EQ(std::string{"HTTP/1.1 200"}, status_line);
EXPECT_EQ(std::string{""}, reason);
EXPECT_EQ(std::string{"nginx"}, header["Server"]);
EXPECT_EQ(std::string{"application/json"}, header["Content-Type"]);
}
TEST(UtilUrlEncodeTests, UnicodeEncoderTest) {
std::string input = "一二三";
std::string result{util::urlEncode(input)};
std::string expected = "%E4%B8%80%E4%BA%8C%E4%B8%89";
EXPECT_EQ(result, expected);
}
TEST(UtilUrlEncodeTests, AsciiEncoderTest) {
std::string input = "Hello World!";
std::string result{util::urlEncode(input)};
std::string expected = "Hello%20World%21";
EXPECT_EQ(result, expected);
}
TEST(UtilUrlDecodeTests, UnicodeDecoderTest) {
std::string input = "%E4%B8%80%E4%BA%8C%E4%B8%89";
std::string result{util::urlDecode(input)};
std::string expected = "一二三";
EXPECT_EQ(result, expected);
}
TEST(UtilUrlDecodeTests, AsciiDecoderTest) {
std::string input = "Hello%20World%21";
std::string result{util::urlDecode(input)};
std::string expected = "Hello World!";
EXPECT_EQ(result, expected);
}
TEST(UtilIsTrueTests, TrueTest) {
{
std::string input = "TRUE";
bool output = util::isTrue(input);
EXPECT_TRUE(output);
}
{
std::string input = "True";
bool output = util::isTrue(input);
EXPECT_TRUE(output);
}
{
std::string input = "true";
bool output = util::isTrue(input);
EXPECT_TRUE(output);
}
}
TEST(UtilIsTrueTests, FalseTest) {
{
std::string input = "FALSE";
bool output = util::isTrue(input);
EXPECT_FALSE(output);
}
{
std::string input = "False";
bool output = util::isTrue(input);
EXPECT_FALSE(output);
}
{
std::string input = "false";
bool output = util::isTrue(input);
EXPECT_FALSE(output);
}
}
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
|