Cédric Luthi

Merge branch 'master' of git://github.com/pokeb/asi-http-request

@@ -56,9 +56,16 @@ typedef enum _ASICacheStoragePolicy { @@ -56,9 +56,16 @@ typedef enum _ASICacheStoragePolicy {
56 // Should return the cache policy that will be used when requests have their cache policy set to ASIUseDefaultCachePolicy 56 // Should return the cache policy that will be used when requests have their cache policy set to ASIUseDefaultCachePolicy
57 - (ASICachePolicy)defaultCachePolicy; 57 - (ASICachePolicy)defaultCachePolicy;
58 58
  59 +// Returns the date a cached response should expire on. Pass a non-zero max age to specify a custom date.
  60 +- (NSDate *)expiryDateForRequest:(ASIHTTPRequest *)request maxAge:(NSTimeInterval)maxAge;
  61 +
  62 +// Updates cached response headers with a new expiry date. Pass a non-zero max age to specify a custom date.
  63 +- (void)updateExpiryForRequest:(ASIHTTPRequest *)request maxAge:(NSTimeInterval)maxAge;
  64 +
  65 +// Looks at the request's cache policy and any cached headers to determine if the cache data is still valid
59 - (BOOL)canUseCachedDataForRequest:(ASIHTTPRequest *)request; 66 - (BOOL)canUseCachedDataForRequest:(ASIHTTPRequest *)request;
60 67
61 -// Should Remove cached data for a particular request 68 +// Removes cached data for a particular request
62 - (void)removeCachedDataForRequest:(ASIHTTPRequest *)request; 69 - (void)removeCachedDataForRequest:(ASIHTTPRequest *)request;
63 70
64 // Should return YES if the cache considers its cached response current for the request 71 // Should return YES if the cache considers its cached response current for the request
@@ -69,6 +76,9 @@ typedef enum _ASICacheStoragePolicy { @@ -69,6 +76,9 @@ typedef enum _ASICacheStoragePolicy {
69 // When a non-zero maxAge is passed, it should be used as the expiry time for the cached response 76 // When a non-zero maxAge is passed, it should be used as the expiry time for the cached response
70 - (void)storeResponseForRequest:(ASIHTTPRequest *)request maxAge:(NSTimeInterval)maxAge; 77 - (void)storeResponseForRequest:(ASIHTTPRequest *)request maxAge:(NSTimeInterval)maxAge;
71 78
  79 +// Removes cached data for a particular url
  80 +- (void)removeCachedDataForURL:(NSURL *)url;
  81 +
72 // Should return an NSDictionary of cached headers for the passed URL, if it is stored in the cache 82 // Should return an NSDictionary of cached headers for the passed URL, if it is stored in the cache
73 - (NSDictionary *)cachedResponseHeadersForURL:(NSURL *)url; 83 - (NSDictionary *)cachedResponseHeadersForURL:(NSURL *)url;
74 84
@@ -80,10 +80,9 @@ @@ -80,10 +80,9 @@
80 zStream.next_in = bytes; 80 zStream.next_in = bytes;
81 zStream.avail_in = (unsigned int)length; 81 zStream.avail_in = (unsigned int)length;
82 zStream.avail_out = 0; 82 zStream.avail_out = 0;
83 - NSError *theError = nil; 83 +
84 -  
85 NSInteger bytesProcessedAlready = zStream.total_out; 84 NSInteger bytesProcessedAlready = zStream.total_out;
86 - while (zStream.avail_in != 0) { 85 + while (zStream.avail_out == 0) {
87 86
88 if (zStream.total_out-bytesProcessedAlready >= [outputData length]) { 87 if (zStream.total_out-bytesProcessedAlready >= [outputData length]) {
89 [outputData increaseLengthBy:halfLength]; 88 [outputData increaseLengthBy:halfLength];
@@ -103,13 +102,6 @@ @@ -103,13 +102,6 @@
103 } 102 }
104 } 103 }
105 104
106 - if (theError) {  
107 - if (err) {  
108 - *err = theError;  
109 - }  
110 - return nil;  
111 - }  
112 -  
113 // Set real length 105 // Set real length
114 [outputData setLength: zStream.total_out-bytesProcessedAlready]; 106 [outputData setLength: zStream.total_out-bytesProcessedAlready];
115 return outputData; 107 return outputData;
@@ -77,7 +77,6 @@ @@ -77,7 +77,6 @@
77 zStream.next_in = bytes; 77 zStream.next_in = bytes;
78 zStream.avail_in = (unsigned int)length; 78 zStream.avail_in = (unsigned int)length;
79 zStream.avail_out = 0; 79 zStream.avail_out = 0;
80 - NSError *theError = nil;  
81 80
82 NSInteger bytesProcessedAlready = zStream.total_out; 81 NSInteger bytesProcessedAlready = zStream.total_out;
83 while (zStream.avail_in != 0) { 82 while (zStream.avail_in != 0) {
@@ -101,13 +100,6 @@ @@ -101,13 +100,6 @@
101 } 100 }
102 } 101 }
103 102
104 - if (theError) {  
105 - if (err) {  
106 - *err = theError;  
107 - }  
108 - return nil;  
109 - }  
110 -  
111 // Set real length 103 // Set real length
112 [outputData setLength: zStream.total_out-bytesProcessedAlready]; 104 [outputData setLength: zStream.total_out-bytesProcessedAlready];
113 return outputData; 105 return outputData;
@@ -35,11 +35,6 @@ @@ -35,11 +35,6 @@
35 // A helper function that determines if the server has requested data should not be cached by looking at the request's response headers 35 // A helper function that determines if the server has requested data should not be cached by looking at the request's response headers
36 + (BOOL)serverAllowsResponseCachingForRequest:(ASIHTTPRequest *)request; 36 + (BOOL)serverAllowsResponseCachingForRequest:(ASIHTTPRequest *)request;
37 37
38 -// A date formatter that can be used to construct an RFC 1123 date  
39 -// The returned formatter is safe to use on the calling thread  
40 -// Do not use this formatter for parsing dates because the format can vary slightly - use ASIHTTPRequest's dateFromRFC1123String: class method instead  
41 -+ (NSDateFormatter *)rfc1123DateFormatter;  
42 -  
43 @property (assign, nonatomic) ASICachePolicy defaultCachePolicy; 38 @property (assign, nonatomic) ASICachePolicy defaultCachePolicy;
44 @property (retain, nonatomic) NSString *storagePath; 39 @property (retain, nonatomic) NSString *storagePath;
45 @property (retain) NSRecursiveLock *accessLock; 40 @property (retain) NSRecursiveLock *accessLock;
This diff is collapsed. Click to expand it.
@@ -2,7 +2,7 @@ @@ -2,7 +2,7 @@
2 // ASIHTTPRequest.h 2 // ASIHTTPRequest.h
3 // 3 //
4 // Created by Ben Copsey on 04/10/2007. 4 // Created by Ben Copsey on 04/10/2007.
5 -// Copyright 2007-2010 All-Seeing Interactive. All rights reserved. 5 +// Copyright 2007-2011 All-Seeing Interactive. All rights reserved.
6 // 6 //
7 // A guide to the main features is available at: 7 // A guide to the main features is available at:
8 // http://allseeing-i.com/ASIHTTPRequest 8 // http://allseeing-i.com/ASIHTTPRequest
@@ -92,7 +92,7 @@ typedef void (^ASIDataBlock)(NSData *data); @@ -92,7 +92,7 @@ typedef void (^ASIDataBlock)(NSData *data);
92 // Temporarily stores the url we are about to redirect to. Will be nil again when we do redirect 92 // Temporarily stores the url we are about to redirect to. Will be nil again when we do redirect
93 NSURL *redirectURL; 93 NSURL *redirectURL;
94 94
95 - // The delegate, you need to manage setting and talking to your delegate in your subclasses 95 + // The delegate - will be notified of various changes in state via the ASIHTTPRequestDelegate protocol
96 id <ASIHTTPRequestDelegate> delegate; 96 id <ASIHTTPRequestDelegate> delegate;
97 97
98 // Another delegate that is also notified of request status changes and progress updates 98 // Another delegate that is also notified of request status changes and progress updates
@@ -100,7 +100,7 @@ typedef void (^ASIDataBlock)(NSData *data); @@ -100,7 +100,7 @@ typedef void (^ASIDataBlock)(NSData *data);
100 // NOTE: WILL BE RETAINED BY THE REQUEST 100 // NOTE: WILL BE RETAINED BY THE REQUEST
101 id <ASIHTTPRequestDelegate, ASIProgressDelegate> queue; 101 id <ASIHTTPRequestDelegate, ASIProgressDelegate> queue;
102 102
103 - // HTTP method to use (GET / POST / PUT / DELETE / HEAD). Defaults to GET 103 + // HTTP method to use (eg: GET / POST / PUT / DELETE / HEAD etc). Defaults to GET
104 NSString *requestMethod; 104 NSString *requestMethod;
105 105
106 // Request body - only used when the whole body is stored in memory (shouldStreamPostDataFromDisk is false) 106 // Request body - only used when the whole body is stored in memory (shouldStreamPostDataFromDisk is false)
@@ -192,6 +192,9 @@ typedef void (^ASIDataBlock)(NSData *data); @@ -192,6 +192,9 @@ typedef void (^ASIDataBlock)(NSData *data);
192 NSString *username; 192 NSString *username;
193 NSString *password; 193 NSString *password;
194 194
  195 + // User-Agent for this request
  196 + NSString *userAgent;
  197 +
195 // Domain used for NTLM authentication 198 // Domain used for NTLM authentication
196 NSString *domain; 199 NSString *domain;
197 200
@@ -343,8 +346,9 @@ typedef void (^ASIDataBlock)(NSData *data); @@ -343,8 +346,9 @@ typedef void (^ASIDataBlock)(NSData *data);
343 // Tells ASIHTTPRequest not to delete partial downloads, and allows it to use an existing file to resume a download. Defaults to NO. 346 // Tells ASIHTTPRequest not to delete partial downloads, and allows it to use an existing file to resume a download. Defaults to NO.
344 BOOL allowResumeForFileDownloads; 347 BOOL allowResumeForFileDownloads;
345 348
346 - // Custom user information associated with the request 349 + // Custom user information associated with the request (not sent to the server)
347 NSDictionary *userInfo; 350 NSDictionary *userInfo;
  351 + NSInteger tag;
348 352
349 // Use HTTP 1.0 rather than 1.1 (defaults to false) 353 // Use HTTP 1.0 rather than 1.1 (defaults to false)
350 BOOL useHTTPVersionOne; 354 BOOL useHTTPVersionOne;
@@ -399,7 +403,10 @@ typedef void (^ASIDataBlock)(NSData *data); @@ -399,7 +403,10 @@ typedef void (^ASIDataBlock)(NSData *data);
399 403
400 // The number of times this request has retried (when numberOfTimesToRetryOnTimeout > 0) 404 // The number of times this request has retried (when numberOfTimesToRetryOnTimeout > 0)
401 int retryCount; 405 int retryCount;
402 - 406 +
  407 + // Temporarily set to YES when a closed connection forces a retry (internally, this stops ASIHTTPRequest cleaning up a temporary post body)
  408 + BOOL willRetryRequest;
  409 +
403 // When YES, requests will keep the connection to the server alive for a while to allow subsequent requests to re-use it for a substantial speed-boost 410 // When YES, requests will keep the connection to the server alive for a while to allow subsequent requests to re-use it for a substantial speed-boost
404 // Persistent connections will not be used if the server explicitly closes the connection 411 // Persistent connections will not be used if the server explicitly closes the connection
405 // Default is YES 412 // Default is YES
@@ -439,7 +446,6 @@ typedef void (^ASIDataBlock)(NSData *data); @@ -439,7 +446,6 @@ typedef void (^ASIDataBlock)(NSData *data);
439 446
440 // This timer checks up on the request every 0.25 seconds, and updates progress 447 // This timer checks up on the request every 0.25 seconds, and updates progress
441 NSTimer *statusTimer; 448 NSTimer *statusTimer;
442 -  
443 449
444 // The download cache that will be used for this request (use [ASIHTTPRequest setDefaultCache:cache] to configure a default cache 450 // The download cache that will be used for this request (use [ASIHTTPRequest setDefaultCache:cache] to configure a default cache
445 id <ASICacheDelegate> downloadCache; 451 id <ASICacheDelegate> downloadCache;
@@ -460,7 +466,6 @@ typedef void (^ASIDataBlock)(NSData *data); @@ -460,7 +466,6 @@ typedef void (^ASIDataBlock)(NSData *data);
460 BOOL shouldContinueWhenAppEntersBackground; 466 BOOL shouldContinueWhenAppEntersBackground;
461 UIBackgroundTaskIdentifier backgroundTask; 467 UIBackgroundTaskIdentifier backgroundTask;
462 #endif 468 #endif
463 -  
464 469
465 // When downloading a gzipped response, the request will use this helper object to inflate the response 470 // When downloading a gzipped response, the request will use this helper object to inflate the response
466 ASIDataDecompressor *dataDecompressor; 471 ASIDataDecompressor *dataDecompressor;
@@ -794,6 +799,7 @@ typedef void (^ASIDataBlock)(NSData *data); @@ -794,6 +799,7 @@ typedef void (^ASIDataBlock)(NSData *data);
794 // Will be used as a user agent if requests do not specify a custom user agent 799 // Will be used as a user agent if requests do not specify a custom user agent
795 // Is only used when you have specified a Bundle Display Name (CFDisplayBundleName) or Bundle Name (CFBundleName) in your plist 800 // Is only used when you have specified a Bundle Display Name (CFDisplayBundleName) or Bundle Name (CFBundleName) in your plist
796 + (NSString *)defaultUserAgentString; 801 + (NSString *)defaultUserAgentString;
  802 ++ (void)setDefaultUserAgentString:(NSString *)agent;
797 803
798 #pragma mark mime-type detection 804 #pragma mark mime-type detection
799 805
@@ -890,6 +896,7 @@ typedef void (^ASIDataBlock)(NSData *data); @@ -890,6 +896,7 @@ typedef void (^ASIDataBlock)(NSData *data);
890 896
891 @property (retain) NSString *username; 897 @property (retain) NSString *username;
892 @property (retain) NSString *password; 898 @property (retain) NSString *password;
  899 +@property (retain) NSString *userAgent;
893 @property (retain) NSString *domain; 900 @property (retain) NSString *domain;
894 901
895 @property (retain) NSString *proxyUsername; 902 @property (retain) NSString *proxyUsername;
@@ -932,7 +939,7 @@ typedef void (^ASIDataBlock)(NSData *data); @@ -932,7 +939,7 @@ typedef void (^ASIDataBlock)(NSData *data);
932 @property (retain,readonly) NSString *responseStatusMessage; 939 @property (retain,readonly) NSString *responseStatusMessage;
933 @property (retain) NSMutableData *rawResponseData; 940 @property (retain) NSMutableData *rawResponseData;
934 @property (assign) NSTimeInterval timeOutSeconds; 941 @property (assign) NSTimeInterval timeOutSeconds;
935 -@property (retain) NSString *requestMethod; 942 +@property (retain, nonatomic) NSString *requestMethod;
936 @property (retain) NSMutableData *postBody; 943 @property (retain) NSMutableData *postBody;
937 @property (assign) unsigned long long contentLength; 944 @property (assign) unsigned long long contentLength;
938 @property (assign) unsigned long long postLength; 945 @property (assign) unsigned long long postLength;
@@ -947,6 +954,7 @@ typedef void (^ASIDataBlock)(NSData *data); @@ -947,6 +954,7 @@ typedef void (^ASIDataBlock)(NSData *data);
947 @property (assign) BOOL allowCompressedResponse; 954 @property (assign) BOOL allowCompressedResponse;
948 @property (assign) BOOL allowResumeForFileDownloads; 955 @property (assign) BOOL allowResumeForFileDownloads;
949 @property (retain) NSDictionary *userInfo; 956 @property (retain) NSDictionary *userInfo;
  957 +@property (assign) NSInteger tag;
950 @property (retain) NSString *postBodyFilePath; 958 @property (retain) NSString *postBodyFilePath;
951 @property (assign) BOOL shouldStreamPostDataFromDisk; 959 @property (assign) BOOL shouldStreamPostDataFromDisk;
952 @property (assign) BOOL didCreateTemporaryPostDataFile; 960 @property (assign) BOOL didCreateTemporaryPostDataFile;
This diff is collapsed. Click to expand it.
@@ -30,3 +30,8 @@ @@ -30,3 +30,8 @@
30 #ifndef DEBUG_PERSISTENT_CONNECTIONS 30 #ifndef DEBUG_PERSISTENT_CONNECTIONS
31 #define DEBUG_PERSISTENT_CONNECTIONS 0 31 #define DEBUG_PERSISTENT_CONNECTIONS 0
32 #endif 32 #endif
  33 +
  34 +// When set to 1, ASIHTTPRequests will print information about HTTP authentication (Basic, Digest or NTLM) to the console
  35 +#ifndef DEBUG_HTTP_AUTHENTICATION
  36 +#define DEBUG_HTTP_AUTHENTICATION 0
  37 +#endif
@@ -377,7 +377,7 @@ static NSMutableArray *requestsUsingXMLParser = nil; @@ -377,7 +377,7 @@ static NSMutableArray *requestsUsingXMLParser = nil;
377 377
378 // Strip the content encoding if the original response was gzipped 378 // Strip the content encoding if the original response was gzipped
379 if ([self isResponseCompressed]) { 379 if ([self isResponseCompressed]) {
380 - NSMutableDictionary *headers = [[self responseHeaders] mutableCopy]; 380 + NSMutableDictionary *headers = [[[self responseHeaders] mutableCopy] autorelease];
381 [headers removeObjectForKey:@"Content-Encoding"]; 381 [headers removeObjectForKey:@"Content-Encoding"];
382 [self setResponseHeaders:headers]; 382 [self setResponseHeaders:headers];
383 } 383 }
@@ -147,7 +147,33 @@ @@ -147,7 +147,33 @@
147 147
148 success = ([inflatedString isEqualToString:originalString]); 148 success = ([inflatedString isEqualToString:originalString]);
149 GHAssertTrue(success,@"deflate data is not the same as that generated by gzip"); 149 GHAssertTrue(success,@"deflate data is not the same as that generated by gzip");
150 - 150 +
  151 + // Test for bug https://github.com/pokeb/asi-http-request/issues/147
  152 + [ASIHTTPRequest removeFileAtPath:gzippedFilePath error:&error];
  153 + [ASIHTTPRequest removeFileAtPath:filePath error:&error];
  154 +
  155 + ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://spaceharvest.com/i/screen6.png"]];
  156 + [request setDownloadDestinationPath:filePath];
  157 + [request startSynchronous];
  158 +
  159 + if (![ASIDataCompressor compressDataFromFile:filePath toFile:gzippedFilePath error:&error]) {
  160 + GHFail(@"Deflate failed because %@",error);
  161 + }
  162 +
  163 + unsigned long long originalFileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:&error] fileSize];
  164 + [ASIHTTPRequest removeFileAtPath:filePath error:&error];
  165 +
  166 + task = [[[NSTask alloc] init] autorelease];
  167 + [task setLaunchPath:@"/usr/bin/gzip"];
  168 + [task setArguments:[NSArray arrayWithObjects:@"-d",gzippedFilePath,nil]];
  169 + [task launch];
  170 + [task waitUntilExit];
  171 +
  172 + unsigned long long inflatedFileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:&error] fileSize];
  173 +
  174 + success = (originalFileSize == inflatedFileSize);
  175 + GHAssertTrue(success,@"inflated data is not the same size as the original");
  176 +
