diff --git a/Example/ReCaptcha/AppDelegate.swift b/Example/ReCaptcha/AppDelegate.swift index 86054b4..7ef06da 100644 --- a/Example/ReCaptcha/AppDelegate.swift +++ b/Example/ReCaptcha/AppDelegate.swift @@ -13,12 +13,11 @@ class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? - func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? - ) -> Bool { + ) -> Bool { // Override point for customization after application launch. - return true + true } } diff --git a/Example/ReCaptcha/ViewController.swift b/Example/ReCaptcha/ViewController.swift index 67b25ad..6e526ab 100644 --- a/Example/ReCaptcha/ViewController.swift +++ b/Example/ReCaptcha/ViewController.swift @@ -11,9 +11,8 @@ import RxCocoa import RxSwift import UIKit - class ViewController: UIViewController { - private struct Constants { + private enum Constants { static let webViewTag = 123 static let testLabelTag = 321 } @@ -24,11 +23,11 @@ class ViewController: UIViewController { private var locale: Locale? private var endpoint = ReCaptcha.Endpoint.default - @IBOutlet private weak var label: UILabel! - @IBOutlet private weak var spinner: UIActivityIndicatorView! - @IBOutlet private weak var localeSegmentedControl: UISegmentedControl! - @IBOutlet private weak var endpointSegmentedControl: UISegmentedControl! - @IBOutlet private weak var visibleChallengeSwitch: UISwitch! + @IBOutlet private var label: UILabel! + @IBOutlet private var spinner: UIActivityIndicatorView! + @IBOutlet private var localeSegmentedControl: UISegmentedControl! + @IBOutlet private var endpointSegmentedControl: UISegmentedControl! + @IBOutlet private var visibleChallengeSwitch: UISwitch! override func viewDidLoad() { super.viewDidLoad() @@ -67,7 +66,7 @@ class ViewController: UIViewController { let validate = recaptcha.rx.validate(on: view, resetOnError: false) .catch { error in - return .just("Error \(error)") + .just("Error \(error)") } .debug("validate") .share() diff --git a/Example/ReCaptcha_Tests/Core/ReCaptchaDecoder__Tests.swift b/Example/ReCaptcha_Tests/Core/ReCaptchaDecoder__Tests.swift index bb08513..6a983c2 100644 --- a/Example/ReCaptcha_Tests/Core/ReCaptchaDecoder__Tests.swift +++ b/Example/ReCaptcha_Tests/Core/ReCaptchaDecoder__Tests.swift @@ -11,7 +11,6 @@ import WebKit import XCTest - class ReCaptchaDecoder__Tests: XCTestCase { fileprivate typealias Result = ReCaptchaDecoder.Result @@ -31,7 +30,6 @@ class ReCaptchaDecoder__Tests: XCTestCase { super.tearDown() } - func test__Send_Error() { let exp = expectation(description: "send error message") var result: Result? @@ -41,20 +39,17 @@ class ReCaptchaDecoder__Tests: XCTestCase { exp.fulfill() } - // Send let err = ReCaptchaError.random() decoder.send(error: err) waitForExpectations(timeout: 1) - // Check XCTAssertNotNil(result) XCTAssertEqual(result, .error(err)) } - func test__Decode__Wrong_Format() { let exp = expectation(description: "send unsupported message") var result: Result? @@ -64,19 +59,16 @@ class ReCaptchaDecoder__Tests: XCTestCase { exp.fulfill() } - // Send let message = MockMessage(message: "foobar") decoder.send(message: message) waitForExpectations(timeout: 1) - // Check XCTAssertEqual(result, .error(ReCaptchaError.wrongMessageFormat)) } - func test__Decode__Unexpected_Action() { let exp = expectation(description: "send message with unexpected action") var result: Result? @@ -86,19 +78,16 @@ class ReCaptchaDecoder__Tests: XCTestCase { exp.fulfill() } - // Send let message = MockMessage(message: ["action": "bar"]) decoder.send(message: message) waitForExpectations(timeout: 1) - // Check XCTAssertEqual(result, .error(ReCaptchaError.wrongMessageFormat)) } - func test__Decode__ShowReCaptcha() { let exp = expectation(description: "send showReCaptcha message") var result: Result? @@ -108,19 +97,16 @@ class ReCaptchaDecoder__Tests: XCTestCase { exp.fulfill() } - // Send let message = MockMessage(message: ["action": "showReCaptcha"]) decoder.send(message: message) waitForExpectations(timeout: 1) - // Check XCTAssertEqual(result, .showReCaptcha) } - func test__Decode__Token() { let exp = expectation(description: "send token message") var result: Result? @@ -130,7 +116,6 @@ class ReCaptchaDecoder__Tests: XCTestCase { exp.fulfill() } - // Send let token = String(arc4random()) let message = MockMessage(message: ["token": token]) @@ -138,12 +123,10 @@ class ReCaptchaDecoder__Tests: XCTestCase { waitForExpectations(timeout: 1) - // Check XCTAssertEqual(result, .token(token)) } - func test__Decode__DidLoad() { let exp = expectation(description: "send did load message") var result: Result? @@ -153,14 +136,12 @@ class ReCaptchaDecoder__Tests: XCTestCase { exp.fulfill() } - // Send let message = MockMessage(message: ["action": "didLoad"]) decoder.send(message: message) waitForExpectations(timeout: 1) - // Check XCTAssertEqual(result, .didLoad) } diff --git a/Example/ReCaptcha_Tests/Core/ReCaptchaResult__Tests.swift b/Example/ReCaptcha_Tests/Core/ReCaptchaResult__Tests.swift index 7922832..a6333de 100644 --- a/Example/ReCaptcha_Tests/Core/ReCaptchaResult__Tests.swift +++ b/Example/ReCaptcha_Tests/Core/ReCaptchaResult__Tests.swift @@ -9,7 +9,6 @@ @testable import ReCaptcha import XCTest - class ReCaptchaResult__Tests: XCTestCase { func test__Get_Token() { let token = UUID().uuidString @@ -18,8 +17,7 @@ class ReCaptchaResult__Tests: XCTestCase { do { let value = try result.dematerialize() XCTAssertEqual(value, token) - } - catch let err { + } catch let err { XCTFail(err.localizedDescription) } } @@ -31,8 +29,7 @@ class ReCaptchaResult__Tests: XCTestCase { do { _ = try result.dematerialize() XCTFail("Shouldn't have completed") - } - catch let err { + } catch let err { XCTAssertEqual(err as? ReCaptchaError, error) } } diff --git a/Example/ReCaptcha_Tests/Core/ReCaptchaWebViewManager__Tests.swift b/Example/ReCaptcha_Tests/Core/ReCaptchaWebViewManager__Tests.swift index 2a9e5b6..3d66fdc 100644 --- a/Example/ReCaptcha_Tests/Core/ReCaptchaWebViewManager__Tests.swift +++ b/Example/ReCaptcha_Tests/Core/ReCaptchaWebViewManager__Tests.swift @@ -11,7 +11,6 @@ import WebKit import XCTest - class ReCaptchaWebViewManager__Tests: XCTestCase { fileprivate var apiKey: String! @@ -50,13 +49,11 @@ class ReCaptchaWebViewManager__Tests: XCTestCase { waitForExpectations(timeout: 10) - // Verify XCTAssertNotNil(result1) XCTAssertNil(result1?.error) XCTAssertEqual(result1?.token, apiKey) - // Validate again let exp2 = expectation(description: "reload token") var result2: ReCaptchaResult? @@ -69,14 +66,12 @@ class ReCaptchaWebViewManager__Tests: XCTestCase { waitForExpectations(timeout: 10) - // Verify XCTAssertNotNil(result2) XCTAssertNil(result2?.error) XCTAssertEqual(result2?.token, apiKey) } - func test__Validate__Show_ReCaptcha() { let exp = expectation(description: "show recaptcha") @@ -93,7 +88,6 @@ class ReCaptchaWebViewManager__Tests: XCTestCase { waitForExpectations(timeout: 10) } - func test__Validate__Message_Error() { var result: ReCaptchaResult? let exp = expectation(description: "message error") @@ -140,7 +134,7 @@ class ReCaptchaWebViewManager__Tests: XCTestCase { XCTAssertNil(result?.token) switch result!.error! { - case .unexpected(let error as NSError): + case let .unexpected(error as NSError): XCTAssertEqual(error.code, WKError.javaScriptExceptionOccurred.rawValue) default: XCTFail("Unexpected error received") diff --git a/Example/ReCaptcha_Tests/Core/ReCaptcha__Tests.swift b/Example/ReCaptcha_Tests/Core/ReCaptcha__Tests.swift index 232b508..d2c2ae2 100644 --- a/Example/ReCaptcha_Tests/Core/ReCaptcha__Tests.swift +++ b/Example/ReCaptcha_Tests/Core/ReCaptcha__Tests.swift @@ -11,10 +11,9 @@ import AppSwizzle import RxSwift import XCTest - class ReCaptcha__Tests: XCTestCase { - fileprivate struct Constants { - struct InfoDictKeys { + fileprivate enum Constants { + enum InfoDictKeys { static let APIKey = "ReCaptchaKey" static let Domain = "ReCaptchaDomain" } @@ -123,10 +122,9 @@ class ReCaptcha__Tests: XCTestCase { } } - -private extension Bundle { - @objc func failHTMLLoad(_ resource: String, type: String) -> String? { - guard resource == "recaptcha" && type == "html" else { +extension Bundle { + @objc fileprivate func failHTMLLoad(_ resource: String, type: String) -> String? { + guard resource == "recaptcha", type == "html" else { return failHTMLLoad(resource, type: type) } diff --git a/Example/ReCaptcha_Tests/Helpers/ReCaptchaDecoder+Helper.swift b/Example/ReCaptcha_Tests/Helpers/ReCaptchaDecoder+Helper.swift index 63b2a8b..6997342 100644 --- a/Example/ReCaptcha_Tests/Helpers/ReCaptchaDecoder+Helper.swift +++ b/Example/ReCaptcha_Tests/Helpers/ReCaptchaDecoder+Helper.swift @@ -12,17 +12,18 @@ import WebKit class MockMessage: WKScriptMessage { override var body: Any { - return storedBody + storedBody } fileprivate let storedBody: Any init(message: Any) { - storedBody = message + self.storedBody = message } } // MARK: - Decoder Helpers + extension ReCaptchaDecoder { func send(message: MockMessage) { userContentController(WKUserContentController(), didReceive: message) @@ -30,9 +31,10 @@ extension ReCaptchaDecoder { } // MARK: - Result Helpers + extension ReCaptchaDecoder.Result: Equatable { var error: ReCaptchaError? { - guard case .error(let error) = self else { return nil } + guard case let .error(error) = self else { return nil } return error } @@ -42,10 +44,10 @@ extension ReCaptchaDecoder.Result: Equatable { (.didLoad, .didLoad): return true - case (.token(let lht), .token(let rht)): + case let (.token(lht), .token(rht)): return lht == rht - case (.error(let lhe), .error(let rhe)): + case let (.error(lhe), .error(rhe)): return lhe == rhe default: diff --git a/Example/ReCaptcha_Tests/Helpers/ReCaptchaError+Equatable.swift b/Example/ReCaptcha_Tests/Helpers/ReCaptchaError+Equatable.swift index 264edfa..87d0861 100644 --- a/Example/ReCaptcha_Tests/Helpers/ReCaptchaError+Equatable.swift +++ b/Example/ReCaptcha_Tests/Helpers/ReCaptchaError+Equatable.swift @@ -20,7 +20,7 @@ extension ReCaptchaError: Equatable { (.responseExpired, .responseExpired), (.failedRender, .failedRender): return true - case (.unexpected(let lhe as NSError), .unexpected(let rhe as NSError)): + case let (.unexpected(lhe as NSError), .unexpected(rhe as NSError)): return lhe == rhe default: return false diff --git a/Example/ReCaptcha_Tests/Helpers/ReCaptchaWebViewManager+Helpers.swift b/Example/ReCaptcha_Tests/Helpers/ReCaptchaWebViewManager+Helpers.swift index 7d4f35f..e2eeb3d 100644 --- a/Example/ReCaptcha_Tests/Helpers/ReCaptchaWebViewManager+Helpers.swift +++ b/Example/ReCaptcha_Tests/Helpers/ReCaptchaWebViewManager+Helpers.swift @@ -11,11 +11,9 @@ import Foundation import WebKit extension ReCaptchaWebViewManager { - private static let unformattedHTML: String! = { - Bundle(for: ReCaptchaWebViewManager__Tests.self) - .path(forResource: "mock", ofType: "html") - .flatMap { try? String(contentsOfFile: $0) } - }() + private static let unformattedHTML: String! = Bundle(for: ReCaptchaWebViewManager__Tests.self) + .path(forResource: "mock", ofType: "html") + .flatMap { try? String(contentsOfFile: $0) } convenience init( messageBody: String = "", @@ -42,7 +40,7 @@ extension ReCaptchaWebViewManager { } func validate(on view: UIView, resetOnError: Bool = true, completion: @escaping (ReCaptchaResult) -> Void) { - self.shouldResetOnError = resetOnError + shouldResetOnError = resetOnError self.completion = completion validate(on: view) diff --git a/Example/ReCaptcha_Tests/Helpers/Result+Helpers.swift b/Example/ReCaptcha_Tests/Helpers/Result+Helpers.swift index c39a055..fa2969e 100644 --- a/Example/ReCaptcha_Tests/Helpers/Result+Helpers.swift +++ b/Example/ReCaptcha_Tests/Helpers/Result+Helpers.swift @@ -8,15 +8,14 @@ @testable import ReCaptcha - extension ReCaptchaResult { var token: String? { - guard case .token(let value) = self else { return nil } + guard case let .token(value) = self else { return nil } return value } var error: ReCaptchaError? { - guard case .error(let error) = self else { return nil } + guard case let .error(error) = self else { return nil } return error } } diff --git a/Example/ReCaptcha_Tests/RxSwift/ReCaptcha+Rx__Tests.swift b/Example/ReCaptcha_Tests/RxSwift/ReCaptcha+Rx__Tests.swift index 9e6c43c..1ac9ebe 100644 --- a/Example/ReCaptcha_Tests/RxSwift/ReCaptcha+Rx__Tests.swift +++ b/Example/ReCaptcha_Tests/RxSwift/ReCaptcha+Rx__Tests.swift @@ -13,7 +13,6 @@ import RxCocoa import RxSwift import XCTest - class ReCaptcha_Rx__Tests: XCTestCase { fileprivate var apiKey: String! @@ -33,7 +32,6 @@ class ReCaptcha_Rx__Tests: XCTestCase { super.tearDown() } - func test__Validate__Token() { let recaptcha = ReCaptcha(manager: ReCaptchaWebViewManager(messageBody: "{token: key}", apiKey: apiKey)) recaptcha.configureWebView { _ in @@ -48,13 +46,11 @@ class ReCaptcha_Rx__Tests: XCTestCase { // Verify XCTAssertEqual(result, apiKey) - } - catch let error { + } catch { XCTFail(error.localizedDescription) } } - func test__Validate__Show_ReCaptcha() { let recaptcha = ReCaptcha( manager: ReCaptchaWebViewManager(messageBody: "{action: \"showReCaptcha\"}", apiKey: apiKey) @@ -73,14 +69,12 @@ class ReCaptcha_Rx__Tests: XCTestCase { .single() XCTFail("should have thrown exception") - } - catch let error { + } catch { XCTAssertEqual(String(describing: error), RxError.timeout.debugDescription) XCTAssertTrue(didConfigureWebView) } } - func test__Validate__Error() { let recaptcha = ReCaptcha(manager: ReCaptchaWebViewManager(messageBody: "\"foobar\"", apiKey: apiKey)) recaptcha.configureWebView { _ in @@ -94,8 +88,7 @@ class ReCaptcha_Rx__Tests: XCTestCase { .single() XCTFail("should have thrown exception") - } - catch let error { + } catch { XCTAssertEqual(error as? ReCaptchaError, .wrongMessageFormat) } } @@ -111,8 +104,7 @@ class ReCaptcha_Rx__Tests: XCTestCase { try recaptcha.rx.didFinishLoading .toBlocking() .first() - } - catch let error { + } catch { XCTFail(error.localizedDescription) } } @@ -134,8 +126,7 @@ class ReCaptcha_Rx__Tests: XCTestCase { XCTAssertEqual(result.count, 2) reset.dispose() - } - catch let error { + } catch { XCTFail(error.localizedDescription) } } @@ -149,8 +140,7 @@ class ReCaptcha_Rx__Tests: XCTestCase { .first() XCTFail("should have timed out") - } - catch let error { + } catch { XCTAssertEqual(String(describing: error), RxError.timeout.debugDescription) } @@ -160,8 +150,7 @@ class ReCaptcha_Rx__Tests: XCTestCase { try recaptcha.rx.didFinishLoading .toBlocking() .first() - } - catch let error { + } catch { XCTFail(error.localizedDescription) } } @@ -218,8 +207,7 @@ class ReCaptcha_Rx__Tests: XCTestCase { _ = try recaptcha.rx.validate(on: presenterView, resetOnError: false) .toBlocking() .single() - } - catch let error { + } catch { XCTAssertEqual(error as? ReCaptchaError, .wrongMessageFormat) // Resets after failure @@ -234,8 +222,7 @@ class ReCaptcha_Rx__Tests: XCTestCase { .single() XCTAssertEqual(result, apiKey) - } - catch let error { + } catch { XCTFail(error.localizedDescription) } } @@ -257,8 +244,7 @@ class ReCaptcha_Rx__Tests: XCTestCase { .single() XCTAssertEqual(result, apiKey) - } - catch let error { + } catch { XCTFail(error.localizedDescription) } } diff --git a/ReCaptcha/Assets/recaptcha.html b/ReCaptcha/Assets/recaptcha.html index 0f338c3..ef8538a 100644 --- a/ReCaptcha/Assets/recaptcha.html +++ b/ReCaptcha/Assets/recaptcha.html @@ -1,102 +1,100 @@ -
- - - - - - - + if (success) { + post({ action: "didLoad" }); + } + }); + }); + }; + + + + + +