mirror of
https://github.com/metabolist/metatext.git
synced 2024-11-29 19:41:01 +00:00
35 lines
859 B
Swift
35 lines
859 B
Swift
|
// Copyright © 2020 Metabolist. All rights reserved.
|
||
|
|
||
|
import Foundation
|
||
|
|
||
|
typealias HTTPStub = Result<(URLResponse, Data), Error>
|
||
|
|
||
|
protocol Stubbing {
|
||
|
func stub(url: URL) -> HTTPStub?
|
||
|
func data(url: URL) -> Data?
|
||
|
func dataString(url: URL) -> String?
|
||
|
func statusCode(url: URL) -> Int?
|
||
|
}
|
||
|
|
||
|
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)
|
||
|
}
|
||
|
|
||
|
func statusCode(url: URL) -> Int? { 200 }
|
||
|
}
|