From aab55cfbcdbcdea3610beb5f02d276d2e417cedb Mon Sep 17 00:00:00 2001 From: Chris Loer Date: Wed, 17 Oct 2018 10:43:58 -0700 Subject: [darwin,test] Add "must-revalidate" support to putResourceWithURL Testing must-revalidate: true required an expiration date in the future, which exposed a round-tripping problem from translating NSDate to time_point based on now(). Use epoch of 1970 instead (although technically it's not guaranteed to be the epoch for system_clock until C++20) --- platform/darwin/src/MGLOfflineStorage.h | 3 ++- platform/darwin/src/MGLOfflineStorage.mm | 7 ++++--- platform/darwin/test/MGLOfflineStorageTests.mm | 9 ++++++--- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/platform/darwin/src/MGLOfflineStorage.h b/platform/darwin/src/MGLOfflineStorage.h index 5065e397d0..415df84646 100644 --- a/platform/darwin/src/MGLOfflineStorage.h +++ b/platform/darwin/src/MGLOfflineStorage.h @@ -355,8 +355,9 @@ MGL_EXPORT * @param modified Optional "modified" response header * @param expires Optional "expires" response header * @param etag Optional "entity tag" response header + * @param mustRevalidate Indicates whether response can be used after it's stale */ --(void)putResourceWithUrl:(NSURL *)url data:(NSData *)data modified:(NSDate * _Nullable)modified expires:(NSDate * _Nullable)expires etag:(NSString * _Nullable)etag; +-(void)putResourceWithUrl:(NSURL *)url data:(NSData *)data modified:(NSDate * _Nullable)modified expires:(NSDate * _Nullable)expires etag:(NSString * _Nullable)etag mustRevalidate:(BOOL)mustRevalidate; @end diff --git a/platform/darwin/src/MGLOfflineStorage.mm b/platform/darwin/src/MGLOfflineStorage.mm index 78bcb53e7c..87df6912fb 100644 --- a/platform/darwin/src/MGLOfflineStorage.mm +++ b/platform/darwin/src/MGLOfflineStorage.mm @@ -473,21 +473,22 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio return attributes.fileSize; } --(void)putResourceWithUrl:(NSURL *)url data:(NSData *)data modified:(NSDate * _Nullable)modified expires:(NSDate * _Nullable)expires etag:(NSString * _Nullable)etag { +-(void)putResourceWithUrl:(NSURL *)url data:(NSData *)data modified:(NSDate * _Nullable)modified expires:(NSDate * _Nullable)expires etag:(NSString * _Nullable)etag mustRevalidate:(BOOL)mustRevalidate { mbgl::Resource resource(mbgl::Resource::Kind::Unknown, [[url absoluteString] UTF8String]); mbgl::Response response; response.data = std::make_shared(static_cast(data.bytes), data.length); + response.mustRevalidate = mustRevalidate; if (etag) { response.etag = std::string([etag UTF8String]); } if (modified) { - response.modified = mbgl::util::now() + std::chrono::duration_cast(MGLDurationFromTimeInterval(modified.timeIntervalSinceNow)); + response.modified = mbgl::Timestamp() + std::chrono::duration_cast(MGLDurationFromTimeInterval(modified.timeIntervalSince1970)); } if (expires) { - response.expires = mbgl::util::now() + std::chrono::duration_cast(MGLDurationFromTimeInterval(expires.timeIntervalSinceNow)); + response.expires = mbgl::Timestamp() + std::chrono::duration_cast(MGLDurationFromTimeInterval(expires.timeIntervalSince1970)); } _mbglFileSource->put(resource, response); diff --git a/platform/darwin/test/MGLOfflineStorageTests.mm b/platform/darwin/test/MGLOfflineStorageTests.mm index cc396814d0..f01e6854b7 100644 --- a/platform/darwin/test/MGLOfflineStorageTests.mm +++ b/platform/darwin/test/MGLOfflineStorageTests.mm @@ -400,7 +400,7 @@ MGLOfflineStorage *os = [MGLOfflineStorage sharedOfflineStorage]; std::string testData("test data"); - [os putResourceWithUrl:styleURL data:[NSData dataWithBytes:testData.c_str() length:testData.length()] modified:nil expires:nil etag:nil]; + [os putResourceWithUrl:styleURL data:[NSData dataWithBytes:testData.c_str() length:testData.length()] modified:nil expires:nil etag:nil mustRevalidate:NO]; auto fs = os.mbglFileSource; const mbgl::Resource resource { mbgl::Resource::Unknown, "https://api.mapbox.com/some/thing" }; @@ -412,6 +412,7 @@ XCTAssertFalse(res.modified, @"Request should not have a modification timestamp"); XCTAssertFalse(res.expires, @"Request should not have an expiration timestamp"); XCTAssertFalse(res.etag, @"Request should not have an entity tag"); + XCTAssertFalse(res.mustRevalidate, @"Request should not require revalidation"); XCTAssertEqual("test data", *res.data, @"Request did not return expected data"); CFRunLoopStop(CFRunLoopGetCurrent()); }); @@ -425,7 +426,8 @@ MGLOfflineStorage *os = [MGLOfflineStorage sharedOfflineStorage]; std::string testData("test data"); NSDate* now = [NSDate date]; - [os putResourceWithUrl:styleURL data:[NSData dataWithBytes:testData.c_str() length:testData.length()] modified:now expires:now etag:@"some etag"]; + NSDate* future = [now dateByAddingTimeInterval:600]; + [os putResourceWithUrl:styleURL data:[NSData dataWithBytes:testData.c_str() length:testData.length()] modified:now expires:future etag:@"some etag" mustRevalidate:YES]; auto fs = os.mbglFileSource; const mbgl::Resource resource { mbgl::Resource::Unknown, "https://api.mapbox.com/some/thing" }; @@ -437,9 +439,10 @@ XCTAssertTrue(res.modified, @"Request should have a modification timestamp"); XCTAssertEqual(MGLTimeIntervalFromDuration(res.modified->time_since_epoch()), floor(now.timeIntervalSince1970), @"Modification timestamp should roundtrip"); XCTAssertTrue(res.expires, @"Request should have an expiration timestamp"); - XCTAssertEqual(MGLTimeIntervalFromDuration(res.expires->time_since_epoch()), floor(now.timeIntervalSince1970), @"Expiration timestamp should roundtrip"); + XCTAssertEqual(MGLTimeIntervalFromDuration(res.expires->time_since_epoch()), floor(future.timeIntervalSince1970), @"Expiration timestamp should roundtrip"); XCTAssertTrue(res.etag, @"Request should have an entity tag"); XCTAssertEqual(*res.etag, "some etag", @"Entity tag should roundtrip"); + XCTAssertTrue(res.mustRevalidate, @"Request should require revalidation"); XCTAssertEqual("test data", *res.data, @"Request did not return expected data"); CFRunLoopStop(CFRunLoopGetCurrent()); }); -- cgit v1.2.1