2020-07-29 23:50:30 +00:00
|
|
|
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
2020-10-05 22:50:05 +00:00
|
|
|
public typealias HTTPStub = Result<(HTTPURLResponse, Data), Error>
|
2020-07-29 23:50:30 +00:00
|
|
|
|
2020-08-31 10:21:01 +00:00
|
|
|
public protocol Stubbing {
|
2020-07-29 23:50:30 +00:00
|
|
|
func stub(url: URL) -> HTTPStub?
|
|
|
|
func data(url: URL) -> Data?
|
|
|
|
func dataString(url: URL) -> String?
|
|
|
|
func statusCode(url: URL) -> Int?
|
|
|
|
}
|
|
|
|
|
2020-08-31 10:21:01 +00:00
|
|
|
public extension Stubbing {
|
2020-07-29 23:50:30 +00:00
|
|
|
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 05:13:37 +00:00
|
|
|
func dataString(url: URL) -> String? { nil }
|
|
|
|
|
2020-07-29 23:50:30 +00:00
|
|
|
func statusCode(url: URL) -> Int? { 200 }
|
|
|
|
}
|