2019-01-06 19:26:08 +00:00
|
|
|
//
|
2019-01-06 21:01:08 +00:00
|
|
|
// NetworkSessionMockFromFile.swift
|
2024-10-01 18:05:41 +00:00
|
|
|
// masTests
|
2019-01-06 19:26:08 +00:00
|
|
|
//
|
|
|
|
// Created by Ben Chatelain on 2019-01-05.
|
|
|
|
// Copyright © 2019 mas-cli. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2021-04-14 19:08:55 +00:00
|
|
|
import Foundation
|
2021-04-23 02:15:57 +00:00
|
|
|
import PromiseKit
|
2019-01-06 19:26:08 +00:00
|
|
|
|
2019-01-06 21:01:08 +00:00
|
|
|
/// Mock NetworkSession for testing with saved JSON response payload files.
|
|
|
|
class NetworkSessionMockFromFile: NetworkSessionMock {
|
|
|
|
/// Path to response payload file relative to test bundle.
|
2019-01-06 19:26:08 +00:00
|
|
|
private let responseFile: String
|
|
|
|
|
|
|
|
/// Initializes a mock URL session with a file for the response.
|
|
|
|
///
|
|
|
|
/// - Parameter responseFile: Name of file containing JSON response body.
|
|
|
|
init(responseFile: String) {
|
|
|
|
self.responseFile = responseFile
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Loads data from a file.
|
|
|
|
///
|
|
|
|
/// - Parameters:
|
|
|
|
/// - url: unused
|
|
|
|
/// - completionHandler: Closure which is delivered either data or an error.
|
2021-04-23 02:15:57 +00:00
|
|
|
override func loadData(from _: URL) -> Promise<Data> {
|
2020-09-06 20:15:02 +00:00
|
|
|
guard let fileURL = Bundle.url(for: responseFile)
|
2021-03-22 05:25:18 +00:00
|
|
|
else { fatalError("Unable to load file \(responseFile)") }
|
2019-01-06 19:26:08 +00:00
|
|
|
|
|
|
|
do {
|
2024-10-02 02:16:06 +00:00
|
|
|
return .value(try Data(contentsOf: fileURL, options: .mappedIfSafe))
|
2019-01-06 19:26:08 +00:00
|
|
|
} catch {
|
|
|
|
print("Error opening file: \(error)")
|
2021-04-23 02:15:57 +00:00
|
|
|
return Promise(error: error)
|
2019-01-06 19:26:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|