2
0

CachedResponseHandlerTests.swift 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. //
  2. // CachedResponseHandlerTests.swift
  3. //
  4. // Copyright (c) 2019 Alamofire Software Foundation (http://alamofire.org/)
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. //
  24. import Alamofire
  25. import Foundation
  26. import XCTest
  27. final class CachedResponseHandlerTestCase: BaseTestCase {
  28. // MARK: Tests - Per Request
  29. @MainActor
  30. func testThatRequestCachedResponseHandlerCanCacheResponse() {
  31. // Given
  32. let session = session()
  33. var response: DataResponse<Data?, AFError>?
  34. let expectation = expectation(description: "Request should cache response")
  35. // When
  36. let request = session.request(.default).cacheResponse(using: ResponseCacher.cache).response { resp in
  37. response = resp
  38. expectation.fulfill()
  39. }
  40. waitForExpectations(timeout: timeout)
  41. // Then
  42. XCTAssertEqual(response?.result.isSuccess, true)
  43. XCTAssertTrue(session.cachedResponseExists(for: request))
  44. }
  45. @MainActor
  46. func testThatRequestCachedResponseHandlerCanNotCacheResponse() {
  47. // Given
  48. let session = session()
  49. var response: DataResponse<Data?, AFError>?
  50. let expectation = expectation(description: "Request should not cache response")
  51. // When
  52. let request = session.request(.default).cacheResponse(using: ResponseCacher.doNotCache).response { resp in
  53. response = resp
  54. expectation.fulfill()
  55. }
  56. waitForExpectations(timeout: timeout)
  57. // Then
  58. XCTAssertEqual(response?.result.isSuccess, true)
  59. XCTAssertFalse(session.cachedResponseExists(for: request))
  60. }
  61. @MainActor
  62. func testThatRequestCachedResponseHandlerCanModifyCacheResponse() {
  63. // Given
  64. let session = session()
  65. var response: DataResponse<Data?, AFError>?
  66. let expectation = expectation(description: "Request should cache response")
  67. // When
  68. let cacher = ResponseCacher(behavior: .modify { _, response in
  69. CachedURLResponse(response: response.response,
  70. data: response.data,
  71. userInfo: ["key": "value"],
  72. storagePolicy: .allowed)
  73. })
  74. let request = session.request(.default).cacheResponse(using: cacher).response { resp in
  75. response = resp
  76. expectation.fulfill()
  77. }
  78. waitForExpectations(timeout: timeout)
  79. // Then
  80. XCTAssertEqual(response?.result.isSuccess, true)
  81. XCTAssertTrue(session.cachedResponseExists(for: request))
  82. XCTAssertEqual(session.cachedResponse(for: request)?.userInfo?["key"] as? String, "value")
  83. }
  84. // MARK: Tests - Per Session
  85. @MainActor
  86. func testThatSessionCachedResponseHandlerCanCacheResponse() {
  87. // Given
  88. let session = session(using: ResponseCacher.cache)
  89. var response: DataResponse<Data?, AFError>?
  90. let expectation = expectation(description: "Request should cache response")
  91. // When
  92. let request = session.request(.default).response { resp in
  93. response = resp
  94. expectation.fulfill()
  95. }
  96. waitForExpectations(timeout: timeout)
  97. // Then
  98. XCTAssertEqual(response?.result.isSuccess, true)
  99. XCTAssertTrue(session.cachedResponseExists(for: request))
  100. }
  101. @MainActor
  102. func testThatSessionCachedResponseHandlerCanNotCacheResponse() {
  103. // Given
  104. let session = session(using: ResponseCacher.doNotCache)
  105. var response: DataResponse<Data?, AFError>?
  106. let expectation = expectation(description: "Request should not cache response")
  107. // When
  108. let request = session.request(.default).cacheResponse(using: ResponseCacher.doNotCache).response { resp in
  109. response = resp
  110. expectation.fulfill()
  111. }
  112. waitForExpectations(timeout: timeout)
  113. // Then
  114. XCTAssertEqual(response?.result.isSuccess, true)
  115. XCTAssertFalse(session.cachedResponseExists(for: request))
  116. }
  117. @MainActor
  118. func testThatSessionCachedResponseHandlerCanModifyCacheResponse() {
  119. // Given
  120. let cacher = ResponseCacher(behavior: .modify { _, response in
  121. CachedURLResponse(response: response.response,
  122. data: response.data,
  123. userInfo: ["key": "value"],
  124. storagePolicy: .allowed)
  125. })
  126. let session = session(using: cacher)
  127. var response: DataResponse<Data?, AFError>?
  128. let expectation = expectation(description: "Request should cache response")
  129. // When
  130. let request = session.request(.default).cacheResponse(using: cacher).response { resp in
  131. response = resp
  132. expectation.fulfill()
  133. }
  134. waitForExpectations(timeout: timeout)
  135. // Then
  136. XCTAssertEqual(response?.result.isSuccess, true)
  137. XCTAssertTrue(session.cachedResponseExists(for: request))
  138. XCTAssertEqual(session.cachedResponse(for: request)?.userInfo?["key"] as? String, "value")
  139. }
  140. // MARK: Tests - Per Request Prioritization
  141. @MainActor
  142. func testThatRequestCachedResponseHandlerIsPrioritizedOverSessionCachedResponseHandler() {
  143. // Given
  144. let session = session(using: ResponseCacher.cache)
  145. var response: DataResponse<Data?, AFError>?
  146. let expectation = expectation(description: "Request should cache response")
  147. // When
  148. let request = session.request(.default).cacheResponse(using: ResponseCacher.doNotCache).response { resp in
  149. response = resp
  150. expectation.fulfill()
  151. }
  152. waitForExpectations(timeout: timeout)
  153. // Then
  154. XCTAssertEqual(response?.result.isSuccess, true)
  155. XCTAssertFalse(session.cachedResponseExists(for: request))
  156. }
  157. // MARK: Private - Test Helpers
  158. private func session(using handler: (any CachedResponseHandler)? = nil) -> Session {
  159. let configuration = URLSessionConfiguration.af.default
  160. let capacity = 100_000_000
  161. let cache: URLCache
  162. #if targetEnvironment(macCatalyst)
  163. let directory = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
  164. cache = URLCache(memoryCapacity: capacity, diskCapacity: capacity, directory: directory)
  165. #else
  166. let directory = (NSTemporaryDirectory() as NSString).appendingPathComponent(UUID().uuidString)
  167. cache = URLCache(memoryCapacity: capacity, diskCapacity: capacity, diskPath: directory)
  168. #endif
  169. configuration.urlCache = cache
  170. return Session(configuration: configuration, cachedResponseHandler: handler)
  171. }
  172. }
  173. final class StaticCachedResponseHandlerTests: BaseTestCase {
  174. func takeCachedResponseHandler(_ handler: any CachedResponseHandler) {
  175. _ = handler
  176. }
  177. func testThatCacheResponseCacherCanBeCreatedStaticallyFromProtocol() {
  178. // Given, When, Then
  179. takeCachedResponseHandler(.cache)
  180. }
  181. func testThatDoNotCacheResponseCacherCanBeCreatedStaticallyFromProtocol() {
  182. // Given, When, Then
  183. takeCachedResponseHandler(.doNotCache)
  184. }
  185. func testThatModifyResponseCacherCanBeCreatedStaticallyFromProtocol() {
  186. // Given, When, Then
  187. takeCachedResponseHandler(.modify { _, _ in nil })
  188. }
  189. }
  190. // MARK: -
  191. extension Session {
  192. fileprivate func cachedResponse(for request: Request) -> CachedURLResponse? {
  193. guard let urlRequest = request.request else { return nil }
  194. return session.configuration.urlCache?.cachedResponse(for: urlRequest)
  195. }
  196. fileprivate func cachedResponseExists(for request: Request) -> Bool {
  197. cachedResponse(for: request) != nil
  198. }
  199. }