Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sessionTimeout should be retriable only to avoid loop #150

Merged
merged 1 commit into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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\"",
Expand Down
2 changes: 1 addition & 1 deletion Example/HCaptcha_Tests/RxSwift/HCaptcha+Rx__Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Void>.just(())
Expand Down
2 changes: 1 addition & 1 deletion Example/HCaptcha_Tests/mock.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

var execute = function() {
if (shouldFail) {
post("error");
post({error: 15}); // .sessionTimeout
} else {
if (rqdata) {
post({"log": rqdata});
Expand Down
18 changes: 7 additions & 11 deletions HCaptcha/Classes/HCaptchaWebViewManager.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// swiftlint:disable file_length
//
// HCaptchaWebViewManager.swift
// HCaptcha
Expand Down Expand Up @@ -245,22 +244,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:
if error == .sessionTimeout {
if shouldResetOnError, let view = webView.superview {
reset()
validate(on: view)
} else {
completion?(HCaptchaResult(error: error))
}
} else {
if let completion = completion {
completion(HCaptchaResult(error: error))
} else {
lastError = error
}
}
}

Expand Down
Loading