Skip to content

Commit

Permalink
Feature/pat config (#178)
Browse files Browse the repository at this point in the history
* chore: update year in license text

* feat: add config.disablePat

* chore: update HCaptchaHtml.swift

* chore: bump version 2.9.3

* fix: test__Did_Finish_Loading__Multiple restrict toBlocking to 5 sec
  • Loading branch information
CAMOBAP authored Feb 6, 2025
1 parent 1971838 commit a721a29
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 10 deletions.
6 changes: 6 additions & 0 deletions Example/HCaptcha_Tests/Core/HCaptcha__Config__Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,10 @@ class HCaptcha__Config__Tests: XCTestCase {
XCTFail("Unexpected error: \(e)")
}
}

func test__Pat() {
let config = try? HCaptchaConfig(disablePat: true)
let actual = config!.actualEndpoint.absoluteString
XCTAssertTrue(actual.contains("pat=off"))
}
}
6 changes: 4 additions & 2 deletions Example/HCaptcha_Tests/Helpers/HCaptchaConfig+Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ extension HCaptchaConfig {
theme: String = "\"light\"",
customTheme: String? = nil,
locale: Locale? = nil,
loadingTimeout: TimeInterval = 5.0) throws {
loadingTimeout: TimeInterval = 5.0,
disablePat: Bool? = nil) throws {

try self.init(html: html,
apiKey: apiKey,
Expand All @@ -45,7 +46,8 @@ extension HCaptchaConfig {
theme: theme,
customTheme: customTheme,
locale: locale,
loadingTimeout: loadingTimeout)
loadingTimeout: loadingTimeout,
disablePat: disablePat)
}

init(apiKey: String = "some-api-key",
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 @@ -133,7 +133,7 @@ class HCaptcha_Rx__Tests: XCTestCase {
let reset = obs.do(onNext: hcaptcha.reset).subscribe()

let result: [Void] = try obs
.toBlocking()
.toBlocking(timeout: 5)
.toArray()

XCTAssertEqual(result.count, 2)
Expand Down
6 changes: 3 additions & 3 deletions Example/Shared/Base.lproj/LaunchScreen.xib
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="19455" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" colorMatched="YES">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="23094" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" colorMatched="YES">
<device id="retina6_1" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="19454"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="23084"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
Expand All @@ -13,7 +13,7 @@
<rect key="frame" x="0.0" y="0.0" width="480" height="480"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text=" (c) 2021 hCaptcha &amp; Contributors. MIT License." textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" minimumFontSize="9" translatesAutoresizingMaskIntoConstraints="NO" id="8ie-xW-0ye">
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text=" (c) 2025 hCaptcha &amp; Contributors. MIT License." textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" minimumFontSize="9" translatesAutoresizingMaskIntoConstraints="NO" id="8ie-xW-0ye">
<rect key="frame" x="20" y="439" width="440" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" systemColor="darkTextColor"/>
Expand Down
6 changes: 4 additions & 2 deletions HCaptcha/Classes/HCaptcha.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ public class HCaptcha: NSObject {
host: String? = nil,
theme: String = "light",
customTheme: String? = nil,
diagnosticLog: Bool = false
diagnosticLog: Bool = false,
disablePat: Bool = false
) throws {
Log.minLevel = diagnosticLog ? .debug : .warning

Expand All @@ -102,7 +103,8 @@ public class HCaptcha: NSObject {
host: host,
theme: theme,
customTheme: customTheme,
locale: locale)
locale: locale,
disablePat: disablePat)

Log.debug(".init with: \(config)")

Expand Down
10 changes: 9 additions & 1 deletion HCaptcha/Classes/HCaptchaConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ struct HCaptchaConfig: CustomDebugStringConvertible {
/// A time before throw the `.htmlLoadError` if HCaptcha is not loaded
let loadingTimeout: TimeInterval

/// A debug property
let disablePat: Bool?

/// Return actual theme value based on init params. It must return valid JS object.
var actualTheme: String {
self.customTheme ?? "\"\(theme)\""
Expand Down Expand Up @@ -209,7 +212,8 @@ struct HCaptchaConfig: CustomDebugStringConvertible {
theme: String,
customTheme: String?,
locale: Locale?,
loadingTimeout: TimeInterval = 5.0) throws {
loadingTimeout: TimeInterval = 5.0,
disablePat: Bool?) throws {
guard let apiKey = apiKey ?? infoPlistKey else {
throw HCaptchaError.apiKeyNotFound
}
Expand Down Expand Up @@ -248,6 +252,7 @@ struct HCaptchaConfig: CustomDebugStringConvertible {
self.customTheme = customTheme
self.locale = locale
self.loadingTimeout = loadingTimeout
self.disablePat = disablePat
}

/**
Expand Down Expand Up @@ -283,6 +288,9 @@ struct HCaptchaConfig: CustomDebugStringConvertible {
if customTheme != nil {
queryItems.append(URLQueryItem(name: "custom", value: String(true)))
}
if let disablePat = disablePat, disablePat {
queryItems.append(URLQueryItem(name: "pat", value: "off"))
}

result.percentEncodedQuery = queryItems.map {
$0.name +
Expand Down
2 changes: 1 addition & 1 deletion HCaptcha/Classes/HCaptchaHtml.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// HCaptchaHtml.swift (Autogenerated)
// HCaptcha
//
// Copyright © 2024 HCaptcha. All rights reserved.
// Copyright © 2025 HCaptcha. All rights reserved.
//

import Foundation
Expand Down

0 comments on commit a721a29

Please sign in to comment.