Skip to content

Commit 2d2ed29

Browse files
committed
fix: Broken non-agent traffic detection + bad mock XHR in component test
1 parent 639c651 commit 2d2ed29

File tree

2 files changed

+34
-19
lines changed

2 files changed

+34
-19
lines changed

src/features/generic_events/instrument/index.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,21 +87,24 @@ export class Instrument extends InstrumentBase {
8787

8888
wrapFetch(this.ee)
8989
wrapXhr(this.ee)
90-
this.ee.on('send-xhr-start', (_, xhr) => {
91-
emitIfNonAgentTraffic.call(this, parseUrl(xhr.responseURL))
90+
this.ee.on('open-xhr-start', (args, xhr) => {
91+
if (!isInternalTraffic(args[1])) {
92+
xhr.addEventListener('readystatechange', () => {
93+
if (xhr.readyState === 2) { // HEADERS_RECEIVED
94+
handle('uaXhr', [], undefined, FEATURE_NAMES.genericEvents, this.ee)
95+
}
96+
})
97+
}
9298
})
9399
this.ee.on('fetch-start', (fetchArguments) => {
94-
if (fetchArguments.length >= 1) { emitIfNonAgentTraffic.call(this, parseUrl(extractUrl(fetchArguments[0]))) }
100+
if (fetchArguments.length >= 1 && !isInternalTraffic(extractUrl(fetchArguments[0]))) {
101+
handle('uaXhr', [], undefined, FEATURE_NAMES.genericEvents, this.ee)
102+
}
95103
})
96104

97-
function emitIfNonAgentTraffic (parsedUrl) {
98-
try {
99-
let host
100-
if (parsedUrl) host = parsedUrl.hostname + ':' + parsedUrl.port
101-
if (host && !agentRef.beacons.includes(host)) {
102-
handle('uaXhr', [], undefined, FEATURE_NAMES.genericEvents, this.ee)
103-
}
104-
} catch {}
105+
function isInternalTraffic (url) {
106+
const parsedUrl = parseUrl(url)
107+
return agentRef.beacons.includes(parsedUrl.hostname + ':' + parsedUrl.port)
105108
}
106109

107110
/** If any of the sources are active, import the aggregator. otherwise deregister */

tests/components/generic_events/instrument/index.test.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -179,25 +179,37 @@ describe('User frustrations - XMLHttpRequest', () => {
179179
class MockXMLHttpRequest {
180180
constructor () {
181181
this.readyState = 0
182-
this.status = 200
182+
this.status = 0
183183
this.responseText = ''
184184
this.responseURL = ''
185-
this.onreadystatechange = null
185+
this._url = ''
186+
}
187+
188+
onreadystatechange (url) {
189+
if (this.readyState === 1) {
190+
this._url = url
191+
} else if (this.readyState === 2) {
192+
this.responseURL = this._url
193+
}
194+
if (this._onreadystatechange) this._onreadystatechange()
186195
}
187196

188197
open (method, url) {
189198
this.method = method
190-
this.responseURL = url
191199
this.readyState = 1
200+
this.onreadystatechange(url)
192201
}
193202

194203
send () {
195-
this.readyState = 4
204+
this.readyState = 2
196205
this.responseText = 'Mock response'
197-
if (this.onreadystatechange) {
198-
this.onreadystatechange()
199-
}
206+
this.status = 200
207+
this.onreadystatechange()
200208
}
201209

202-
addEventListener = jest.fn()
210+
addEventListener = (event, handler) => {
211+
if (event === 'readystatechange' && typeof handler === 'function') {
212+
this._onreadystatechange = handler
213+
}
214+
}
203215
}

0 commit comments

Comments
 (0)