mirror of
https://github.com/divkit/divkit.git
synced 2026-05-07 20:02:32 +00:00
8cf91c73a3
commit_hash:e22b43dde31492aae7d9ad8723ec9409b4980cd7
47 lines
1.1 KiB
Swift
47 lines
1.1 KiB
Swift
import Foundation
|
|
import VGSLFundamentals
|
|
|
|
public final class MockURLResourceRequester: URLResourceRequesting {
|
|
public var requestedURLs: [URL] = []
|
|
public var shouldSucceed = true
|
|
public var customBehavior: ((URL) -> Bool)?
|
|
|
|
public init() {}
|
|
|
|
@MainActor
|
|
public func getDataWithSource(
|
|
from url: URL,
|
|
completion: @escaping CompletionHandlerWithSource
|
|
) -> Cancellable? {
|
|
requestedURLs.append(url)
|
|
|
|
if let customBehavior {
|
|
if customBehavior(url) {
|
|
completion(.success(URLRequestResult(data: Data(), source: .network)))
|
|
} else {
|
|
completion(.failure(NSError(domain: "test", code: 0)))
|
|
}
|
|
} else {
|
|
if shouldSucceed {
|
|
completion(.success(URLRequestResult(data: Data(), source: .network)))
|
|
} else {
|
|
completion(.failure(NSError(domain: "test", code: 0)))
|
|
}
|
|
}
|
|
|
|
return CancellableImpl {}
|
|
}
|
|
}
|
|
|
|
private struct CancellableImpl: Cancellable {
|
|
private let closure: @Sendable () -> Void
|
|
|
|
init(_ closure: @escaping @Sendable () -> Void = {}) {
|
|
self.closure = closure
|
|
}
|
|
|
|
func cancel() {
|
|
closure()
|
|
}
|
|
}
|