@@ -13,6 +13,14 @@ import {
13
13
import { addExtensionMethods } from '../../src/hubextensions' ;
14
14
import * as tracingUtils from '../../src/utils' ;
15
15
16
+ // This is a normal base64 regex, modified to reflect that fact that we strip the trailing = or == off
17
+ const stripped_base64 = '([a-zA-Z0-9+/]{4})*([a-zA-Z0-9+/]{2,3})?' ;
18
+
19
+ const TRACESTATE_HEADER_REGEX = new RegExp (
20
+ `sentry=(${ stripped_base64 } )` + // our part of the header - should be the only part or at least the first part
21
+ `(,\\w+=\\w+)*` , // any number of copies of a comma followed by `name=value`
22
+ ) ;
23
+
16
24
beforeAll ( ( ) => {
17
25
addExtensionMethods ( ) ;
18
26
// @ts -ignore need to override global Request because it's not in the jest environment (even with an
@@ -63,7 +71,7 @@ describe('callbacks', () => {
63
71
const fetchHandlerData : FetchData = {
64
72
args : [ 'http://dogs.are.great/' , { } ] ,
65
73
fetchData : { url : 'http://dogs.are.great/' , method : 'GET' } ,
66
- startTimestamp : 1356996072000 ,
74
+ startTimestamp : 2012112120121231 ,
67
75
} ;
68
76
const xhrHandlerData : XHRData = {
69
77
xhr : {
@@ -78,18 +86,27 @@ describe('callbacks', () => {
78
86
// setRequestHeader: XMLHttpRequest.prototype.setRequestHeader,
79
87
setRequestHeader,
80
88
} ,
81
- startTimestamp : 1353501072000 ,
89
+ startTimestamp : 2012112120121231 ,
82
90
} ;
83
- const endTimestamp = 1356996072000 ;
91
+ const endTimestamp = 2013041520130908 ;
84
92
85
93
beforeAll ( ( ) => {
86
- hub = new Hub ( new BrowserClient ( { tracesSampleRate : 1 } ) ) ;
94
+ hub = new Hub (
95
+ new BrowserClient ( {
96
+ dsn : 'https://dogsarebadatkeepingsecrets@squirrelchasers.ingest.sentry.io/12312012' ,
97
+ environment : 'dogpark' ,
98
+ release : 'off.leash.park' ,
99
+
100
+ tracesSampleRate : 1 ,
101
+ } ) ,
102
+ ) ;
87
103
makeMain ( hub ) ;
88
104
} ) ;
89
105
90
106
beforeEach ( ( ) => {
91
- transaction = hub . startTransaction ( { name : 'organizations/users/:userid ' , op : 'pageload ' } ) as Transaction ;
107
+ transaction = hub . startTransaction ( { name : 'meetNewDogFriend ' , op : 'wag.tail ' } ) as Transaction ;
92
108
hub . configureScope ( scope => scope . setSpan ( transaction ) ) ;
109
+ jest . clearAllMocks ( ) ;
93
110
} ) ;
94
111
95
112
describe ( 'fetchCallback()' , ( ) => {
@@ -117,20 +134,17 @@ describe('callbacks', () => {
117
134
expect ( spans ) . toEqual ( { } ) ;
118
135
} ) ;
119
136
120
- it ( 'does not add fetch request headers if tracing is disabled' , ( ) => {
137
+ it ( 'does not add tracing headers if tracing is disabled' , ( ) => {
121
138
hasTracingEnabled . mockReturnValueOnce ( false ) ;
122
139
123
140
// make a local copy so the global one doesn't get mutated
124
- const handlerData : FetchData = {
125
- args : [ 'http://dogs.are.great/' , { } ] ,
126
- fetchData : { url : 'http://dogs.are.great/' , method : 'GET' } ,
127
- startTimestamp : 1353501072000 ,
128
- } ;
141
+ const handlerData = { ...fetchHandlerData } ;
129
142
130
143
fetchCallback ( handlerData , alwaysCreateSpan , { } ) ;
131
144
132
145
const headers = ( handlerData . args [ 1 ] . headers as Record < string , string > ) || { } ;
133
146
expect ( headers [ 'sentry-trace' ] ) . not . toBeDefined ( ) ;
147
+ expect ( headers [ 'tracestate' ] ) . not . toBeDefined ( ) ;
134
148
} ) ;
135
149
136
150
it ( 'creates and finishes fetch span on active transaction' , ( ) => {
@@ -185,8 +199,15 @@ describe('callbacks', () => {
185
199
expect ( newSpan ! . status ) . toBe ( SpanStatus . fromHttpCode ( 404 ) ) ;
186
200
} ) ;
187
201
188
- it ( 'adds sentry-trace header to fetch requests' , ( ) => {
189
- // TODO
202
+ it ( 'adds tracing headers to fetch requests' , ( ) => {
203
+ // make a local copy so the global one doesn't get mutated
204
+ const handlerData = { ...fetchHandlerData } ;
205
+
206
+ fetchCallback ( handlerData , alwaysCreateSpan , { } ) ;
207
+
208
+ const headers = ( handlerData . args [ 1 ] . headers as Record < string , string > ) || { } ;
209
+ expect ( headers [ 'sentry-trace' ] ) . toBeDefined ( ) ;
210
+ expect ( headers [ 'tracestate' ] ) . toBeDefined ( ) ;
190
211
} ) ;
191
212
} ) ;
192
213
@@ -207,21 +228,22 @@ describe('callbacks', () => {
207
228
expect ( spans ) . toEqual ( { } ) ;
208
229
} ) ;
209
230
210
- it ( 'does not add xhr request headers if tracing is disabled' , ( ) => {
231
+ it ( 'does not add tracing headers if tracing is disabled' , ( ) => {
211
232
hasTracingEnabled . mockReturnValueOnce ( false ) ;
212
233
213
234
xhrCallback ( xhrHandlerData , alwaysCreateSpan , { } ) ;
214
235
215
236
expect ( setRequestHeader ) . not . toHaveBeenCalled ( ) ;
216
237
} ) ;
217
238
218
- it ( 'adds sentry-trace header to XHR requests' , ( ) => {
239
+ it ( 'adds tracing headers to XHR requests' , ( ) => {
219
240
xhrCallback ( xhrHandlerData , alwaysCreateSpan , { } ) ;
220
241
221
242
expect ( setRequestHeader ) . toHaveBeenCalledWith (
222
243
'sentry-trace' ,
223
244
expect . stringMatching ( tracingUtils . TRACEPARENT_REGEXP ) ,
224
245
) ;
246
+ expect ( setRequestHeader ) . toHaveBeenCalledWith ( 'tracestate' , expect . stringMatching ( TRACESTATE_HEADER_REGEX ) ) ;
225
247
} ) ;
226
248
227
249
it ( 'creates and finishes XHR span on active transaction' , ( ) => {
0 commit comments