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