From 84b19c55c7b841f8ace2943a96e740b8c90e66a5 Mon Sep 17 00:00:00 2001 From: Alexande B Date: Wed, 17 Apr 2024 16:36:40 +0200 Subject: [PATCH] fix: sessionTimeout should be retriable only to avoid loop --- .../Core/HCaptchaWebViewManager__Tests.swift | 4 ++-- .../HCaptchaWebViewManager+Helpers.swift | 2 +- .../RxSwift/HCaptcha+Rx__Tests.swift | 2 +- Example/HCaptcha_Tests/mock.html | 2 +- HCaptcha/Classes/HCaptchaWebViewManager.swift | 17 +++++++---------- 5 files changed, 12 insertions(+), 15 deletions(-) diff --git a/Example/HCaptcha_Tests/Core/HCaptchaWebViewManager__Tests.swift b/Example/HCaptcha_Tests/Core/HCaptchaWebViewManager__Tests.swift index 0e2f21a..1ed9d0b 100644 --- a/Example/HCaptcha_Tests/Core/HCaptchaWebViewManager__Tests.swift +++ b/Example/HCaptcha_Tests/Core/HCaptchaWebViewManager__Tests.swift @@ -360,7 +360,7 @@ class HCaptchaWebViewManager__Tests: XCTestCase { exp2.fulfill() } - XCTAssertEqual(result1?.error, .wrongMessageFormat) + XCTAssertEqual(result1?.error, .sessionTimeout) // Resets and tries again let exp3 = expectation(description: "validates after reset") @@ -396,7 +396,7 @@ class HCaptchaWebViewManager__Tests: XCTestCase { manager.onEvent = { (event, error) in XCTAssertEqual(.error, event) - XCTAssertEqual(HCaptchaError.wrongMessageFormat, error as? HCaptchaError) + XCTAssertEqual(HCaptchaError.sessionTimeout, error as? HCaptchaError) exp1.fulfill() } diff --git a/Example/HCaptcha_Tests/Helpers/HCaptchaWebViewManager+Helpers.swift b/Example/HCaptcha_Tests/Helpers/HCaptchaWebViewManager+Helpers.swift index a1a4b94..0b73451 100644 --- a/Example/HCaptcha_Tests/Helpers/HCaptchaWebViewManager+Helpers.swift +++ b/Example/HCaptcha_Tests/Helpers/HCaptchaWebViewManager+Helpers.swift @@ -21,7 +21,7 @@ extension HCaptchaWebViewManager { messageBody: String = "undefined", apiKey: String? = nil, endpoint: URL? = nil, - shouldFail: Bool = false, + shouldFail: Bool = false, // will fail with retriable sessionTimeout size: HCaptchaSize = .invisible, rqdata: String? = nil, theme: String = "\"light\"", diff --git a/Example/HCaptcha_Tests/RxSwift/HCaptcha+Rx__Tests.swift b/Example/HCaptcha_Tests/RxSwift/HCaptcha+Rx__Tests.swift index 05d09e1..48bd237 100644 --- a/Example/HCaptcha_Tests/RxSwift/HCaptcha+Rx__Tests.swift +++ b/Example/HCaptcha_Tests/RxSwift/HCaptcha+Rx__Tests.swift @@ -231,7 +231,7 @@ class HCaptcha_Rx__Tests: XCTestCase { .single() } catch let error { - XCTAssertEqual(error as? HCaptchaError, .wrongMessageFormat) + XCTAssertEqual(error as? HCaptchaError, .sessionTimeout) // Resets after failure _ = Observable.just(()) diff --git a/Example/HCaptcha_Tests/mock.html b/Example/HCaptcha_Tests/mock.html index c951faa..e6ff52f 100644 --- a/Example/HCaptcha_Tests/mock.html +++ b/Example/HCaptcha_Tests/mock.html @@ -29,7 +29,7 @@ var execute = function() { if (shouldFail) { - post("error"); + post({error: 15}); // .sessionTimeout } else { if (rqdata) { post({"log": rqdata}); diff --git a/HCaptcha/Classes/HCaptchaWebViewManager.swift b/HCaptcha/Classes/HCaptchaWebViewManager.swift index 00f7758..b0fcd14 100644 --- a/HCaptcha/Classes/HCaptchaWebViewManager.swift +++ b/HCaptcha/Classes/HCaptchaWebViewManager.swift @@ -1,4 +1,3 @@ -// swiftlint:disable file_length // // HCaptchaWebViewManager.swift // HCaptcha @@ -246,21 +245,19 @@ fileprivate extension HCaptchaWebViewManager { private func handle(error: HCaptchaError) { switch error { - case HCaptchaError.challengeClosed: - completion?(HCaptchaResult(error: error)) - case HCaptchaError.networkError: - if let completion = completion { - completion(HCaptchaResult(error: error)) - } else { - lastError = error - } - default: + case .sessionTimeout: if shouldResetOnError, let view = webView.superview { reset() validate(on: view) } else { completion?(HCaptchaResult(error: error)) } + default: + if let completion = completion { + completion(HCaptchaResult(error: error)) + } else { + lastError = error + } } }