Skip to content

Instantly share code, notes, and snippets.

@yoching
Created September 19, 2017 03:16
Show Gist options
  • Save yoching/c656eab56b9a6a7cc60b0f78419f0b07 to your computer and use it in GitHub Desktop.
Save yoching/c656eab56b9a6a7cc60b0f78419f0b07 to your computer and use it in GitHub Desktop.
OHHTTPStubs wrapper
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