ImageDownloaderTests.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. //
  2. // ImageDownloaderTests.swift
  3. // Kingfisher
  4. //
  5. // Created by Wei Wang on 15/4/10.
  6. //
  7. // Copyright (c) 2017 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. super.tearDown()
  37. LSNocilla.sharedInstance().stop()
  38. }
  39. override func setUp() {
  40. super.setUp()
  41. // Put setup code here. This method is called before the invocation of each test method in the class.
  42. downloader = ImageDownloader(name: "test")
  43. }
  44. override func tearDown() {
  45. // Put teardown code here. This method is called after the invocation of each test method in the class.
  46. LSNocilla.sharedInstance().clearStubs()
  47. downloader = nil
  48. super.tearDown()
  49. }
  50. func testDownloadAnImage() {
  51. let expectation = self.expectation(description: "wait for downloading image")
  52. let URLString = testKeys[0]
  53. _ = stubRequest("GET", URLString).andReturn(200)?.withBody(testImageData)
  54. let url = URL(string: URLString)!
  55. downloader.downloadImage(with: url, options: nil, progressBlock: { (receivedSize, totalSize) -> () in
  56. return
  57. }) { (image, error, imageURL, data) -> () in
  58. expectation.fulfill()
  59. XCTAssert(image != nil, "Download should be able to finished for URL: \(String(describing: imageURL))")
  60. }
  61. waitForExpectations(timeout: 5, handler: nil)
  62. }
  63. func testDownloadMultipleImages() {
  64. let expectation = self.expectation(description: "wait for all downloading finish")
  65. let group = DispatchGroup()
  66. for URLString in testKeys {
  67. if let url = URL(string: URLString) {
  68. group.enter()
  69. _ = stubRequest("GET", URLString).andReturn(200)?.withBody(testImageData)
  70. downloader.downloadImage(with: url, options: nil, progressBlock: { (receivedSize, totalSize) -> () in
  71. }, completionHandler: { (image, error, imageURL, data) -> () in
  72. XCTAssert(image != nil, "Download should be able to finished for URL: \(String(describing: imageURL)).")
  73. group.leave()
  74. })
  75. }
  76. }
  77. group.notify(queue: .main, execute: expectation.fulfill)
  78. waitForExpectations(timeout: 5, handler: nil)
  79. }
  80. func testDownloadAnImageWithMultipleCallback() {
  81. let expectation = self.expectation(description: "wait for downloading image")
  82. let group = DispatchGroup()
  83. let URLString = testKeys[0]
  84. _ = stubRequest("GET", URLString).andReturn(200)?.withBody(testImageData)
  85. for _ in 0...5 {
  86. group.enter()
  87. downloader.downloadImage(with: URL(string: URLString)!, options: nil, progressBlock: { (receivedSize, totalSize) -> () in
  88. }) { (image, error, imageURL, data) -> () in
  89. XCTAssert(image != nil, "Download should be able to finished for URL: \(String(describing: imageURL)).")
  90. group.leave()
  91. }
  92. }
  93. group.notify(queue: .main, execute: expectation.fulfill)
  94. waitForExpectations(timeout: 5, handler: nil)
  95. }
  96. func testDownloadWithModifyingRequest() {
  97. let expectation = self.expectation(description: "wait for downloading image")
  98. let URLString = testKeys[0]
  99. _ = stubRequest("GET", URLString).andReturn(200)?.withBody(testImageData)
  100. modifier.url = URL(string: URLString)
  101. let someURL = URL(string: "some_strange_url")!
  102. downloader.downloadImage(with: someURL, options: [.requestModifier(modifier)], progressBlock: { (receivedSize, totalSize) -> () in
  103. }) { (image, error, imageURL, data) -> () in
  104. XCTAssert(image != nil, "Download should be able to finished for URL: \(String(describing: imageURL)).")
  105. XCTAssertEqual(imageURL!, URL(string: URLString)!, "The returned imageURL should be the replaced one")
  106. expectation.fulfill()
  107. }
  108. waitForExpectations(timeout: 5, handler: nil)
  109. }
  110. func testServerNotModifiedResponse() {
  111. let expectation = self.expectation(description: "wait for server response 304")
  112. let URLString = testKeys[0]
  113. _ = stubRequest("GET", URLString).andReturn(304)
  114. downloader.downloadImage(with: URL(string: URLString)!, options: nil, progressBlock: { (receivedSize, totalSize) -> () in
  115. }) { (image, error, imageURL, data) -> () in
  116. XCTAssertNotNil(error, "There should be an error since server returning 304 and no image downloaded.")
  117. XCTAssertEqual(error!.code, KingfisherError.notModified.rawValue, "The error should be NotModified.")
  118. expectation.fulfill()
  119. }
  120. waitForExpectations(timeout: 5, handler: nil)
  121. }
  122. func testServerInvalidStatusCode() {
  123. let expectation = self.expectation(description: "wait for response which has invalid status code")
  124. let URLString = testKeys[0]
  125. _ = stubRequest("GET", URLString).andReturn(404)?.withBody(testImageData)
  126. downloader.downloadImage(with: URL(string: URLString)!, options: nil, progressBlock: { (receivedSize, totalSize) -> () in
  127. }) { (image, error, imageURL, data) -> () in
  128. XCTAssertNotNil(error, "There should be an error since server returning 404")
  129. XCTAssertEqual(error!.code, KingfisherError.invalidStatusCode.rawValue, "The error should be InvalidStatusCode.")
  130. XCTAssertEqual(error!.userInfo["statusCode"]! as? Int, 404, "The error should be InvalidStatusCode.")
  131. expectation.fulfill()
  132. }
  133. waitForExpectations(timeout: 5, handler: nil)
  134. }
  135. // Since we could not receive one challage, no test for trusted hosts currently.
  136. // See http://stackoverflow.com/questions/27065372/why-is-a-https-nsurlsession-connection-only-challenged-once-per-domain for more.
  137. func testSSLCertificateValidation() {
  138. LSNocilla.sharedInstance().stop()
  139. let downloader = ImageDownloader(name: "ssl.test")
  140. let url = URL(string: "https://testssl-expire.disig.sk/Expired.png")!
  141. let expectation = self.expectation(description: "wait for download from an invalid ssl site.")
  142. downloader.downloadImage(with: url, progressBlock: nil, completionHandler: { (image, error, imageURL, data) -> () in
  143. XCTAssertNotNil(error, "Error should not be nil")
  144. XCTAssert(error?.code == NSURLErrorServerCertificateUntrusted || error?.code == NSURLErrorSecureConnectionFailed, "Error should be NSURLErrorServerCertificateUntrusted, but \(String(describing: error))")
  145. expectation.fulfill()
  146. LSNocilla.sharedInstance().start()
  147. })
  148. waitForExpectations(timeout: 20) { (error) in
  149. XCTAssertNil(error, "\(String(describing: error))")
  150. LSNocilla.sharedInstance().start()
  151. }
  152. }
  153. func testDownloadResultErrorAndRetry() {
  154. let expectation = self.expectation(description: "wait for downloading error")
  155. let URLString = testKeys[0]
  156. stubRequest("GET", URLString).andFailWithError(NSError(domain: "stubError", code: -1, userInfo: nil))
  157. let url = URL(string: URLString)!
  158. downloader.downloadImage(with: url, progressBlock: nil) { (image, error, imageURL, data) -> () in
  159. XCTAssertNotNil(error, "Should return with an error")
  160. LSNocilla.sharedInstance().clearStubs()
  161. _ = stubRequest("GET", URLString).andReturn(200)?.withBody(testImageData)
  162. // Retry the download
  163. self.downloader.downloadImage(with: url, progressBlock: nil, completionHandler: { (image, error, imageURL, data) -> () in
  164. XCTAssertNil(error, "Download should be finished without error")
  165. expectation.fulfill()
  166. })
  167. }
  168. waitForExpectations(timeout: 5, handler: nil)
  169. }
  170. func testDownloadEmptyURL() {
  171. let expectation = self.expectation(description: "wait for downloading error")
  172. modifier.url = nil
  173. let url = URL(string: "http://onevcat.com")
  174. downloader.downloadImage(with: url!, options: [.requestModifier(modifier)], progressBlock: { (receivedSize, totalSize) -> () in
  175. XCTFail("The progress block should not be called.")
  176. }) { (image, error, imageURL, originalData) -> () in
  177. XCTAssertNotNil(error, "An error should happen for empty URL")
  178. XCTAssertEqual(error!.code, KingfisherError.invalidURL.rawValue)
  179. self.downloader.delegate = nil
  180. expectation.fulfill()
  181. }
  182. waitForExpectations(timeout: 5, handler: nil)
  183. }
  184. func testDownloadTaskProperty() {
  185. let task = downloader.downloadImage(with: URL(string: "1234")!, progressBlock: { (receivedSize, totalSize) -> () in
  186. }) { (image, error, imageURL, originalData) -> () in
  187. }
  188. XCTAssertNotNil(task, "The task should exist.")
  189. XCTAssertTrue(task!.ownerDownloader === downloader, "The owner downloader should be correct")
  190. XCTAssertEqual(task!.url, URL(string: "1234"), "The request URL should equal.")
  191. }
  192. func testCancelDownloadTask() {
  193. let expectation = self.expectation(description: "wait for downloading")
  194. let URLString = testKeys[0]
  195. let stub = stubRequest("GET", URLString).andReturn(200)?.withBody(testImageData)?.delay()
  196. let url = URL(string: URLString)!
  197. var progressBlockIsCalled = false
  198. let downloadTask = downloader.downloadImage(with: url, progressBlock: { (receivedSize, totalSize) -> () in
  199. progressBlockIsCalled = true
  200. }) { (image, error, imageURL, originalData) -> () in
  201. XCTAssertNotNil(error)
  202. XCTAssertEqual(error!.code, NSURLErrorCancelled)
  203. XCTAssert(progressBlockIsCalled == false, "ProgressBlock should not be called since it is canceled.")
  204. // delay(0.1, block: expectation.fulfill)
  205. expectation.fulfill()
  206. }
  207. XCTAssertNotNil(downloadTask)
  208. downloadTask!.cancel()
  209. _ = stub!.go()
  210. waitForExpectations(timeout: 5, handler: nil)
  211. }
  212. // Issue 532 https://github.com/onevcat/Kingfisher/issues/532#issuecomment-305644311
  213. func _testCancelThenRestartSameDownload() {
  214. let expectation = self.expectation(description: "wait for downloading")
  215. let URLString = testKeys[0]
  216. let stub = stubRequest("GET", URLString).andReturn(200)?.withBody(testImageData)?.delay()
  217. let url = URL(string: URLString)!
  218. var progressBlockIsCalled = false
  219. let group = DispatchGroup()
  220. group.enter()
  221. let downloadTask = downloader.downloadImage(with: url, progressBlock: { (receivedSize, totalSize) -> () in
  222. progressBlockIsCalled = true
  223. }) { (image, error, imageURL, originalData) -> () in
  224. XCTAssertNotNil(error)
  225. XCTAssertEqual(error!.code, NSURLErrorCancelled)
  226. XCTAssert(progressBlockIsCalled == false, "ProgressBlock should not be called since it is canceled.")
  227. group.leave()
  228. }
  229. XCTAssertNotNil(downloadTask)
  230. downloadTask!.cancel()
  231. _ = stub!.go()
  232. group.enter()
  233. downloader.downloadImage(with: url, progressBlock: { (receivedSize, totalSize) -> () in
  234. progressBlockIsCalled = true
  235. }) { (image, error, imageURL, originalData) -> () in
  236. XCTAssertNotNil(image)
  237. group.leave()
  238. }
  239. group.notify(queue: .main) {
  240. // delay(0.1, block: expectation.fulfill)
  241. expectation.fulfill()
  242. }
  243. waitForExpectations(timeout: 5, handler: nil)
  244. }
  245. func testDownloadTaskNil() {
  246. modifier.url = nil
  247. let downloadTask = downloader.downloadImage(with: URL(string: "url")!, options: [.requestModifier(modifier)], progressBlock: nil, completionHandler: nil)
  248. XCTAssertNil(downloadTask)
  249. downloader.delegate = nil
  250. }
  251. func testDownloadWithProcessor() {
  252. let expectation = self.expectation(description: "wait for downloading image")
  253. let URLString = testKeys[0]
  254. _ = stubRequest("GET", URLString).andReturn(200)?.withBody(testImageData)
  255. let url = URL(string: URLString)!
  256. let p = RoundCornerImageProcessor(cornerRadius: 40)
  257. let roundcornered = testImage.kf.image(withRoundRadius: 40, fit: testImage.kf.size)
  258. downloader.downloadImage(with: url, options: [.processor(p)], progressBlock: { (receivedSize, totalSize) -> () in
  259. }) { (image, error, imageURL, data) -> () in
  260. expectation.fulfill()
  261. XCTAssert(image != nil, "Download should be able to finished for URL: \(String(describing: imageURL))")
  262. XCTAssertFalse(image!.renderEqual(to: testImage), "The processed image should not equal to the original one.")
  263. XCTAssertTrue(image!.renderEqual(to: roundcornered), "The processed image should equal to the one directly processed from original one.")
  264. XCTAssertEqual(NSData(data: data!), testImageData, "But the original data should equal each other.")
  265. }
  266. waitForExpectations(timeout: 5, handler: nil)
  267. }
  268. func testDownloadWithDifferentProcessors() {
  269. let expectation = self.expectation(description: "wait for downloading image")
  270. let URLString = testKeys[0]
  271. _ = stubRequest("GET", URLString).andReturn(200)?.withBody(testImageData)
  272. let url = URL(string: URLString)!
  273. let p1 = RoundCornerImageProcessor(cornerRadius: 40)
  274. let roundcornered = testImage.kf.image(withRoundRadius: 40, fit: testImage.kf.size)
  275. let p2 = BlurImageProcessor(blurRadius: 3.0)
  276. let blurred = testImage.kf.blurred(withRadius: 3.0)
  277. var count = 0
  278. downloader.downloadImage(with: url, options: [.processor(p1)], progressBlock: { (receivedSize, totalSize) -> () in
  279. }) { (image, error, imageURL, data) -> () in
  280. XCTAssertTrue(image!.renderEqual(to: roundcornered), "The processed image should equal to the one directly processed from original one.")
  281. count += 1
  282. if count == 2 { expectation.fulfill() }
  283. }
  284. downloader.downloadImage(with: url, options: [.processor(p2)], progressBlock: { (receivedSize, totalSize) -> () in
  285. }) { (image, error, imageURL, data) -> () in
  286. XCTAssertTrue(image!.renderEqual(to: blurred), "The processed image should equal to the one directly processed from original one.")
  287. count += 1
  288. if count == 2 { expectation.fulfill() }
  289. }
  290. waitForExpectations(timeout: 5, handler: nil)
  291. }
  292. }
  293. class URLModifier: ImageDownloadRequestModifier {
  294. var url: URL? = nil
  295. func modified(for request: URLRequest) -> URLRequest? {
  296. var r = request
  297. r.url = url
  298. return r
  299. }
  300. }