mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-04-30 21:52:30 +08:00
Move management of RKResponseHasBeenMapped to RKObjectRequestOperation instead of RKManagedObjectRequestOperation. Add unit tests.
This commit is contained in:
@@ -148,7 +148,7 @@ NSSet *RKSetByRemovingSubkeypathsFromSet(NSSet *setOfKeyPaths);
|
||||
}
|
||||
|
||||
// 304 'Not Modified'
|
||||
- (void)testThatManagedObjectsAreFetchedWhenHandlingANotModifiedResponse
|
||||
- (void)testThatManagedObjectsAreFetchedWhenHandlingAResponseThatCanSkipMapping
|
||||
{
|
||||
RKFetchRequestBlock fetchRequestBlock = ^(NSURL *URL){
|
||||
return [NSFetchRequest fetchRequestWithEntityName:@"Human"];
|
||||
@@ -156,7 +156,15 @@ NSSet *RKSetByRemovingSubkeypathsFromSet(NSSet *setOfKeyPaths);
|
||||
|
||||
RKManagedObjectStore *managedObjectStore = [RKTestFactory managedObjectStore];
|
||||
RKHuman *human = [NSEntityDescription insertNewObjectForEntityForName:@"Human" inManagedObjectContext:managedObjectStore.mainQueueManagedObjectContext];
|
||||
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/204_with_not_modified_status" relativeToURL:[RKTestFactory baseURL]]];
|
||||
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/204" relativeToURL:[RKTestFactory baseURL]]];
|
||||
|
||||
// Store a cache entry indicating that the response has been previously mapped
|
||||
NSData *responseData = [@"{}" dataUsingEncoding:NSUTF8StringEncoding];
|
||||
NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:[request URL] statusCode:200 HTTPVersion:@"1.1" headerFields:nil];
|
||||
NSAssert(response, @"Failed to build cached response");
|
||||
NSCachedURLResponse *cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:response data:responseData userInfo:@{RKResponseHasBeenMappedCacheUserInfoKey: @YES} storagePolicy:NSURLCacheStorageAllowed];
|
||||
[[NSURLCache sharedURLCache] storeCachedResponse:cachedResponse forRequest:request];
|
||||
|
||||
RKEntityMapping *humanMapping = [RKEntityMapping mappingForEntityForName:@"Human" inManagedObjectStore:managedObjectStore];
|
||||
[humanMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:@"name" toKeyPath:@"name"]];
|
||||
|
||||
|
||||
@@ -112,6 +112,7 @@
|
||||
NSArray *validErrorCodes = @[ @(NSURLErrorCannotDecodeContentData), @(NSURLErrorCannotFindHost) ];
|
||||
assertThat(validErrorCodes, hasItem(@([requestOperation.error code])));
|
||||
}
|
||||
|
||||
- (void)testSendingAnObjectRequestOperationToAnBrokenURL
|
||||
{
|
||||
NSMutableURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://invalid••™¡.is"]];
|
||||
@@ -752,4 +753,38 @@
|
||||
expect(user.phone).to.equal(@"867-5309");
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
|
||||
- (void)testThatCacheEntryIsFlaggedWhenMappingCompletes
|
||||
{
|
||||
RKObjectMapping *userMapping = [RKObjectMapping mappingForClass:[RKTestComplexUser class]];
|
||||
[userMapping addAttributeMappingsFromDictionary:@{ @"name": @"email" }];
|
||||
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:userMapping pathPattern:nil keyPath:@"human" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
|
||||
|
||||
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/coredata/etag" relativeToURL:[RKTestFactory baseURL]]];
|
||||
RKObjectRequestOperation *requestOperation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[ responseDescriptor ]];
|
||||
[requestOperation start];
|
||||
expect(requestOperation.error).to.beNil();
|
||||
|
||||
NSCachedURLResponse *response = [[NSURLCache sharedURLCache] cachedResponseForRequest:request];
|
||||
expect(response).notTo.beNil();
|
||||
expect([response.userInfo valueForKey:RKResponseHasBeenMappedCacheUserInfoKey]).to.beTruthy();
|
||||
}
|
||||
|
||||
- (void)testThatCacheEntryIsNotFlaggedWhenMappingFails
|
||||
{
|
||||
RKObjectMapping *userMapping = [RKObjectMapping mappingForClass:[RKTestComplexUser class]];
|
||||
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:userMapping pathPattern:@"/mismatch" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
|
||||
|
||||
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/coredata/etag" relativeToURL:[RKTestFactory baseURL]]];
|
||||
RKObjectRequestOperation *requestOperation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[ responseDescriptor ]];
|
||||
[requestOperation start];
|
||||
expect(requestOperation.error).notTo.beNil();
|
||||
expect([requestOperation.error localizedDescription]).to.equal(@"No response descriptors match the response loaded.");
|
||||
|
||||
NSCachedURLResponse *response = [[NSURLCache sharedURLCache] cachedResponseForRequest:request];
|
||||
expect(response).notTo.beNil();
|
||||
expect([response.userInfo valueForKey:RKResponseHasBeenMappedCacheUserInfoKey]).to.beFalsy();
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user