mas/Tests/masTests/Network/MockFromFileNetworkSession.swift

37 lines
1.1 KiB
Swift
Raw Normal View History

2019-01-06 19:26:08 +00:00
//
// MockFromFileNetworkSession.swift
// masTests
2019-01-06 19:26:08 +00:00
//
// Created by Ben Chatelain on 2019-01-05.
// Copyright © 2019 mas-cli. All rights reserved.
//
import Foundation
import PromiseKit
2019-01-06 19:26:08 +00:00
/// Mock NetworkSession for testing with saved JSON response payload files.
class MockFromFileNetworkSession: MockNetworkSession {
/// 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
}
override func loadData(from _: URL) -> Promise<Data> {
guard let fileURL = Bundle.url(for: responseFile) else {
fatalError("Unable to load file \(responseFile)")
}
2019-01-06 19:26:08 +00:00
do {
return .value(try Data(contentsOf: fileURL, options: .mappedIfSafe))
2019-01-06 19:26:08 +00:00
} catch {
print("Error opening file: \(error)")
return Promise(error: error)
2019-01-06 19:26:08 +00:00
}
}
}