@@ -23,13 +23,57 @@ final class UserAgentTests: XCTestCase {
2323 }
2424
2525 func testUserAgent( ) throws {
26- #if canImport(WebKit)
26+ let userAgent = UserAgent . value
27+
28+ // Test that it's not empty
29+ XCTAssertFalse ( userAgent. isEmpty, " UserAgent should not be empty " )
30+
31+ #if os(iOS)
32+ // Test format and expected components
33+ XCTAssertTrue ( userAgent. contains ( " Mozilla/5.0 " ) , " Should start with Mozilla/5.0 " )
34+ XCTAssertTrue ( userAgent. contains ( " iPhone " ) || userAgent. contains ( " iPad " ) , " Should contain device type " )
35+ XCTAssertTrue ( userAgent. contains ( " CPU " ) , " Should contain CPU " )
36+ XCTAssertTrue ( userAgent. contains ( " like Mac OS X " ) , " Should contain Mac OS X reference " )
37+ XCTAssertTrue ( userAgent. contains ( " AppleWebKit/605.1.15 " ) , " Should contain WebKit version " )
38+ XCTAssertTrue ( userAgent. contains ( " Mobile/15E148 " ) , " Should contain Mobile identifier " )
39+
40+ // Test that OS version is present and formatted correctly (e.g., "26_1" or "26_1_0")
41+ let osVersionRegex = try NSRegularExpression ( pattern: " OS \\ d+_ \\ d+(_ \\ d+)? " , options: [ ] )
42+ let range = NSRange ( userAgent. startIndex... , in: userAgent)
43+ XCTAssertNotNil ( osVersionRegex. firstMatch ( in: userAgent, range: range) , " Should contain properly formatted OS version " )
44+
45+ #elseif os(macOS)
46+ XCTAssertTrue ( userAgent. contains ( " Macintosh " ) , " Should contain Macintosh " )
47+ XCTAssertTrue ( userAgent. contains ( " Mac OS X 10_15_7 " ) , " Should contain hardcoded macOS version " )
48+
49+ #elseif os(visionOS)
50+ XCTAssertTrue ( userAgent. contains ( " iPad " ) , " visionOS should report as iPad " )
51+ XCTAssertTrue ( userAgent. contains ( " CPU OS " ) , " Should contain CPU OS " )
52+
53+ #endif
54+
55+ print ( " Generated UserAgent: \( userAgent) " )
56+ }
57+
58+ #if !os(tvOS) && !os(watchOS)
59+ func testUserAgentWithCustomAppName( ) throws {
60+ /*#if canImport(WebKit)
2761 let wkUserAgent = WKWebView().value(forKey: "userAgent") as! String
2862 #else
2963 let wkUserAgent = "unknown"
3064 #endif
31- let userAgent = UserAgent . value
32- XCTAssertEqual ( wkUserAgent, userAgent, " UserAgent's don't match! system: \( wkUserAgent) , generated: \( userAgent) " )
65+ print(wkUserAgent)*/
66+
67+ let customUA = UserAgent . value ( applicationName: " MyApp/1.0 " )
68+ XCTAssertTrue ( customUA. contains ( " MyApp/1.0 " ) , " Should contain custom app name " )
69+ }
70+ #endif
71+
72+ func testUserAgentCaching( ) throws {
73+ let ua1 = UserAgent . value
74+ let ua2 = UserAgent . value
75+ XCTAssertEqual ( ua1, ua2, " UserAgent should be cached and return same value " )
3376 }
3477
78+
3579}
0 commit comments