From 20852243764c5e33a9facb613227054c513e4be9 Mon Sep 17 00:00:00 2001 From: Ivo van Dongen Date: Thu, 7 Jul 2016 10:54:32 +0200 Subject: [android] #5456 - ported the nodejs test server to cpp --- test/src/mbgl/test/server.cpp | 109 ++++++++++++++++++++++++++++-------- test/storage/http_file_source.cpp | 2 +- test/storage/online_file_source.cpp | 2 +- 3 files changed, 88 insertions(+), 25 deletions(-) diff --git a/test/src/mbgl/test/server.cpp b/test/src/mbgl/test/server.cpp index cc6f01a31..f2bebc15c 100644 --- a/test/src/mbgl/test/server.cpp +++ b/test/src/mbgl/test/server.cpp @@ -1,7 +1,10 @@ #include #include #include +#include +#include #include +#include #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wshadow" @@ -23,19 +26,6 @@ std::string timestampToISO(const time_t rawtime){ return std::string(buffer); } -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(); @@ -43,16 +33,8 @@ Server::Server(bool) { #pragma GCC diagnostic ignored "-Wunused-parameter" svr->get("/test", [](const auto& req, auto& res) { - //XXX Remove - std::cout << "test " << req.url << "\n"; - for (auto it = req.params.begin(); it != req.params.end(); ++it) { - const auto& x = *it; - std::cout << "Param: " << x.first << " - " << x.second << " / " << req.has_param(x.first.c_str()) << "\n"; - } - if(req.has_param("modified")) { res.set_header("Last-Modified", timestampToISO(std::stoi(req.params.at("modified"))).c_str()); - std::cout << "Modified " << timestampToISO(std::stoi(req.params.at("modified"))); } if (req.has_param("expires")) { res.set_header("Expires", timestampToISO(std::stoi(req.params.at("expires"))).c_str()); @@ -67,8 +49,7 @@ Server::Server(bool) { }); svr->get("/stale", [] (const auto& req, auto& res) { - std::cout << "stale " << req.url << "\n"; - //TODO: Don't respond + //Don't respond }); svr->get("/cache", [](const auto& req, auto& res) { @@ -90,6 +71,88 @@ Server::Server(bool) { res.set_content("Response", "text/plain"); } }); + + svr->get("/clockskew", [](const auto& req, auto& res) { + static int expiresCounter = 0; + char dateString[25]; + sprintf(dateString, "Feb 01 2010 10:0%d:00", ++expiresCounter); + res.set_header("Expires", dateString); + res.status = 200; + res.set_content("Response", "text/plain"); + }); + + svr->get("/revalidate-modified", [](const auto& req, auto& res) { + Timestamp jan1 = util::parseTimestamp("Jan 01 2015"); + + if (req.has_header("If-Modified-Since")) { + Timestamp modified_since = util::parseTimestamp(req.headers.find("If-Modified-Since")->second.c_str()); + if (modified_since >= jan1) { + res.set_header("Cache-Control", "max-age=30"); + res.status = 304; + return; + } + } + + // First request must always be revalidated. + res.set_header("Last-Modified", timestampToISO(std::chrono::system_clock::to_time_t(jan1)).c_str()); + res.set_header("Cache-Control", "must-revalidate"); + res.status = 200; + res.set_content("Response", "text/plain"); + }); + + + svr->get("/revalidate-etag", [](const auto& req, auto& res) { + static int revalidateEtagCounter = 1; + res.set_header("ETag", std::string("response-").append(std::to_string(revalidateEtagCounter)).c_str()); + res.set_header("Cache-Control", "must-revalidate"); + + res.status = 200; + res.set_content(std::string("Response ").append(std::to_string(revalidateEtagCounter)), "text/plain"); + revalidateEtagCounter++; + }); + + + svr->get("/empty-data", [](const auto& req, auto& res) { + res.status = 200; + }); + + svr->get("/no-content", [](const auto& req, auto& res) { + res.status = 204; + }); + + svr->get("/not-found", [](const auto& req, auto& res) { + res.status = 404; + res.set_content("Not Found", "text/plain"); + }); + + svr->get("/permanent-error", [](const auto& req, auto& res) { + res.status = 500; + res.set_content("Server Error", "text/plain"); + }); + + svr->get("/temporary-error", [](const auto& req, auto& res) { + static int temporaryErrorCounter = 0; + if (temporaryErrorCounter == 0) { + res.status = 500; + } else { + res.status = 200; + res.set_content("Hello World!", "text/plain"); + } + + temporaryErrorCounter++; + }); + + svr->get("/delayed", [](const auto& req, auto& res) { + std::this_thread::sleep_for(std::chrono::milliseconds(200)); + res.status = 200; + res.set_content("Response", "text/plain"); + }); + + svr->get(R"(/load/(\d+))", [](const auto& req, auto& res) { + std::string number = req.matches.str(1); + res.set_content(std::string("Request ").append(number), "text/plain"); + }); + #pragma GCC diagnostic pop } diff --git a/test/storage/http_file_source.cpp b/test/storage/http_file_source.cpp index 5b081d7d5..7f8a02fbb 100644 --- a/test/storage/http_file_source.cpp +++ b/test/storage/http_file_source.cpp @@ -143,7 +143,7 @@ TEST(HTTPFileSource, TEST_REQUIRES_SERVER(CacheControlParsing)) { util::RunLoop loop; HTTPFileSource fs; - auto req = fs.request({ Resource::Unknown, "http://127.0.0.1:3000/test?cachecontrol=max-age=120" }, [&](Response res) { + auto req = fs.request({ Resource::Unknown, "http://127.0.0.1:3000/test?cachecontrol=max-age%3D120" }, [&](Response res) { EXPECT_EQ(nullptr, res.error); ASSERT_TRUE(res.data.get()); EXPECT_EQ("Hello World!", *res.data); diff --git a/test/storage/online_file_source.cpp b/test/storage/online_file_source.cpp index 18179c844..c4d9d3fc1 100644 --- a/test/storage/online_file_source.cpp +++ b/test/storage/online_file_source.cpp @@ -118,7 +118,7 @@ TEST(OnlineFileSource, TEST_REQUIRES_SERVER(Timeout)) { int counter = 0; - const Resource resource { Resource::Unknown, "http://127.0.0.1:3000/test?cachecontrol=max-age=1" }; + const Resource resource { Resource::Unknown, "http://127.0.0.1:3000/test?cachecontrol=max-age%3D1" }; std::unique_ptr req = fs.request(resource, [&](Response res) { counter++; EXPECT_EQ(nullptr, res.error); -- cgit v1.2.3