mas/MasKitTests/Network/NetworkSessionMock.swift

44 lines
1.3 KiB
Swift
Raw Normal View History

2019-01-06 19:26:08 +00:00
//
// NetworkSessionMock
2019-01-06 19:26:08 +00:00
// MasKitTests
//
// Created by Ben Chatelain on 11/13/18.
// Copyright © 2018 mas-cli. All rights reserved.
//
import MasKit
/// Mock NetworkSession for testing.
class NetworkSessionMock: NetworkSession {
2019-01-06 19:26:08 +00:00
typealias CompletionHandler = (Data?, URLResponse?, Error?) -> Void
// Properties that enable us to set exactly what data or error
// we want our mocked URLSession to return for any request.
var data: Data?
var error: Error?
/// Creates a mock data task
///
/// - Parameters:
/// - url: unused
/// - completionHandler: Closure which is delivered both data and error properties (only one should be non-nil)
/// - Returns: Mock data task
2019-01-30 06:15:24 +00:00
func dataTask(with _: URL, completionHandler: @escaping CompletionHandler) -> URLSessionDataTask {
2019-01-06 19:26:08 +00:00
let data = self.data
let error = self.error
return URLSessionDataTaskMock {
2019-01-06 19:26:08 +00:00
completionHandler(data, nil, error)
}
}
/// Immediately passes data and error to completion handler.
///
/// - Parameters:
/// - url: unused
/// - completionHandler: Closure which is delivered either data or an error.
2019-01-30 06:15:24 +00:00
@objc func loadData(from _: URL, completionHandler: @escaping (Data?, Error?) -> Void) {
2019-01-06 19:26:08 +00:00
completionHandler(data, error)
}
}