1
+ /* globals expect */
2
+ /* globals describe */
3
+ /* globals it */
4
+ /* globals sinon */
5
+
6
+ var Instrumenter = require ( '../src/browser/telemetry' ) ;
7
+
8
+ describe ( 'instrumentNetwork' , function ( ) {
9
+ it ( 'should capture XHR requests with string URL' , function ( done ) {
10
+ var callback = sinon . spy ( ) ;
11
+ var windowMock = {
12
+ XMLHttpRequest : function ( ) { }
13
+ }
14
+
15
+ windowMock . XMLHttpRequest . prototype . open = function ( ) { }
16
+ windowMock . XMLHttpRequest . prototype . send = function ( ) { }
17
+
18
+ var i = createInstrumenter ( callback , windowMock )
19
+ i . instrumentNetwork ( )
20
+
21
+ var xhr = new windowMock . XMLHttpRequest ( ) ;
22
+ xhr . open ( 'GET' , 'http://first.call' )
23
+ xhr . send ( )
24
+ xhr . onreadystatechange ( )
25
+
26
+ expect ( callback . callCount ) . to . eql ( 1 )
27
+ expect ( callback . args [ 0 ] [ 0 ] . url ) . to . eql ( 'http://first.call' )
28
+
29
+ i . deinstrumentNetwork ( )
30
+ i = createInstrumenter ( callback , windowMock )
31
+ i . instrumentNetwork ( )
32
+ var xhr = new windowMock . XMLHttpRequest ( ) ;
33
+ xhr . open ( 'GET' , new URL ( 'http://second.call' ) )
34
+ xhr . send ( )
35
+ xhr . onreadystatechange ( )
36
+ expect ( callback . callCount ) . to . eql ( 2 )
37
+ expect ( callback . args [ 1 ] [ 0 ] . url ) . to . eql ( 'http://second.call/' )
38
+
39
+ done ( )
40
+ } )
41
+
42
+ it ( 'should capture XHR requests with string URL' , function ( done ) {
43
+ var callback = sinon . spy ( ) ;
44
+ var windowMock = {
45
+ fetch : function ( ) { return Promise . resolve ( ) }
46
+ }
47
+
48
+ var i = createInstrumenter ( callback , windowMock ) ;
49
+ i . instrumentNetwork ( )
50
+
51
+ windowMock . fetch ( 'http://first.call' )
52
+ expect ( callback . callCount ) . to . eql ( 1 )
53
+ expect ( callback . args [ 0 ] [ 0 ] . url ) . to . eql ( 'http://first.call' )
54
+
55
+ i . deinstrumentNetwork ( )
56
+ i = createInstrumenter ( callback , windowMock )
57
+ i . instrumentNetwork ( )
58
+
59
+ windowMock . fetch ( new URL ( 'http://second.call' ) )
60
+ expect ( callback . callCount ) . to . eql ( 2 )
61
+ expect ( callback . args [ 1 ] [ 0 ] . url ) . to . eql ( 'http://second.call/' )
62
+
63
+ done ( )
64
+ } )
65
+ } )
66
+
67
+ function createInstrumenter ( callback , windowMock ) {
68
+ return new Instrumenter ( { scrubFields : [ ] } , { captureNetwork : callback } , { wrap : function ( ) { } , client : { notifier : { diagnostic : { } } } } , windowMock ) ;
69
+ }
0 commit comments