metatext-app-ios-iphone-ipad/HTTP/Sources/Stubbing/Stubbing.swift

37 lines
934 B
Swift
Raw Normal View History

// Copyright © 2020 Metabolist. All rights reserved.
import Foundation
2020-10-06 00:50:05 +02:00
public typealias HTTPStub = Result<(HTTPURLResponse, Data), Error>
2020-08-31 12:21:01 +02:00
public protocol Stubbing {
func stub(url: URL) -> HTTPStub?
func data(url: URL) -> Data?
func dataString(url: URL) -> String?
func statusCode(url: URL) -> Int?
}
2020-08-31 12:21:01 +02:00
public extension Stubbing {
func stub(url: URL) -> HTTPStub? {
if let data = data(url: url),
let statusCode = statusCode(url: url),
let response = HTTPURLResponse(
url: url,
statusCode: statusCode,
httpVersion: nil,
headerFields: nil) {
return .success((response, data))
}
return nil
}
func data(url: URL) -> Data? {
dataString(url: url)?.data(using: .utf8)
}
2020-08-18 07:13:37 +02:00
func dataString(url: URL) -> String? { nil }
func statusCode(url: URL) -> Int? { 200 }
}