Skip to content
New issue

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

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

Already on GitHub? # to your account

Expose userAgent as get only property #75

Merged
merged 4 commits into from
Jan 29, 2025
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
21 changes: 17 additions & 4 deletions Source/HotwireConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@ public struct HotwireConfig {
HotwireLogger.debugLoggingEnabled = debugLoggingEnabled
}
}

/// Gets the user agent that the library builds to identify the app
/// and its registered bridge components.
///
/// The user agent includes:
/// - Your (optional) custom `applicationUserAgentPrefix`
/// - "Native iOS; Turbo Native iOS;"
/// - "bridge-components: [your bridge components];"
public var userAgent: String {
get {
return UserAgent.build(
applicationPrefix: applicationUserAgentPrefix,
componentTypes: Hotwire.bridgeComponentTypes
)
}
}

// MARK: Turbo

Expand Down Expand Up @@ -85,10 +101,7 @@ public struct HotwireConfig {
private func makeWebViewConfiguration() -> WKWebViewConfiguration {
let configuration = WKWebViewConfiguration()
configuration.defaultWebpagePreferences?.preferredContentMode = .mobile
configuration.applicationNameForUserAgent = UserAgent.build(
applicationPrefix: applicationUserAgentPrefix,
componentTypes: Hotwire.bridgeComponentTypes
)
configuration.applicationNameForUserAgent = userAgent
configuration.processPool = sharedProcessPool
return configuration
}
Expand Down
18 changes: 18 additions & 0 deletions Tests/Turbo/HotwireConfigTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import XCTest
@testable import HotwireNative

final class HotwireConfigTests: XCTestCase {
func testUserAgent() {
var config = HotwireConfig()
config.applicationUserAgentPrefix = "TestApp/1.0"

let testComponent = MockBridgeComponent.self
Hotwire.registerBridgeComponents([testComponent])

XCTAssertEqual(config.userAgent, "TestApp/1.0 Hotwire Native iOS; Turbo Native iOS; bridge-components: [MockComponent]")
}
}

private class MockBridgeComponent: BridgeComponent {
static override var name: String { "MockComponent" }
}