PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/WebServiceDemo/Required_Libs/HTTPRequest/Classes/Tests/ASIDownloadCacheTests.m

https://gitlab.com/praveenvelanati/ios-demo
Objective C | 562 lines | 408 code | 112 blank | 42 comment | 17 complexity | 5bd790856c4fc8aeba58d1697adc3430 MD5 | raw file
  1. //
  2. // ASIDownloadCacheTests.m
  3. // Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest
  4. //
  5. // Created by Ben Copsey on 03/05/2010.
  6. // Copyright 2010 All-Seeing Interactive. All rights reserved.
  7. //
  8. #import "ASIDownloadCacheTests.h"
  9. #import "ASIDownloadCache.h"
  10. #import "ASIHTTPRequest.h"
  11. // Stop clang complaining about undeclared selectors
  12. @interface ASIDownloadCacheTests ()
  13. - (void)runCacheOnlyCallsRequestFinishedOnceTest;
  14. - (void)finishCached:(ASIHTTPRequest *)request;
  15. - (void)runRedirectTest;
  16. @end
  17. @implementation ASIDownloadCacheTests
  18. - (void)testDownloadCache
  19. {
  20. [[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy];
  21. [[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
  22. [[ASIDownloadCache sharedCache] setDefaultCachePolicy:ASIUseDefaultCachePolicy];
  23. [ASIHTTPRequest setDefaultCache:nil];
  24. // Ensure a request without a download cache does not pull from the cache
  25. ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/cache-away"]];
  26. [request startSynchronous];
  27. BOOL success = ![request didUseCachedResponse];
  28. GHAssertTrue(success,@"Used cached response when we shouldn't have");
  29. // Make all requests use the cache
  30. [ASIHTTPRequest setDefaultCache:[ASIDownloadCache sharedCache]];
  31. // Check a request isn't setting didUseCachedResponse when the data is not in the cache
  32. request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/cache-away"]];
  33. [request startSynchronous];
  34. success = ![request didUseCachedResponse];
  35. GHAssertTrue(success,@"Cached response should not have been available");
  36. // Test read from the cache
  37. request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/cache-away"]];
  38. [request startSynchronous];
  39. success = [request didUseCachedResponse];
  40. GHAssertTrue(success,@"Failed to use cached response");
  41. // Test preventing reads from the cache
  42. [[ASIDownloadCache sharedCache] setShouldRespectCacheControlHeaders:YES];
  43. request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/cache-away"]];
  44. [request setCachePolicy:ASIAskServerIfModifiedWhenStaleCachePolicy|ASIDoNotReadFromCacheCachePolicy];
  45. [request startSynchronous];
  46. success = ![request didUseCachedResponse];
  47. GHAssertTrue(success,@"Used the cache when reads were not enabled");
  48. //Empty the cache
  49. [[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy];
  50. // Test preventing writes to the cache
  51. request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/cache-away"]];
  52. [request setCachePolicy:ASIAskServerIfModifiedWhenStaleCachePolicy|ASIDoNotWriteToCacheCachePolicy];
  53. [request startSynchronous];
  54. request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/cache-away"]];
  55. [request setCachePolicy:ASIAskServerIfModifiedWhenStaleCachePolicy];
  56. [request startSynchronous];
  57. success = ![request didUseCachedResponse];
  58. GHAssertTrue(success,@"Used cached response when the cache should have been empty");
  59. // Test respecting etag
  60. request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/content-always-new"]];
  61. [request startSynchronous];
  62. success = ![request didUseCachedResponse];
  63. GHAssertTrue(success,@"Used cached response when we shouldn't have");
  64. // Etag will be different on the second request
  65. request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/content-always-new"]];
  66. // Note: we are forcing it to perform a conditional GET
  67. [request setCachePolicy:ASIDoNotReadFromCacheCachePolicy|ASIAskServerIfModifiedCachePolicy];
  68. [request startSynchronous];
  69. success = ![request didUseCachedResponse];
  70. GHAssertTrue(success,@"Used cached response when we shouldn't have");
  71. // Test ignoring server headers
  72. request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/no-cache"]];
  73. [[ASIDownloadCache sharedCache] setShouldRespectCacheControlHeaders:NO];
  74. [request startSynchronous];
  75. request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/no-cache"]];
  76. [[ASIDownloadCache sharedCache] setShouldRespectCacheControlHeaders:NO];
  77. [request startSynchronous];
  78. success = [request didUseCachedResponse];
  79. GHAssertTrue(success,@"Failed to use cached response");
  80. // Test ASIOnlyLoadIfNotCachedCachePolicy
  81. [[ASIDownloadCache sharedCache] setShouldRespectCacheControlHeaders:YES];
  82. request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/content-always-new"]];
  83. [[ASIDownloadCache sharedCache] setDefaultCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy];
  84. [request startSynchronous];
  85. success = [request didUseCachedResponse];
  86. GHAssertTrue(success,@"Failed to use cached response");
  87. // Test clearing the cache
  88. [[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy];
  89. request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/cache-away"]];
  90. [request startSynchronous];
  91. success = ![request didUseCachedResponse];
  92. GHAssertTrue(success,@"Cached response should not have been available");
  93. // Test ASIAskServerIfModifiedWhenStaleCachePolicy
  94. [[ASIDownloadCache sharedCache] setDefaultCachePolicy:ASIUseDefaultCachePolicy];
  95. request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/content-always-new"]];
  96. [request setSecondsToCache:2];
  97. [request startSynchronous];
  98. // This request should not go to the network
  99. request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/content-always-new"]];
  100. [request startSynchronous];
  101. success = [request didUseCachedResponse];
  102. GHAssertTrue(success,@"Failed to use cached response");
  103. [NSThread sleepForTimeInterval:2];
  104. // This request will perform a conditional GET
  105. request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/content-always-new"]];
  106. [request setSecondsToCache:2];
  107. [request startSynchronous];
  108. success = ![request didUseCachedResponse];
  109. GHAssertTrue(success,@"Failed to use cached response");
  110. // Test ASIFallbackToCacheIfLoadFailsCachePolicy
  111. // Store something in the cache
  112. [request setURL:[NSURL URLWithString:@"http://"]];
  113. [request setResponseHeaders:[NSDictionary dictionaryWithObject:@"test" forKey:@"test"]];
  114. [request setRawResponseData:(NSMutableData *)[@"test" dataUsingEncoding:NSUTF8StringEncoding]];
  115. [[ASIDownloadCache sharedCache] storeResponseForRequest:request maxAge:0];
  116. request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://"]];
  117. [request setCachePolicy:ASIFallbackToCacheIfLoadFailsCachePolicy];
  118. [request startSynchronous];
  119. success = [request didUseCachedResponse];
  120. GHAssertTrue(success,@"Failed to use cached response");
  121. success = [[request responseString] isEqualToString:@"test"];
  122. GHAssertTrue(success,@"Failed to read cached response");
  123. success = [[[request responseHeaders] valueForKey:@"test"] isEqualToString:@"test"];
  124. GHAssertTrue(success,@"Failed to read cached response headers");
  125. // Remove the stuff from the cache, and try again
  126. [request setURL:[NSURL URLWithString:@"http://"]];
  127. [[ASIDownloadCache sharedCache] removeCachedDataForRequest:request];
  128. request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://"]];
  129. [request setCachePolicy:ASIFallbackToCacheIfLoadFailsCachePolicy];
  130. [request startSynchronous];
  131. success = ![request didUseCachedResponse];
  132. GHAssertTrue(success,@"Request says it used a cached response, but there wasn't one to use");
  133. success = ([request error] != nil);
  134. GHAssertTrue(success,@"Request had no error set");
  135. // Cache some data
  136. NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/cache-away"];
  137. request = [ASIHTTPRequest requestWithURL:url];
  138. [request startSynchronous];
  139. NSString *path = [[ASIDownloadCache sharedCache] pathToStoreCachedResponseDataForRequest:request];
  140. success = (path != nil);
  141. GHAssertTrue(success,@"Cache failed to store data");
  142. path = [[ASIDownloadCache sharedCache] pathToStoreCachedResponseHeadersForRequest:request];
  143. success = (path != nil);
  144. GHAssertTrue(success,@"Cache failed to store data");
  145. // Make sure data gets removed
  146. [[ASIDownloadCache sharedCache] removeCachedDataForURL:url];
  147. path = [[ASIDownloadCache sharedCache] pathToCachedResponseDataForURL:url];
  148. success = (path == nil);
  149. GHAssertTrue(success,@"Cache failed to remove data");
  150. path = [[ASIDownloadCache sharedCache] pathToCachedResponseHeadersForURL:url];
  151. success = (path == nil);
  152. GHAssertTrue(success,@"Cache failed to remove data");
  153. // Test ASIDontLoadCachePolicy
  154. [[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy];
  155. request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/content-always-new"]];
  156. [request setCachePolicy:ASIDontLoadCachePolicy];
  157. [request startSynchronous];
  158. success = ![request error];
  159. GHAssertTrue(success,@"Request had an error");
  160. success = ![request contentLength];
  161. GHAssertTrue(success,@"Request had a response");
  162. }
  163. - (void)testDefaultPolicy
  164. {
  165. ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/the_great_american_novel_(abridged).txt"]];
  166. [request setDownloadCache:[ASIDownloadCache sharedCache]];
  167. [request startSynchronous];
  168. BOOL success = ([request cachePolicy] == [[ASIDownloadCache sharedCache] defaultCachePolicy]);
  169. GHAssertTrue(success,@"Failed to use the cache policy from the cache");
  170. request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/the_great_american_novel_(abridged).txt"]];
  171. [request setDownloadCache:[ASIDownloadCache sharedCache]];
  172. [request setCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy];
  173. [request startSynchronous];
  174. success = ([request cachePolicy] == ASIOnlyLoadIfNotCachedCachePolicy);
  175. GHAssertTrue(success,@"Failed to use the cache policy from the cache");
  176. }
  177. - (void)testNoCache
  178. {
  179. // Test server no-cache headers
  180. [[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy];
  181. NSArray *cacheHeaders = [NSArray arrayWithObjects:@"cache-control/no-cache",@"cache-control/no-store",@"pragma/no-cache",nil];
  182. for (NSString *cacheType in cacheHeaders) {
  183. NSString *url = [NSString stringWithFormat:@"http://allseeing-i.com/ASIHTTPRequest/tests/%@",cacheType];
  184. ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]];
  185. [request setDownloadCache:[ASIDownloadCache sharedCache]];
  186. [request startSynchronous];
  187. request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]];
  188. [request setDownloadCache:[ASIDownloadCache sharedCache]];
  189. [request startSynchronous];
  190. BOOL success = ![request didUseCachedResponse];
  191. GHAssertTrue(success,@"Data should not have been stored in the cache");
  192. }
  193. }
  194. - (void)testSharedCache
  195. {
  196. [[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy];
  197. // Make using the cache automatic
  198. [ASIHTTPRequest setDefaultCache:[ASIDownloadCache sharedCache]];
  199. ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/the_great_american_novel_(abridged).txt"]];
  200. [request startSynchronous];
  201. request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/the_great_american_novel_(abridged).txt"]];
  202. [request startSynchronous];
  203. BOOL success = [request didUseCachedResponse];
  204. GHAssertTrue(success,@"Failed to use data cached in default cache");
  205. [ASIHTTPRequest setDefaultCache:nil];
  206. request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/the_great_american_novel_(abridged).txt"]];
  207. [request startSynchronous];
  208. success = ![request didUseCachedResponse];
  209. GHAssertTrue(success,@"Should not have used data cached in default cache");
  210. }
  211. - (void)testExpiry
  212. {
  213. [[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy];
  214. [[ASIDownloadCache sharedCache] setDefaultCachePolicy:ASIAskServerIfModifiedCachePolicy];
  215. NSArray *headers = [NSArray arrayWithObjects:@"last-modified",@"etag",@"expires",@"max-age",nil];
  216. for (NSString *header in headers) {
  217. ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://allseeing-i.com/ASIHTTPRequest/tests/content-always-new/%@",header]]];
  218. [request setDownloadCache:[ASIDownloadCache sharedCache]];
  219. [request startSynchronous];
  220. if ([header isEqualToString:@"last-modified"]) {
  221. [NSThread sleepForTimeInterval:2];
  222. }
  223. request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://allseeing-i.com/ASIHTTPRequest/tests/content-always-new/%@",header]]];
  224. [request setDownloadCache:[ASIDownloadCache sharedCache]];
  225. [request startSynchronous];
  226. BOOL success = ![request didUseCachedResponse];
  227. GHAssertTrue(success,@"Cached data should have expired");
  228. }
  229. }
  230. - (void)testMaxAgeParsing
  231. {
  232. [[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy];
  233. [[ASIDownloadCache sharedCache] setDefaultCachePolicy:ASIUseDefaultCachePolicy];
  234. ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/cache-control-max-age-parsing"]];
  235. [request setDownloadCache:[ASIDownloadCache sharedCache]];
  236. [request startSynchronous];
  237. request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/cache-control-max-age-parsing"]];
  238. [request setDownloadCache:[ASIDownloadCache sharedCache]];
  239. [request startSynchronous];
  240. BOOL success = [request didUseCachedResponse];
  241. GHAssertTrue(success,@"Failed to use cached response");
  242. }
  243. - (void)testExtensionHandling
  244. {
  245. NSArray *extensions = [ASIDownloadCache fileExtensionsToHandleAsHTML];
  246. for (NSString *extension in extensions) {
  247. NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://allseeing-i.com/file.%@",extension]];
  248. ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
  249. NSString *path = [[ASIDownloadCache sharedCache] pathToStoreCachedResponseDataForRequest:request];
  250. BOOL success = [[path pathExtension] isEqualToString:@"html"];
  251. GHAssertTrue(success, @"Failed to use html extension on cached path for a resource we know a webview won't be able to open locally");
  252. }
  253. NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com/"];
  254. ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
  255. NSString *path = [[ASIDownloadCache sharedCache] pathToStoreCachedResponseDataForRequest:request];
  256. BOOL success = [[path pathExtension] isEqualToString:@"html"];
  257. GHAssertTrue(success, @"Failed to use html extension on cached path for a url without an extension");
  258. url = [NSURL URLWithString:@"http://allseeing-i.com/i/logo.png"];
  259. request = [ASIHTTPRequest requestWithURL:url];
  260. path = [[ASIDownloadCache sharedCache] pathToStoreCachedResponseDataForRequest:request];
  261. success = [[path pathExtension] isEqualToString:@"png"];
  262. GHAssertTrue(success, @"Failed to preserve file extension on cached path");
  263. }
  264. - (void)testCustomExpiry
  265. {
  266. [[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy];
  267. [[ASIDownloadCache sharedCache] setDefaultCachePolicy:ASIUseDefaultCachePolicy];
  268. [[ASIDownloadCache sharedCache] setShouldRespectCacheControlHeaders:YES];
  269. ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/cache-away"]];
  270. [request setDownloadCache:[ASIDownloadCache sharedCache]];
  271. [request setSecondsToCache:-2];
  272. [request startSynchronous];
  273. request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/cache-away"]];
  274. [request setDownloadCache:[ASIDownloadCache sharedCache]];
  275. [request startSynchronous];
  276. BOOL success = ![request didUseCachedResponse];
  277. GHAssertTrue(success,@"Cached data should have expired");
  278. request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/cache-away"]];
  279. [request setDownloadCache:[ASIDownloadCache sharedCache]];
  280. [request setSecondsToCache:20];
  281. [request startSynchronous];
  282. request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/cache-away"]];
  283. [request setDownloadCache:[ASIDownloadCache sharedCache]];
  284. [request startSynchronous];
  285. success = [request didUseCachedResponse];
  286. GHAssertTrue(success,@"Cached data should have been used");
  287. }
  288. - (void)test304
  289. {
  290. NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/the_great_american_novel_(abridged).txt"];
  291. // Test default cache policy
  292. [[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy];
  293. [[ASIDownloadCache sharedCache] setDefaultCachePolicy:ASIUseDefaultCachePolicy];
  294. ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
  295. [request setDownloadCache:[ASIDownloadCache sharedCache]];
  296. [request startSynchronous];
  297. request = [ASIHTTPRequest requestWithURL:url];
  298. [request setDownloadCache:[ASIDownloadCache sharedCache]];
  299. [request startSynchronous];
  300. BOOL success = ([request responseStatusCode] == 200);
  301. GHAssertTrue(success,@"Failed to perform a conditional get");
  302. success = [request didUseCachedResponse];
  303. GHAssertTrue(success,@"Cached data should have been used");
  304. success = ([[request responseData] length]);
  305. GHAssertTrue(success,@"Response was empty");
  306. // Test 304 updates expiry date
  307. url = [NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/content_not_modified_but_expires_tomorrow"];
  308. request = [ASIHTTPRequest requestWithURL:url];
  309. [request setDownloadCache:[ASIDownloadCache sharedCache]];
  310. [request startSynchronous];
  311. NSTimeInterval expiryTimestamp = [[[[ASIDownloadCache sharedCache] cachedResponseHeadersForURL:url] objectForKey:@"X-ASIHTTPRequest-Expires"] doubleValue];
  312. // Wait to give the expiry date a chance to change
  313. sleep(2);
  314. request = [ASIHTTPRequest requestWithURL:url];
  315. [request setCachePolicy:ASIAskServerIfModifiedCachePolicy];
  316. [request setDownloadCache:[ASIDownloadCache sharedCache]];
  317. [request startSynchronous];
  318. success = [request didUseCachedResponse];
  319. GHAssertTrue(success, @"Cached data should have been used");
  320. NSTimeInterval newExpiryTimestamp = [[[[ASIDownloadCache sharedCache] cachedResponseHeadersForURL:url] objectForKey:@"X-ASIHTTPRequest-Expires"] doubleValue];
  321. NSLog(@"%@",[request responseString]);
  322. success = (newExpiryTimestamp > expiryTimestamp);
  323. GHAssertTrue(success, @"Failed to update expiry timestamp on 304");
  324. }
  325. - (void)testStringEncoding
  326. {
  327. [ASIHTTPRequest setDefaultCache:[ASIDownloadCache sharedCache]];
  328. [[ASIDownloadCache sharedCache] setShouldRespectCacheControlHeaders:NO];
  329. [[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy];
  330. [[ASIDownloadCache sharedCache] setDefaultCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy];
  331. NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/Character-Encoding/UTF-16"];
  332. ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
  333. [request startSynchronous];
  334. BOOL success = ([request responseEncoding] == NSUnicodeStringEncoding);
  335. GHAssertTrue(success,@"Got the wrong encoding back, cannot proceed with test");
  336. request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
  337. [request startSynchronous];
  338. success = [request responseEncoding] == NSUnicodeStringEncoding;
  339. GHAssertTrue(success,@"Failed to set the correct encoding on the cached response");
  340. [ASIHTTPRequest setDefaultCache:nil];
  341. }
  342. - (void)testCookies
  343. {
  344. [ASIHTTPRequest setDefaultCache:[ASIDownloadCache sharedCache]];
  345. [[ASIDownloadCache sharedCache] setShouldRespectCacheControlHeaders:NO];
  346. [[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy];
  347. [[ASIDownloadCache sharedCache] setDefaultCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy];
  348. NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/set_cookie"];
  349. ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
  350. [request startSynchronous];
  351. NSArray *cookies = [request responseCookies];
  352. BOOL success = ([cookies count]);
  353. GHAssertTrue(success,@"Got no cookies back, cannot proceed with test");
  354. request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
  355. [request startSynchronous];
  356. NSUInteger i;
  357. for (i=0; i<[cookies count]; i++) {
  358. if (![[[cookies objectAtIndex:i] name] isEqualToString:[[[request responseCookies] objectAtIndex:i] name]]) {
  359. GHAssertTrue(success,@"Failed to set response cookies correctly");
  360. return;
  361. }
  362. }
  363. [ASIHTTPRequest setDefaultCache:nil];
  364. }
  365. // Text fix for a bug where the didFinishSelector would be called twice for a cached response using ASIReloadIfDifferentCachePolicy
  366. - (void)testCacheOnlyCallsRequestFinishedOnce
  367. {
  368. // Run this request on the main thread to force delegate calls to happen synchronously
  369. [self performSelectorOnMainThread:@selector(runCacheOnlyCallsRequestFinishedOnceTest) withObject:nil waitUntilDone:YES];
  370. }
  371. - (void)runCacheOnlyCallsRequestFinishedOnceTest
  372. {
  373. [[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy];
  374. ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/i/logo.png"]];
  375. [request setCachePolicy:ASIUseDefaultCachePolicy];
  376. [request setDownloadCache:[ASIDownloadCache sharedCache]];
  377. [request setDelegate:self];
  378. [request startSynchronous];
  379. requestsFinishedCount = 0;
  380. request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/i/logo.png"]];
  381. [request setCachePolicy:ASIUseDefaultCachePolicy];
  382. [request setDownloadCache:[ASIDownloadCache sharedCache]];
  383. [request setDidFinishSelector:@selector(finishCached:)];
  384. [request setDelegate:self];
  385. [request startSynchronous];
  386. BOOL success = (requestsFinishedCount == 1);
  387. GHAssertTrue(success,@"didFinishSelector called more than once");
  388. }
  389. - (void)finishCached:(ASIHTTPRequest *)request
  390. {
  391. requestsFinishedCount++;
  392. }
  393. - (void)testRedirect
  394. {
  395. // Run this request on the main thread to force delegate calls to happen synchronously
  396. [self performSelectorOnMainThread:@selector(runRedirectTest) withObject:nil waitUntilDone:YES];
  397. }
  398. - (void)runRedirectTest
  399. {
  400. [[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy];
  401. [[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
  402. [[ASIDownloadCache sharedCache] setDefaultCachePolicy:ASIUseDefaultCachePolicy];
  403. [ASIHTTPRequest setDefaultCache:[ASIDownloadCache sharedCache]];
  404. ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/cached-redirect"]];
  405. [request startSynchronous];
  406. BOOL success = ([[[request url] absoluteString] isEqualToString:@"http://allseeing-i.com/i/logo.png"]);
  407. GHAssertTrue(success,@"Request did not redirect correctly, cannot proceed with test");
  408. requestRedirectedWasCalled = NO;
  409. request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/cached-redirect"]];
  410. [request setDelegate:self];
  411. [request startSynchronous];
  412. success = ([request didUseCachedResponse]);
  413. GHAssertTrue(success,@"Failed to cache final response");
  414. GHAssertTrue(requestRedirectedWasCalled,@"Failed to call requestRedirected");
  415. }
  416. - (void)requestRedirected:(ASIHTTPRequest *)redirected
  417. {
  418. requestRedirectedWasCalled = YES;
  419. }
  420. - (void)request:(ASIHTTPRequest *)request willRedirectToURL:(NSURL *)newURL
  421. {
  422. BOOL success = ([[newURL absoluteString] isEqualToString:@"http://allseeing-i.com/i/logo.png"]);
  423. GHAssertTrue(success,@"Request did not redirect correctly, cannot proceed with test");
  424. success = ([request didUseCachedResponse]);
  425. GHAssertTrue(success,@"Failed to cache redirect response");
  426. [request redirectToURL:newURL];
  427. }
  428. - (void)testCachedFileOverwritten
  429. {
  430. // Test for https://github.com/pokeb/asi-http-request/pull/211
  431. // This test ensures that items in the cache are correctly overwritten when a downloadDestinationPath is set,
  432. // and they need to be copied to the cache at the end of the request
  433. // This url returns different content every time
  434. NSURL *url = [NSURL URLWithString:@"http://asi/ASIHTTPRequest/tests/random-content"];
  435. ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
  436. [request setDownloadCache:[ASIDownloadCache sharedCache]];
  437. [request setSecondsToCache:0.5f];
  438. [request startSynchronous];
  439. NSString *path = [[ASIDownloadCache sharedCache] pathToCachedResponseDataForURL:url];
  440. NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];
  441. sleep(1);
  442. request = [ASIHTTPRequest requestWithURL:url];
  443. [request setDownloadCache:[ASIDownloadCache sharedCache]];
  444. [request setDownloadDestinationPath:[[self filePathForTemporaryTestFiles] stringByAppendingPathComponent:@"test.html"]];
  445. [request startSynchronous];
  446. NSString *content2 = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];
  447. BOOL success = ![content isEqualToString:content2];
  448. GHAssertTrue(success, @"Failed to overwrite response in cache");
  449. }
  450. @end