Skip to content

Commit

Permalink
Add escaping and tests for async test
Browse files Browse the repository at this point in the history
  • Loading branch information
onevcat committed Dec 30, 2020
1 parent d87ee58 commit 09e5f68
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
8 changes: 4 additions & 4 deletions Sources/Networking/ImageDownloader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ open class ImageDownloader {
private func createDownloadContext(
with url: URL,
options: KingfisherParsedOptionsInfo,
done: ((Result<DownloadingContext, KingfisherError>) -> Void)
done: @escaping ((Result<DownloadingContext, KingfisherError>) -> Void)
)
{
func checkRequestAndDone(r: URLRequest) {
Expand Down Expand Up @@ -340,7 +340,7 @@ open class ImageDownloader {
return downloadTask
}

// MARK: Dowloading Task
// MARK: Downloading Task
/// Downloads an image with a URL and option. Invoked internally by Kingfisher. Subclasses must invoke super.
///
/// - Parameters:
Expand All @@ -364,9 +364,9 @@ open class ImageDownloader {
// `AsyncImageDownloadRequestModifier` is used the returned `downloadTask` of this method will be `nil`
// and the actual "delayed" task is given in `AsyncImageDownloadRequestModifier.onDownloadTaskStarted`
// callback.
downloadTask = startDownloadTask(
downloadTask = self.startDownloadTask(
context: context,
callback: createTaskCallback(completionHandler, options: options)
callback: self.createTaskCallback(completionHandler, options: options)
)
if let modifier = options.requestModifier {
modifier.onDownloadTaskStarted?(downloadTask)
Expand Down
6 changes: 3 additions & 3 deletions Sources/Networking/RequestModifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import Foundation

public protocol AsyncImageDownloadRequestModifier {
func modified(for request: URLRequest, modify: (URLRequest?) -> Void)
func modified(for request: URLRequest, reportModified: @escaping (URLRequest?) -> Void)
var onDownloadTaskStarted: ((DownloadTask?) -> Void)? { get }
}

Expand All @@ -52,9 +52,9 @@ public protocol ImageDownloadRequestModifier: AsyncImageDownloadRequestModifier
}

extension ImageDownloadRequestModifier {
public func modified(for request: URLRequest, modify: (URLRequest?) -> Void) {
public func modified(for request: URLRequest, reportModified: @escaping (URLRequest?) -> Void) {
let request = modified(for: request)
modify(request)
reportModified(request)
}

public var onDownloadTaskStarted: ((DownloadTask?) -> Void)? { return nil }
Expand Down
43 changes: 42 additions & 1 deletion Tests/KingfisherTests/ImageDownloaderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,40 @@ class ImageDownloaderTests: XCTestCase {
modifier.url = url

let someURL = URL(string: "some_strange_url")!
downloader.downloadImage(with: someURL, options: [.requestModifier(modifier)]) { result in
let task = downloader.downloadImage(with: someURL, options: [.requestModifier(modifier)]) { result in
XCTAssertNotNil(result.value)
XCTAssertEqual(result.value?.url, url)
exp.fulfill()
}
XCTAssertNotNil(task)
waitForExpectations(timeout: 3, handler: nil)
}

func testDownloadWithAsyncModifyingRequest() {
let exp = expectation(description: #function)

let url = testURLs[0]
stub(url, data: testImageData)

var downloadTaskCalled = false

let asyncModifier = AsyncURLModifier()
asyncModifier.url = url
asyncModifier.onDownloadTaskStarted = { task in
XCTAssertNotNil(task)
downloadTaskCalled = true
}


let someURL = URL(string: "some_strage_url")!
let task = downloader.downloadImage(with: someURL, options: [.requestModifier(asyncModifier)]) { result in
XCTAssertNotNil(result.value)
XCTAssertEqual(result.value?.url, url)
XCTAssertTrue(downloadTaskCalled)
exp.fulfill()
}
// The returned task is nil since the download is not starting immediately.
XCTAssertNil(task)
waitForExpectations(timeout: 3, handler: nil)
}

Expand Down Expand Up @@ -535,3 +564,15 @@ class URLModifier: ImageDownloadRequestModifier {
}
}

class AsyncURLModifier: AsyncImageDownloadRequestModifier {
var url: URL? = nil
var onDownloadTaskStarted: ((DownloadTask?) -> Void)?

func modified(for request: URLRequest, reportModified: @escaping (URLRequest?) -> Void) {
var r = request
r.url = url
DispatchQueue.main.async {
reportModified(r)
}
}
}

0 comments on commit 09e5f68

Please sign in to comment.