diff --git a/test/src/mbgl/test/http_server.cpp b/test/src/mbgl/test/http_server.cpp index 92c651b3751..25bb6cee3a1 100644 --- a/test/src/mbgl/test/http_server.cpp +++ b/test/src/mbgl/test/http_server.cpp @@ -9,6 +9,8 @@ #include #include +#include +#include using namespace std::literals::string_literals; @@ -19,6 +21,7 @@ void runServer(std::unique_ptr& server) { using namespace httplib; server->Get("/test", [](const Request& req, Response& res) { + std::string content = "Hello World!"; if (req.has_param("modified")) { std::string str = util::rfc1123(util::parseTimestamp(std::stoi(req.get_param_value("modified")))); res.set_header("Last-Modified", str); @@ -33,7 +36,15 @@ void runServer(std::unique_ptr& server) { if (req.has_param("cachecontrol")) { res.set_header("Cache-Control", "max-age=" + req.get_param_value("cachecontrol")); } - res.set_content("Hello World!", "text/plain"); + if (req.has_param("range")) { + std::string str = req.get_param_value("range"); + str = str.substr(std::char_traits::length("bytes=")); + uint64_t start = std::strtoull(str.substr(0, str.find("-")).c_str(), nullptr, 10); + uint64_t end = std::strtoull(str.substr(str.find("-") + 1).c_str(), nullptr, 10); + content = content.substr(start, end - start + 1); + res.status = 206; + } + res.set_content(content, "text/plain"); }); server->Get("/stale", [](const Request&, Response&) { diff --git a/test/storage/http_file_source.test.cpp b/test/storage/http_file_source.test.cpp index 68c5e890bdb..e17a9489a31 100644 --- a/test/storage/http_file_source.test.cpp +++ b/test/storage/http_file_source.test.cpp @@ -37,6 +37,27 @@ TEST(HTTPFileSource, TEST_REQUIRES_SERVER(HTTP200)) { loop.run(); } +TEST(HTTPFileSource, TEST_REQUIRES_SERVER(HTTP206)) { + util::RunLoop loop; + HTTPFileSource fs(ResourceOptions::Default(), ClientOptions()); + + Resource resource(Resource::Unknown, "http://127.0.0.1:3000/test"); + resource.dataRange = std::make_pair(3, 8); + + auto req = fs.request(resource, [&](Response res) { + EXPECT_EQ(nullptr, res.error); + ASSERT_TRUE(res.data.get()); + EXPECT_EQ("lo Wor", *res.data); + EXPECT_FALSE(bool(res.expires)); + EXPECT_FALSE(res.mustRevalidate); + EXPECT_FALSE(bool(res.modified)); + EXPECT_FALSE(bool(res.etag)); + loop.stop(); + }); + + loop.run(); +} + TEST(HTTPFileSource, TEST_REQUIRES_SERVER(HTTP404)) { util::RunLoop loop; HTTPFileSource fs(ResourceOptions::Default(), ClientOptions()); diff --git a/test/storage/local_file_source.test.cpp b/test/storage/local_file_source.test.cpp index c845d5bf8be..e0e6c211b4b 100644 --- a/test/storage/local_file_source.test.cpp +++ b/test/storage/local_file_source.test.cpp @@ -5,6 +5,7 @@ #include #include +#include #include #if defined(WIN32) @@ -25,7 +26,7 @@ std::string toAbsoluteURL(const std::string& fileName) { #else char* cwd = getcwd(buff, PATH_MAX + 1); #endif - std::string url = {"file://" + std::string(cwd) + "/test/fixtures/storage/assets/" + fileName}; + std::string url = {mbgl::util::FILE_PROTOCOL + std::string(cwd) + "/test/fixtures/storage/assets/" + fileName}; assert(url.size() <= PATH_MAX); return url; } @@ -76,6 +77,25 @@ TEST(LocalFileSource, NonEmptyFile) { loop.run(); } +TEST(LocalFileSource, PartialFile) { + util::RunLoop loop; + + LocalFileSource fs(ResourceOptions::Default(), ClientOptions()); + + Resource resource(Resource::Unknown, toAbsoluteURL("nonempty")); + resource.dataRange = std::make_pair(4, 12); + + std::unique_ptr req = fs.request(resource, [&](Response res) { + req.reset(); + EXPECT_EQ(nullptr, res.error); + ASSERT_TRUE(res.data.get()); + EXPECT_EQ("ent is he", *res.data); + loop.stop(); + }); + + loop.run(); +} + TEST(LocalFileSource, NonExistentFile) { util::RunLoop loop; diff --git a/test/storage/server.js b/test/storage/server.js index a7de0944d4a..04ccc430bfc 100755 --- a/test/storage/server.js +++ b/test/storage/server.js @@ -15,6 +15,7 @@ var app = express(); app.disable('etag'); app.get('/test', function (req, res) { + var content = 'Hello World!'; if (req.query.modified) { res.setHeader('Last-Modified', (new Date(req.query.modified * 1000)).toUTCString()); } @@ -27,7 +28,12 @@ app.get('/test', function (req, res) { if (req.query.cachecontrol) { res.setHeader('Cache-Control', req.query.cachecontrol); } - res.send('Hello World!'); + if (req.range()) { + const [ range ] = req.range(); + content = content.substring(range.start, range.end + 1); + res.status(206); + } + res.send(content); }); app.get('/stale/*', function() {