summaryrefslogtreecommitdiff
path: root/test/tgbot/net
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/tgbot/net
parentcea20d4078f2088dea0dd589f1cc9dd7ee22461b (diff)
Refactor http clients, fix webhook server, add more samples, change tabs to 4 spaces
Diffstat (limited to 'test/tgbot/net')
-rw-r--r--test/tgbot/net/HttpParser.cpp167
-rw-r--r--test/tgbot/net/Url.cpp36
2 files changed, 100 insertions, 103 deletions
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()