Skip to content

Commit

Permalink
fix: sessionTimeout should be retriable only to avoid loop
Browse files Browse the repository at this point in the history
  • Loading branch information
CAMOBAP committed Apr 17, 2024
1 parent 9f6f439 commit 26be239
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 16 deletions.
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

0 comments on commit 26be239

Please sign in to comment.