Skip to content

Commit

Permalink
Add receive response delegate for response disposition control
Browse files Browse the repository at this point in the history
  • Loading branch information
onevcat committed Mar 16, 2023
1 parent 2de0581 commit 3f149cc
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 6 deletions.
89 changes: 87 additions & 2 deletions Tests/KingfisherTests/ImageDownloaderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -547,9 +547,9 @@ class ImageDownloaderTests: XCTestCase {


func testSessionDelegate() {
class ExtensionDelegate:SessionDelegate {
class ExtensionDelegate: SessionDelegate {
//'exp' only for test
public let exp:XCTestExpectation
public let exp: XCTestExpectation
init(_ expectation:XCTestExpectation) {
exp = expectation
}
Expand All @@ -566,6 +566,78 @@ class ImageDownloaderTests: XCTestCase {
}
waitForExpectations(timeout: 3, handler: nil)
}

func testDownloaderReceiveResponsePass() {

let exp = expectation(description: #function)

let url = testURLs[0]
stub(url, data: testImageData, headers: ["someKey": "someValue"])

let handler = TaskResponseCompletion()
let obj = NSObject()
handler.onReceiveResponse.delegate(on: obj) { (obj, response) in
guard let httpResponse = response as? HTTPURLResponse else {
XCTFail("Should be an HTTP response.")
return .cancel
}
XCTAssertEqual(httpResponse.statusCode, 200)
XCTAssertEqual(httpResponse.url, url)
XCTAssertEqual(httpResponse.allHeaderFields["someKey"] as? String, "someValue")

return .allow
}

downloader.delegate = handler
downloader.downloadImage(with: url) { result in
XCTAssertNotNil(result.value)
XCTAssertNil(result.error)

self.downloader.delegate = nil
// hold delegate
_ = handler
exp.fulfill()
}
waitForExpectations(timeout: 3, handler: nil)
}

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

let url = testURLs[0]
stub(url, data: testImageData, headers: ["someKey": "someValue"])

let handler = TaskResponseCompletion()
let obj = NSObject()
handler.onReceiveResponse.delegate(on: obj) { (obj, response) in
guard let httpResponse = response as? HTTPURLResponse else {
XCTFail("Should be an HTTP response.")
return .cancel
}
XCTAssertEqual(httpResponse.statusCode, 200)
XCTAssertEqual(httpResponse.url, url)
XCTAssertEqual(httpResponse.allHeaderFields["someKey"] as? String, "someValue")

return .cancel
}

downloader.delegate = handler
downloader.downloadImage(with: url) { result in
XCTAssertNil(result.value)
XCTAssertNotNil(result.error)

if case .responseError(reason: .cancelledByDelegate) = result.error! {
} else {
XCTFail()
}

self.downloader.delegate = nil
// hold delegate
_ = handler
exp.fulfill()
}
waitForExpectations(timeout: 3, handler: nil)
}
}

class URLNilDataModifier: ImageDownloaderDelegate {
Expand All @@ -580,6 +652,19 @@ class TaskNilDataModifier: ImageDownloaderDelegate {
}
}

class TaskResponseCompletion: ImageDownloaderDelegate {

let onReceiveResponse = Delegate<URLResponse, URLSession.ResponseDisposition>()

func imageDownloader(
_ downloader: ImageDownloader,
didReceive response: URLResponse,
completionHandler: @escaping (URLSession.ResponseDisposition) -> Void
) {
completionHandler(onReceiveResponse.call(response)!)
}
}

class URLModifier: ImageDownloadRequestModifier {
var url: URL? = nil
func modified(for request: URLRequest) -> URLRequest? {
Expand Down
21 changes: 17 additions & 4 deletions Tests/KingfisherTests/Utils/StubHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,29 @@
import Foundation

@discardableResult
func stub(_ url: URL, data: Data, statusCode: Int = 200, length: Int? = nil) -> LSStubResponseDSL {
var stubResult = stubRequest("GET", url.absoluteString as NSString).andReturn(statusCode)?.withBody(data as NSData)
func stub(_ url: URL,
data: Data,
statusCode: Int = 200,
length: Int? = nil,
headers: [String: String] = [:]
) -> LSStubResponseDSL {
var stubResult = stubRequest("GET", url.absoluteString as NSString)
.andReturn(statusCode)?
.withHeaders(headers)?
.withBody(data as NSData)
if let length = length {
stubResult = stubResult?.withHeader("Content-Length", "\(length)")
}
return stubResult!
}

func delayedStub(_ url: URL, data: Data, statusCode: Int = 200, length: Int? = nil) -> LSStubResponseDSL {
let result = stub(url, data: data, statusCode: statusCode, length: length)
func delayedStub(_ url: URL,
data: Data,
statusCode: Int = 200,
length: Int? = nil,
headers: [String: String] = [:]
) -> LSStubResponseDSL {
let result = stub(url, data: data, statusCode: statusCode, length: length, headers: headers)
return result.delay()!
}

Expand Down

0 comments on commit 3f149cc

Please sign in to comment.