[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)
This commit is contained in:
Chris Loer
2018-10-17 10:43:58 -07:00
committed by Chris Loer
parent c66f92f452
commit aab55cfbcd
3 changed files with 12 additions and 7 deletions

View File

@@ -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

View File

@@ -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<std::string>(static_cast<const char*>(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<mbgl::Seconds>(MGLDurationFromTimeInterval(modified.timeIntervalSinceNow));
response.modified = mbgl::Timestamp() + std::chrono::duration_cast<mbgl::Seconds>(MGLDurationFromTimeInterval(modified.timeIntervalSince1970));
}
if (expires) {
response.expires = mbgl::util::now() + std::chrono::duration_cast<mbgl::Seconds>(MGLDurationFromTimeInterval(expires.timeIntervalSinceNow));
response.expires = mbgl::Timestamp() + std::chrono::duration_cast<mbgl::Seconds>(MGLDurationFromTimeInterval(expires.timeIntervalSince1970));
}
_mbglFileSource->put(resource, response);

View File

@@ -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());
});