Improved the MockURLProtocol class in the Foundation library to support optional object in its Response struct.

This commit is contained in:
Javier Cicchelli 2024-03-17 23:00:30 +01:00
parent ac1074324d
commit f6a72e8465

View File

@ -66,12 +66,12 @@ extension MockURLProtocol {
// MARK: Constants // MARK: Constants
public let statusCode: Int public let statusCode: Int
public let object: any Encodable public let object: (any Encodable)?
// MARK: Initialisers // MARK: Initialisers
public init( public init(
statusCode: Int, statusCode: Int,
object: any Codable object: (any Codable)? = nil
) { ) {
self.statusCode = statusCode self.statusCode = statusCode
self.object = object self.object = object
@ -80,7 +80,9 @@ extension MockURLProtocol {
// MARK: Computed // MARK: Computed
var data: Data? { var data: Data? {
get throws { get throws {
try JSONEncoder().encode(object) guard let object else { return nil }
return try JSONEncoder.default.encode(object)
} }
} }