summaryrefslogtreecommitdiff
path: root/test/storage/main_resource_loader.test.cpp
blob: ee9211b0645c6dea235d3a6c55429052151ca668 (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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
#include <mbgl/actor/actor.hpp>
#include <mbgl/storage/database_file_source.hpp>
#include <mbgl/storage/file_source_manager.hpp>
#include <mbgl/storage/main_resource_loader.hpp>
#include <mbgl/storage/network_status.hpp>
#include <mbgl/storage/online_file_source.hpp>
#include <mbgl/storage/resource_options.hpp>
#include <mbgl/storage/resource_transform.hpp>
#include <mbgl/test/util.hpp>
#include <mbgl/util/run_loop.hpp>

using namespace mbgl;

TEST(MainResourceLoader, TEST_REQUIRES_SERVER(CacheResponse)) {
    util::RunLoop loop;
    MainResourceLoader fs(ResourceOptions{});

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

    std::unique_ptr<AsyncRequest> req1;
    std::unique_ptr<AsyncRequest> 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(res.mustRevalidate);
        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.mustRevalidate, res2.mustRevalidate);
            EXPECT_EQ(response.modified, res2.modified);
            EXPECT_EQ(response.etag, res2.etag);

            loop.stop();
        });
    });

    loop.run();
}

TEST(MainResourceLoader, TEST_REQUIRES_SERVER(CacheRevalidateSame)) {
    util::RunLoop loop;
    MainResourceLoader fs(ResourceOptions{});

    const Resource revalidateSame{Resource::Unknown, "http://127.0.0.1:3000/revalidate-same"};
    std::unique_ptr<AsyncRequest> req1;
    std::unique_ptr<AsyncRequest> req2;
    bool gotResponse = false;

    // First request causes the response to get cached.
    req1 = fs.request(revalidateSame, [&](Response res) {
        req1.reset();

        EXPECT_EQ(nullptr, res.error);
        EXPECT_FALSE(res.notModified);
        ASSERT_TRUE(res.data.get());
        EXPECT_EQ("Response", *res.data);
        EXPECT_FALSE(bool(res.expires));
        EXPECT_TRUE(res.mustRevalidate);
        EXPECT_FALSE(bool(res.modified));
        EXPECT_EQ("snowfall", *res.etag);

        // The first response is stored in the cache, but it has 'must-revalidate' set. This means
        // it can't return the cached response right away and we must wait for the revalidation
        // request to complete. We can distinguish the cached response from the revalided response
        // because the latter has an expiration date, while the cached response doesn't.
        req2 = fs.request(revalidateSame, [&](Response res2) {
            if (!gotResponse) {
                // Even though we could find the response in the database, we send a revalidation
                // request and get a 304 response. Since we haven't sent a reply yet, we're forcing
                // notModified to be false so that implementations can continue to use the
                // notModified flag to skip parsing new data.
                gotResponse = true;
                EXPECT_EQ(nullptr, res2.error);
                EXPECT_FALSE(res2.notModified);
                ASSERT_TRUE(res2.data.get());
                EXPECT_EQ("Response", *res2.data);
                EXPECT_TRUE(bool(res2.expires));
                EXPECT_TRUE(res2.mustRevalidate);
                EXPECT_FALSE(bool(res2.modified));
                EXPECT_EQ("snowfall", *res2.etag);
            } else {
                // The test server sends a Cache-Control header with a max-age of 1 second. This
                // means that our OnlineFileSource implementation will request the tile again after
                // 1 second. This time, our request already had a prior response, so we don't need
                // to send the data again, and instead can actually forward the notModified flag.
                req2.reset();
                EXPECT_EQ(nullptr, res2.error);
                EXPECT_TRUE(res2.notModified);
                EXPECT_FALSE(res2.data.get());
                EXPECT_TRUE(bool(res2.expires));
                EXPECT_TRUE(res2.mustRevalidate);
                EXPECT_FALSE(bool(res2.modified));
                EXPECT_EQ("snowfall", *res2.etag);
                loop.stop();
            }
        });
    });

    loop.run();
}

