summaryrefslogtreecommitdiff
path: root/test/storage/cache_response.cpp
blob: aa5e5dbfcfa9c627bc8b5413c03a248ae3ade74f (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include "storage.hpp"

#include <mbgl/storage/online_file_source.hpp>
#include <mbgl/storage/sqlite_cache.hpp>
#include <mbgl/util/chrono.hpp>
#include <mbgl/util/run_loop.hpp>

TEST_F(Storage, CacheResponse) {
    SCOPED_TEST(CacheResponse);

    using namespace mbgl;

    util::RunLoop loop;
    SQLiteCache cache(":memory:");
    OnlineFileSource fs(&cache);

    const Resource resource { Resource::Unknown, "http://127.0.0.1:3000/cache" };
    Response response;

    std::unique_ptr<FileRequest> req1;
    std::unique_ptr<FileRequest> req2;

    req1 = fs.request(resource, [&](Response res) {
        req1.reset();
        EXPECT_EQ(nullptr, res.error);
        ASSERT_TRUE(res.data.get());
        EXPECT_EQ("Response 1", *res.data);
        EXPECT_TRUE(bool(res.expires));
        EXPECT_FALSE(bool(res.modified));
        EXPECT_FALSE(bool(res.etag));
        response = res;

        // Now test that we get the same values as in the previous request. If we'd go to the server
        // again, we'd get different values.
        req2 = fs.request(resource, [&](Response res2) {
            req2.reset();
            EXPECT_EQ(response.error, res2.error);
            ASSERT_TRUE(res2.data.get());
            EXPECT_EQ(*response.data, *res2.data);
            EXPECT_EQ(response.expires, res2.expires);
            EXPECT_EQ(response.modified, res2.modified);
            EXPECT_EQ(response.etag, res2.etag);

            loop.stop();
            CacheResponse.finish();
        });
    });

    loop.run();
}

// Make sure we /do/ store 404 Not Found responses into the cache
TEST_F(Storage, CacheNotFound) {
    SCOPED_TEST(CacheNotFound);

    using namespace mbgl;

    util::RunLoop loop;
    SQLiteCache cache(":memory:");
    OnlineFileSource fs(&cache);

    const Resource resource{ Resource::Unknown, "http://127.0.0.1:3000/not-found" };

    // Insert existing data into the cache that will be marked as stale.
    Response response;
    response.data = std::make_shared<const std::string>("existing data");
    cache.put(resource, response);

    std::unique_ptr<FileRequest> req1;
    std::unique_ptr<WorkRequest> req2;

    int counter = 0;

    // Then, request the actual URL and check that we're getting the rigged cache response first,
    // then the connection error message.
    req1 = fs.request(resource, [&](Response res) {
        if (counter == 0) {
            EXPECT_EQ(nullptr, res.error);
            ASSERT_TRUE(res.data.get());
            EXPECT_EQ("existing data", *res.data);
            EXPECT_FALSE(bool(res.expires));
            EXPECT_FALSE(bool(res.modified));
            EXPECT_FALSE(bool(res.etag));
        } else if (counter == 1) {
            EXPECT_NE(nullptr, res.error);
            EXPECT_EQ(Response::Error::Reason::NotFound, res.error->reason);
            ASSERT_TRUE(res.data.get());
            EXPECT_EQ("Not Found!", *res.data);
            req1.reset();

            // Finally, check the cache to make sure we cached the 404 response.
            req2 = cache.get(resource, [&](std::unique_ptr<Response> res2) {
                EXPECT_NE(nullptr, res2->error);
                EXPECT_EQ(Response::Error::Reason::NotFound, res2->error->reason);
                ASSERT_TRUE(res2->data.get());
                EXPECT_EQ("Not Found!", *res2->data);
                CacheNotFound.finish();
                loop.stop();
            });
        } else {
            FAIL() << "Got too many responses";
        }
        counter++;
    });

    loop.run();
}

// Make sure we don't store a connection error into the cache
TEST_F(Storage, DontCacheConnectionErrors) {
    SCOPED_TEST(DontCacheConnectionErrors);

    using namespace mbgl;

    util::RunLoop loop;
    SQLiteCache cache(":memory:");
    OnlineFileSource fs(&cache);

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

    // Insert existing data into the cache that will be marked as stale.
    Response response;
    response.data = std::make_shared<const std::string>("existing data");
    cache.put(resource, response);

    std::unique_ptr<FileRequest> req1;
    std::unique_ptr<WorkRequest> req2;

    int counter = 0;

    // Then, request the actual URL and check that we're getting the rigged cache response first,
    // then the connection error message.
    req1 = fs.request(resource, [&](Response res) {
        if (counter == 0) {
            EXPECT_EQ(nullptr, res.error);
            ASSERT_TRUE(res.data.get());
            EXPECT_EQ("existing data", *res.data);
            EXPECT_FALSE(bool(res.expires));
            EXPECT_FALSE(bool(res.modified));
            EXPECT_FALSE(bool(res.etag));
        } else if (counter == 1) {
            EXPECT_NE(nullptr, res.error);
            EXPECT_EQ(Response::Error::Reason::Connection, res.error->reason);
            req1.reset();

            // Finally, check the cache to make sure we still have our original data in there rather
            // than the failed connection attempt.
            req2 = cache.get(resource, [&](std::unique_ptr<Response> res2) {
                EXPECT_EQ(nullptr, res2->error);
                ASSERT_TRUE(res2->data.get());
                EXPECT_EQ("existing data", *res2->data);
                DontCacheConnectionErrors.finish();
                loop.stop();
            });
        } else {
            FAIL() << "Got too many responses";
        }
        counter++;
    });

    loop.run();
}

// Make sure we don't store a bad server response into the cache
TEST_F(Storage, DontCacheServerErrors) {
    SCOPED_TEST(DontCacheServerErrors);

    using namespace mbgl;

    util::RunLoop loop;
    SQLiteCache cache(":memory:");
    OnlineFileSource fs(&cache);

    const Resource resource{ Resource::Unknown, "http://127.0.0.1:3000/permanent-error" };

    // Insert existing data into the cache that will be marked as stale.
    Response response;
    response.data = std::make_shared<const std::string>("existing data");
    cache.put(resource, response);

    std::unique_ptr<FileRequest> req1;
    std::unique_ptr<WorkRequest> req2;

    int counter = 0;

    // Then, request the actual URL and check that we're getting the rigged cache response first,
    // then the server error message.
    req1 = fs.request(resource, [&](Response res) {
        if (counter == 0) {
            EXPECT_EQ(nullptr, res.error);
            ASSERT_TRUE(res.data.get());
            EXPECT_EQ("existing data", *res.data);
            EXPECT_FALSE(bool(res.expires));
            EXPECT_FALSE(bool(res.modified));
            EXPECT_FALSE(bool(res.etag));
        } else if (counter == 1) {
            EXPECT_NE(nullptr, res.error);
            EXPECT_EQ(Response::Error::Reason::Server, res.error->reason);
            ASSERT_TRUE(res.data.get());
            EXPECT_EQ("Server Error!", *res.data);
            req1.reset();

            // Finally, check the cache to make sure we still have our original data in there rather
            // than the failed connection attempt.
            req2 = cache.get(resource, [&](std::unique_ptr<Response> res2) {
                EXPECT_EQ(nullptr, res2->error);
                ASSERT_TRUE(res2->data.get());
                EXPECT_EQ("existing data", *res2->data);
                DontCacheServerErrors.finish();
                loop.stop();
            });
        } else {
            FAIL() << "Got too many responses";
        }
        counter++;
    });

    loop.run();
}