summaryrefslogtreecommitdiff
path: root/test/fixtures/fixture_request.cpp
blob: cc01760ae110ff47e4288b2b0325134365642c65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#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 <iostream>

#include <uv.h>

#include <cassert>


const std::string base_directory = []{
    std::string fn = __FILE__;
    fn.erase(fn.find_last_of("/"));
    fn.erase(fn.find_last_of("/"));
    fn.erase(fn.find_last_of("/"));
    return fn + "/test/suite/";
}();

namespace mbgl {

void HTTPRequestBaton::start(const util::ptr<HTTPRequestBaton> &baton) {
    assert(uv_thread_self() == baton->thread_id);

    std::string clean_url = util::percentDecode(baton->path);
    if (clean_url.find("local://") == 0) {
        clean_url = base_directory + clean_url.substr(8);
    }

    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);
        const size_t read = fread(&baton->response->data[0], 1, size, file);
        fclose(file);

        if (read == size) {
            baton->response->code = 200;
            baton->type = HTTPResponseType::Successful;
        } else {
            baton->response->code = 500;
            baton->type = HTTPResponseType::PermanentError;
            baton->response->message = "Read bytes differed from file size";
        }
    } else {
        baton->type = HTTPResponseType::PermanentError;
        baton->response->code = 404;
    }

    uv_async_send(baton->async);
}

void HTTPRequestBaton::stop(const util::ptr<HTTPRequestBaton> &/*baton*/) {
    fprintf(stderr, "HTTP request cannot be canceled because it is answered immediately");
    abort();
}

namespace platform {

std::string defaultCacheDatabase() {
    // Disables the cache.
    return "";
}

}

}