diff --git a/Classes/ASIHTTPRequest.m b/Classes/ASIHTTPRequest.m index f159e428..21ec469b 100644 --- a/Classes/ASIHTTPRequest.m +++ b/Classes/ASIHTTPRequest.m @@ -23,7 +23,7 @@ // Automatically set on build -NSString *ASIHTTPRequestVersion = @"v1.7-51 2010-08-18"; +NSString *ASIHTTPRequestVersion = @"v1.7-52 2010-08-30"; NSString* const NetworkRequestErrorDomain = @"ASIHTTPRequestErrorDomain"; @@ -652,6 +652,7 @@ - (void)main } [self setComplete:NO]; + [self setDidUseCachedResponse:NO]; if (![self url]) { [self failWithError:ASIUnableToCreateRequestError]; @@ -2829,7 +2830,7 @@ - (void)handleStreamComplete } // Save to the cache - if ([self downloadCache]) { + if ([self downloadCache] && ![self didUseCachedResponse]) { [[self downloadCache] storeResponseForRequest:self maxAge:[self secondsToCache]]; } @@ -2851,7 +2852,7 @@ - (void)handleStreamComplete [self destroyReadStream]; } - if (![self needsRedirect] && ![self authenticationNeeded]) { + if (![self needsRedirect] && ![self authenticationNeeded] && ![self didUseCachedResponse]) { if (fileError) { [self failWithError:fileError]; @@ -2923,10 +2924,10 @@ - (BOOL)useDataFromCache return NO; } } - + // only 200 responses are stored in the cache, so let the client know // this was a successful response - self.responseStatusCode = 200; + [self setResponseStatusCode:200]; [self setDidUseCachedResponse:YES]; diff --git a/Classes/Tests/ASIDownloadCacheTests.h b/Classes/Tests/ASIDownloadCacheTests.h index 88574abd..f31f3829 100644 --- a/Classes/Tests/ASIDownloadCacheTests.h +++ b/Classes/Tests/ASIDownloadCacheTests.h @@ -10,7 +10,7 @@ @interface ASIDownloadCacheTests : ASITestCase { - + NSUInteger requestsFinishedCount; } @end diff --git a/Classes/Tests/ASIDownloadCacheTests.m b/Classes/Tests/ASIDownloadCacheTests.m index edcd441d..014966ba 100644 --- a/Classes/Tests/ASIDownloadCacheTests.m +++ b/Classes/Tests/ASIDownloadCacheTests.m @@ -291,4 +291,37 @@ - (void)testCookies [ASIHTTPRequest setDefaultCache:nil]; } +// Text fix for a bug where the didFinishSelector would be called twice for a cached response using ASIReloadIfDifferentCachePolicy +- (void)testCacheOnlyCallsRequestFinishedOnce +{ + // Run this request on the main thread to force delegate calls to happen synchronously + [self performSelectorOnMainThread:@selector(runCacheOnlyCallsRequestFinishedOnceTest) withObject:nil waitUntilDone:YES]; +} + +- (void)runCacheOnlyCallsRequestFinishedOnceTest +{ + [[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy]; + ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/i/logo.png"]]; + [request setCachePolicy:ASIReloadIfDifferentCachePolicy]; + [request setDownloadCache:[ASIDownloadCache sharedCache]]; + [request setDelegate:self]; + [request startSynchronous]; + + requestsFinishedCount = 0; + request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/i/logo.png"]]; + [request setCachePolicy:ASIReloadIfDifferentCachePolicy]; + [request setDownloadCache:[ASIDownloadCache sharedCache]]; + [request setDidFinishSelector:@selector(finishCached:)]; + [request setDelegate:self]; + [request startSynchronous]; + + BOOL success = (requestsFinishedCount == 1); + GHAssertTrue(success,@"didFinishSelector called more than once"); +} + +- (void)finishCached:(ASIHTTPRequest *)request +{ + requestsFinishedCount++; +} + @end diff --git a/Classes/Tests/ASINetworkQueueTests.m b/Classes/Tests/ASINetworkQueueTests.m index 13e18dda..b71f4c14 100755 --- a/Classes/Tests/ASINetworkQueueTests.m +++ b/Classes/Tests/ASINetworkQueueTests.m @@ -1201,6 +1201,7 @@ - (void)queueFailureFinish:(ASINetworkQueue *)queue complete = YES; } + @synthesize immediateCancelQueue; @synthesize failedRequests; @synthesize finishedRequests;