1
0
mirror of https://github.com/metabolist/metatext synced 2024-12-19 13:02:49 +01:00
metatext-app-ios-iphone-ipad/HTTP/Sources/Stubbing/Stubbing.swift
Justin Mazzocchi 5f96f59ac3
wip
2020-08-31 03:21:01 -07:00

37 lines
930 B
Swift

// Copyright © 2020 Metabolist. All rights reserved.
import Foundation
public typealias HTTPStub = Result<(URLResponse, Data), Error>
public protocol Stubbing {
func stub(url: URL) -> HTTPStub?
func data(url: URL) -> Data?
func dataString(url: URL) -> String?
func statusCode(url: URL) -> Int?
}
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)
}
func dataString(url: URL) -> String? { nil }
func statusCode(url: URL) -> Int? { 200 }
}