Skip to content

Commit 41c0aa8

Browse files
test: Extract isTargetTests to own test class (#6352)
## 📜 Description Refactored trace propagation tests by moving them from `SentryNetworkTrackerTests` to a dedicated `SentryTracePropagationTests` file. The tests were also improved with better organization, more descriptive test names, and clearer arrange-act-assert patterns. Closes #6353
1 parent 687ccf6 commit 41c0aa8

File tree

3 files changed

+84
-31
lines changed

3 files changed

+84
-31
lines changed

Sentry.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@
123123
62A456E32B0370AA003F19A1 /* SentryUIEventTrackerTransactionMode.h in Headers */ = {isa = PBXBuildFile; fileRef = 62A456E22B0370AA003F19A1 /* SentryUIEventTrackerTransactionMode.h */; };
124124
62A456E52B0370E0003F19A1 /* SentryUIEventTrackerTransactionMode.m in Sources */ = {isa = PBXBuildFile; fileRef = 62A456E42B0370E0003F19A1 /* SentryUIEventTrackerTransactionMode.m */; };
125125
62AB8C9E2BF3925700BFC2AC /* WeakReference.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62AB8C9D2BF3925700BFC2AC /* WeakReference.swift */; };
126+
62B220BB2E93A9EC004620FF /* SentryTracePropagationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62B220BA2E93A9EC004620FF /* SentryTracePropagationTests.swift */; };
126127
62BDDD122D51FD540024CCD1 /* SentryThreadCodable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62BDDD112D51FD540024CCD1 /* SentryThreadCodable.swift */; };
127128
62C1AFAB2B7E10EA0038C5F7 /* SentrySpotlightTransport.m in Sources */ = {isa = PBXBuildFile; fileRef = 62C1AFAA2B7E10EA0038C5F7 /* SentrySpotlightTransport.m */; };
128129
62C25C862B075F4900C68CBD /* TestOptions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62C25C852B075F4900C68CBD /* TestOptions.swift */; };
@@ -1360,6 +1361,7 @@
13601361
62A456E22B0370AA003F19A1 /* SentryUIEventTrackerTransactionMode.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryUIEventTrackerTransactionMode.h; path = include/SentryUIEventTrackerTransactionMode.h; sourceTree = "<group>"; };
13611362
62A456E42B0370E0003F19A1 /* SentryUIEventTrackerTransactionMode.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryUIEventTrackerTransactionMode.m; sourceTree = "<group>"; };
13621363
62AB8C9D2BF3925700BFC2AC /* WeakReference.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WeakReference.swift; sourceTree = "<group>"; };
1364+
62B220BA2E93A9EC004620FF /* SentryTracePropagationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryTracePropagationTests.swift; sourceTree = "<group>"; };
13631365
62BDDD112D51FD540024CCD1 /* SentryThreadCodable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryThreadCodable.swift; sourceTree = "<group>"; };
13641366
62C1AFA92B7E10D30038C5F7 /* SentrySpotlightTransport.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentrySpotlightTransport.h; path = include/SentrySpotlightTransport.h; sourceTree = "<group>"; };
13651367
62C1AFAA2B7E10EA0038C5F7 /* SentrySpotlightTransport.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentrySpotlightTransport.m; sourceTree = "<group>"; };
@@ -3825,6 +3827,7 @@
38253827
8EAE8E5C2681768000D6958B /* URLSessionTaskMock.h */,
38263828
8EAE8E5D2681768000D6958B /* URLSessionTaskMock.m */,
38273829
8EA05EEC267C2AB200C82B30 /* SentryNetworkTrackerTests.swift */,
3830+
62B220BA2E93A9EC004620FF /* SentryTracePropagationTests.swift */,
38283831
7B8CA85626DD4E6200DD872C /* SentryNetworkTrackerIntegrationTests.swift */,
38293832
6276E68A2E7A779B002A4A8F /* SentryNetworkTrackerIntegrationTestServerTests.swift */,
38303833
D8751FA4274743710032F4DE /* SentryNSURLSessionTaskSearchTests.swift */,
@@ -6046,6 +6049,7 @@
60466049
7BFAA6E7297AA16A00E7E02E /* SentryCrashMonitor_CppException_Tests.mm in Sources */,
60476050
9286059929A50BAB00F96038 /* SentryGeoTests.swift in Sources */,
60486051
621655662DB12A8900810504 /* SentryCrashMach-OTests.m in Sources */,
6052+
62B220BB2E93A9EC004620FF /* SentryTracePropagationTests.swift in Sources */,
60496053
D8B76B0828081461000A58C4 /* TestSentryScreenshotProvider.swift in Sources */,
60506054
A8AFFCD22907DA7600967CD7 /* SentryHttpStatusCodeRangeTests.swift in Sources */,
60516055
7BE2C7F8257000A4003B66C7 /* SentryTestIntegration.m in Sources */,