151 } 177 }
152 178
153 @end 179 @end
@@ -11,6 +11,7 @@ @@ -11,6 +11,7 @@
11 11
12 @interface ASIDownloadCacheTests : ASITestCase { 12 @interface ASIDownloadCacheTests : ASITestCase {
13 NSUInteger requestsFinishedCount; 13 NSUInteger requestsFinishedCount;
  14 + BOOL requestRedirectedWasCalled;
14 } 15 }
15 16
16 @end 17 @end
@@ -14,6 +14,7 @@ @@ -14,6 +14,7 @@
14 @interface ASIDownloadCacheTests () 14 @interface ASIDownloadCacheTests ()
15 - (void)runCacheOnlyCallsRequestFinishedOnceTest; 15 - (void)runCacheOnlyCallsRequestFinishedOnceTest;
16 - (void)finishCached:(ASIHTTPRequest *)request; 16 - (void)finishCached:(ASIHTTPRequest *)request;
  17 +- (void)runRedirectTest;
17 @end 18 @end
18 19
19 20
@@ -164,9 +165,32 @@ @@ -164,9 +165,32 @@
164 success = ![request didUseCachedResponse]; 165 success = ![request didUseCachedResponse];
165 GHAssertTrue(success,@"Request says it used a cached response, but there wasn't one to use"); 166 GHAssertTrue(success,@"Request says it used a cached response, but there wasn't one to use");
166 167
167 - success = !![request error]; 168 + success = ([request error] != nil);
168 GHAssertTrue(success,@"Request had no error set"); 169 GHAssertTrue(success,@"Request had no error set");
169 170
  171 + // Cache some data
  172 + NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/cache-away"];
  173 + request = [ASIHTTPRequest requestWithURL:url];
  174 + [request startSynchronous];
  175 +
  176 + NSString *path = [[ASIDownloadCache sharedCache] pathToStoreCachedResponseDataForRequest:request];
  177 + success = (path != nil);
  178 + GHAssertTrue(success,@"Cache failed to store data");
  179 +
  180 + path = [[ASIDownloadCache sharedCache] pathToStoreCachedResponseHeadersForRequest:request];
  181 + success = (path != nil);
  182 + GHAssertTrue(success,@"Cache failed to store data");
  183 +
  184 + // Make sure data gets removed
  185 + [[ASIDownloadCache sharedCache] removeCachedDataForURL:url];
  186 +
  187 + path = [[ASIDownloadCache sharedCache] pathToCachedResponseDataForURL:url];
  188 + success = (path == nil);
  189 + GHAssertTrue(success,@"Cache failed to remove data");
  190 +
  191 + path = [[ASIDownloadCache sharedCache] pathToCachedResponseHeadersForURL:url];
  192 + success = (path == nil);
  193 + GHAssertTrue(success,@"Cache failed to remove data");
