summaryrefslogtreecommitdiff
path: root/test/storage/http_environment.cpp
blob: efd7b78ad4cedc07806c857e76ebe2761ca45f4e (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
#include "storage.hpp"

#include <uv.h>

#include <mbgl/storage/default_file_source.hpp>
#include <mbgl/storage/network_status.hpp>

#include <cmath>

TEST_F(Storage, HTTPCancelEnvironment) {
    SCOPED_TEST(HTTPRetainedEnvironment)
    SCOPED_TEST(HTTPCanceledEnvironment)

    using namespace mbgl;

    DefaultFileSource fs(nullptr, uv_default_loop());

    // Create two fake environment pointers. The FileSource implementation treats these as opaque
    // pointers and doesn't reach into them.
    auto &env1 = *reinterpret_cast<const Environment *>(1);
    auto &env2 = *reinterpret_cast<const Environment *>(2);

    const Resource resource { Resource::Unknown, "http://127.0.0.1:3000/delayed" };

    // Environment 1
    fs.request(resource, uv_default_loop(), env1, [&](const Response &res) {
        // This environment gets aborted below. This means the request is marked as failing and
        // will return an error here.
        EXPECT_EQ(Response::Error, res.status);
        EXPECT_EQ("", res.data);
        EXPECT_EQ(0, res.expires);
        EXPECT_EQ(0, res.modified);
        EXPECT_EQ("", res.etag);
        EXPECT_EQ("Environment is terminating", res.message);
        HTTPCanceledEnvironment.finish();
    });

    // Environment 2
    fs.request(resource, uv_default_loop(), env2, [&](const Response &res) {
        // The same request as above, but in a different environment which doesn't get aborted. This
        // means the request should succeed.
        EXPECT_EQ(Response::Successful, res.status);
        EXPECT_EQ("Response", res.data);
        EXPECT_EQ(0, res.expires);
        EXPECT_EQ(0, res.modified);
        EXPECT_EQ("", res.etag);
        EXPECT_EQ("", res.message);
        HTTPRetainedEnvironment.finish();
    });

    fs.abort(env1);

    uv_run(uv_default_loop(), UV_RUN_DEFAULT);
}