metatext/Development Assets/MockWebAuthSession.swift

67 lines
1.9 KiB
Swift
Raw Normal View History

// Copyright © 2020 Metabolist. All rights reserved.
import Foundation
2020-08-12 09:01:21 +00:00
class MockWebAuthSession: WebAuthSession {
2020-08-02 22:23:01 +00:00
let completionHandler: WebAuthSessionCompletionHandler
let url: URL
let callbackURLScheme: String?
2020-08-02 22:23:01 +00:00
var presentationContextProvider: WebAuthPresentationContextProviding?
required init(
url URL: URL,
callbackURLScheme: String?,
2020-08-02 22:23:01 +00:00
completionHandler: @escaping WebAuthSessionCompletionHandler) {
self.url = URL
self.callbackURLScheme = callbackURLScheme
self.completionHandler = completionHandler
}
func start() -> Bool {
completionHandler(completionHandlerURL, completionHandlerError)
return true
}
var completionHandlerURL: URL? {
nil
}
var completionHandlerError: Error? {
nil
}
}
2020-08-09 01:37:46 +00:00
class SuccessfulMockWebAuthSession: MockWebAuthSession {
private let redirectURL: URL
required init(
url URL: URL,
callbackURLScheme: String?,
2020-08-02 22:23:01 +00:00
completionHandler: @escaping WebAuthSessionCompletionHandler) {
redirectURL = Foundation.URL(
string: URLComponents(url: URL, resolvingAgainstBaseURL: true)!
.queryItems!.first(where: { $0.name == "redirect_uri" })!.value!)!
super.init(
url: URL,
callbackURLScheme: callbackURLScheme,
completionHandler: completionHandler)
}
override var completionHandlerURL: URL? {
var components = URLComponents(url: redirectURL, resolvingAgainstBaseURL: true)!
var queryItems = components.queryItems ?? []
queryItems.append(URLQueryItem(name: "code", value: UUID().uuidString))
components.queryItems = queryItems
return components.url
}
}
2020-08-09 01:37:46 +00:00
class CanceledLoginMockWebAuthSession: MockWebAuthSession {
override var completionHandlerError: Error? {
2020-08-02 22:23:01 +00:00
WebAuthSessionError(.canceledLogin)
}
}