diff --git a/Example/ReCaptcha.xcodeproj/project.pbxproj b/Example/ReCaptcha.xcodeproj/project.pbxproj index 75b89a1..bfce56a 100644 --- a/Example/ReCaptcha.xcodeproj/project.pbxproj +++ b/Example/ReCaptcha.xcodeproj/project.pbxproj @@ -254,7 +254,7 @@ attributes = { LastSwiftUpdateCheck = 0830; LastUpgradeCheck = 0820; - ORGANIZATIONNAME = CocoaPods; + ORGANIZATIONNAME = ReCaptcha; TargetAttributes = { 607FACCF1AFB9204008FA782 = { CreatedOnToolsVersion = 6.3.1; diff --git a/Example/ReCaptcha_Tests/Core/ReCaptchaWebViewManager__Tests.swift b/Example/ReCaptcha_Tests/Core/ReCaptchaWebViewManager__Tests.swift index 5254c31..8da042a 100644 --- a/Example/ReCaptcha_Tests/Core/ReCaptchaWebViewManager__Tests.swift +++ b/Example/ReCaptcha_Tests/Core/ReCaptchaWebViewManager__Tests.swift @@ -198,4 +198,50 @@ class ReCaptchaWebViewManager__Tests: XCTestCase { waitForExpectations(timeout: 3) } + + // MARK: Setup + + func test__Key_Setup() { + let exp = expectation(description: "setup key") + var result: ReCaptchaWebViewManager.Response? + + // Validate + let manager = ReCaptchaWebViewManager(messageBody: "{token: key}", apiKey: apiKey) + manager.configureWebView { _ in + XCTFail("should not ask to configure the webview") + } + + manager.validate(on: presenterView) { response in + result = response + exp.fulfill() + } + + waitForExpectations(timeout: 3) + + XCTAssertNotNil(result) + XCTAssertNil(result?.error) + XCTAssertEqual(result?.value, apiKey) + } + + func test__Endpoint_Setup() { + let exp = expectation(description: "setup endpoint") + let endpoint = String(describing: arc4random()) + var result: ReCaptchaWebViewManager.Response? + + let manager = ReCaptchaWebViewManager(messageBody: "{token: endpoint}", endpoint: endpoint) + manager.configureWebView { _ in + XCTFail("should not ask to configure the webview") + } + + manager.validate(on: presenterView) { response in + result = response + exp.fulfill() + } + + waitForExpectations(timeout: 3) + + XCTAssertNotNil(result) + XCTAssertNil(result?.error) + XCTAssertEqual(result?.value, endpoint) + } } diff --git a/Example/ReCaptcha_Tests/Helpers/ReCaptchaWebViewManager+Helpers.swift b/Example/ReCaptcha_Tests/Helpers/ReCaptchaWebViewManager+Helpers.swift index 1a2f9bf..2a4cca7 100644 --- a/Example/ReCaptcha_Tests/Helpers/ReCaptchaWebViewManager+Helpers.swift +++ b/Example/ReCaptcha_Tests/Helpers/ReCaptchaWebViewManager+Helpers.swift @@ -12,12 +12,18 @@ import Foundation extension ReCaptchaWebViewManager { - convenience init(messageBody: String, apiKey: String? = nil) { + convenience init(messageBody: String, apiKey: String? = nil, endpoint: String? = nil) { + let localhost = URL(string: "http://localhost")! let html = Bundle(for: ReCaptchaWebViewManager__Tests.self) .path(forResource: "mock", ofType: "html") .flatMap { try? String(contentsOfFile: $0) } - .map { String(format: $0, "%@", messageBody) } + .map { String(format: $0, arguments: ["message": messageBody]) } - self.init(html: html!, apiKey: apiKey ?? String(arc4random()), baseURL: URL(string: "http://localhost")!) + self.init( + html: html!, + apiKey: apiKey ?? String(arc4random()), + baseURL: localhost, + endpoint: endpoint ?? localhost.absoluteString + ) } } diff --git a/Example/ReCaptcha_Tests/mock.html b/Example/ReCaptcha_Tests/mock.html index b315536..1e741ba 100644 --- a/Example/ReCaptcha_Tests/mock.html +++ b/Example/ReCaptcha_Tests/mock.html @@ -2,10 +2,11 @@
diff --git a/README.md b/README.md index 98b5750..0ee200c 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,19 @@ recaptcha.rx.validate(on: view) }) ``` +#### Alternte endpoint + +If your app has firewall limitations that may be blocking Google's API, the JS endpoint may be changed on initialization. +It'll then point to `https://www.recaptcha.net/recaptcha/api.js`: + +``` swift +public enum Endpoint { + case default, alternate +} + +let recaptcha = try? ReCaptcha(endpoint: .alternate) // Defaults to `default` when unset +``` + ## License ReCaptcha is available under the MIT license. See the LICENSE file for more info. diff --git a/ReCaptcha/Assets/recaptcha.html b/ReCaptcha/Assets/recaptcha.html index 0237452..a6361f2 100644 --- a/ReCaptcha/Assets/recaptcha.html +++ b/ReCaptcha/Assets/recaptcha.html @@ -26,7 +26,7 @@ var onloadCallback = function() { grecaptcha.render('submit', { - 'sitekey' : '%@', + 'sitekey' : '${apiKey}', 'callback' : onSubmit, 'size': 'invisible' }); @@ -35,6 +35,6 @@ - +