| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- //
- // CacheTests.swift
- //
- // Copyright (c) 2014-2018 Alamofire Software Foundation (http://alamofire.org/)
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- import Alamofire
- import Foundation
- import XCTest
- /// This test case tests all implemented cache policies against various `Cache-Control` header values. These tests
- /// are meant to cover the main cases of `Cache-Control` header usage, but are by no means exhaustive.
- ///
- /// These tests work as follows:
- ///
- /// - Set up an `URLCache`
- /// - Set up an `Alamofire.SessionManager`
- /// - Execute requests for all `Cache-Control` header values to prime the `NSURLCache` with cached responses
- /// - Start up a new test
- /// - Execute another round of the same requests with a given `URLRequestCachePolicy`
- /// - Verify whether the response came from the cache or from the network
- /// - This is determined by whether the cached response timestamp matches the new response timestamp
- ///
- /// An important thing to note is the difference in behavior between iOS and macOS. On iOS, a response with
- /// a `Cache-Control` header value of `no-store` is still written into the `NSURLCache` where on macOS, it is not.
- /// The different tests below reflect and demonstrate this behavior.
- ///
- /// For information about `Cache-Control` HTTP headers, please refer to RFC 2616 - Section 14.9.
- class CacheTestCase: BaseTestCase {
- // MARK: -
- struct CacheControl {
- static let publicControl = "public"
- static let privateControl = "private"
- static let maxAgeNonExpired = "max-age=3600"
- static let maxAgeExpired = "max-age=0"
- static let noCache = "no-cache"
- static let noStore = "no-store"
- static var allValues: [String] {
- [CacheControl.publicControl,
- CacheControl.privateControl,
- CacheControl.maxAgeNonExpired,
- CacheControl.maxAgeExpired,
- CacheControl.noCache,
- CacheControl.noStore]
- }
- }
- // MARK: - Properties
- var urlCache: URLCache!
- var manager: Session!
- let urlString = "https://httpbin.org/response-headers"
- let requestTimeout: TimeInterval = 30
- var requests: [String: URLRequest] = [:]
- var timestamps: [String: String] = [:]
- // MARK: - Setup and Teardown
- override func setUp() {
- super.setUp()
- urlCache = {
- let capacity = 50 * 1024 * 1024 // MBs
- #if targetEnvironment(macCatalyst)
- return URLCache(memoryCapacity: capacity, diskCapacity: capacity)
- #else
- return URLCache(memoryCapacity: capacity, diskCapacity: capacity, diskPath: nil)
- #endif
- }()
- manager = {
- let configuration: URLSessionConfiguration = {
- let configuration = URLSessionConfiguration.default
- configuration.headers = HTTPHeaders.default
- configuration.requestCachePolicy = .useProtocolCachePolicy
- configuration.urlCache = urlCache
- return configuration
- }()
- let manager = Session(configuration: configuration)
- return manager
- }()
- primeCachedResponses()
- }
- override func tearDown() {
- super.tearDown()
- requests.removeAll()
- timestamps.removeAll()
- urlCache.removeAllCachedResponses()
- }
- // MARK: - Cache Priming Methods
- /**
- Executes a request for all `Cache-Control` header values to load the response into the `URLCache`.
- This implementation leverages dispatch groups to execute all the requests as well as wait an additional
- second before returning. This ensures the cache contains responses for all requests that are at least
- one second old. This allows the tests to distinguish whether the subsequent responses come from the cache
- or the network based on the timestamp of the response.
- */
- func primeCachedResponses() {
- let dispatchGroup = DispatchGroup()
- let serialQueue = DispatchQueue(label: "org.alamofire.cache-tests")
- for cacheControl in CacheControl.allValues {
- dispatchGroup.enter()
- let request = startRequest(cacheControl: cacheControl,
- queue: serialQueue,
- completion: { _, response in
- let timestamp = response!.allHeaderFields["Date"] as! String
- self.timestamps[cacheControl] = timestamp
- dispatchGroup.leave()
- })
- requests[cacheControl] = request
- }
- // Wait for all requests to complete
- _ = dispatchGroup.wait(timeout: .now() + 30)
- // Pause for 1 additional second to ensure all timestamps will be different
- dispatchGroup.enter()
- serialQueue.asyncAfter(deadline: .now() + 1) {
- dispatchGroup.leave()
- }
- // Wait for our 1 second pause to complete
- _ = dispatchGroup.wait(timeout: .now() + 1.25)
- }
- // MARK: - Request Helper Methods
- func urlRequest(cacheControl: String, cachePolicy: URLRequest.CachePolicy) -> URLRequest {
- let parameters = ["Cache-Control": cacheControl]
- let url = URL(string: urlString)!
- var urlRequest = URLRequest(url: url, cachePolicy: cachePolicy, timeoutInterval: requestTimeout)
- urlRequest.httpMethod = HTTPMethod.get.rawValue
- do {
- return try URLEncoding.default.encode(urlRequest, with: parameters)
- } catch {
- return urlRequest
- }
- }
- @discardableResult
- func startRequest(cacheControl: String,
- cachePolicy: URLRequest.CachePolicy = .useProtocolCachePolicy,
- queue: DispatchQueue = .main,
- completion: @escaping (URLRequest?, HTTPURLResponse?) -> Void)
- -> URLRequest {
- let urlRequest = self.urlRequest(cacheControl: cacheControl, cachePolicy: cachePolicy)
- let request = manager.request(urlRequest)
- request.response(queue: queue,
- completionHandler: { response in
- completion(response.request, response.response)
- })
- return urlRequest
- }
- // MARK: - Test Execution and Verification
- func executeTest(cachePolicy: URLRequest.CachePolicy,
- cacheControl: String,
- shouldReturnCachedResponse: Bool) {
- // Given
- let expectation = self.expectation(description: "GET request to httpbin")
- var response: HTTPURLResponse?
- // When
- startRequest(cacheControl: cacheControl, cachePolicy: cachePolicy) { _, responseResponse in
- response = responseResponse
- expectation.fulfill()
- }
- waitForExpectations(timeout: timeout, handler: nil)
- // Then
- verifyResponse(response, forCacheControl: cacheControl, isCachedResponse: shouldReturnCachedResponse)
- }
- func verifyResponse(_ response: HTTPURLResponse?, forCacheControl cacheControl: String, isCachedResponse: Bool) {
- guard let cachedResponseTimestamp = timestamps[cacheControl] else {
- XCTFail("cached response timestamp should not be nil")
- return
- }
- if let response = response, let timestamp = response.allHeaderFields["Date"] as? String {
- if isCachedResponse {
- XCTAssertEqual(timestamp, cachedResponseTimestamp, "timestamps should be equal")
- } else {
- XCTAssertNotEqual(timestamp, cachedResponseTimestamp, "timestamps should not be equal")
- }
- } else {
- XCTFail("response should not be nil")
- }
- }
- // MARK: - Tests
- func testURLCacheContainsCachedResponsesForAllRequests() {
- // Given
- let publicRequest = requests[CacheControl.publicControl]!
- let privateRequest = requests[CacheControl.privateControl]!
- let maxAgeNonExpiredRequest = requests[CacheControl.maxAgeNonExpired]!
- let maxAgeExpiredRequest = requests[CacheControl.maxAgeExpired]!
- let noCacheRequest = requests[CacheControl.noCache]!
- let noStoreRequest = requests[CacheControl.noStore]!
- // When
- let publicResponse = urlCache.cachedResponse(for: publicRequest)
- let privateResponse = urlCache.cachedResponse(for: privateRequest)
- let maxAgeNonExpiredResponse = urlCache.cachedResponse(for: maxAgeNonExpiredRequest)
- let maxAgeExpiredResponse = urlCache.cachedResponse(for: maxAgeExpiredRequest)
- let noCacheResponse = urlCache.cachedResponse(for: noCacheRequest)
- let noStoreResponse = urlCache.cachedResponse(for: noStoreRequest)
- // Then
- XCTAssertNotNil(publicResponse, "\(CacheControl.publicControl) response should not be nil")
- XCTAssertNotNil(privateResponse, "\(CacheControl.privateControl) response should not be nil")
- XCTAssertNotNil(maxAgeNonExpiredResponse, "\(CacheControl.maxAgeNonExpired) response should not be nil")
- XCTAssertNotNil(maxAgeExpiredResponse, "\(CacheControl.maxAgeExpired) response should not be nil")
- XCTAssertNotNil(noCacheResponse, "\(CacheControl.noCache) response should not be nil")
- XCTAssertNil(noStoreResponse, "\(CacheControl.noStore) response should be nil")
- }
- func testDefaultCachePolicy() {
- let cachePolicy: URLRequest.CachePolicy = .useProtocolCachePolicy
- executeTest(cachePolicy: cachePolicy, cacheControl: CacheControl.publicControl, shouldReturnCachedResponse: false)
- executeTest(cachePolicy: cachePolicy, cacheControl: CacheControl.privateControl, shouldReturnCachedResponse: false)
- executeTest(cachePolicy: cachePolicy, cacheControl: CacheControl.maxAgeNonExpired, shouldReturnCachedResponse: true)
- executeTest(cachePolicy: cachePolicy, cacheControl: CacheControl.maxAgeExpired, shouldReturnCachedResponse: false)
- executeTest(cachePolicy: cachePolicy, cacheControl: CacheControl.noCache, shouldReturnCachedResponse: false)
- executeTest(cachePolicy: cachePolicy, cacheControl: CacheControl.noStore, shouldReturnCachedResponse: false)
- }
- func testIgnoreLocalCacheDataPolicy() {
- let cachePolicy: URLRequest.CachePolicy = .reloadIgnoringLocalCacheData
- executeTest(cachePolicy: cachePolicy, cacheControl: CacheControl.publicControl, shouldReturnCachedResponse: false)
- executeTest(cachePolicy: cachePolicy, cacheControl: CacheControl.privateControl, shouldReturnCachedResponse: false)
- executeTest(cachePolicy: cachePolicy, cacheControl: CacheControl.maxAgeNonExpired, shouldReturnCachedResponse: false)
- executeTest(cachePolicy: cachePolicy, cacheControl: CacheControl.maxAgeExpired, shouldReturnCachedResponse: false)
- executeTest(cachePolicy: cachePolicy, cacheControl: CacheControl.noCache, shouldReturnCachedResponse: false)
- executeTest(cachePolicy: cachePolicy, cacheControl: CacheControl.noStore, shouldReturnCachedResponse: false)
- }
- func testUseLocalCacheDataIfExistsOtherwiseLoadFromNetworkPolicy() {
- let cachePolicy: URLRequest.CachePolicy = .returnCacheDataElseLoad
- executeTest(cachePolicy: cachePolicy, cacheControl: CacheControl.publicControl, shouldReturnCachedResponse: true)
- executeTest(cachePolicy: cachePolicy, cacheControl: CacheControl.privateControl, shouldReturnCachedResponse: true)
- executeTest(cachePolicy: cachePolicy, cacheControl: CacheControl.maxAgeNonExpired, shouldReturnCachedResponse: true)
- executeTest(cachePolicy: cachePolicy, cacheControl: CacheControl.maxAgeExpired, shouldReturnCachedResponse: true)
- executeTest(cachePolicy: cachePolicy, cacheControl: CacheControl.noCache, shouldReturnCachedResponse: true)
- executeTest(cachePolicy: cachePolicy, cacheControl: CacheControl.noStore, shouldReturnCachedResponse: false)
- }
- func testUseLocalCacheDataAndDontLoadFromNetworkPolicy() {
- let cachePolicy: URLRequest.CachePolicy = .returnCacheDataDontLoad
- executeTest(cachePolicy: cachePolicy, cacheControl: CacheControl.publicControl, shouldReturnCachedResponse: true)
- executeTest(cachePolicy: cachePolicy, cacheControl: CacheControl.privateControl, shouldReturnCachedResponse: true)
- executeTest(cachePolicy: cachePolicy, cacheControl: CacheControl.maxAgeNonExpired, shouldReturnCachedResponse: true)
- executeTest(cachePolicy: cachePolicy, cacheControl: CacheControl.maxAgeExpired, shouldReturnCachedResponse: true)
- executeTest(cachePolicy: cachePolicy, cacheControl: CacheControl.noCache, shouldReturnCachedResponse: true)
- // Given
- let expectation = self.expectation(description: "GET request to httpbin")
- var response: HTTPURLResponse?
- // When
- startRequest(cacheControl: CacheControl.noStore, cachePolicy: cachePolicy) { _, responseResponse in
- response = responseResponse
- expectation.fulfill()
- }
- waitForExpectations(timeout: timeout, handler: nil)
- // Then
- XCTAssertNil(response, "response should be nil")
- }
- }
|