Skip to content

Commit f9d7b49

Browse files
chore: spa/xhr test conversion (#1038)
1 parent 59a9ff4 commit f9d7b49

16 files changed

+257
-362
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Simple XHR</title>
5+
{init} {config} {loader}
6+
<script>
7+
var origXHR = window.XMLHttpRequest
8+
window.XMLHttpRequest = function (flags) {
9+
return new origXHR(flags)
10+
}
11+
12+
for (prop in origXHR) {
13+
if (typeof origXHR[prop] === 'function') {
14+
window.XMLHttpRequest[prop] = origXHR[prop]
15+
}
16+
}
17+
</script>
18+
</head>
19+
<body>
20+
<div>
21+
This page uses inline code to simulate a bad 3rd party wrapping of the XMLHttpRequest
22+
constructor after the agent has loaded. This page uses a button to initiate an XHR. When
23+
running in a spec file, setting `window.disableAjaxHashChange = true` will disable the
24+
hash route change so the ajax call is fully processed by the ajax feature instead of the SPA feature.
25+
</div>
26+
<div><button id="sendAjax">Send Ajax</button></div>
27+
<script>
28+
window.disableAjaxHashChange = false
29+
document.getElementById('sendAjax').addEventListener('click', function () {
30+
var xhr = new XMLHttpRequest()
31+
xhr.addEventListener('loadend', function () {
32+
if (!disableAjaxHashChange) {
33+
window.location.hash = Math.random()
34+
}
35+
})
36+
xhr.open('GET', '/json')
37+
xhr.send()
38+
})
39+
</script>
40+
</body>
41+
</html>
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
<!DOCTYPE html>
2-
<!--
3-
Copyright 2020 New Relic Corporation.
4-
PDX-License-Identifier: Apache-2.0
5-
-->
62
<html>
7-
<head>
8-
<title>XHR constructor runtime-patching test</title>
9-
{init}
10-
{config}
11-
{loader}
12-
<script type="text/javascript">
3+
<head>
4+
<title>Simple XHR</title>
5+
{init} {config} {loader}
6+
<script>
137
var wrapperInvoked = false
148

159
function wrap(callback) {
@@ -34,16 +28,28 @@
3428
proto = Object.getPrototypeOf(proto)
3529
}
3630
patchAddEventListener(proto)
37-
38-
var xhrDone = false
31+
</script>
32+
</head>
33+
<body>
34+
<div>
35+
This page uses inline code to simulate a 3rd party patching of the XMLHttpRequest
36+
addEventListener after the agent has loaded. This page uses a button to initiate an XHR. When
37+
running in a spec file, setting `window.disableAjaxHashChange = true` will disable the
38+
hash route change so the ajax call is fully processed by the ajax feature instead of the SPA feature.
39+
</div>
40+
<div><button id="sendAjax">Send Ajax</button></div>
41+
<script>
42+
window.disableAjaxHashChange = false
43+
document.getElementById('sendAjax').addEventListener('click', function () {
3944
var xhr = new XMLHttpRequest()
45+
xhr.addEventListener('loadend', function () {
46+
if (!disableAjaxHashChange) {
47+
window.location.hash = Math.random()
48+
}
49+
})
4050
xhr.open('GET', '/json')
41-
xhr.addEventListener('load', function () { xhrDone = true })
4251
xhr.send()
43-
</script>
44-
</head>
45-
<body>
46-
The inline JS on this page emulates 3rd-party code which runtime-patches
47-
the EventTarget.prototype.addEventListener after our patching.
48-
</body>
52+
})
53+
</script>
54+
</body>
4955
</html>

tests/assets/ajax/xhr-with-timer.html

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Simple XHR</title>
5+
{init} {config} {loader}
6+
</head>
7+
<body>
8+
<div>
9+
This page uses a button to initiate multiple XHR and setTimeout simultaneously.
10+
When running in a spec file, setting
11+
`window.disableAjaxHashChange = true` will disable the hash route change so the
12+
ajax call is fully processed by the ajax feature instead of the SPA feature.
13+
</div>
14+
<div><button id="sendAjax">Send Ajax</button></div>
15+
<script>
16+
window.disableAjaxHashChange = false
17+
18+
var callbacks = 0
19+
function cbDecrement () {
20+
callbacks -= 1
21+
if (!disableAjaxHashChange) {
22+
window.location.hash = Math.random()
23+
}
24+
}
25+
document.getElementById('sendAjax').addEventListener('click', function () {
26+
callbacks = 4
27+
28+
var xhr1 = new XMLHttpRequest()
29+
xhr1.addEventListener('load', cbDecrement, true)
30+
xhr1.open('GET', '/json')
31+
xhr1.send()
32+
33+
var xhr2 = new XMLHttpRequest()
34+
xhr2.addEventListener('load', cbDecrement, true)
35+
xhr2.open('GET', '/json')
36+
xhr2.send()
37+
38+
setTimeout(function () {
39+
newrelic.interaction().createTracer('timer')()
40+
cbDecrement
41+
}, 10)
42+
setTimeout(function () {
43+
newrelic.interaction().createTracer('timer')()
44+
cbDecrement
45+
}, 5)
46+
})
47+
</script>
48+
</body>
49+
</html>

tests/assets/xhr-abort-onload.html

-27
This file was deleted.

tests/assets/xhr-constructor-runtime-patched.html

-36
This file was deleted.

tests/assets/xhr-large-payload.html

-35
This file was deleted.

tests/assets/xhr-outside-interaction.html

-26
This file was deleted.

tests/browser/spa/parallel-xhr-and-timers.browser.js

-65
This file was deleted.

tests/browser/spa/parallel-xhr-and-timers.spec.js

-2
This file was deleted.

tests/functional/xhr/add-event-listener-patched.test.js

-35
This file was deleted.

0 commit comments

Comments
 (0)