@@ -21,6 +21,8 @@ import {
21
21
X_B3_SAMPLED ,
22
22
X_B3_SPAN_ID ,
23
23
X_B3_TRACE_ID ,
24
+ isWrapped ,
25
+ NoopLogger ,
24
26
} from '@opentelemetry/core' ;
25
27
import { ZoneContextManager } from '@opentelemetry/context-zone' ;
26
28
import * as tracing from '@opentelemetry/tracing' ;
@@ -63,7 +65,6 @@ const getData = (url: string, callbackAfterSend: Function, async?: boolean) => {
63
65
resolve ( ) ;
64
66
} ;
65
67
req . send ( ) ;
66
-
67
68
callbackAfterSend ( ) ;
68
69
} ) ;
69
70
} ;
@@ -132,6 +133,7 @@ describe('xhr', () => {
132
133
let spyEntries : any ;
133
134
const url = `${ window . location . origin } /xml-http-request.js` ;
134
135
let fakeNow = 0 ;
136
+ let xmlHttpRequestPlugin : XMLHttpRequestPlugin ;
135
137
136
138
clearData = ( ) => {
137
139
requests = [ ] ;
@@ -163,13 +165,13 @@ describe('xhr', () => {
163
165
164
166
spyEntries = sandbox . stub ( performance , 'getEntriesByType' ) ;
165
167
spyEntries . withArgs ( 'resource' ) . returns ( resources ) ;
166
-
168
+ xmlHttpRequestPlugin = new XMLHttpRequestPlugin ( {
169
+ propagateTraceHeaderCorsUrls : propagateTraceHeaderCorsUrls ,
170
+ } ) ;
167
171
webTracerProviderWithZone = new WebTracerProvider ( {
168
172
logLevel : LogLevel . ERROR ,
169
173
plugins : [
170
- new XMLHttpRequestPlugin ( {
171
- propagateTraceHeaderCorsUrls : propagateTraceHeaderCorsUrls ,
172
- } ) ,
174
+ xmlHttpRequestPlugin ,
173
175
] ,
174
176
} ) ;
175
177
webTracerWithZone = webTracerProviderWithZone . getTracer ( 'xhr-test' ) ;
@@ -211,6 +213,20 @@ describe('xhr', () => {
211
213
clearData ( ) ;
212
214
} ) ;
213
215
216
+ it ( 'should patch to wrap XML HTTP Requests when enabled' , ( ) => {
217
+ let xhttp = new XMLHttpRequest ( ) ;
218
+ assert . ok ( isWrapped ( xhttp . send ) ) ;
219
+ xmlHttpRequestPlugin . enable ( XMLHttpRequest . prototype , new api . NoopTracerProvider ( ) , new NoopLogger ( ) ) ;
220
+ assert . ok ( isWrapped ( xhttp . send ) ) ;
221
+ } ) ;
222
+
223
+ it ( 'should unpatch to unwrap XML HTTP Requests when disabled' , ( ) => {
224
+ let xhttp = new XMLHttpRequest ( ) ;
225
+ assert . ok ( isWrapped ( xhttp . send ) ) ;
226
+ xmlHttpRequestPlugin . disable ( )
227
+ assert . ok ( ! isWrapped ( xhttp . send ) ) ;
228
+ } ) ;
229
+
214
230
it ( 'should create a span with correct root span' , ( ) => {
215
231
const span : tracing . ReadableSpan = exportSpy . args [ 0 ] [ 0 ] [ 0 ] ;
216
232
assert . strictEqual (
@@ -346,6 +362,87 @@ describe('xhr', () => {
346
362
) ;
347
363
348
364
assert . strictEqual ( events . length , 12 , 'number of events is wrong' ) ;
365
+
366
+ // it('should create a span for preflight request', () => {
367
+ // const span: tracing.ReadableSpan = exportSpy.args[0][0][0];
368
+ // const parentSpan: tracing.ReadableSpan = exportSpy.args[1][0][0];
369
+ // assert.strictEqual(
370
+ // span.parentSpanId,
371
+ // parentSpan.spanContext.spanId,
372
+ // 'parent span is not root span'
373
+ // );
374
+ // });
375
+
376
+ // it('preflight request span should have correct name', () => {
377
+ // const span: tracing.ReadableSpan = exportSpy.args[0][0][0];
378
+ // assert.strictEqual(
379
+ // span.name,
380
+ // 'CORS Preflight',
381
+ // 'preflight request span has wrong name'
382
+ // );
383
+ // });
384
+
385
+ // it('preflight request span should have correct kind', () => {
386
+ // const span: tracing.ReadableSpan = exportSpy.args[0][0][0];
387
+ // assert.strictEqual(
388
+ // span.kind,
389
+ // api.SpanKind.INTERNAL,
390
+ // 'span has wrong kind'
391
+ // );
392
+ // });
393
+
394
+ // it('preflight request span should have correct events', () => {
395
+ // const span: tracing.ReadableSpan = exportSpy.args[0][0][0];
396
+ // const events = span.events;
397
+ // assert.strictEqual(events.length, 9, 'number of events is wrong');
398
+
399
+ // assert.strictEqual(
400
+ // events[0].name,
401
+ // PTN.FETCH_START,
402
+ // `event ${PTN.FETCH_START} is not defined`
403
+ // );
404
+ // assert.strictEqual(
405
+ // events[1].name,
406
+ // PTN.DOMAIN_LOOKUP_START,
407
+ // `event ${PTN.DOMAIN_LOOKUP_START} is not defined`
408
+ // );
409
+ // assert.strictEqual(
410
+ // events[2].name,
411
+ // PTN.DOMAIN_LOOKUP_END,
412
+ // `event ${PTN.DOMAIN_LOOKUP_END} is not defined`
413
+ // );
414
+ // assert.strictEqual(
415
+ // events[3].name,
416
+ // PTN.CONNECT_START,
417
+ // `event ${PTN.CONNECT_START} is not defined`
418
+ // );
419
+ // assert.strictEqual(
420
+ // events[4].name,
421
+ // PTN.SECURE_CONNECTION_START,
422
+ // `event ${PTN.SECURE_CONNECTION_START} is not defined`
423
+ // );
424
+ // assert.strictEqual(
425
+ // events[5].name,
426
+ // PTN.CONNECT_END,
427
+ // `event ${PTN.CONNECT_END} is not defined`
428
+ // );
429
+ // assert.strictEqual(
430
+ // events[6].name,
431
+ // PTN.REQUEST_START,
432
+ // `event ${PTN.REQUEST_START} is not defined`
433
+ // );
434
+ // assert.strictEqual(
435
+ // events[7].name,
436
+ // PTN.RESPONSE_START,
437
+ // `event ${PTN.RESPONSE_START} is not defined`
438
+ // );
439
+ // assert.strictEqual(
440
+ // events[8].name,
441
+ // PTN.RESPONSE_END,
442
+ // `event ${PTN.RESPONSE_END} is not defined`
443
+ // );
444
+ // });
445
+
349
446
} ) ;
350
447
351
448
describe ( 'AND origin match with window.location' , ( ) => {
0 commit comments