Skip to content
This repository
Browse code

Fix issues with 304 caching when there are no Fetch Request Blocks re…

…gistered that match the URL. closes #1294
  • Loading branch information...
commit 5052b61c4eda4578de335b98f3c4458e1b6c4973 1 parent de98fd4
Blake Watters authored March 23, 2013
6  Code/Network/RKManagedObjectRequestOperation.m
@@ -397,10 +397,10 @@ - (BOOL)canSkipMapping
397 397
 
398 398
 - (RKMappingResult *)performMappingOnResponse:(NSError **)error
399 399
 {
400  
-    if ([self canSkipMapping]) {
  400
+    NSURL *URL = RKRelativeURLFromURLAndResponseDescriptors(self.HTTPRequestOperation.response.URL, self.responseDescriptors);
  401
+    NSArray *fetchRequests = RKArrayOfFetchRequestFromBlocksWithURL(self.fetchRequestBlocks, URL);
  402
+    if ([fetchRequests count] && [self canSkipMapping]) {
401 403
         RKLogDebug(@"Managed object mapping requested for cached response which was previously mapped: skipping...");
402  
-        NSURL *URL = RKRelativeURLFromURLAndResponseDescriptors(self.HTTPRequestOperation.response.URL, self.responseDescriptors);
403  
-        NSArray *fetchRequests = RKArrayOfFetchRequestFromBlocksWithURL(self.fetchRequestBlocks, URL);
404 404
         NSMutableArray *managedObjects = [NSMutableArray array];
405 405
         [self.privateContext performBlockAndWait:^{
406 406
             NSError *error = nil;
30  Tests/Logic/Network/RKManagedObjectRequestOperationTest.m
@@ -204,6 +204,36 @@ - (void)testThatManagedObjectsAreFetchedWhenHandlingAResponseThatCanSkipMapping
204 204
     expect([mappedObjects[0] objectID]).to.equal([human objectID]);
205 205
 }
206 206
 
  207
+// 304 'Not Modified'
  208
+- (void)testThatManagedObjectsAreMappedWhenHandlingAResponseThatCanSkipMappingButThereIsNoFetchRequestBlockRegistered
  209
+{
  210
+    RKLogConfigureByName("*", RKLogLevelTrace);
  211
+    RKManagedObjectStore *managedObjectStore = [RKTestFactory managedObjectStore];
  212
+    RKHuman *human = [NSEntityDescription insertNewObjectForEntityForName:@"Human" inManagedObjectContext:managedObjectStore.mainQueueManagedObjectContext];
  213
+    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/humans/1" relativeToURL:[RKTestFactory baseURL]]];
  214
+    
  215
+    // Store a cache entry indicating that the response has been previously mapped
  216
+    NSData *responseData = [@"{ \"name\": \"Blake\"}" dataUsingEncoding:NSUTF8StringEncoding];
  217
+    NSDictionary *headers = @{ @"Etag": @"\"2cdd0a2b329541d81e82ab20aff6281b\"", @"Content-Type": @"application/json" };
  218
+    NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:[request URL] statusCode:200 HTTPVersion:@"1.1" headerFields:headers];
  219
+    NSAssert(response, @"Failed to build cached response");
  220
+    NSCachedURLResponse *cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:response data:responseData userInfo:@{RKResponseHasBeenMappedCacheUserInfoKey: @YES} storagePolicy:NSURLCacheStorageAllowed];
  221
+    [[NSURLCache sharedURLCache] storeCachedResponse:cachedResponse forRequest:request];
  222
+    
  223
+    RKEntityMapping *humanMapping = [RKEntityMapping mappingForEntityForName:@"Human" inManagedObjectStore:managedObjectStore];
  224
+    [humanMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:@"name" toKeyPath:@"name"]];
  225
+    
  226
+    RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:humanMapping pathPattern:nil keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
  227
+    RKManagedObjectRequestOperation *managedObjectRequestOperation = [[RKManagedObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[responseDescriptor]];
  228
+    managedObjectRequestOperation.managedObjectContext = managedObjectStore.mainQueueManagedObjectContext;
  229
+    
  230
+    [managedObjectRequestOperation start];
  231
+    [managedObjectRequestOperation waitUntilFinished];
  232
+    expect(managedObjectRequestOperation.mappingResult).notTo.beNil();
  233
+    NSArray *mappedObjects = [managedObjectRequestOperation.mappingResult array];
  234
+    expect(mappedObjects).to.haveCountOf(1);
  235
+}
  236
+
207 237
 - (void)testThatCachedNotModifiedResponseIsNotUsedWhenMappingFailedToComplete
208 238
 {
209 239
     [[NSURLCache sharedURLCache] removeAllCachedResponses];

0 notes on commit 5052b61

Please sign in to comment.
Something went wrong with that request. Please try again.