-
-
Notifications
You must be signed in to change notification settings - Fork 414
Expand file tree
/
Copy pathSentryTracePropagationTests.swift
More file actions
180 lines (144 loc) · 8.31 KB
/
Copy pathSentryTracePropagationTests.swift
File metadata and controls
180 lines (144 loc) · 8.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import XCTest
final class SentryTracePropagationTests: XCTestCase {
func testAddTraceparent_Sampled() throws {
// Arrange
let defaultRegex = try XCTUnwrap(NSRegularExpression(pattern: ".*"))
let emptyBaggage = Baggage()
let sessionTask = try createSessionTask()
let traceID = SentryId()
let spanID = SpanId()
let traceHeader = TraceHeader(trace: traceID, spanId: spanID, sampled: SentrySampleDecision.yes)
// Act
SentryTracePropagation.addBaggageHeader(emptyBaggage, traceHeader: traceHeader, propagateTraceparent: true, tracePropagationTargets: [defaultRegex], toRequest: sessionTask)
// Assert
let traceParent = try XCTUnwrap(sessionTask.currentRequest?.allHTTPHeaderFields?["traceparent"])
XCTAssertEqual(traceParent, "00-\(traceID.sentryIdString)-\(spanID.sentrySpanIdString)-01")
}
func testAddTraceparent_NotSampled() throws {
// Arrange
let defaultRegex = try XCTUnwrap(NSRegularExpression(pattern: ".*"))
let emptyBaggage = Baggage()
let sessionTask = try createSessionTask()
let traceID = SentryId()
let spanID = SpanId()
let traceHeader = TraceHeader(trace: traceID, spanId: spanID, sampled: SentrySampleDecision.no)
// Act
SentryTracePropagation.addBaggageHeader(emptyBaggage, traceHeader: traceHeader, propagateTraceparent: true, tracePropagationTargets: [defaultRegex], toRequest: sessionTask)
// Assert
let traceParent = try XCTUnwrap(sessionTask.currentRequest?.allHTTPHeaderFields?["traceparent"])
XCTAssertEqual(traceParent, "00-\(traceID.sentryIdString)-\(spanID.sentrySpanIdString)-00")
}
func testAddTraceparent_UndecidedSampled() throws {
// Arrange
let defaultRegex = try XCTUnwrap(NSRegularExpression(pattern: ".*"))
let emptyBaggage = Baggage()
let sessionTask = try createSessionTask()
let traceID = SentryId()
let spanID = SpanId()
let traceHeader = TraceHeader(trace: traceID, spanId: spanID, sampled: SentrySampleDecision.undecided)
// Act
SentryTracePropagation.addBaggageHeader(emptyBaggage, traceHeader: traceHeader, propagateTraceparent: true, tracePropagationTargets: [defaultRegex], toRequest: sessionTask)
// Assert
let traceParent = try XCTUnwrap(sessionTask.currentRequest?.allHTTPHeaderFields?["traceparent"])
XCTAssertEqual(traceParent, "00-\(traceID.sentryIdString)-\(spanID.sentrySpanIdString)-00")
}
func testAddTraceparent_NotAddedWhenTargetDoesntMatch() throws {
// Arrange
let emptyBaggage = Baggage()
let sessionTask = try createSessionTask()
let traceID = SentryId()
let spanID = SpanId()
let traceHeader = TraceHeader(trace: traceID, spanId: spanID, sampled: SentrySampleDecision.no)
// Act
SentryTracePropagation.addBaggageHeader(emptyBaggage, traceHeader: traceHeader, propagateTraceparent: true, tracePropagationTargets: ["localhost"], toRequest: sessionTask)
// Assert
XCTAssertNil(sessionTask.currentRequest?.allHTTPHeaderFields?["traceparent"])
}
func testIsTargetMatchWithDefaultRegex_MatchesAllURLs() throws {
// Arrange
let defaultRegex = try XCTUnwrap(NSRegularExpression(pattern: ".*"))
let localhostURL = try XCTUnwrap(URL(string: "http://localhost"))
let exampleURL = try XCTUnwrap(URL(string: "http://www.example.com/api/projects"))
// Act & Assert
XCTAssertTrue(SentryTracePropagation.isTargetMatch(localhostURL, withTargets: [defaultRegex]))
XCTAssertTrue(SentryTracePropagation.isTargetMatch(exampleURL, withTargets: [defaultRegex]))
}
func testIsTargetMatchWithStringHostname_MatchesExactHostname() throws {
// Arrange
let localhostURL = try XCTUnwrap(URL(string: "http://localhost"))
let exampleURL = try XCTUnwrap(URL(string: "http://www.example.com/api/projects"))
let apiExampleURL = try XCTUnwrap(URL(string: "http://api.example.com/api/projects"))
let localhostTargets = ["localhost"]
let exampleTargets = ["www.example.com"]
// Act & Assert
XCTAssertTrue(SentryTracePropagation.isTargetMatch(localhostURL, withTargets: localhostTargets))
XCTAssertFalse(SentryTracePropagation.isTargetMatch(exampleURL, withTargets: localhostTargets))
XCTAssertFalse(SentryTracePropagation.isTargetMatch(localhostURL, withTargets: exampleTargets))
XCTAssertTrue(SentryTracePropagation.isTargetMatch(exampleURL, withTargets: exampleTargets))
XCTAssertFalse(SentryTracePropagation.isTargetMatch(apiExampleURL, withTargets: exampleTargets))
}
func testIsTargetMatchWithStringHostname_MatchesSubstrings() throws {
// Arrange
let localhostExtendedURL = try XCTUnwrap(URL(string: "http://localhost-but-not-really"))
let evilURL = try XCTUnwrap(URL(string: "http://www.example.com.evil.com/api/projects"))
let localhostTargets = ["localhost"]
let exampleTargets = ["www.example.com"]
// Act & Assert
XCTAssertTrue(SentryTracePropagation.isTargetMatch(localhostExtendedURL, withTargets: localhostTargets))
XCTAssertTrue(SentryTracePropagation.isTargetMatch(evilURL, withTargets: exampleTargets))
}
func testIsTargetMatchWithRegexPattern_MatchesSpecificPatterns() throws {
// Arrange
let regex = try XCTUnwrap(NSRegularExpression(pattern: "http://www.example.com/api/.*"))
let localhostURL = try XCTUnwrap(URL(string: "http://localhost"))
let nonAPIURL = try XCTUnwrap(URL(string: "http://www.example.com/url"))
let apiURL = try XCTUnwrap(URL(string: "http://www.example.com/api/projects"))
// Act & Assert
XCTAssertFalse(SentryTracePropagation.isTargetMatch(localhostURL, withTargets: [regex]))
XCTAssertFalse(SentryTracePropagation.isTargetMatch(nonAPIURL, withTargets: [regex]))
XCTAssertTrue(SentryTracePropagation.isTargetMatch(apiURL, withTargets: [regex]))
}
func testIsTargetMatchWithMixedRegexAndString_MatchesEitherTarget() throws {
// Arrange
let regex = try XCTUnwrap(NSRegularExpression(pattern: "http://www.example.com/api/.*"))
let localhostURL = try XCTUnwrap(URL(string: "http://localhost"))
let nonAPIURL = try XCTUnwrap(URL(string: "http://www.example.com/url"))
let apiURL = try XCTUnwrap(URL(string: "http://www.example.com/api/projects"))
let mixedTargets = ["localhost", regex] as [Any]
// Act & Assert
XCTAssertTrue(SentryTracePropagation.isTargetMatch(localhostURL, withTargets: mixedTargets))
XCTAssertFalse(SentryTracePropagation.isTargetMatch(nonAPIURL, withTargets: mixedTargets))
XCTAssertTrue(SentryTracePropagation.isTargetMatch(apiURL, withTargets: mixedTargets))
}
func testIsTargetMatchWithInvalidInput_DoesNotCrash() throws {
// Arrange
let localhostURL = try XCTUnwrap(URL(string: "http://localhost"))
let targetsWithInvalidType = ["localhost", 123] as [Any]
// Act & Assert
XCTAssertTrue(SentryTracePropagation.isTargetMatch(localhostURL, withTargets: targetsWithInvalidType))
}
// MARK: - currentRequest snapshot safety (issue #8012)
func testAddBaggageHeader_TaskWithNoCurrentRequest_DoesNotCrash() throws {
let emptyBaggage = Baggage()
let traceHeader = TraceHeader(
trace: SentryId(),
spanId: SpanId(),
sampled: .yes
)
let task = URLSessionDataTaskMock()
SentryTracePropagation.addBaggageHeader(
emptyBaggage,
traceHeader: traceHeader,
propagateTraceparent: true,
tracePropagationTargets: [try XCTUnwrap(NSRegularExpression(pattern: ".*"))],
toRequest: task
)
XCTAssertNil(task.currentRequest?.value(forHTTPHeaderField: "sentry-trace"))
}
private func createSessionTask(method: String = "GET") throws -> URLSessionDownloadTaskMock {
let url = try XCTUnwrap(URL(string: "https://www.domain.com/api?query=value&query2=value2#fragment"))
var request = URLRequest(url: url)
request.httpMethod = method
return URLSessionDownloadTaskMock(request: request)
}
}