TEST(MainResourceLoader, TEST_REQUIRES_SERVER(CacheRevalidateModified)) {
    util::RunLoop loop;
    MainResourceLoader fs(ResourceOptions{});

    const Resource revalidateModified{Resource::Unknown, "http://127.0.0.1:3000/revalidate-modified"};
    std::unique_ptr<AsyncRequest> req1;
    std::unique_ptr<AsyncRequest> req2;
    bool gotResponse = false;

    // First request causes the response to get cached.
    req1 = fs.request(revalidateModified, [&](Response res) {
        req1.reset();

        EXPECT_EQ(nullptr, res.error);
        EXPECT_FALSE(res.notModified);
        ASSERT_TRUE(res.data.get());
        EXPECT_EQ("Response", *res.data);
        EXPECT_FALSE(bool(res.expires));
        EXPECT_TRUE(res.mustRevalidate);
        EXPECT_EQ(Timestamp{Seconds(1420070400)}, *res.modified);
        EXPECT_FALSE(res.etag);

        // The first response is stored in the cache, but it has 'must-revalidate' set. This means
        // it can't return the cached response right away and we must wait for the revalidation
        // request to complete. We can distinguish the cached response from the revalided response
        // because the latter has an expiration date, while the cached response doesn't.
        req2 = fs.request(revalidateModified, [&, res](Response res2) {
            if (!gotResponse) {
                // Even though we could find the response in the database, we send a revalidation
                // request and get a 304 response. Since we haven't sent a reply yet, we're forcing
                // notModified to be false so that implementations can continue to use the
                // notModified flag to skip parsing new data.
                gotResponse = true;
                EXPECT_EQ(nullptr, res2.error);
                EXPECT_FALSE(res2.notModified);
                ASSERT_TRUE(res2.data.get());
                EXPECT_EQ("Response", *res2.data);
                EXPECT_TRUE(bool(res2.expires));
                EXPECT_TRUE(res2.mustRevalidate);
                EXPECT_EQ(Timestamp{Seconds(1420070400)}, *res2.modified);
                EXPECT_FALSE(res2.etag);
            } else {
                // The test server sends a Cache-Control header with a max-age of 1 second. This
                // means that our OnlineFileSource implementation will request the tile again after
                // 1 second. This time, our request already had a prior response, so we don't need
                // to send the data again, and instead can actually forward the notModified flag.
                req2.reset();
                EXPECT_EQ(nullptr, res2.error);
                EXPECT_TRUE(res2.notModified);
                EXPECT_FALSE(res2.data.get());
                EXPECT_TRUE(bool(res2.expires));
                EXPECT_TRUE(res2.mustRevalidate);
                EXPECT_EQ(Timestamp{Seconds(1420070400)}, *res2.modified);
                EXPECT_FALSE(res2.etag);
                loop.stop();
            }
        });
    });

    loop.run();
}

TEST(MainResourceLoader, TEST_REQUIRES_SERVER(CacheRevalidateEtag)) {
    util::RunLoop loop;
    MainResourceLoader fs(ResourceOptions{});

    const Resource revalidateEtag{Resource::Unknown, "http://127.0.0.1:3000/revalidate-etag"};
    std::unique_ptr<AsyncRequest> req1;
    std::unique_ptr<AsyncRequest> req2;

    // First request causes the response to get cached.
    req1 = fs.request(revalidateEtag, [&](Response res) {
        req1.reset();

        EXPECT_EQ(nullptr, res.error);
        ASSERT_TRUE(res.data.get());
        EXPECT_EQ("Response 1", *res.data);
        EXPECT_FALSE(bool(res.expires));
        EXPECT_TRUE(res.mustRevalidate);
        EXPECT_FALSE(bool(res.modified));
        EXPECT_EQ("response-1", *res.etag);

        // Second request does not return the cached response, since it had Cache-Control: must-revalidate set.
        req2 = fs.request(revalidateEtag, [&, res](Response res2) {
            req2.reset();

            EXPECT_EQ(nullptr, res2.error);
            ASSERT_TRUE(res2.data.get());
            EXPECT_NE(res.data, res2.data);
            EXPECT_EQ("Response 2", *res2.data);
            EXPECT_FALSE(bool(res2.expires));
            EXPECT_TRUE(res2.mustRevalidate);
            EXPECT_FALSE(bool(res2.modified));
            EXPECT_EQ("response-2", *res2.etag);

            loop.stop();
        });
    });

    loop.run();
}

