Created
September 19, 2017 03:16
-
-
Save yoching/c656eab56b9a6a7cc60b0f78419f0b07 to your computer and use it in GitHub Desktop.
OHHTTPStubs wrapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public protocol ApiRequestSetting { | |
static var hostname: String { get } | |
static var basePath: String { get } | |
} | |
final class StubConfigurator { | |
private let setting: ApiRequestSetting.Type | |
init(setting: ApiRequestSetting.Type) { | |
self.setting = setting | |
} | |
func setStub(endPoint: String, jsonObject: [String: Any], statusCode: Int32) { | |
stub(condition: isHost(setting.hostname) | |
&& isPath("\(setting.basePath)\(endPoint)")) { _ -> OHHTTPStubsResponse in | |
return OHHTTPStubsResponse( | |
jsonObject: jsonObject, | |
statusCode: statusCode, | |
headers: nil | |
) | |
} | |
} | |
func setStub(endPoint: String, fromFile fileName: String, statusCode: Int32, caller: Swift.AnyClass) { | |
stub(condition: isHost(setting.hostname) | |
&& isPath("\(setting.basePath)\(endPoint)")) { _ -> OHHTTPStubsResponse in | |
return OHHTTPStubsResponse( | |
fileAtPath: OHPathForFile(fileName, caller)!, | |
statusCode: 200, | |
headers: nil | |
) | |
} | |
} | |
// class method | |
static func removeAllStubs() { | |
OHHTTPStubs.removeAllStubs() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment