summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorKonstantin Käfer <mail@kkaefer.com>2014-09-30 17:28:42 +0200
committerKonstantin Käfer <mail@kkaefer.com>2014-09-30 17:29:10 +0200
commit8fcc74e2531f53b6e09584fb58022f852a32b26c (patch)
treed2575142d5d82a32f03d2ce9ad3f501e8bc8bb76 /test
parenta9039cf5d75f4da667d9279d61f7549c43f7ea51 (diff)
downloadqtlocation-mapboxgl-8fcc74e2531f53b6e09584fb58022f852a32b26c.tar.gz
fix headless tests
Diffstat (limited to 'test')
-rw-r--r--test/fixtures/fixture_request.cpp115
1 files changed, 37 insertions, 78 deletions
diff --git a/test/fixtures/fixture_request.cpp b/test/fixtures/fixture_request.cpp
index 7e351ecfac..3f72b890db 100644
--- a/test/fixtures/fixture_request.cpp
+++ b/test/fixtures/fixture_request.cpp
@@ -1,9 +1,15 @@
-#include <mbgl/platform/platform.hpp>
-#include <mbgl/platform/request.hpp>
-#include <mbgl/util/uv_detail.hpp>
+#include <mbgl/storage/http_request_baton.hpp>
+#include <mbgl/storage/file_request_baton.hpp>
+#include <mbgl/storage/response.hpp>
#include <mbgl/util/url.hpp>
+#include <mbgl/util/std.hpp>
#include <mbgl/platform/log.hpp>
+#include <uv.h>
+
+#include <cassert>
+
+
const std::string base_directory = []{
std::string fn = __FILE__;
fn.erase(fn.find_last_of("/"));
@@ -12,95 +18,48 @@ const std::string base_directory = []{
return fn + "/node_modules/mapbox-gl-test-suite/";
}();
-
namespace mbgl {
-std::shared_ptr<platform::Request>
-platform::request_http(const std::string &url,
- std::function<void(Response *)> callback,
- std::shared_ptr<uv::loop> loop) {
- uv_loop_t *l = nullptr;
- if (loop) {
- l = **loop;
- } else {
- l = uv_default_loop();
- }
+void HTTPRequestBaton::start(const util::ptr<HTTPRequestBaton> &baton) {
+ assert(uv_thread_self() == baton->thread_id);
- std::string clean_url = util::percentDecode(url);
+ std::string clean_url = util::percentDecode(baton->path);
if (clean_url.find("local://") == 0) {
clean_url = base_directory + clean_url.substr(8);
}
- std::shared_ptr<Request> req = std::make_shared<Request>(url, callback, loop);
-
- int err;
-
- uv_fs_t open_req;
- err = uv_fs_open(l, &open_req, clean_url.c_str(), O_RDONLY, S_IRUSR, nullptr);
- uv_fs_req_cleanup(&open_req);
- if (err < 0) {
- req->res->code = err;
- req->res->error_message = uv_strerror(err);
- Log::Warning(Event::HttpRequest, err, url + ": " + uv_strerror(err));
- req->complete();
- return req;
- }
- uv_file fd = err;
-
- uv_fs_t stat_req;
- uv_fs_fstat(l, &stat_req, fd, nullptr);
- uv_fs_req_cleanup(&stat_req);
- err = uv_fs_fstat(l, &stat_req, fd, nullptr);
- if (err < 0) {
- req->res->code = err;
- req->res->error_message = uv_strerror(err);
- Log::Warning(Event::HttpRequest, err, url + ": " + uv_strerror(err));
- req->complete();
- return req;
- }
-
- const uint64_t size = static_cast<const uv_stat_t*>(stat_req.ptr)->st_size;
-
-
- std::string body;
- body.resize(size);
- uv_buf_t uvbuf = uv_buf_init(const_cast<char *>(body.data()), body.size());
-
- uv_fs_t read_req;
- err = uv_fs_read(l, &read_req, fd, &uvbuf, 1, 0, nullptr);
- uv_fs_req_cleanup(&read_req);
- if (err < 0) {
- req->res->code = err;
- req->res->error_message = uv_strerror(err);
- Log::Warning(Event::HttpRequest, err, url + ": " + uv_strerror(err));
- req->complete();
- return req;
+ baton->response = std::make_unique<Response>();
+ FILE *file = fopen(clean_url.c_str(),"rb");
+ if (file != NULL) {
+ fseek(file, 0, SEEK_END);
+ const size_t size = ftell(file);
+ fseek(file, 0, SEEK_SET);
+ baton->response->data.resize(size);
+ fread(&baton->response->data[0], size, 1, file);
+ fclose(file);
+
+ baton->response->code = 200;
+ baton->type = HTTPResponseType::Successful;
+ } else {
+ baton->type = HTTPResponseType::PermanentError;
+ baton->response->code = 404;
}
+ uv_async_send(baton->async);
+}
- uv_fs_t close_req;
- err = uv_fs_close(l, &close_req, fd, nullptr);
- uv_fs_req_cleanup(&close_req);
- if (err < 0) {
- req->res->code = err;
- req->res->error_message = uv_strerror(err);
- Log::Warning(Event::HttpRequest, err, url + ": " + uv_strerror(err));
- req->complete();
- return req;
- }
+void HTTPRequestBaton::stop(const util::ptr<HTTPRequestBaton> &baton) {
+ fprintf(stderr, "HTTP request cannot be canceled because it is answered immediately");
+ abort();
+}
- req->res->body.swap(body);
- req->res->code = 200;
- Log::Info(Event::HttpRequest, 200, url);
- req->complete();
+namespace platform {
- return req;
+std::string defaultCacheDatabase() {
+ // Disables the cache.
+ return "";
}
-void platform::cancel_request_http(const std::shared_ptr<Request> &req) {
- if (req) {
- req->cancelled = true;
- }
}
}