// Test for https://github.com/mapbox/mapbox-gl-native/issue/1369
//
// A request for http://example.com is made. This triggers a cache get. While the cache get is
// pending, the request is canceled. This removes it from pending. Then, still while the cache get
// is pending, a second request is made for the same resource. This adds an entry back to pending
// and queues another cache request, even though the first one is still pending. Now both cache
// requests resolve to misses, resulting in two HTTP requests for the same resource. The first one
// will notify as expected, the second one will have bound a DefaultFileRequest* in the lambda that
// gets invalidated by the first notify's pending.erase, and when it gets notified, the crash
// occurs.
TEST(MainResourceLoader, TEST_REQUIRES_SERVER(HTTPIssue1369)) {
    util::RunLoop loop;
    MainResourceLoader fs(ResourceOptions{});

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

    auto req = fs.request(resource, [&](Response) { ADD_FAILURE() << "Callback should not be called"; });
    req.reset();
    req = fs.request(resource, [&](Response res) {
        req.reset();
        EXPECT_EQ(nullptr, res.error);
        ASSERT_TRUE(res.data.get());
        EXPECT_EQ("Hello World!", *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(MainResourceLoader, OptionalNonExpired) {
    util::RunLoop loop;
    MainResourceLoader fs(ResourceOptions{});

    const Resource optionalResource{
        Resource::Unknown, "http://127.0.0.1:3000/test", {}, Resource::LoadingMethod::CacheOnly};

    using namespace std::chrono_literals;

    Response response;
    response.data = std::make_shared<std::string>("Cached value");
    response.expires = util::now() + 1h;

    std::unique_ptr<AsyncRequest> req;
    auto dbfs = FileSourceManager::get()->getFileSource(FileSourceType::Database, ResourceOptions{});
    dbfs->forward(optionalResource, response, [&] {
        req = fs.request(optionalResource, [&](Response res) {
            req.reset();
            EXPECT_EQ(nullptr, res.error);
            ASSERT_TRUE(res.data.get());
            EXPECT_EQ("Cached value", *res.data);
            ASSERT_TRUE(bool(res.expires));
            EXPECT_EQ(*response.expires, *res.expires);
            EXPECT_FALSE(res.mustRevalidate);
            EXPECT_FALSE(bool(res.modified));
            EXPECT_FALSE(bool(res.etag));
            loop.stop();
        });
    });

    loop.run();
}

TEST(MainResourceLoader, OptionalExpired) {
    util::RunLoop loop;
    MainResourceLoader fs(ResourceOptions{});

    const Resource optionalResource{
        Resource::Unknown, "http://127.0.0.1:3000/test", {}, Resource::LoadingMethod::CacheOnly};

    using namespace std::chrono_literals;

    Response response;
    response.data = std::make_shared<std::string>("Cached value");
    response.expires = util::now() - 1h;
    auto dbfs = FileSourceManager::get()->getFileSource(FileSourceType::Database, ResourceOptions{});
    std::unique_ptr<AsyncRequest> req;
    dbfs->forward(optionalResource, response, [&] {
        req = fs.request(optionalResource, [&](Response res) {
            req.reset();
            EXPECT_EQ(nullptr, res.error);
            ASSERT_TRUE(res.data.get());
            EXPECT_EQ("Cached value", *res.data);
            ASSERT_TRUE(bool(res.expires));
            EXPECT_EQ(*response.expires, *res.expires);
            EXPECT_FALSE(res.mustRevalidate);
            EXPECT_FALSE(bool(res.modified));
            EXPECT_FALSE(bool(res.etag));
            loop.stop();
        });
    });

    loop.run();
}

TEST(MainResourceLoader, OptionalNotFound) {
    util::RunLoop loop;
    MainResourceLoader fs(ResourceOptions{});

    const Resource optionalResource{
        Resource::Unknown, "http://127.0.0.1:3000/test", {}, Resource::LoadingMethod::CacheOnly};

    using namespace std::chrono_literals;

    std::unique_ptr<AsyncRequest> req;
    req = fs.request(optionalResource, [&](Response res) {
        req.reset();
        ASSERT_TRUE(res.error.get());
        EXPECT_EQ(Response::Error::Reason::NotFound, res.error->reason);
        EXPECT_EQ("Not found in offline database", res.error->message);
        EXPECT_FALSE(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 that a network only request doesn't attempt to load data from the cache.
TEST(MainResourceLoader, TEST_REQUIRES_SERVER(NoCacheRefreshEtagNotModified)) {
    util::RunLoop loop;
    MainResourceLoader fs(ResourceOptions{});

    Resource resource { Resource::Unknown, "http://127.0.0.1:3000/revalidate-same" };
    resource.loadingMethod = Resource::LoadingMethod::NetworkOnly;
    resource.priorEtag.emplace("snowfall");

    using namespace std::chrono_literals;

    // Put a fake value into the cache to make sure we're not retrieving anything from the cache.
    Response response;
    response.data = std::make_shared<std::string>("Cached value");
    response.expires = util::now() + 1h;
    std::unique_ptr<AsyncRequest> req;
    auto dbfs = FileSourceManager::get()->getFileSource(FileSourceType::Database, ResourceOptions{});
    dbfs->forward(resource, response, [&] {
        req = fs.request(resource, [&](Response res) {
            req.reset();
            EXPECT_EQ(nullptr, res.error);
            EXPECT_TRUE(res.notModified);
            EXPECT_FALSE(res.data.get());
            ASSERT_TRUE(bool(res.expires));
            EXPECT_LT(util::now(), *res.expires);
            EXPECT_TRUE(res.mustRevalidate);
            EXPECT_FALSE(bool(res.modified));
            ASSERT_TRUE(bool(res.etag));
            EXPECT_EQ("snowfall", *res.etag);
            loop.stop();
        });
    });

    loop.run();
}

// Test that a network only request doesn't attempt to load data from the cache.
TEST(MainResourceLoader, TEST_REQUIRES_SERVER(NoCacheRefreshEtagModified)) {
    util::RunLoop loop;
    MainResourceLoader fs(ResourceOptions{});

    Resource resource { Resource::Unknown, "http://127.0.0.1:3000/revalidate-same" };
    resource.loadingMethod = Resource::LoadingMethod::NetworkOnly;
    resource.priorEtag.emplace("sunshine");

    using namespace std::chrono_literals;

    // Put a fake value into the cache to make sure we're not retrieving anything from the cache.
    Response response;
    response.data = std::make_shared<std::string>("Cached value");
    response.expires = util::now() + 1h;
    std::unique_ptr<AsyncRequest> req;
    auto dbfs = FileSourceManager::get()->getFileSource(FileSourceType::Database, ResourceOptions{});
    dbfs->forward(resource, response, [&] {
        req = fs.request(resource, [&](Response res) {
            req.reset();
            EXPECT_EQ(nullptr, res.error);
            EXPECT_FALSE(res.notModified);
            ASSERT_TRUE(res.data.get());
            EXPECT_EQ("Response", *res.data);
            EXPECT_FALSE(bool(res.expires));
            EXPECT_TRUE(res.mustRevalidate);
            EXPECT_FALSE(bool(res.modified));
            ASSERT_TRUE(bool(res.etag));
            EXPECT_EQ("snowfall", *res.etag);
            loop.stop();
        });
    });

    loop.run();
}

// Test that a network only request doesn't attempt to load data from the cache.
TEST(MainResourceLoader, TEST_REQUIRES_SERVER(NoCacheFull)) {
    util::RunLoop loop;
    MainResourceLoader fs(ResourceOptions{});

    Resource resource { Resource::Unknown, "http://127.0.0.1:3000/revalidate-same" };
    resource.loadingMethod = Resource::LoadingMethod::NetworkOnly;

    using namespace std::chrono_literals;

    // Put a fake value into the cache to make sure we're not retrieving anything from the cache.
    Response response;
    response.data = std::make_shared<std::string>("Cached value");
    response.expires = util::now() + 1h;
    std::unique_ptr<AsyncRequest> req;
    auto dbfs = FileSourceManager::get()->getFileSource(FileSourceType::Database, ResourceOptions{});
    dbfs->forward(resource, response, [&] {
        req = fs.request(resource, [&](Response res) {
            req.reset();
            EXPECT_EQ(nullptr, res.error);
            EXPECT_FALSE(res.notModified);
            ASSERT_TRUE(res.data.get());
            EXPECT_EQ("Response", *res.data);
            EXPECT_FALSE(bool(res.expires));
            EXPECT_TRUE(res.mustRevalidate);
            EXPECT_FALSE(bool(res.modified));
            ASSERT_TRUE(bool(res.etag));
            EXPECT_EQ("snowfall", *res.etag);
            loop.stop();
        });
    });

    loop.run();
}

// Test that we can make a request with a Modified field that doesn't first try to load
// from cache like a regular request
TEST(MainResourceLoader, TEST_REQUIRES_SERVER(NoCacheRefreshModifiedNotModified)) {
    util::RunLoop loop;
    MainResourceLoader fs(ResourceOptions{});

    Resource resource { Resource::Unknown, "http://127.0.0.1:3000/revalidate-modified" };
    resource.loadingMethod = Resource::LoadingMethod::NetworkOnly;
    resource.priorModified.emplace(Seconds(1420070400)); // January 1, 2015

    using namespace std::chrono_literals;

    // Put a fake value into the cache to make sure we're not retrieving anything from the cache.
    Response response;
    response.data = std::make_shared<std::string>("Cached value");
    response.expires = util::now() + 1h;
    std::unique_ptr<AsyncRequest> req;
    auto dbfs = FileSourceManager::get()->getFileSource(FileSourceType::Database, ResourceOptions{});
    dbfs->forward(resource, response, [&] {
        req = fs.request(resource, [&](Response res) {
            req.reset();
            EXPECT_EQ(nullptr, res.error);
            EXPECT_TRUE(res.notModified);
            EXPECT_FALSE(res.data.get());
            ASSERT_TRUE(bool(res.expires));
            EXPECT_LT(util::now(), *res.expires);
            EXPECT_TRUE(res.mustRevalidate);
            ASSERT_TRUE(bool(res.modified));
            EXPECT_EQ(Timestamp{Seconds(1420070400)}, *res.modified);
            EXPECT_FALSE(bool(res.etag));
            loop.stop();
        });
    });

    loop.run();
}

// Test that we can make a request with a Modified field that doesn't first try to load
// from cache like a regular request
TEST(MainResourceLoader, TEST_REQUIRES_SERVER(NoCacheRefreshModifiedModified)) {
    util::RunLoop loop;
    MainResourceLoader fs(ResourceOptions{});

    Resource resource { Resource::Unknown, "http://127.0.0.1:3000/revalidate-modified" };
    resource.loadingMethod = Resource::LoadingMethod::NetworkOnly;
    resource.priorModified.emplace(Seconds(1417392000)); // December 1, 2014

    using namespace std::chrono_literals;

    // Put a fake value into the cache to make sure we're not retrieving anything from the cache.
    Response response;
    response.data = std::make_shared<std::string>("Cached value");
    response.expires = util::now() + 1h;
    std::unique_ptr<AsyncRequest> req;
    auto dbfs = FileSourceManager::get()->getFileSource(FileSourceType::Database, ResourceOptions{});
    dbfs->forward(resource, response, [&] {
        req = fs.request(resource, [&](Response res) {
            req.reset();
            EXPECT_EQ(nullptr, res.error);
            EXPECT_FALSE(res.notModified);
            ASSERT_TRUE(res.data.get());
            EXPECT_EQ("Response", *res.data);
            EXPECT_FALSE(bool(res.expires));
            EXPECT_TRUE(res.mustRevalidate);
            EXPECT_EQ(Timestamp{Seconds(1420070400)}, *res.modified);
            EXPECT_FALSE(res.etag);
            loop.stop();
        });
    });

    loop.run();
}

TEST(MainResourceLoader, TEST_REQUIRES_SERVER(SetResourceTransform)) {
    util::RunLoop loop;
    MainResourceLoader fs(ResourceOptions{});

    auto onlinefs = std::static_pointer_cast<OnlineFileSource>(
        FileSourceManager::get()->getFileSource(FileSourceType::Network, ResourceOptions{}));

    // Translates the URL "localhost://test to http://127.0.0.1:3000/test
    Actor<ResourceTransform::TransformCallback> transform(
        loop, [](Resource::Kind, const std::string& url, ResourceTransform::FinishedCallback cb) {
            if (url == "localhost://test") {
                cb("http://127.0.0.1:3000/test");
            } else {
                cb(url);
            }
        });

    onlinefs->setResourceTransform(
        {[actorRef = transform.self()](
             Resource::Kind kind, const std::string& url, ResourceTransform::FinishedCallback cb) {
            actorRef.invoke(&ResourceTransform::TransformCallback::operator(), kind, url, std::move(cb));
        }});
    const Resource resource1{Resource::Unknown, "localhost://test"};

    std::unique_ptr<AsyncRequest> req;
    req = fs.request(resource1, [&](Response res) {
        req.reset();
        EXPECT_EQ(nullptr, res.error);
        ASSERT_TRUE(res.data.get());
        EXPECT_EQ("Hello World!", *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();

    onlinefs->setResourceTransform({});
    const Resource resource2{Resource::Unknown, "http://127.0.0.1:3000/test"};

    req = fs.request(resource2, [&](Response res) {
        req.reset();
        EXPECT_EQ(nullptr, res.error);
        ASSERT_TRUE(res.data.get());
        EXPECT_EQ("Hello World!", *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(MainResourceLoader, SetResourceCachePath) {
    util::RunLoop loop;
    MainResourceLoader fs(ResourceOptions{});
    auto dbfs = std::static_pointer_cast<DatabaseFileSource>(
        FileSourceManager::get()->getFileSource(FileSourceType::Database, ResourceOptions{}));
    dbfs->setDatabasePath("./new_offline.db", [&loop] { loop.stop(); });
    loop.run();
}

// Test that a stale cache file that has must-revalidate set will trigger a response.
TEST(MainResourceLoader, TEST_REQUIRES_SERVER(RespondToStaleMustRevalidate)) {
    util::RunLoop loop;
    MainResourceLoader fs(ResourceOptions{});

    Resource resource { Resource::Unknown, "http://127.0.0.1:3000/revalidate-same" };
    resource.loadingMethod = Resource::LoadingMethod::CacheOnly;

    // using namespace std::chrono_literals;

    // Put an existing value in the cache that has expired, and has must-revalidate set.
    Response response;
    response.data = std::make_shared<std::string>("Cached value");
    response.modified = Timestamp(Seconds(1417392000)); // December 1, 2014
    response.expires = Timestamp(Seconds(1417392000));
    response.mustRevalidate = true;
    response.etag.emplace("snowfall");
    std::unique_ptr<AsyncRequest> req;
    auto dbfs = FileSourceManager::get()->getFileSource(FileSourceType::Database, ResourceOptions{});
    dbfs->forward(resource, response, [&] {
        req = fs.request(resource, [&](Response res) {
            req.reset();
            ASSERT_TRUE(res.error.get());
            EXPECT_EQ(Response::Error::Reason::NotFound, res.error->reason);
            EXPECT_EQ("Cached resource is unusable", res.error->message);
            EXPECT_FALSE(res.notModified);
            ASSERT_TRUE(res.data.get());
            EXPECT_EQ("Cached value", *res.data);
            ASSERT_TRUE(res.expires);
            EXPECT_EQ(Timestamp{Seconds(1417392000)}, *res.expires);
            EXPECT_TRUE(res.mustRevalidate);
            ASSERT_TRUE(res.modified);
            EXPECT_EQ(Timestamp{Seconds(1417392000)}, *res.modified);
            ASSERT_TRUE(res.etag);
            EXPECT_EQ("snowfall", *res.etag);

            resource.priorEtag = res.etag;
            resource.priorModified = res.modified;
            resource.priorExpires = res.expires;
            resource.priorData = res.data;

            loop.stop();
        });
    });

    loop.run();

    // Now run this request again, with the data we gathered from the previous stale/unusable
    // request. We're replacing the data so that we can check that the MainResourceLoader doesn't
    // attempt another database access if we already have the value.
    resource.loadingMethod = Resource::LoadingMethod::NetworkOnly;
    resource.priorData = std::make_shared<std::string>("Prior value");

    req = fs.request(resource, [&](Response res) {
        req.reset();
        ASSERT_EQ(nullptr, res.error.get());
        // Since the data was found in the cache, we're doing a revalidation request. Yet, since
        // this request hasn't returned data before, we're setting notModified to false in the
        // OnlineFileSource to ensure that requestors know that this is the first time they're
        // seeing this data.
        EXPECT_FALSE(res.notModified);
        ASSERT_TRUE(res.data.get());
        // Ensure that it's the value that we manually inserted into the cache rather than the value
        // the server returns, since we should be executing a revalidation request which doesn't
        // return new data, only a 304 Not Modified response.
        EXPECT_EQ("Prior value", *res.data);
        ASSERT_TRUE(res.expires);
        EXPECT_LE(util::now(), *res.expires);
        EXPECT_TRUE(res.mustRevalidate);
        ASSERT_TRUE(res.modified);
        EXPECT_EQ(Timestamp{ Seconds(1417392000) }, *res.modified);
        ASSERT_TRUE(res.etag);
        EXPECT_EQ("snowfall", *res.etag);
        loop.stop();
    });

    loop.run();
}

// Test that requests for expired resources have lower priority than requests for new resources
TEST(MainResourceLoader, TEST_REQUIRES_SERVER(CachedResourceLowPriority)) {
    util::RunLoop loop;
    MainResourceLoader fs(ResourceOptions{});

    Response response;
    std::size_t online_response_counter = 0;

    using namespace std::chrono_literals;
    response.expires = util::now() - 1h;

    auto dbfs = FileSourceManager::get()->getFileSource(FileSourceType::Database, ResourceOptions{});
    auto onlineFs = FileSourceManager::get()->getFileSource(FileSourceType::Network, ResourceOptions{});

    // Put existing values into the cache.
    Resource resource1{Resource::Unknown, "http://127.0.0.1:3000/load/3", {}, Resource::LoadingMethod::All};
    response.data = std::make_shared<std::string>("Cached Request 3");
    dbfs->forward(resource1, response);

    Resource resource2{Resource::Unknown, "http://127.0.0.1:3000/load/4", {}, Resource::LoadingMethod::All};
    response.data = std::make_shared<std::string>("Cached Request 4");
    dbfs->forward(resource2, response);

    onlineFs->setProperty(MAX_CONCURRENT_REQUESTS_KEY, 1u);
    fs.pause();
    NetworkStatus::Set(NetworkStatus::Status::Offline);

    // Ensure that the online requests for new resources are processed first.
    Resource nonCached0{Resource::Unknown, "http://127.0.0.1:3000/load/0", {}, Resource::LoadingMethod::All};
    std::unique_ptr<AsyncRequest> req0 = fs.request(nonCached0, [&](Response res) {
        req0.reset();
        EXPECT_EQ(online_response_counter, 0); // make sure this is responded first
        EXPECT_EQ("Request 0", *res.data);
    });

    Resource nonCached1{Resource::Unknown, "http://127.0.0.1:3000/load/1", {}, Resource::LoadingMethod::All};
    std::unique_ptr<AsyncRequest> req1 = fs.request(nonCached1, [&](Response res) {
        online_response_counter++;
        req1.reset();
        EXPECT_EQ(online_response_counter, 1); // make sure this is responded second
        EXPECT_EQ("Request 1", *res.data);
    });

    bool req3CachedResponseReceived = false;
    std::unique_ptr<AsyncRequest> req3 = fs.request(resource1, [&](Response res) {
        // Offline callback is received first
        if (!req3CachedResponseReceived) {
            EXPECT_EQ("Cached Request 3", *res.data);
            req3CachedResponseReceived = true;
        } else {
            online_response_counter++;
            req3.reset();
            EXPECT_EQ(online_response_counter, 3);
            EXPECT_EQ("Request 3", *res.data);
        }
    });

    bool req4CachedResponseReceived = false;
    std::unique_ptr<AsyncRequest> req4 = fs.request(resource2, [&](Response res) {
        // Offline callback is received first
        if (!req4CachedResponseReceived) {
            EXPECT_EQ("Cached Request 4", *res.data);
            req4CachedResponseReceived = true;
        } else {
            online_response_counter++;
            req4.reset();
            EXPECT_EQ(online_response_counter, 4);
            EXPECT_EQ("Request 4", *res.data);
            loop.stop();
        }
    });

    Resource nonCached2{Resource::Unknown, "http://127.0.0.1:3000/load/2", {}, Resource::LoadingMethod::All};
    std::unique_ptr<AsyncRequest> req2 = fs.request(nonCached2, [&](Response res) {
        online_response_counter++;
        req2.reset();
        EXPECT_EQ(online_response_counter, 2); // make sure this is responded third
        EXPECT_EQ("Request 2", *res.data);
    });

    fs.resume();
    NetworkStatus::Set(NetworkStatus::Status::Online);

    loop.run();
}