Skip to content

Commit

Permalink
Unit tests for partial file requests
Browse files Browse the repository at this point in the history
  • Loading branch information
tdcosta100 committed Oct 21, 2024
1 parent 285e1a7 commit 8554ab9
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
12 changes: 11 additions & 1 deletion test/src/mbgl/test/http_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <httplib.h>

#include <atomic>
#include <cstdint>
#include <string>

using namespace std::literals::string_literals;

Expand All @@ -19,6 +21,7 @@ void runServer(std::unique_ptr<httplib::Server>& 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);
Expand All @@ -33,7 +36,14 @@ void runServer(std::unique_ptr<httplib::Server>& 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<char>::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.set_content(content, "text/plain");
});

server->Get("/stale", [](const Request&, Response&) {
Expand Down
21 changes: 21 additions & 0 deletions test/storage/http_file_source.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint64_t, uint64_t>(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());
Expand Down
22 changes: 21 additions & 1 deletion test/storage/local_file_source.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <mbgl/util/run_loop.hpp>

#include <climits>
#include <cstdint>
#include <gtest/gtest.h>

#if defined(WIN32)
Expand All @@ -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;
}
Expand Down Expand Up @@ -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<uint64_t, uint64_t>(4, 12);

std::unique_ptr<AsyncRequest> 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;

Expand Down

0 comments on commit 8554ab9

Please sign in to comment.