AutoCat2/AutoCatCoreTests/Api/Mocks/ApiMethodMock.swift

40 lines
1.1 KiB
Swift

import Foundation
import AutoCatCore
open class ApiMethodMock: ApiMethodMockProtocol {
private(set) var path: String
private(set) var httpMethod: String
init(httpMethod: String, path: String) {
self.httpMethod = httpMethod
self.path = path
}
func readData(from path: String) -> Data? {
guard let url = Bundle(for: type(of: self)).url(forResource: path, withExtension: "json") else { return nil }
return try? Data(contentsOf: url)
}
func error(message: String, code: Int? = nil) -> Data? {
var errorData: [String: AnyEncodable] = [
"success": false,
"error": AnyEncodable(message)
]
if let code = code {
errorData["errorCode"] = AnyEncodable(code)
}
return try? JSONEncoder().encode(errorData)
}
func notFoundResponse() -> (status: Int, data: Data?) {
return (status: 404, data: self.error(message: "Not found"))
}
open func response(headers: [String : String], params: [String : Any]) -> (status: Int, data: Data?) {
return self.notFoundResponse()
}
}