summaryrefslogtreecommitdiff
path: root/test/tgbot
diff options
context:
space:
mode:
authorOleg Morozenkov <omorozenkov@gmail.com>2015-07-09 15:03:59 +0300
committerOleg Morozenkov <omorozenkov@gmail.com>2015-07-09 15:03:59 +0300
commit51d2176d1535c8c8176426909e1c7b70633d794b (patch)
tree4301e0bb6f93913c53d4f41bdd96ddf1fcff9221 /test/tgbot
parent28ef71aa3c6d9473d4127dd648161a0afa109895 (diff)
Refactoring
Diffstat (limited to 'test/tgbot')
-rw-r--r--test/tgbot/net/HttpParser.cpp136
-rw-r--r--test/tgbot/net/Url.cpp58
-rw-r--r--test/tgbot/tools/StringTools.cpp63
3 files changed, 257 insertions, 0 deletions
diff --git a/test/tgbot/net/HttpParser.cpp b/test/tgbot/net/HttpParser.cpp
new file mode 100644
index 0000000..57ba187
--- /dev/null
+++ b/test/tgbot/net/HttpParser.cpp
@@ -0,0 +1,136 @@
+/*
+ * Copyright (c) 2015 Oleg Morozenkov
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <boost/test/unit_test.hpp>
+
+#include <tgbot/net/HttpParser.h>
+
+#include "utils.h"
+
+using namespace std;
+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, diff(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\"\r\n"
+ "Content-Type: text/plain\r\n"
+ "\r\n"
+ "Hello, world!\r\n";
+ BOOST_CHECK_MESSAGE(t == e, diff(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, diff(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, diff(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";
+
+ map<string, string> tHeaders;
+ string t = HttpParser::getInstance().parseRequest(data, tHeaders);
+
+ 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, diff(t, e));
+ BOOST_CHECK_MESSAGE(tHeaders == eHeaders, diff(tHeaders, eHeaders, [](const pair<const string, string>& item) -> string {
+ return item.first + '=' + item.second;
+ }));
+}
+
+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";
+
+ map<string, string> tHeaders;
+ string t = HttpParser::getInstance().parseResponse(data, tHeaders);
+
+ map<string, string> eHeaders = {
+ { "status", "200" },
+ { "content-type", "text/plain" },
+ { "content-length", "8" }
+ };
+ string e = "testdata";
+
+ BOOST_CHECK_MESSAGE(t == e, diff(t, e));
+ BOOST_CHECK_MESSAGE(tHeaders == eHeaders, diff(tHeaders, eHeaders, [](const pair<const string, string>& item) -> string {
+ return item.first + '=' + item.second;
+ }));
+}
+
+BOOST_AUTO_TEST_SUITE_END()
diff --git a/test/tgbot/net/Url.cpp b/test/tgbot/net/Url.cpp
new file mode 100644
index 0000000..0974ab6
--- /dev/null
+++ b/test/tgbot/net/Url.cpp
@@ -0,0 +1,58 @@
+/*
+ * Copyright (c) 2015 Oleg Morozenkov
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <boost/test/unit_test.hpp>
+
+#include <tgbot/net/Url.h>
+
+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");
+}
+
+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");
+}
+
+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");
+}
+
+BOOST_AUTO_TEST_SUITE_END()
diff --git a/test/tgbot/tools/StringTools.cpp b/test/tgbot/tools/StringTools.cpp
new file mode 100644
index 0000000..c3bf709
--- /dev/null
+++ b/test/tgbot/tools/StringTools.cpp
@@ -0,0 +1,63 @@
+/*
+ *
+ * Copyright (c) 2015 Oleg Morozenkov
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <string>
+#include <vector>
+
+#include <boost/test/unit_test.hpp>
+
+#include <tgbot/tools/StringTools.h>
+
+#include "utils.h"
+
+using namespace std;
+
+BOOST_AUTO_TEST_SUITE(tStringTools)
+
+BOOST_AUTO_TEST_CASE(startsWith) {
+ BOOST_CHECK(StringTools::startsWith("abc123", "abc"));
+ BOOST_CHECK(StringTools::startsWith("abc", "abc123"));
+}
+
+BOOST_AUTO_TEST_CASE(endsWith) {
+ BOOST_CHECK(StringTools::endsWith("abc123", "123"));
+ BOOST_CHECK(StringTools::endsWith("123", "abc123"));
+}
+
+BOOST_AUTO_TEST_CASE(split) {
+ BOOST_CHECK(StringTools::split("123 456 789", ' ') == vector<string>({"123", "456", "789"}));
+}
+
+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%3aZXCVBNM%3c%3e%3f%20";
+ BOOST_CHECK_MESSAGE(t == e, diff(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, diff(t, e));
+}
+
+BOOST_AUTO_TEST_SUITE_END()