Tests/SentryTests/Integrations/Performance/Network/SentryNetworkTrackerTests.swift

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -956,37 +956,6 @@ class SentryNetworkTrackerTests: XCTestCase {
956956
XCTAssertNil(task.currentRequest?.allHTTPHeaderFields?["sentry-trace"])
957957
}
958958

959-
func testIsTargetMatch() throws {
960-
// Default: all urls
961-
let defaultRegex = try XCTUnwrap(NSRegularExpression(pattern: ".*"))
962-
XCTAssertTrue(SentryTracePropagation.isTargetMatch(try XCTUnwrap(URL(string: "http://localhost")), withTargets: [ defaultRegex ]))
963-
XCTAssertTrue(SentryTracePropagation.isTargetMatch(try XCTUnwrap(URL(string: "http://www.example.com/api/projects")), withTargets: [ defaultRegex ]))
964-
965-
// Strings: hostname
966-
XCTAssertTrue(SentryTracePropagation.isTargetMatch(try XCTUnwrap(URL(string: "http://localhost")), withTargets: ["localhost"]))
967-
XCTAssertTrue(SentryTracePropagation.isTargetMatch(try XCTUnwrap(URL(string: "http://localhost-but-not-really")), withTargets: ["localhost"])) // works because of `contains`
968-
XCTAssertFalse(SentryTracePropagation.isTargetMatch(try XCTUnwrap(URL(string: "http://www.example.com/api/projects")), withTargets: ["localhost"]))
969-
970-
XCTAssertFalse(SentryTracePropagation.isTargetMatch(try XCTUnwrap(URL(string: "http://localhost")), withTargets: ["www.example.com"]))
971-
XCTAssertTrue(SentryTracePropagation.isTargetMatch(try XCTUnwrap(URL(string: "http://www.example.com/api/projects")), withTargets: ["www.example.com"]))
972-
XCTAssertFalse(SentryTracePropagation.isTargetMatch(try XCTUnwrap(URL(string: "http://api.example.com/api/projects")), withTargets: ["www.example.com"]))
973-
XCTAssertTrue(SentryTracePropagation.isTargetMatch(try XCTUnwrap(URL(string: "http://www.example.com.evil.com/api/projects")), withTargets: ["www.example.com"])) // works because of `contains`
974-
975-
// Test regex
976-
let regex = try XCTUnwrap(NSRegularExpression(pattern: "http://www.example.com/api/.*"))
977-
XCTAssertFalse(SentryTracePropagation.isTargetMatch(try XCTUnwrap(URL(string: "http://localhost")), withTargets: [regex]))
978-
XCTAssertFalse(SentryTracePropagation.isTargetMatch(try XCTUnwrap(URL(string: "http://www.example.com/url")), withTargets: [regex]))
979-
XCTAssertTrue(SentryTracePropagation.isTargetMatch(try XCTUnwrap(URL(string: "http://www.example.com/api/projects")), withTargets: [regex]))
980-
981-
// Regex and string
982-
XCTAssertTrue(SentryTracePropagation.isTargetMatch(try XCTUnwrap(URL(string: "http://localhost")), withTargets: ["localhost", regex]))
983-
XCTAssertFalse(SentryTracePropagation.isTargetMatch(try XCTUnwrap(URL(string: "http://www.example.com/url")), withTargets: ["localhost", regex]))
984-
XCTAssertTrue(SentryTracePropagation.isTargetMatch(try XCTUnwrap(URL(string: "http://www.example.com/api/projects")), withTargets: ["localhost", regex]))
985-
986-
// String and integer (which isn't valid, make sure it doesn't crash)
987-
XCTAssertTrue(SentryTracePropagation.isTargetMatch(try XCTUnwrap(URL(string: "http://localhost")), withTargets: ["localhost", 123]))
988-
}
989-
990959
func testCaptureHTTPClientErrorRequest() throws {
991960
let sut = fixture.getSut()
992961

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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

Comments
 (0)