Last active
April 3, 2023 13:47
-
-
Save LukeSmith16/04fa99d20fe8387e242a3295ee1375ef to your computer and use it in GitHub Desktop.
Responsible for creating a custom mock response and returning it given a path, domain model and status code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public struct MockResponse { | |
struct Content { | |
let statusCode: Int | |
let data: Data | |
} | |
enum Response { | |
case success(Content) | |
case error(Error) | |
static func model<T: Codable>(_ model: T, statusCode: Int) -> Response { | |
let data = try? JSONEncoder().encode(model) | |
return .success(.init(statusCode: statusCode, data: data ?? Data())) | |
} | |
} | |
let url: URL | |
let response: Response | |
init(path: String, response: Response) { | |
guard let url = URL(string: path) else { | |
fatalError("Bad path formatting") | |
} | |
self.url = url | |
self.response = response | |
} | |
} | |
extension MockResponse { | |
public static var list: MockResponse { | |
.init( | |
path: "https://www.server.com/api/shopping-list", | |
response: .model( | |
ShoppingList.mock(with: ["Bread", "Apples"]), statusCode: 200 | |
) | |
) | |
} | |
// Add more mock response's here... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment