summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorOleg Morozenkov <m@oleg.rocks>2018-07-23 01:56:42 +0300
committerOleg Morozenkov <m@oleg.rocks>2018-07-23 01:56:42 +0300
commitd47ee877be5d1175bdc36f2d87881ddaf875a8e9 (patch)
tree7fd20cdc1236fe6b832ae980de12afd7071ebab9 /test
parentcea20d4078f2088dea0dd589f1cc9dd7ee22461b (diff)
Refactor http clients, fix webhook server, add more samples, change tabs to 4 spaces
Diffstat (limited to 'test')
-rw-r--r--test/CMakeLists.txt8
-rw-r--r--test/tgbot/net/HttpParser.cpp167
-rw-r--r--test/tgbot/net/Url.cpp36
-rw-r--r--test/tgbot/tools/StringTools.cpp36
-rw-r--r--test/utils.h136
5 files changed, 195 insertions, 188 deletions
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 65781e6..3f8249b 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -1,8 +1,8 @@
set(TEST_SRC_LIST
- main.cpp
- tgbot/net/Url.cpp
- tgbot/net/HttpParser.cpp
- tgbot/tools/StringTools.cpp
+ main.cpp
+ tgbot/net/Url.cpp
+ tgbot/net/HttpParser.cpp
+ tgbot/tools/StringTools.cpp
)
include_directories("${PROJECT_SOURCE_DIR}/test")
diff --git a/test/tgbot/net/HttpParser.cpp b/test/tgbot/net/HttpParser.cpp
index d0a3703..0d588ef 100644
--- a/test/tgbot/net/HttpParser.cpp
+++ b/test/tgbot/net/HttpParser.cpp
@@ -32,106 +32,103 @@ using namespace TgBot;
BOOST_AUTO_TEST_SUITE(tHttpParser)
BOOST_AUTO_TEST_CASE(generateRequest) {
- vector<HttpReqArg> args = { HttpReqArg("email", "test@example.com"), HttpReqArg("text", "Hello, world!") };
- string t = HttpParser::getInstance().generateRequest(Url("http://example.com/index.html"), args, true);
- string e = ""
- "POST /index.html HTTP/1.1\r\n"
- "Host: example.com\r\n"
- "Connection: keep-alive\r\n"
- "Content-Type: application/x-www-form-urlencoded\r\n"
- "Content-Length: 49\r\n"
- "\r\n"
- "email=test%40example.com&text=Hello%2C%20world%21";
- BOOST_CHECK_MESSAGE(t == e, diffS(t, e));
+ vector<HttpReqArg> args = { HttpReqArg("email", "test@example.com"), HttpReqArg("text", "Hello, world!") };
+ string t = HttpParser().generateRequest(Url("http://example.com/index.html"), args, true);
+ string e = ""
+ "POST /index.html HTTP/1.1\r\n"
+ "Host: example.com\r\n"
+ "Connection: keep-alive\r\n"
+ "Content-Type: application/x-www-form-urlencoded\r\n"
+ "Content-Length: 49\r\n"
+ "\r\n"
+ "email=test%40example.com&text=Hello%2C%20world%21";
+ BOOST_CHECK_MESSAGE(t == e, diffS(t, e));
}
BOOST_AUTO_TEST_CASE(generateMultipartFormData) {
- vector<HttpReqArg> args = { HttpReqArg("email", "test@example.com"), HttpReqArg("text", "Hello, world!", true) };
- string boundary = HttpParser::getInstance().generateMultipartBoundary(args);
- string t = HttpParser::getInstance().generateMultipartFormData(args, boundary);
- string e = ""
- "--" + boundary + "\r\n"
- "Content-Disposition: form-data; name=\"email\"\r\n"
- "\r\n"
- "test@example.com\r\n"
- "--" + boundary + "\r\n"
- "Content-Disposition: form-data; name=\"text\"; filename=\"\"\r\n"
- "Content-Type: text/plain\r\n"
- "\r\n"
- "Hello, world!\r\n"
- "--" + boundary + "--\r\n";
- BOOST_CHECK_MESSAGE(t == e, diffS(t, e));
+ vector<HttpReqArg> args = { HttpReqArg("email", "test@example.com"), HttpReqArg("text", "Hello, world!", true) };
+ string boundary = HttpParser().generateMultipartBoundary(args);
+ string t = HttpParser().generateMultipartFormData(args, boundary);
+ string e = ""
+ "--" + boundary + "\r\n"
+ "Content-Disposition: form-data; name=\"email\"\r\n"
+ "\r\n"
+ "test@example.com\r\n"
+ "--" + boundary + "\r\n"
+ "Content-Disposition: form-data; name=\"text\"; filename=\"\"\r\n"
+ "Content-Type: text/plain\r\n"
+ "\r\n"
+ "Hello, world!\r\n"
+ "--" + boundary + "--\r\n";
+ BOOST_CHECK_MESSAGE(t == e, diffS(t, e));
}
BOOST_AUTO_TEST_CASE(generateWwwFormUrlencoded) {
- vector<HttpReqArg> args = { HttpReqArg("email", "test@example.com"), HttpReqArg("text", "Hello, world!") };
- string t = HttpParser::getInstance().generateWwwFormUrlencoded(args);
- string e = "email=test%40example.com&text=Hello%2C%20world%21";
- BOOST_CHECK_MESSAGE(t == e, diffS(t, e));
+ vector<HttpReqArg> args = { HttpReqArg("email", "test@example.com"), HttpReqArg("text", "Hello, world!") };
+ string t = HttpParser().generateWwwFormUrlencoded(args);
+ string e = "email=test%40example.com&text=Hello%2C%20world%21";
+ BOOST_CHECK_MESSAGE(t == e, diffS(t, e));
}
BOOST_AUTO_TEST_CASE(generateResponse) {
- string t = HttpParser::getInstance().generateResponse("testdata");
- string e = ""
- "HTTP/1.1 200 OK\r\n"
- "Content-Type: text/plain\r\n"
- "Content-Length: 8\r\n"
- "\r\n"
- "testdata";
- BOOST_CHECK_MESSAGE(t == e, diffS(t, e));
+ string t = HttpParser().generateResponse("testdata", "text/plain", 200, "OK", false);
+ string e = ""
+ "HTTP/1.1 200 OK\r\n"
+ "Content-Type: text/plain\r\n"
+ "Content-Length: 8\r\n"
+ "Connection: close\r\n"
+ "\r\n"
+ "testdata";
+ BOOST_CHECK_MESSAGE(t == e, diffS(t, e));
}
BOOST_AUTO_TEST_CASE(parseRequest) {
- string data = ""
- "POST /index.html HTTP/1.1\r\n"
- "Host: example.com\r\n"
- "Connection: keep-alive\r\n"
- "Content-Type: text/plain\r\n"
- "Content-Length: 8\r\n"
- "\r\n"
- "testdata";
-
- unordered_map<string, string> tHeaders;
- string t = HttpParser::getInstance().parseRequest(data, tHeaders);
-
- unordered_map<string, string> eHeaders = {
- { "method", "POST" },
- { "path", "/index.html" },
- { "host", "example.com" },
- { "connection", "keep-alive" },
- { "content-type", "text/plain" },
- { "content-length", "8" }
- };
- string e = "testdata";
-
- BOOST_CHECK_MESSAGE(t == e, diffS(t, e));
- BOOST_CHECK_MESSAGE(tHeaders == eHeaders, diff(tHeaders, eHeaders, [](const pair<const string, string>& item) -> string {
- return item.first + '=' + item.second;
- }));
+ string data = ""
+ "POST /index.html HTTP/1.1\r\n"
+ "Host: example.com\r\n"
+ "Connection: keep-alive\r\n"
+ "Content-Type: text/plain\r\n"
+ "Content-Length: 8\r\n"
+ "\r\n"
+ "testdata";
+
+ unordered_map<string, string> tHeaders = HttpParser().parseHeader(data.substr(0, data.rfind("\r\n")), true);
+ string tBody = HttpParser().extractBody(data);
+
+ unordered_map<string, string> eHeaders = {
+ { "_method", "POST" },
+ { "_path", "/index.html" },
+ { "Host", "example.com" },
+ { "Connection", "keep-alive" },
+ { "Content-Type", "text/plain" },
+ { "Content-Length", "8" }
+ };
+ string eBody = "testdata";
+
+ BOOST_CHECK_MESSAGE(tBody == eBody, diffS(tBody, eBody));
+ BOOST_CHECK_MESSAGE(tHeaders == eHeaders, diffMSS(tHeaders, eHeaders));
}
BOOST_AUTO_TEST_CASE(parseResponse) {
- string data = ""
- "HTTP/1.1 200 OK\r\n"
- "Content-Type: text/plain\r\n"
- "Content-Length: 8\r\n"
- "\r\n"
- "testdata";
-
- unordered_map<string, string> tHeaders;
- string t = HttpParser::getInstance().parseResponse(data, tHeaders);
-
- unordered_map<string, string> eHeaders = {
- { "status", "200" },
- { "content-type", "text/plain" },
- { "content-length", "8" }
- };
- string e = "testdata";
-
- BOOST_CHECK_MESSAGE(t == e, diffS(t, e));
- BOOST_CHECK_MESSAGE(tHeaders == eHeaders, diff(tHeaders, eHeaders, [](const pair<const string, string>& item) -> string {
- return item.first + '=' + item.second;
- }));
+ string data = ""
+ "HTTP/1.1 200 OK\r\n"
+ "Content-Type: text/plain\r\n"
+ "Content-Length: 8\r\n"
+ "\r\n"
+ "testdata";
+
+ unordered_map<string, string> tHeaders = HttpParser().parseHeader(data.substr(0, data.rfind("\r\n")), false);
+ string tBody = HttpParser().extractBody(data);
+
+ unordered_map<string, string> eHeaders = {
+ { "_status", "200" },
+ { "Content-Type", "text/plain" },
+ { "Content-Length", "8" }
+ };
+ string eBody = "testdata";
+
+ BOOST_CHECK_MESSAGE(tBody == eBody, diffS(tBody, eBody));
+ BOOST_CHECK_MESSAGE(tHeaders == eHeaders, diffMSS(tHeaders, eHeaders));
}
BOOST_AUTO_TEST_SUITE_END()
diff --git a/test/tgbot/net/Url.cpp b/test/tgbot/net/Url.cpp
index abd90cb..0974ab6 100644
--- a/test/tgbot/net/Url.cpp
+++ b/test/tgbot/net/Url.cpp
@@ -29,30 +29,30 @@ using namespace TgBot;
BOOST_AUTO_TEST_SUITE(tUrl)
BOOST_AUTO_TEST_CASE(parsingUrlNoPath) {
- Url t("https://test.example.com?test=123&123=test#title");
- BOOST_CHECK_EQUAL(t.protocol, "https");
- BOOST_CHECK_EQUAL(t.host, "test.example.com");
- BOOST_CHECK_EQUAL(t.path, "/");
- BOOST_CHECK_EQUAL(t.query, "test=123&123=test");
- BOOST_CHECK_EQUAL(t.fragment, "title");
+ Url t("https://test.example.com?test=123&123=test#title");
+ BOOST_CHECK_EQUAL(t.protocol, "https");
+ BOOST_CHECK_EQUAL(t.host, "test.example.com");
+ BOOST_CHECK_EQUAL(t.path, "/");
+ BOOST_CHECK_EQUAL(t.query, "test=123&123=test");
+ BOOST_CHECK_EQUAL(t.fragment, "title");
}
BOOST_AUTO_TEST_CASE(parsingUrlNoPathAndQuery) {
- Url t("https://test.example.com#title");
- BOOST_CHECK_EQUAL(t.protocol, "https");
- BOOST_CHECK_EQUAL(t.host, "test.example.com");
- BOOST_CHECK_EQUAL(t.path, "/");
- BOOST_CHECK_EQUAL(t.query, "");
- BOOST_CHECK_EQUAL(t.fragment, "title");
+ Url t("https://test.example.com#title");
+ BOOST_CHECK_EQUAL(t.protocol, "https");
+ BOOST_CHECK_EQUAL(t.host, "test.example.com");
+ BOOST_CHECK_EQUAL(t.path, "/");
+ BOOST_CHECK_EQUAL(t.query, "");
+ BOOST_CHECK_EQUAL(t.fragment, "title");
}
BOOST_AUTO_TEST_CASE(parsingUrlFull) {
- Url t("https://test.example.com/example-page/index.html?test=123&123=test#title");
- BOOST_CHECK_EQUAL(t.protocol, "https");
- BOOST_CHECK_EQUAL(t.host, "test.example.com");
- BOOST_CHECK_EQUAL(t.path, "/example-page/index.html");
- BOOST_CHECK_EQUAL(t.query, "test=123&123=test");
- BOOST_CHECK_EQUAL(t.fragment, "title");
+ Url t("https://test.example.com/example-page/index.html?test=123&123=test#title");
+ BOOST_CHECK_EQUAL(t.protocol, "https");
+ BOOST_CHECK_EQUAL(t.host, "test.example.com");
+ BOOST_CHECK_EQUAL(t.path, "/example-page/index.html");
+ BOOST_CHECK_EQUAL(t.query, "test=123&123=test");
+ BOOST_CHECK_EQUAL(t.fragment, "title");
}
BOOST_AUTO_TEST_SUITE_END()
diff --git a/test/tgbot/tools/StringTools.cpp b/test/tgbot/tools/StringTools.cpp
index 969c3d8..d92fc86 100644
--- a/test/tgbot/tools/StringTools.cpp
+++ b/test/tgbot/tools/StringTools.cpp
@@ -35,36 +35,36 @@ using namespace std;
BOOST_AUTO_TEST_SUITE(tStringTools)
BOOST_AUTO_TEST_CASE(startsWith) {
- BOOST_CHECK(StringTools::startsWith("abc123", "abc"));
- BOOST_CHECK(!StringTools::startsWith("abc123", "aac"));
- BOOST_CHECK(!StringTools::startsWith("abc123", "Xabc"));
- BOOST_CHECK(!StringTools::startsWith("abc123", "abcX"));
- BOOST_CHECK(!StringTools::startsWith("abc", "abc123"));
+ BOOST_CHECK(StringTools::startsWith("abc123", "abc"));
+ BOOST_CHECK(!StringTools::startsWith("abc123", "aac"));
+ BOOST_CHECK(!StringTools::startsWith("abc123", "Xabc"));
+ BOOST_CHECK(!StringTools::startsWith("abc123", "abcX"));
+ BOOST_CHECK(!StringTools::startsWith("abc", "abc123"));
}
BOOST_AUTO_TEST_CASE(endsWith) {
- BOOST_CHECK(StringTools::endsWith("abc123", "123"));
- BOOST_CHECK(!StringTools::endsWith("abc123", "113"));
- BOOST_CHECK(!StringTools::endsWith("abc123", "X123"));
- BOOST_CHECK(!StringTools::endsWith("abc123", "123X"));
- BOOST_CHECK(!StringTools::endsWith("123", "abc123"));
+ BOOST_CHECK(StringTools::endsWith("abc123", "123"));
+ BOOST_CHECK(!StringTools::endsWith("abc123", "113"));
+ BOOST_CHECK(!StringTools::endsWith("abc123", "X123"));
+ BOOST_CHECK(!StringTools::endsWith("abc123", "123X"));
+ BOOST_CHECK(!StringTools::endsWith("123", "abc123"));
}
BOOST_AUTO_TEST_CASE(split) {
- BOOST_CHECK(StringTools::split("123 456 789", ' ') == vector<string>({"123", "456", "789"}));
- BOOST_CHECK(StringTools::split("aaa", ' ') == vector<string>({"aaa"}));
+ BOOST_CHECK(StringTools::split("123 456 789", ' ') == vector<string>({"123", "456", "789"}));
+ BOOST_CHECK(StringTools::split("aaa", ' ') == vector<string>({"aaa"}));
}
BOOST_AUTO_TEST_CASE(urlEncode) {
- string t = StringTools::urlEncode("`1234567890-qwertyuiop[]\\asdfghjkl;'zxcvbnm,./~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:ZXCVBNM<>? ");
- string e = "%601234567890-qwertyuiop%5B%5D%5Casdfghjkl%3B%27zxcvbnm%2C.%2F~%21%40%23%24%25%5E%26%2A%28%29_%2BQWERTYUIOP%7B%7D%7CASDFGHJKL:ZXCVBNM%3C%3E%3F%20";
- BOOST_CHECK_MESSAGE(t == e, diffS(t, e));
+ string t = StringTools::urlEncode("`1234567890-qwertyuiop[]\\asdfghjkl;'zxcvbnm,./~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:ZXCVBNM<>? ");
+ string e = "%601234567890-qwertyuiop%5B%5D%5Casdfghjkl%3B%27zxcvbnm%2C.%2F~%21%40%23%24%25%5E%26%2A%28%29_%2BQWERTYUIOP%7B%7D%7CASDFGHJKL:ZXCVBNM%3C%3E%3F%20";
+ BOOST_CHECK_MESSAGE(t == e, diffS(t, e));
}
BOOST_AUTO_TEST_CASE(urlDecode) {
- string t = StringTools::urlDecode("%601234567890-qwertyuiop%5b%5d%5casdfghjkl%3b%27zxcvbnm%2c.%2f~%21%40%23%24%25%5e%26%2a%28%29_%2bQWERTYUIOP%7b%7d%7cASDFGHJKL%3aZXCVBNM%3c%3e%3f%20");
- string e = "`1234567890-qwertyuiop[]\\asdfghjkl;'zxcvbnm,./~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:ZXCVBNM<>? ";
- BOOST_CHECK_MESSAGE(t == e, diffS(t, e));
+ string t = StringTools::urlDecode("%601234567890-qwertyuiop%5b%5d%5casdfghjkl%3b%27zxcvbnm%2c.%2f~%21%40%23%24%25%5e%26%2a%28%29_%2bQWERTYUIOP%7b%7d%7cASDFGHJKL%3aZXCVBNM%3c%3e%3f%20");
+ string e = "`1234567890-qwertyuiop[]\\asdfghjkl;'zxcvbnm,./~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:ZXCVBNM<>? ";
+ BOOST_CHECK_MESSAGE(t == e, diffS(t, e));
}
BOOST_AUTO_TEST_SUITE_END()
diff --git a/test/utils.h b/test/utils.h
index 93f64ff..e0db42b 100644
--- a/test/utils.h
+++ b/test/utils.h
@@ -26,79 +26,89 @@
#include <string>
#include <vector>
#include <sstream>
+#include <map>
+#include <unordered_map>
#include <boost/lexical_cast.hpp>
template<typename T>
inline std::string diff(const T& test, const T& expected, std::string (*toStringFunc)(const typename T::value_type&)) {
- std::string result;
- result += "\n*** BEGIN *** Count: t=";
- result += boost::lexical_cast<std::string>(test.size());
- result += " e=";
- result += boost::lexical_cast<std::string>(expected.size());
- result += '\n';
+ std::string result;
+ result += "\n*** BEGIN *** Count: t=";
+ result += boost::lexical_cast<std::string>(test.size());
+ result += " e=";
+ result += boost::lexical_cast<std::string>(expected.size());
+ result += '\n';
- typename T::const_iterator iter1 = test.begin();
- typename T::const_iterator end1 = test.end();
- typename T::const_iterator iter2 = expected.begin();
- typename T::const_iterator end2 = expected.end();
- bool r1, r2;
- std::string s1, s2;
- size_t i = 0;
- do {
- r1 = iter1 != end1;
- r2 = iter2 != end2;
- if (r1) {
- s1 = toStringFunc(*iter1++);
- }
- if (r2) {
- s2 = toStringFunc(*iter2++);
- }
- if (r1 && r2 && s1 == s2) {
- result += boost::lexical_cast<std::string>(i);
- result += " [=] ";
- result += s1;
- result += "\n";
- } else {
- if (r1) {
- result += boost::lexical_cast<std::string>(i);
- result += " [t] ";
- result += s1;
- result += "\n";
- }
- if (r2) {
- result += boost::lexical_cast<std::string>(i);
- result += " [e] ";
- result += s2;
- result += "\n";
- }
- }
- ++i;
- } while (r1 || r2);
+ typename T::const_iterator iter1 = test.begin();
+ typename T::const_iterator end1 = test.end();
+ typename T::const_iterator iter2 = expected.begin();
+ typename T::const_iterator end2 = expected.end();
+ bool r1, r2;
+ std::string s1, s2;
+ size_t i = 0;
+ do {
+ r1 = iter1 != end1;
+ r2 = iter2 != end2;
+ if (r1) {
+ s1 = toStringFunc(*iter1++);
+ }
+ if (r2) {
+ s2 = toStringFunc(*iter2++);
+ }
+ if (r1 && r2 && s1 == s2) {
+ result += boost::lexical_cast<std::string>(i);
+ result += " [=] ";
+ result += s1;
+ result += "\n";
+ } else {
+ if (r1) {
+ result += boost::lexical_cast<std::string>(i);
+ result += " [t] ";
+ result += s1;
+ result += "\n";
+ }
+ if (r2) {
+ result += boost::lexical_cast<std::string>(i);
+ result += " [e] ";
+ result += s2;
+ result += "\n";
+ }
+ }
+ ++i;
+ } while (r1 || r2);
- result += "*** END ***\n";
- return result;
+ result += "*** END ***\n";
+ return result;
}
inline std::string diffS(const std::string& test, const std::string& expected) {
- std::vector<std::string> v1, v2;
- std::istringstream ss1(test);
- std::istringstream ss2(expected);
- std::string s1, s2;
- bool r1, r2;
- do {
- r1 = std::getline(ss1, s1) ? true : false;
- r2 = std::getline(ss2, s2) ? true : false;
- if (r1) {
- v1.push_back(s1);
- }
- if (r2) {
- v2.push_back(s2);
- }
- } while (r1 || r2);
- return diff(v1, v2, [](const std::string& item) {
- return item;
- });
+ std::vector<std::string> v1, v2;
+ std::istringstream ss1(test);
+ std::istringstream ss2(expected);
+ std::string s1, s2;
+ bool r1, r2;
+ do {
+ r1 = std::getline(ss1, s1) ? true : false;
+ r2 = std::getline(ss2, s2) ? true : false;
+ if (r1) {
+ v1.push_back(s1);
+ }
+ if (r2) {
+ v2.push_back(s2);
+ }
+ } while (r1 || r2);
+ return diff(v1, v2, [](const std::string& item) {
+ return item;
+ });
+}
+
+inline std::string diffMSS(const std::unordered_map<std::string, std::string>& test, const std::unordered_map<std::string, std::string>& expected) {
+ std::map<std::string, std::string> v1(test.begin(), test.end());
+ std::map<std::string, std::string> v2(expected.begin(), expected.end());
+ return diff(v1, v2, [](const std::pair<const std::string, std::string>& item) {
+ return item.first + '=' + item.second;
+ });
}
#endif //TGBOT_UTILS_H