|
| 1 | +import XCTest |
| 2 | + |
| 3 | +final class SentryTracePropagationTests: XCTestCase { |
| 4 | + |
| 5 | + func testIsTargetMatchWithDefaultRegex_MatchesAllURLs() throws { |
| 6 | + // Arrange |
| 7 | + let defaultRegex = try XCTUnwrap(NSRegularExpression(pattern: ".*")) |
| 8 | + let localhostURL = try XCTUnwrap(URL(string: "http://localhost")) |
| 9 | + let exampleURL = try XCTUnwrap(URL(string: "http://www.example.com/api/projects")) |
| 10 | + |
| 11 | + // Act & Assert |
| 12 | + XCTAssertTrue(SentryTracePropagation.isTargetMatch(localhostURL, withTargets: [defaultRegex])) |
| 13 | + XCTAssertTrue(SentryTracePropagation.isTargetMatch(exampleURL, withTargets: [defaultRegex])) |
| 14 | + } |
| 15 | + |
| 16 | + func testIsTargetMatchWithStringHostname_MatchesExactHostname() throws { |
| 17 | + // Arrange |
| 18 | + let localhostURL = try XCTUnwrap(URL(string: "http://localhost")) |
| 19 | + let exampleURL = try XCTUnwrap(URL(string: "http://www.example.com/api/projects")) |
| 20 | + let apiExampleURL = try XCTUnwrap(URL(string: "http://api.example.com/api/projects")) |
| 21 | + let localhostTargets = ["localhost"] |
| 22 | + let exampleTargets = ["www.example.com"] |
| 23 | + |
| 24 | + // Act & Assert |
| 25 | + XCTAssertTrue(SentryTracePropagation.isTargetMatch(localhostURL, withTargets: localhostTargets)) |
| 26 | + XCTAssertFalse(SentryTracePropagation.isTargetMatch(exampleURL, withTargets: localhostTargets)) |
| 27 | + XCTAssertFalse(SentryTracePropagation.isTargetMatch(localhostURL, withTargets: exampleTargets)) |
| 28 | + XCTAssertTrue(SentryTracePropagation.isTargetMatch(exampleURL, withTargets: exampleTargets)) |
| 29 | + XCTAssertFalse(SentryTracePropagation.isTargetMatch(apiExampleURL, withTargets: exampleTargets)) |
| 30 | + } |
| 31 | + |
| 32 | + func testIsTargetMatchWithStringHostname_MatchesSubstrings() throws { |
| 33 | + // Arrange |
| 34 | + let localhostExtendedURL = try XCTUnwrap(URL(string: "http://localhost-but-not-really")) |
| 35 | + let evilURL = try XCTUnwrap(URL(string: "http://www.example.com.evil.com/api/projects")) |
| 36 | + let localhostTargets = ["localhost"] |
| 37 | + let exampleTargets = ["www.example.com"] |
| 38 | + |
| 39 | + // Act & Assert |
| 40 | + XCTAssertTrue(SentryTracePropagation.isTargetMatch(localhostExtendedURL, withTargets: localhostTargets)) |
| 41 | + XCTAssertTrue(SentryTracePropagation.isTargetMatch(evilURL, withTargets: exampleTargets)) |
| 42 | + } |
| 43 | + |
| 44 | + func testIsTargetMatchWithRegexPattern_MatchesSpecificPatterns() throws { |
| 45 | + // Arrange |
| 46 | + let regex = try XCTUnwrap(NSRegularExpression(pattern: "http://www.example.com/api/.*")) |
| 47 | + let localhostURL = try XCTUnwrap(URL(string: "http://localhost")) |
| 48 | + let nonAPIURL = try XCTUnwrap(URL(string: "http://www.example.com/url")) |
| 49 | + let apiURL = try XCTUnwrap(URL(string: "http://www.example.com/api/projects")) |
| 50 | + |
| 51 | + // Act & Assert |
| 52 | + XCTAssertFalse(SentryTracePropagation.isTargetMatch(localhostURL, withTargets: [regex])) |
| 53 | + XCTAssertFalse(SentryTracePropagation.isTargetMatch(nonAPIURL, withTargets: [regex])) |
| 54 | + XCTAssertTrue(SentryTracePropagation.isTargetMatch(apiURL, withTargets: [regex])) |
| 55 | + } |
| 56 | + |
| 57 | + func testIsTargetMatchWithMixedRegexAndString_MatchesEitherTarget() throws { |
| 58 | + // Arrange |
| 59 | + let regex = try XCTUnwrap(NSRegularExpression(pattern: "http://www.example.com/api/.*")) |
| 60 | + let localhostURL = try XCTUnwrap(URL(string: "http://localhost")) |
| 61 | + let nonAPIURL = try XCTUnwrap(URL(string: "http://www.example.com/url")) |
| 62 | + let apiURL = try XCTUnwrap(URL(string: "http://www.example.com/api/projects")) |
| 63 | + let mixedTargets = ["localhost", regex] as [Any] |
| 64 | + |
| 65 | + // Act & Assert |
| 66 | + XCTAssertTrue(SentryTracePropagation.isTargetMatch(localhostURL, withTargets: mixedTargets)) |
| 67 | + XCTAssertFalse(SentryTracePropagation.isTargetMatch(nonAPIURL, withTargets: mixedTargets)) |
| 68 | + XCTAssertTrue(SentryTracePropagation.isTargetMatch(apiURL, withTargets: mixedTargets)) |
| 69 | + } |
| 70 | + |
| 71 | + func testIsTargetMatchWithInvalidInput_DoesNotCrash() throws { |
| 72 | + // Arrange |
| 73 | + let localhostURL = try XCTUnwrap(URL(string: "http://localhost")) |
| 74 | + let targetsWithInvalidType = ["localhost", 123] as [Any] |
| 75 | + |
| 76 | + // Act & Assert |
| 77 | + XCTAssertTrue(SentryTracePropagation.isTargetMatch(localhostURL, withTargets: targetsWithInvalidType)) |
| 78 | + } |
| 79 | + |
| 80 | +} |
0 commit comments