ImageDownloaderTests.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. //
  2. // ImageDownloaderTests.swift
  3. // Kingfisher
  4. //
  5. // Created by Wei Wang on 15/4/10.
  6. //
  7. // Copyright (c) 2019 Wei Wang <onevcat@gmail.com>
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. import XCTest
  27. @testable import Kingfisher
  28. class ImageDownloaderTests: XCTestCase {
  29. var downloader: ImageDownloader!
  30. var modifier = URLModifier()
  31. override class func setUp() {
  32. super.setUp()
  33. LSNocilla.sharedInstance().start()
  34. }
  35. override class func tearDown() {
  36. LSNocilla.sharedInstance().stop()
  37. super.tearDown()
  38. }
  39. override func setUp() {
  40. super.setUp()
  41. downloader = ImageDownloader(name: "test")
  42. }
  43. override func tearDown() {
  44. LSNocilla.sharedInstance().clearStubs()
  45. downloader = nil
  46. super.tearDown()
  47. }
  48. func testDownloadAnImage() {
  49. let exp = expectation(description: #function)
  50. let url = testURLs[0]
  51. stub(url, data: testImageData)
  52. downloader.downloadImage(with: url) { result in
  53. XCTAssertNotNil(result.value)
  54. exp.fulfill()
  55. }
  56. waitForExpectations(timeout: 3, handler: nil)
  57. }
  58. func testDownloadMultipleImages() {
  59. let exp = expectation(description: #function)
  60. let group = DispatchGroup()
  61. for url in testURLs {
  62. group.enter()
  63. stub(url, data: testImageData)
  64. downloader.downloadImage(with: url) { result in
  65. XCTAssertNotNil(result.value)
  66. group.leave()
  67. }
  68. }
  69. group.notify(queue: .main, execute: exp.fulfill)
  70. waitForExpectations(timeout: 3, handler: nil)
  71. }
  72. func testDownloadAnImageWithMultipleCallback() {
  73. let exp = expectation(description: #function)
  74. let group = DispatchGroup()
  75. let url = testURLs[0]
  76. stub(url, data: testImageData)
  77. for _ in 0...5 {
  78. group.enter()
  79. downloader.downloadImage(with: url) { result in
  80. XCTAssertNotNil(result.value)
  81. group.leave()
  82. }
  83. }
  84. group.notify(queue: .main, execute: exp.fulfill)
  85. waitForExpectations(timeout: 5, handler: nil)
  86. }
  87. func testDownloadWithModifyingRequest() {
  88. let exp = expectation(description: #function)
  89. let url = testURLs[0]
  90. stub(url, data: testImageData)
  91. modifier.url = url
  92. let someURL = URL(string: "some_strange_url")!
  93. downloader.downloadImage(with: someURL, options: [.requestModifier(modifier)]) { result in
  94. XCTAssertNotNil(result.value)
  95. XCTAssertEqual(result.value?.url, url)
  96. exp.fulfill()
  97. }
  98. waitForExpectations(timeout: 3, handler: nil)
  99. }
  100. func testDownloadWithModifyingRequestToNil() {
  101. let nilModifier = AnyModifier { _ in
  102. return nil
  103. }
  104. let exp = expectation(description: #function)
  105. let someURL = URL(string: "some_strange_url")!
  106. downloader.downloadImage(with: someURL, options: [.requestModifier(nilModifier)]) { result in
  107. XCTAssertNotNil(result.error)
  108. guard case .requestError(reason: .emptyRequest) = result.error! else {
  109. XCTFail()
  110. fatalError()
  111. }
  112. exp.fulfill()
  113. }
  114. waitForExpectations(timeout: 3, handler: nil)
  115. }
  116. func testServerInvalidStatusCode() {
  117. let exp = expectation(description: #function)
  118. let url = testURLs[0]
  119. stub(url, data: testImageData, statusCode: 404)
  120. downloader.downloadImage(with: url) { result in
  121. XCTAssertNotNil(result.error)
  122. XCTAssertTrue(result.error!.isInvalidResponseStatusCode(404))
  123. exp.fulfill()
  124. }
  125. waitForExpectations(timeout: 3, handler: nil)
  126. }
  127. // Since we could not receive one challage, no test for trusted hosts currently.
  128. // See http://stackoverflow.com/questions/27065372/ for more.
  129. func testSSLCertificateValidation() {
  130. LSNocilla.sharedInstance().stop()
  131. let exp = expectation(description: #function)
  132. let downloader = ImageDownloader(name: "ssl.test")
  133. let url = URL(string: "https://testssl-expire.disig.sk/Expired.png")!
  134. downloader.downloadImage(with: url) { result in
  135. XCTAssertNotNil(result.error)
  136. if case .responseError(reason: .URLSessionError(let error)) = result.error! {
  137. let nsError = error as NSError
  138. XCTAssert(nsError.code == NSURLErrorServerCertificateUntrusted ||
  139. nsError.code == NSURLErrorSecureConnectionFailed,
  140. "Error should be NSURLErrorServerCertificateUntrusted, but \(String(describing: error))")
  141. } else {
  142. XCTFail()
  143. }
  144. exp.fulfill()
  145. LSNocilla.sharedInstance().start()
  146. }
  147. waitForExpectations(timeout: 20) { error in
  148. XCTAssertNil(error, "\(String(describing: error))")
  149. LSNocilla.sharedInstance().start()
  150. }
  151. }
  152. func testDownloadResultErrorAndRetry() {
  153. let exp = expectation(description: #function)
  154. let url = testURLs[0]
  155. stub(url, errorCode: -1)
  156. downloader.downloadImage(with: url) { result in
  157. XCTAssertNotNil(result.error)
  158. LSNocilla.sharedInstance().clearStubs()
  159. stub(url, data: testImageData)
  160. // Retry the download
  161. self.downloader.downloadImage(with: url) { result in
  162. XCTAssertNil(result.error)
  163. exp.fulfill()
  164. }
  165. }
  166. waitForExpectations(timeout: 3, handler: nil)
  167. }
  168. func testDownloadEmptyURL() {
  169. let exp = expectation(description: #function)
  170. modifier.url = nil
  171. let url = URL(string: "http://onevcat.com")!
  172. downloader.downloadImage(
  173. with: url,
  174. options: [.requestModifier(modifier)],
  175. progressBlock: { received, totalSize in XCTFail("The progress block should not be called.") })
  176. {
  177. result in
  178. XCTAssertNotNil(result.error)
  179. if case .requestError(reason: .invalidURL(let request)) = result.error! {
  180. XCTAssertNil(request.url)
  181. } else {
  182. XCTFail()
  183. }
  184. exp.fulfill()
  185. }
  186. waitForExpectations(timeout: 3, handler: nil)
  187. }
  188. func testDownloadTaskProperty() {
  189. let task = downloader.downloadImage(with: URL(string: "1234")!)
  190. XCTAssertNotNil(task, "The task should exist.")
  191. }
  192. func testCancelDownloadTask() {
  193. let exp = expectation(description: #function)
  194. let url = testURLs[0]
  195. let stub = delayedStub(url, data: testImageData, length: 123)
  196. let task = downloader.downloadImage(
  197. with: url,
  198. progressBlock: { _, _ in XCTFail() })
  199. {
  200. result in
  201. XCTAssertNotNil(result.error)
  202. XCTAssertTrue(result.error!.isTaskCancelled)
  203. delay(0.1) { exp.fulfill() }
  204. }
  205. XCTAssertNotNil(task)
  206. task!.cancel()
  207. _ = stub.go()
  208. waitForExpectations(timeout: 3, handler: nil)
  209. }
  210. func testCancelOneDownloadTask() {
  211. let exp = expectation(description: #function)
  212. let url = testURLs[0]
  213. let stub = delayedStub(url, data: testImageData)
  214. let group = DispatchGroup()
  215. group.enter()
  216. let task1 = downloader.downloadImage(with: url) { result in
  217. XCTAssertNotNil(result.error)
  218. group.leave()
  219. }
  220. group.enter()
  221. _ = downloader.downloadImage(with: url) { result in
  222. XCTAssertNotNil(result.value?.image)
  223. group.leave()
  224. }
  225. task1?.cancel()
  226. delay(0.1) { _ = stub.go() }
  227. group.notify(queue: .main) {
  228. delay(0.1) { exp.fulfill() }
  229. }
  230. waitForExpectations(timeout: 3, handler: nil)
  231. }
  232. func testCancelAllDownloadTasks() {
  233. let exp = expectation(description: #function)
  234. let url1 = testURLs[0]
  235. let stub1 = delayedStub(url1, data: testImageData)
  236. let url2 = testURLs[1]
  237. let stub2 = delayedStub(url2, data: testImageData)
  238. let group = DispatchGroup()
  239. let urls = [url1, url1, url2]
  240. urls.forEach {
  241. group.enter()
  242. downloader.downloadImage(with: $0) { result in
  243. XCTAssertNotNil(result.error)
  244. XCTAssertTrue(result.error!.isTaskCancelled)
  245. group.leave()
  246. }
  247. }
  248. delay(0.1) {
  249. self.downloader.cancelAll()
  250. _ = stub1.go()
  251. _ = stub2.go()
  252. }
  253. group.notify(queue: .main) {
  254. delay(0.1) { exp.fulfill() }
  255. }
  256. waitForExpectations(timeout: 3, handler: nil)
  257. }
  258. func testCancelDownloadTaskForURL() {
  259. let exp = expectation(description: #function)
  260. let url1 = testURLs[0]
  261. let stub1 = delayedStub(url1, data: testImageData)
  262. let url2 = testURLs[1]
  263. let stub2 = delayedStub(url2, data: testImageData)
  264. let group = DispatchGroup()
  265. group.enter()
  266. downloader.downloadImage(with: url1) { result in
  267. XCTAssertNotNil(result.error)
  268. XCTAssertTrue(result.error!.isTaskCancelled)
  269. group.leave()
  270. }
  271. group.enter()
  272. downloader.downloadImage(with: url1) { result in
  273. XCTAssertNotNil(result.error)
  274. XCTAssertTrue(result.error!.isTaskCancelled)
  275. group.leave()
  276. }
  277. group.enter()
  278. downloader.downloadImage(with: url2) { result in
  279. XCTAssertNotNil(result.value)
  280. group.leave()
  281. }
  282. delay(0.1) {
  283. self.downloader.cancel(url: url1)
  284. _ = stub1.go()
  285. _ = stub2.go()
  286. }
  287. group.notify(queue: .main) {
  288. delay(0.1) { exp.fulfill() }
  289. }
  290. waitForExpectations(timeout: 3, handler: nil)
  291. }
  292. // Issue 532 https://github.com/onevcat/Kingfisher/issues/532#issuecomment-305644311
  293. func testCancelThenRestartSameDownload() {
  294. let exp = expectation(description: #function)
  295. let url = testURLs[0]
  296. let stub = delayedStub(url, data: testImageData, length: 123)
  297. let group = DispatchGroup()
  298. group.enter()
  299. let downloadTask = downloader.downloadImage(
  300. with: url,
  301. progressBlock: { _, _ in XCTFail()})
  302. {
  303. result in
  304. XCTAssertNotNil(result.error)
  305. XCTAssertTrue(result.error!.isTaskCancelled)
  306. group.leave()
  307. }
  308. XCTAssertNotNil(downloadTask)
  309. downloadTask!.cancel()
  310. _ = stub.go()
  311. group.enter()
  312. downloader.downloadImage(with: url) {
  313. result in
  314. XCTAssertNotNil(result.value)
  315. if let error = result.error {
  316. print(error)
  317. }
  318. group.leave()
  319. }
  320. group.notify(queue: .main) {
  321. delay(0.1) { exp.fulfill() }
  322. }
  323. waitForExpectations(timeout: 3, handler: nil)
  324. }
  325. func testDownloadTaskNil() {
  326. modifier.url = nil
  327. let downloadTask = downloader.downloadImage(with: URL(string: "url")!, options: [.requestModifier(modifier)])
  328. XCTAssertNil(downloadTask)
  329. }
  330. func testDownloadWithProcessor() {
  331. let exp = expectation(description: #function)
  332. let url = testURLs[0]
  333. stub(url, data: testImageData)
  334. let p = RoundCornerImageProcessor(cornerRadius: 40)
  335. let roundcornered = testImage.kf.image(withRoundRadius: 40, fit: testImage.kf.size)
  336. downloader.downloadImage(with: url, options: [.processor(p)]) { result in
  337. XCTAssertNotNil(result.value)
  338. let image = result.value!.image
  339. XCTAssertFalse(image.renderEqual(to: testImage))
  340. XCTAssertTrue(image.renderEqual(to: roundcornered))
  341. XCTAssertEqual(result.value!.originalData, testImageData)
  342. exp.fulfill()
  343. }
  344. waitForExpectations(timeout: 3, handler: nil)
  345. }
  346. func testDownloadWithDifferentProcessors() {
  347. let exp = expectation(description: #function)
  348. let url = testURLs[0]
  349. let stub = delayedStub(url, data: testImageData)
  350. let p1 = RoundCornerImageProcessor(cornerRadius: 40)
  351. let roundcornered = testImage.kf.image(withRoundRadius: 40, fit: testImage.kf.size)
  352. let p2 = BlurImageProcessor(blurRadius: 3.0)
  353. let blurred = testImage.kf.blurred(withRadius: 3.0)
  354. let group = DispatchGroup()
  355. group.enter()
  356. let task1 = downloader.downloadImage(with: url, options: [.processor(p1)]) { result in
  357. XCTAssertTrue(result.value!.image.renderEqual(to: roundcornered))
  358. group.leave()
  359. }
  360. group.enter()
  361. let task2 = downloader.downloadImage(with: url, options: [.processor(p2)]) { result in
  362. XCTAssertTrue(result.value!.image.renderEqual(to: blurred))
  363. group.leave()
  364. }
  365. XCTAssertNotNil(task1)
  366. XCTAssertEqual(task1?.sessionTask.task, task2?.sessionTask.task)
  367. _ = stub.go()
  368. group.notify(queue: .main, execute: exp.fulfill)
  369. waitForExpectations(timeout: 3, handler: nil)
  370. }
  371. func testDownloadedDataCouldBeModified() {
  372. let exp = expectation(description: #function)
  373. let url = testURLs[0]
  374. stub(url, data: testImageData)
  375. downloader.delegate = self
  376. downloader.downloadImage(with: url) { result in
  377. XCTAssertNil(result.value)
  378. XCTAssertNotNil(result.error)
  379. if case .responseError(reason: .dataModifyingFailed) = result.error! {
  380. } else {
  381. XCTFail()
  382. }
  383. self.downloader.delegate = nil
  384. exp.fulfill()
  385. }
  386. waitForExpectations(timeout: 3, handler: nil)
  387. }
  388. #if os(iOS) || os(tvOS) || os(watchOS)
  389. func testDownloadedImageCouldBeModified() {
  390. let exp = expectation(description: #function)
  391. let url = testURLs[0]
  392. stub(url, data: testImageData)
  393. var modifierCalled = false
  394. let modifier = AnyImageModifier { image in
  395. modifierCalled = true
  396. return image.withRenderingMode(.alwaysTemplate)
  397. }
  398. downloader.downloadImage(with: url, options: [.imageModifier(modifier)]) { result in
  399. XCTAssertTrue(modifierCalled)
  400. XCTAssertEqual(result.value?.image.renderingMode, .alwaysTemplate)
  401. exp.fulfill()
  402. }
  403. waitForExpectations(timeout: 3, handler: nil)
  404. }
  405. #endif
  406. func testDownloadTaskTakePriorityOption() {
  407. let exp = expectation(description: #function)
  408. let url = testURLs[0]
  409. stub(url, data: testImageData)
  410. let task = downloader.downloadImage(with: url, options: [.downloadPriority(URLSessionTask.highPriority)])
  411. {
  412. _ in
  413. exp.fulfill()
  414. }
  415. XCTAssertEqual(task?.sessionTask.task.priority, URLSessionTask.highPriority)
  416. waitForExpectations(timeout: 3, handler: nil)
  417. }
  418. }
  419. extension ImageDownloaderTests: ImageDownloaderDelegate {
  420. func imageDownloader(_ downloader: ImageDownloader, didDownload data: Data, for url: URL) -> Data? {
  421. return nil
  422. }
  423. }
  424. class URLModifier: ImageDownloadRequestModifier {
  425. var url: URL? = nil
  426. func modified(for request: URLRequest) -> URLRequest? {
  427. var r = request
  428. r.url = url
  429. return r
  430. }
  431. }