Browse Source

Updated cache tests to pass on all OS's including iOS 8.1 and 8.2.

Christian Noon 10 years ago
parent
commit
d3afcca4f6
1 changed files with 43 additions and 15 deletions
  1. 43 15
      Tests/CacheTests.swift

+ 43 - 15
Tests/CacheTests.swift

@@ -231,6 +231,22 @@ class CacheTestCase: BaseTestCase {
         }
         }
     }
     }
 
 
+    // MARK: - Cache Helper Methods
+
+    private func isCachedResponseForNoStoreHeaderExpected() -> Bool {
+        var storedInCache = false
+
+        #if os(iOS)
+            let operatingSystemVersion = NSOperatingSystemVersion(majorVersion: 8, minorVersion: 3, patchVersion: 0)
+
+            if !NSProcessInfo().isOperatingSystemAtLeastVersion(operatingSystemVersion) {
+                storedInCache = true
+            }
+        #endif
+
+        return storedInCache
+    }
+
     // MARK: - Tests
     // MARK: - Tests
 
 
     func testURLCacheContainsCachedResponsesForAllRequests() {
     func testURLCacheContainsCachedResponsesForAllRequests() {
@@ -256,7 +272,12 @@ class CacheTestCase: BaseTestCase {
         XCTAssertNotNil(maxAgeNonExpiredResponse, "\(CacheControl.MaxAgeNonExpired) response should not be nil")
         XCTAssertNotNil(maxAgeNonExpiredResponse, "\(CacheControl.MaxAgeNonExpired) response should not be nil")
         XCTAssertNotNil(maxAgeExpiredResponse, "\(CacheControl.MaxAgeExpired) response should not be nil")
         XCTAssertNotNil(maxAgeExpiredResponse, "\(CacheControl.MaxAgeExpired) response should not be nil")
         XCTAssertNotNil(noCacheResponse, "\(CacheControl.NoCache) response should not be nil")
         XCTAssertNotNil(noCacheResponse, "\(CacheControl.NoCache) response should not be nil")
-        XCTAssertNil(noStoreResponse, "\(CacheControl.NoStore) response should be nil")
+
+        if isCachedResponseForNoStoreHeaderExpected() {
+            XCTAssertNotNil(noStoreResponse, "\(CacheControl.NoStore) response should not be nil")
+        } else {
+            XCTAssertNil(noStoreResponse, "\(CacheControl.NoStore) response should be nil")
+        }
     }
     }
 
 
     func testDefaultCachePolicy() {
     func testDefaultCachePolicy() {
@@ -289,7 +310,12 @@ class CacheTestCase: BaseTestCase {
         executeTest(cachePolicy: cachePolicy, cacheControl: CacheControl.MaxAgeNonExpired, shouldReturnCachedResponse: true)
         executeTest(cachePolicy: cachePolicy, cacheControl: CacheControl.MaxAgeNonExpired, shouldReturnCachedResponse: true)
         executeTest(cachePolicy: cachePolicy, cacheControl: CacheControl.MaxAgeExpired, shouldReturnCachedResponse: true)
         executeTest(cachePolicy: cachePolicy, cacheControl: CacheControl.MaxAgeExpired, shouldReturnCachedResponse: true)
         executeTest(cachePolicy: cachePolicy, cacheControl: CacheControl.NoCache, shouldReturnCachedResponse: true)
         executeTest(cachePolicy: cachePolicy, cacheControl: CacheControl.NoCache, shouldReturnCachedResponse: true)
-        executeTest(cachePolicy: cachePolicy, cacheControl: CacheControl.NoStore, shouldReturnCachedResponse: false)
+
+        if isCachedResponseForNoStoreHeaderExpected() {
+            executeTest(cachePolicy: cachePolicy, cacheControl: CacheControl.NoStore, shouldReturnCachedResponse: true)
+        } else {
+            executeTest(cachePolicy: cachePolicy, cacheControl: CacheControl.NoStore, shouldReturnCachedResponse: false)
+        }
     }
     }
 
 
     func testUseLocalCacheDataAndDontLoadFromNetworkPolicy() {
     func testUseLocalCacheDataAndDontLoadFromNetworkPolicy() {
@@ -301,21 +327,23 @@ class CacheTestCase: BaseTestCase {
         executeTest(cachePolicy: cachePolicy, cacheControl: CacheControl.MaxAgeExpired, shouldReturnCachedResponse: true)
         executeTest(cachePolicy: cachePolicy, cacheControl: CacheControl.MaxAgeExpired, shouldReturnCachedResponse: true)
         executeTest(cachePolicy: cachePolicy, cacheControl: CacheControl.NoCache, shouldReturnCachedResponse: true)
         executeTest(cachePolicy: cachePolicy, cacheControl: CacheControl.NoCache, shouldReturnCachedResponse: true)
 
 
-        // Execute Test - CacheControl.NoStore
+        if isCachedResponseForNoStoreHeaderExpected() {
+            executeTest(cachePolicy: cachePolicy, cacheControl: CacheControl.NoStore, shouldReturnCachedResponse: true)
+        } else {
+            // Given
+            let expectation = expectationWithDescription("GET request to httpbin")
+            var response: NSHTTPURLResponse?
+
+            // When
+            let request = startRequest(cacheControl: CacheControl.NoStore, cachePolicy: cachePolicy) { _, responseResponse in
+                response = responseResponse
+                expectation.fulfill()
+            }
 
 
-        // Given
-        let expectation = expectationWithDescription("GET request to httpbin")
-        var response: NSHTTPURLResponse?
+            waitForExpectationsWithTimeout(self.defaultTimeout, handler: nil)
 
 
-        // When
-        let request = startRequest(cacheControl: CacheControl.NoStore, cachePolicy: cachePolicy) { _, responseResponse in
-            response = responseResponse
-            expectation.fulfill()
+            // Then
+            XCTAssertNil(response, "response should be nil")
         }
         }
-
-        waitForExpectationsWithTimeout(self.defaultTimeout, handler: nil)
-
-        // Then
-        XCTAssertNil(response, "response should be nil")
     }
     }
 }
 }