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 26, 2024
1 parent 4495c20 commit cd32d69
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 3 deletions.
13 changes: 12 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,15 @@ 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.status = 206;
}
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
8 changes: 7 additions & 1 deletion test/storage/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -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() {
Expand Down

0 comments on commit cd32d69

Please sign in to comment.