diff options
author | Ivo van Dongen <info@ivovandongen.nl> | 2016-07-05 21:43:13 +0200 |
---|---|---|
committer | Ivo van Dongen <info@ivovandongen.nl> | 2016-07-07 10:08:46 +0200 |
commit | 97cd15932adf0168b4e026a340e51c392ae819bb (patch) | |
tree | c3343bd3efca362cf816fdc4e45c20ace36dec75 | |
parent | e804523613b4a4131129169643674746b388197f (diff) | |
download | qtlocation-mapboxgl-97cd15932adf0168b4e026a340e51c392ae819bb.tar.gz |
[android] #5456 - extracted server in separate source files
-rw-r--r-- | test/src/mbgl/test/server.cpp | 105 | ||||
-rw-r--r-- | test/src/mbgl/test/server.hpp | 27 | ||||
-rw-r--r-- | test/src/mbgl/test/test.cpp | 25 | ||||
-rw-r--r-- | test/src/mbgl/test/util.cpp | 97 | ||||
-rw-r--r-- | test/src/mbgl/test/util.hpp | 20 | ||||
-rw-r--r-- | test/test.gypi | 2 |
6 files changed, 150 insertions, 126 deletions
diff --git a/test/src/mbgl/test/server.cpp b/test/src/mbgl/test/server.cpp new file mode 100644 index 0000000000..38fa031901 --- /dev/null +++ b/test/src/mbgl/test/server.cpp @@ -0,0 +1,105 @@ +#include <mbgl/test/server.hpp> +#include <mbgl/platform/log.hpp> +#include <iostream> +#include <future> + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wshadow" +#pragma GCC diagnostic ignored "-Wformat" +#include "httplib.h" +#pragma GCC diagnostic pop + +#include <memory> + +namespace mbgl { +namespace test { + +std::string dumpHeaders(const httplib::MultiMap& headers) { + std::string s; + char buf[BUFSIZ]; + + for (auto it = headers.begin(); it != headers.end(); ++it) { + const auto& x = *it; + snprintf(buf, sizeof(buf), "%s: %s\n", x.first.c_str(), x.second.c_str()); + s += buf; + } + + return s; +} + +Server::Server(bool) { + svr = std::make_unique<httplib::Server>(); + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-parameter" + + svr->get("/test", [](const auto& req, auto& res) { + std::cout << "test\n"; + if(req.has_param("modified")) { + //res.setHeader('Last-Modified', (new Date(req.query.modified * 1000)).toUTCString()); + std::cout << "Modified " << req.params.find("modified")->second; + } + if (req.has_param("expires")) { + //res.setHeader('Expires', (new Date(req.query.expires * 1000)).toUTCString()); + std::cout << "Exipres " << req.params.find("expires")->second; + } + if (req.has_param("etag")) { + std::cout << "etag " << req.params.find("etag")->second; + res.set_header("ETag", req.params.find("etag")->second.c_str()); + } + if (req.has_param("cachecontrol")) { + std::cout << "cachecontrol " << req.params.find("cachecontrol")->second; + res.set_header("Cache-Control", req.params.find("cachecontrol")->second.c_str()); + } + res.set_content("Hello World!", "text/plain"); + }); + + svr->get("/stale", [] (const auto& req, auto& res) { + std::cout << "stale " << req.url << "\n"; + //TODO: Don't respond + }); + + svr->get("/cache", [](const auto& req, auto& res) { + static int cacheCounter; + res.set_header("Cache-Control", "max-age=30"); + res.set_content("Response " + std::to_string(++cacheCounter), "text/plain"); + }); + + svr->get("/revalidate-same", [](const auto& req, auto& res) { + std::cout << "revalidate same\n"; + std::cout << dumpHeaders(req.headers) << "\n\n"; + if (req.has_header("if-non-match") && req.headers.find("if-none-match")->second == "snowfall") { + // Second request can be cached for 30 seconds. + res.set_header("Cache-Control", "max-age=30"); + res.status = 304; + } else { + // First request must always be revalidated. + res.set_header("ETag", "snowfall"); + res.set_header("Cache-Control", "must-revalidate"); + res.status = 200; + res.set_content("Response", "text/plain"); + } + }); +#pragma GCC diagnostic pop +} + +Server::~Server() { + svr->stop(); +} + +void Server::start() { + const int port = 3000; + std::cout << "Starting server on port: " << port << "\n"; + f_ = std::async(std::launch::async, [&](){ + up_ = true; + svr->listen("127.0.0.1", port); + }); + + while (!up_) { + usleep(1000); + } + std::cout << "Server should be up\n"; +} + +} // namespace test +} // namespace mbgl diff --git a/test/src/mbgl/test/server.hpp b/test/src/mbgl/test/server.hpp new file mode 100644 index 0000000000..4b36abcee0 --- /dev/null +++ b/test/src/mbgl/test/server.hpp @@ -0,0 +1,27 @@ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wshadow" +#pragma GCC diagnostic ignored "-Wformat" +#include "httplib.h" +#pragma GCC diagnostic pop + +#include <memory> +#include <future> + +namespace mbgl { + +class Map; + +namespace test { + +class Server { +public: + Server(bool); + ~Server(); + void start(); +private: + std::unique_ptr<httplib::Server> svr; + std::future<void> f_; + bool up_ = false; +}; +} // namespace test +} // namespace mbgl diff --git a/test/src/mbgl/test/test.cpp b/test/src/mbgl/test/test.cpp index 6949d2fb90..db123ebedf 100644 --- a/test/src/mbgl/test/test.cpp +++ b/test/src/mbgl/test/test.cpp @@ -1,22 +1,29 @@ #include <mbgl/test.hpp> #include <mbgl/test/util.hpp> +#include <mbgl/test/server.hpp> #include <mbgl/util/thread.hpp> #include <gtest/gtest.h> namespace mbgl { -int runTests(int argc, char *argv[]) { + int runTests(int argc, char *argv[]) { #if TEST_HAS_SERVER - std::cout << "Starting server\n"; - util::Thread<test::Server> server({"Webserver"}, true); - server.invokeSync(&test::Server::start); + std::cout << "Starting server\n"; + util::Thread<test::Server> server({"Webserver"}, true); + server.invokeSync(&test::Server::start); - //Sleep so we know the server actually started - usleep(5 * 1000 * 1000); + //Make sure the server is up before continueing + httplib::Client cli("127.0.0.1", 3000); + int resCode = cli.get("/test")->status; + while(resCode !=200) { + std::cout << "No response yet\n"; + usleep(1000*1000); + resCode = cli.get("/test")->status; + } #endif - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); + } } // namespace mbgl diff --git a/test/src/mbgl/test/util.cpp b/test/src/mbgl/test/util.cpp index 644b848519..8eebe64028 100644 --- a/test/src/mbgl/test/util.cpp +++ b/test/src/mbgl/test/util.cpp @@ -10,109 +10,12 @@ #include <mapbox/pixelmatch.hpp> #include <csignal> -#include <future> #include <unistd.h> -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wshadow" -#pragma GCC diagnostic ignored "-Wformat" -#include "httplib.h" -#pragma GCC diagnostic pop - -#include <memory> - namespace mbgl { namespace test { -std::string dumpHeaders(const httplib::MultiMap& headers) { - std::string s; - char buf[BUFSIZ]; - - for (auto it = headers.begin(); it != headers.end(); ++it) { - const auto& x = *it; - snprintf(buf, sizeof(buf), "%s: %s\n", x.first.c_str(), x.second.c_str()); - s += buf; - } - - return s; -} - -Server::Server(bool) { - svr = std::make_unique<httplib::Server>(); - -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wunused-parameter" - - svr->get("/test", [](const auto& req, auto& res) { - std::cout << "test\n"; - if(req.has_param("modified")) { - //res.setHeader('Last-Modified', (new Date(req.query.modified * 1000)).toUTCString()); - std::cout << "Modified " << req.params.find("modified")->second; - } - if (req.has_param("expires")) { - //res.setHeader('Expires', (new Date(req.query.expires * 1000)).toUTCString()); - std::cout << "Exipres " << req.params.find("expires")->second; - } - if (req.has_param("etag")) { - std::cout << "etag " << req.params.find("etag")->second; - res.set_header("ETag", req.params.find("etag")->second.c_str()); - } - if (req.has_param("cachecontrol")) { - std::cout << "cachecontrol " << req.params.find("cachecontrol")->second; - res.set_header("Cache-Control", req.params.find("cachecontrol")->second.c_str()); - } - res.set_content("Hello World!", "text/plain"); - }); - - svr->get("/stale", [] (const auto& req, auto& res) { - std::cout << "stale " << req.url << "\n"; - //TODO: Don't respond - }); - - svr->get("/cache", [](const auto& req, auto& res) { - static int cacheCounter; - res.set_header("Cache-Control", "max-age=30"); - res.set_content("Response " + std::to_string(++cacheCounter), "text/plain"); - }); - - svr->get("/revalidate-same", [](const auto& req, auto& res) { - std::cout << "revalidate same\n"; - std::cout << dumpHeaders(req.headers) << "\n\n"; - if (req.has_header("if-non-match") && req.headers.find("if-none-match")->second == "snowfall") { - // Second request can be cached for 30 seconds. - res.set_header("Cache-Control", "max-age=30"); - res.status = 304; - } else { - // First request must always be revalidated. - res.set_header("ETag", "snowfall"); - res.set_header("Cache-Control", "must-revalidate"); - res.status = 200; - res.set_content("Response", "text/plain"); - } - }); -#pragma GCC diagnostic pop -} - -Server::~Server() { - svr->stop(); -} - -void Server::start() { - std::cout << "Starting server on port 3000\n"; - //svr->listen("localhost", 3000); - //std::cout << "Stopped!!\n"; - f_ = std::async(std::launch::async, [&](){ - up_ = true; - svr->listen("localhost", 3000); - }); - - while (!up_) { - usleep(1000); - } - std::cout << "Server should be up\n"; -} - PremultipliedImage render(Map& map) { PremultipliedImage result; map.renderStill([&result](std::exception_ptr, PremultipliedImage&& image) { diff --git a/test/src/mbgl/test/util.hpp b/test/src/mbgl/test/util.hpp index f658a341f1..6cea6d39fc 100644 --- a/test/src/mbgl/test/util.hpp +++ b/test/src/mbgl/test/util.hpp @@ -50,32 +50,12 @@ ~name() { if (!completed) ADD_FAILURE() << #name " didn't complete."; } \ } name; -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wshadow" -#pragma GCC diagnostic ignored "-Wformat" -#include "httplib.h" -#pragma GCC diagnostic pop - -#include <memory> -#include <future> - namespace mbgl { class Map; namespace test { -class Server { -public: - Server(bool); - ~Server(); - void start(); -private: - std::unique_ptr<httplib::Server> svr; - std::future<void> f_; - bool up_ = false; -}; - PremultipliedImage render(Map&); void checkImage(const std::string& base, diff --git a/test/test.gypi b/test/test.gypi index 57d7412f22..03aece517c 100644 --- a/test/test.gypi +++ b/test/test.gypi @@ -90,6 +90,8 @@ 'src/mbgl/test/stub_file_source.cpp', 'src/mbgl/test/util.hpp', 'src/mbgl/test/util.cpp', + 'src/mbgl/test/server.hpp', + 'src/mbgl/test/server.cpp', 'src/mbgl/test/fixture_log_observer.hpp', 'src/mbgl/test/fixture_log_observer.cpp', 'src/mbgl/test/test.cpp' |