CachedResponseHandlerTests.swift 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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: Properties
  29. private let urlString = "https://httpbin.org/get"
  30. // MARK: Tests - Per Request
  31. func testThatRequestCachedResponseHandlerCanCacheResponse() {
  32. // Given
  33. let session = self.session()
  34. var response: DataResponse<Data?>?
  35. let expectation = self.expectation(description: "Request should cache response")
  36. // When
  37. let request = session.request(urlString).cacheResponse(using: ResponseCacher.cache).response { resp in
  38. response = resp
  39. expectation.fulfill()
  40. }
  41. waitForExpectations(timeout: timeout, handler: nil)
  42. // Then
  43. XCTAssertEqual(response?.result.isSuccess, true)
  44. XCTAssertTrue(session.cachedResponseExists(for: request))
  45. }
  46. func testThatRequestCachedResponseHandlerCanNotCacheResponse() {
  47. // Given
  48. let session = self.session()
  49. var response: DataResponse<Data?>?
  50. let expectation = self.expectation(description: "Request should not cache response")
  51. // When
  52. let request = session.request(urlString).cacheResponse(using: ResponseCacher.doNotCache).response { resp in
  53. response = resp
  54. expectation.fulfill()
  55. }
  56. waitForExpectations(timeout: timeout, handler: nil)
  57. // Then
  58. XCTAssertEqual(response?.result.isSuccess, true)
  59. XCTAssertFalse(session.cachedResponseExists(for: request))
  60. }
  61. func testThatRequestCachedResponseHandlerCanModifyCacheResponse() {
  62. // Given
  63. let session = self.session()
  64. var response: DataResponse<Data?>?
  65. let expectation = self.expectation(description: "Request should cache response")
  66. // When
  67. let cacher = ResponseCacher(
  68. behavior: .modify { _, response in
  69. return CachedURLResponse(
  70. response: response.response,
  71. data: response.data,
  72. userInfo: ["key": "value"],
  73. storagePolicy: .allowed
  74. )
  75. }
  76. )
  77. let request = session.request(urlString).cacheResponse(using: cacher).response { resp in
  78. response = resp
  79. expectation.fulfill()
  80. }
  81. waitForExpectations(timeout: timeout, handler: nil)
  82. // Then
  83. XCTAssertEqual(response?.result.isSuccess, true)
  84. XCTAssertTrue(session.cachedResponseExists(for: request))
  85. XCTAssertEqual(session.cachedResponse(for: request)?.userInfo?["key"] as? String, "value")
  86. }
  87. // MARK: Tests - Per Session
  88. func testThatSessionCachedResponseHandlerCanCacheResponse() {
  89. // Given
  90. let session = self.session(using: ResponseCacher.cache)
  91. var response: DataResponse<Data?>?
  92. let expectation = self.expectation(description: "Request should cache response")
  93. // When
  94. let request = session.request(urlString).response { resp in
  95. response = resp
  96. expectation.fulfill()
  97. }
  98. waitForExpectations(timeout: timeout, handler: nil)
  99. // Then
  100. XCTAssertEqual(response?.result.isSuccess, true)
  101. XCTAssertTrue(session.cachedResponseExists(for: request))
  102. }
  103. func testThatSessionCachedResponseHandlerCanNotCacheResponse() {
  104. // Given
  105. let session = self.session(using: ResponseCacher.doNotCache)
  106. var response: DataResponse<Data?>?
  107. let expectation = self.expectation(description: "Request should not cache response")
  108. // When
  109. let request = session.request(urlString).cacheResponse(using: ResponseCacher.doNotCache).response { resp in
  110. response = resp
  111. expectation.fulfill()
  112. }
  113. waitForExpectations(timeout: timeout, handler: nil)
  114. // Then
  115. XCTAssertEqual(response?.result.isSuccess, true)
  116. XCTAssertFalse(session.cachedResponseExists(for: request))
  117. }
  118. func testThatSessionCachedResponseHandlerCanModifyCacheResponse() {
  119. // Given
  120. let cacher = ResponseCacher(
  121. behavior: .modify { _, response in
  122. return CachedURLResponse(
  123. response: response.response,
  124. data: response.data,
  125. userInfo: ["key": "value"],
  126. storagePolicy: .allowed
  127. )
  128. }
  129. )
  130. let session = self.session(using: cacher)
  131. var response: DataResponse<Data?>?
  132. let expectation = self.expectation(description: "Request should cache response")
  133. // When
  134. let request = session.request(urlString).cacheResponse(using: cacher).response { resp in
  135. response = resp
  136. expectation.fulfill()
  137. }
  138. waitForExpectations(timeout: timeout, handler: nil)
  139. // Then
  140. XCTAssertEqual(response?.result.isSuccess, true)
  141. XCTAssertTrue(session.cachedResponseExists(for: request))
  142. XCTAssertEqual(session.cachedResponse(for: request)?.userInfo?["key"] as? String, "value")
  143. }
  144. // MARK: Tests - Per Request Prioritization
  145. func testThatRequestCachedResponseHandlerIsPrioritizedOverSessionCachedResponseHandler() {
  146. // Given
  147. let session = self.session(using: ResponseCacher.cache)
  148. var response: DataResponse<Data?>?
  149. let expectation = self.expectation(description: "Request should cache response")
  150. // When
  151. let request = session.request(urlString).cacheResponse(using: ResponseCacher.doNotCache).response { resp in
  152. response = resp
  153. expectation.fulfill()
  154. }
  155. waitForExpectations(timeout: timeout, handler: nil)
  156. // Then
  157. XCTAssertEqual(response?.result.isSuccess, true)
  158. XCTAssertFalse(session.cachedResponseExists(for: request))
  159. }
  160. // MARK: Private - Test Helpers
  161. private func session(using handler: CachedResponseHandler? = nil) -> Session {
  162. let configuration = URLSessionConfiguration.alamofireDefault
  163. configuration.urlCache = URLCache(memoryCapacity: 100_000_000, diskCapacity: 100_000_000, diskPath: UUID().uuidString)
  164. return Session(configuration: configuration, cachedResponseHandler: handler)
  165. }
  166. }
  167. // MARK: -
  168. extension Session {
  169. fileprivate func cachedResponse(for request: Request) -> CachedURLResponse? {
  170. guard let urlRequest = request.request else { return nil }
  171. return session.configuration.urlCache?.cachedResponse(for: urlRequest)
  172. }
  173. fileprivate func cachedResponseExists(for request: Request) -> Bool {
  174. return cachedResponse(for: request) != nil
  175. }
  176. }