Skip to content

Commit b84020e

Browse files
committed
Fix userAgent tests
1 parent ac06743 commit b84020e

File tree

1 file changed

+41
-6
lines changed

1 file changed

+41
-6
lines changed

Tests/Segment-Tests/UserAgentTests.swift

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,48 @@ final class UserAgentTests: XCTestCase {
2323
}
2424

2525
func testUserAgent() throws {
26-
#if canImport(WebKit)
27-
let wkUserAgent = WKWebView().value(forKey: "userAgent") as! String
28-
#else
29-
let wkUserAgent = "unknown"
30-
#endif
3126
let userAgent = UserAgent.value
32-
XCTAssertEqual(wkUserAgent, userAgent, "UserAgent's don't match! system: \(wkUserAgent), generated: \(userAgent)")
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+
func testUserAgentWithCustomAppName() throws {
59+
let customUA = UserAgent.value(applicationName: "MyApp/1.0")
60+
XCTAssertTrue(customUA.contains("MyApp/1.0"), "Should contain custom app name")
3361
}
3462

63+
func testUserAgentCaching() throws {
64+
let ua1 = UserAgent.value
65+
let ua2 = UserAgent.value
66+
XCTAssertEqual(ua1, ua2, "UserAgent should be cached and return same value")
67+
}
68+
69+
3570
}

0 commit comments

Comments
 (0)