170 194
171 // Test ASIDontLoadCachePolicy 195 // Test ASIDontLoadCachePolicy
172 [[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy]; 196 [[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy];
@@ -307,14 +331,16 @@ @@ -307,14 +331,16 @@
307 331
308 - (void)test304 332 - (void)test304
309 { 333 {
  334 + NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/the_great_american_novel_(abridged).txt"];
  335 +
310 // Test default cache policy 336 // Test default cache policy
311 [[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy]; 337 [[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy];
312 [[ASIDownloadCache sharedCache] setDefaultCachePolicy:ASIUseDefaultCachePolicy]; 338 [[ASIDownloadCache sharedCache] setDefaultCachePolicy:ASIUseDefaultCachePolicy];
313 - ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/the_great_american_novel_(abridged).txt"]]; 339 + ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
314 [request setDownloadCache:[ASIDownloadCache sharedCache]]; 340 [request setDownloadCache:[ASIDownloadCache sharedCache]];
315 [request startSynchronous]; 341 [request startSynchronous];
316 342
317 - request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/the_great_american_novel_(abridged).txt"]]; 343 + request = [ASIHTTPRequest requestWithURL:url];
318 [request setDownloadCache:[ASIDownloadCache sharedCache]]; 344 [request setDownloadCache:[ASIDownloadCache sharedCache]];
319 [request startSynchronous]; 345 [request startSynchronous];
320 BOOL success = ([request responseStatusCode] == 200); 346 BOOL success = ([request responseStatusCode] == 200);
@@ -325,6 +351,30 @@ @@ -325,6 +351,30 @@
325 351
326 success = ([[request responseData] length]); 352 success = ([[request responseData] length]);
327 GHAssertTrue(success,@"Response was empty"); 353 GHAssertTrue(success,@"Response was empty");
  354 +
  355 + // Test 304 updates expiry date
  356 + url = [NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/content_not_modified_but_expires_tomorrow"];
  357 + request = [ASIHTTPRequest requestWithURL:url];
  358 + [request setDownloadCache:[ASIDownloadCache sharedCache]];
  359 + [request startSynchronous];
  360 +
  361 + NSTimeInterval expiryTimestamp = [[[[ASIDownloadCache sharedCache] cachedResponseHeadersForURL:url] objectForKey:@"X-ASIHTTPRequest-Expires"] doubleValue];
  362 +
  363 + // Wait to give the expiry date a chance to change
  364 + sleep(2);
  365 +
  366 + request = [ASIHTTPRequest requestWithURL:url];
  367 + [request setCachePolicy:ASIAskServerIfModifiedCachePolicy];
  368 + [request setDownloadCache:[ASIDownloadCache sharedCache]];
  369 + [request startSynchronous];
  370 +
  371 + success = [request didUseCachedResponse];
  372 + GHAssertTrue(success, @"Cached data should have been used");
  373 +
  374 + NSTimeInterval newExpiryTimestamp = [[[[ASIDownloadCache sharedCache] cachedResponseHeadersForURL:url] objectForKey:@"X-ASIHTTPRequest-Expires"] doubleValue];
  375 + NSLog(@"%@",[request responseString]);
  376 + success = (newExpiryTimestamp > expiryTimestamp);
  377 + GHAssertTrue(success, @"Failed to update expiry timestamp on 304");
328 } 378 }
329 379
330 - (void)testStringEncoding 380 - (void)testStringEncoding
@@ -410,4 +460,50 @@ @@ -410,4 +460,50 @@
410 requestsFinishedCount++; 460 requestsFinishedCount++;
411 } 461 }
412 462
  463 +- (void)testRedirect
  464 +{
  465 + // Run this request on the main thread to force delegate calls to happen synchronously
  466 + [self performSelectorOnMainThread:@selector(runRedirectTest) withObject:nil waitUntilDone:YES];
  467 +}
  468 +
  469 +- (void)runRedirectTest
  470 +{
  471 + [[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy];
  472 + [[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
  473 + [[ASIDownloadCache sharedCache] setDefaultCachePolicy:ASIUseDefaultCachePolicy];
  474 + [ASIHTTPRequest setDefaultCache:[ASIDownloadCache sharedCache]];
  475 +
  476 + ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/cached-redirect"]];
  477 + [request startSynchronous];
  478 +
  479 + BOOL success = ([[[request url] absoluteString] isEqualToString:@"http://allseeing-i.com/i/logo.png"]);
  480 + GHAssertTrue(success,@"Request did not redirect correctly, cannot proceed with test");
  481 +
  482 + requestRedirectedWasCalled = NO;
  483 + request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/cached-redirect"]];
  484 + [request setDelegate:self];
  485 + [request startSynchronous];
  486 +
  487 + success = ([request didUseCachedResponse]);
  488 + GHAssertTrue(success,@"Failed to cache final response");
  489 +
  490 + GHAssertTrue(requestRedirectedWasCalled,@"Failed to call requestRedirected");
  491 +}
  492 +
  493 +- (void)requestRedirected:(ASIHTTPRequest *)redirected
  494 +{
  495 + requestRedirectedWasCalled = YES;
  496 +}
  497 +
  498 +- (void)request:(ASIHTTPRequest *)request willRedirectToURL:(NSURL *)newURL
  499 +{
  500 + BOOL success = ([[newURL absoluteString] isEqualToString:@"http://allseeing-i.com/i/logo.png"]);
  501 + GHAssertTrue(success,@"Request did not redirect correctly, cannot proceed with test");
  502 +
  503 + success = ([request didUseCachedResponse]);
  504 + GHAssertTrue(success,@"Failed to cache redirect response");
  505 +
  506 + [request redirectToURL:newURL];
  507 +}
  508 +
413 @end 509 @end
@@ -20,6 +20,7 @@ @@ -20,6 +20,7 @@
20 } 20 }
21 21
22 - (void)testBasicDownload; 22 - (void)testBasicDownload;
  23 +- (void)testBase64Encode;
23 - (void)testDelegateMethods; 24 - (void)testDelegateMethods;
24 - (void)testConditionalGET; 25 - (void)testConditionalGET;
25 - (void)testException; 26 - (void)testException;
@@ -56,7 +57,7 @@ @@ -56,7 +57,7 @@
56 #endif 57 #endif
57 - (void)testAutomaticRetry; 58 - (void)testAutomaticRetry;
58 - (void)testCloseConnection; 59 - (void)testCloseConnection;
59 -- (void)testPersistentConnectionTimeout; 60 +- (void)testPersistentConnections;
60 - (void)testNilPortCredentialsMatching; 61 - (void)testNilPortCredentialsMatching;
61 62
62 @property (retain, nonatomic) NSMutableData *responseData; 63 @property (retain, nonatomic) NSMutableData *responseData;
This diff is collapsed. Click to expand it.
@@ -15,6 +15,7 @@ Felis de Brito @@ -15,6 +15,7 @@ Felis de Brito
15 Sam DeVore (http://scidsolutions.com) 15 Sam DeVore (http://scidsolutions.com)
16 Nathan de Vries (http://www.atnan.com) 16 Nathan de Vries (http://www.atnan.com)
17 Matthew Frederick 17 Matthew Frederick
  18 +Ole Gammelgaard (http://www.trickclown.dk)
18 Ganasa LLC 19 Ganasa LLC
19 James Hartzell 20 James Hartzell
20 Hunter Hillegas 21 Hunter Hillegas
@@ -29,9 +30,11 @@ John Paul May (http://smhk.com) @@ -29,9 +30,11 @@ John Paul May (http://smhk.com)
29 Rosario Milone (http://vgmtorrents.net) 30 Rosario Milone (http://vgmtorrents.net)
30 Jirapong Nanta (http://bananacoding.com) 31 Jirapong Nanta (http://bananacoding.com)
31 Ben Pettit (http://http://digimulti.com) 32 Ben Pettit (http://http://digimulti.com)
  33 +George Photakis
32 Spencer Pieters (http://www.appwizard.be) 34 Spencer Pieters (http://www.appwizard.be)
33 Peter Pistorius (http://appfactory.co.za) 35 Peter Pistorius (http://appfactory.co.za)
34 Shawn Roske 36 Shawn Roske
  37 +Collin Ruffenach
35 Alessandro Segala (http://letsdev.it) 38 Alessandro Segala (http://letsdev.it)
36 Ben Scheirman 39 Ben Scheirman
37 Basil Shkara (http://www.oiledmachine.com) 40 Basil Shkara (http://www.oiledmachine.com)
@@ -70,14 +70,14 @@ @@ -70,14 +70,14 @@
70 [[UIApplication sharedApplication] cancelAllLocalNotifications]; 70 [[UIApplication sharedApplication] cancelAllLocalNotifications];
71 71
72 // Create a new notification 72 // Create a new notification
73 - UILocalNotification* alarm = [[[UILocalNotification alloc] init] autorelease]; 73 + UILocalNotification *notification = [[[UILocalNotification alloc] init] autorelease];
74 - if (alarm) { 74 + if (notification) {
75 - [alarm setFireDate:[NSDate date]]; 75 + [notification setFireDate:[NSDate date]];
76 - [alarm setTimeZone:[NSTimeZone defaultTimeZone]]; 76 + [notification setTimeZone:[NSTimeZone defaultTimeZone]];
77 - [alarm setRepeatInterval:0]; 77 + [notification setRepeatInterval:0];
78 - [alarm setSoundName:@"alarmsound.caf"]; 78 + [notification setSoundName:@"alarmsound.caf"];
79 - [alarm setAlertBody:@"Boom!\r\n\r\nUpload is finished!"]; 79 + [notification setAlertBody:@"Boom!\r\n\r\nUpload is finished!"];
80 - [[UIApplication sharedApplication] scheduleLocalNotification:alarm]; 80 + [[UIApplication sharedApplication] scheduleLocalNotification:notification];
81 } 81 }
82 #endif 82 #endif
83 } 83 }