-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathajax-preload.html
349 lines (309 loc) · 15.8 KB
/
ajax-preload.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<!DOCTYPE HTML>
<html>
<head>
<meta charset = 'UTF-8' />
<meta name='viewport' content='width=device-width, initial-scale=1'>
<script type='text/javascript' src='/ajax-preload.js'></script>
<script type='text/javascript' src='/ajax-bypass.js'></script>
<!-- <script type='text/javascript' src='/dist/ajax-preload.js'></script> -->
<!-- <script type='text/javascript' src='/dist/ajax-bypass.js'></script> -->
<!-- <script type='text/javascript' src='/ajax-helper.js'></script> -->
<script type='text/javascript' src='/ajaximpls/onreadystatechange.js'></script>
<script type='text/javascript' src='/ajaximpls/listener.js'></script>
<script type='text/javascript' src='/ajaximpls/angular.js'></script>
<script type='text/javascript' src='https://cdnjs.cloudflare.com/ajax/libs/reqwest/2.0.5/reqwest.js'></script>
<script type='text/javascript' src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js'></script>
<script type='text/javascript' src='/ajaximpls/all.js'></script>
<link rel='stylesheet' href='https://unpkg.com/purecss@0.6.1/build/pure-min.css'>
<link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/flexboxgrid/6.3.1/flexboxgrid.min.css'>
<style>
div.container {margin: 1.3em;}
h1 {margin: 0 0 20px 90px; font-size: 1.5em; }
img.logo { width: 75px; float: left; }
h2 { text-align: right; font-size: 1em; }
p { text-align: right; }
table { margin-left: auto; }
table th,td { text-align: left; }
</style>
<title>Prerender.cloud AJAX monkey patch test</title>
</head>
<body>
<div class='container row around-xs'>
<div class='col-xs-12 col-lg-6'>
<a href="/"><img class='logo' src='/prerender-cloud-logo.svg' /></a>
<h1><a href='https://www.prerender.cloud/' target='_blank'>Prerender.cloud</a> AJAX monkey patch test</h1>
<p>This is a test for an XHR and window.fetch monkey patch included by default from <a href='https://www.prerender.cloud/' target='_blank'>prerender.cloud</a>. It should be run against all major browsers.</p>
<p>
The monkey patch stubs GET requests with a url->payload mapping from a base64 encoded JSON object. Once an endpoint is called, it is cleared from the stub/patch.
</p>
<div>
<p>The data structure for absolute URLs looks like:</p>
<pre><code>{'http://example.org/ajax-endpoint': 'aGVsbG8gd29ybGQ='}</code></pre>
<p>and for absolute or relative against window.location.origin:</p>
<pre><code>{'/ajax-endpoint': 'aGVsbG8gd29ybGQ='}</code></pre>
</div>
<ul>
<li>base64 is used because escaping JSON payloads in the DOM is confusing and hard to understand after the fact</li>
<li>multi-byte characters are used in this test since there's <a href='https://coolaj86.com/articles/base64-unicode-utf-8-javascript-and-you/'>potential for loss with base64 encoding</a></li>
</ul>
</div>
<div class='col-xs-12 col-lg-6'>
<h2>Bad base64</h2>
<table class='pure-table pure-table-bordered'>
<tr>
<th>1st req should come from server</th>
<td id='xhr1bad-result' data-expected='from-server: I ½ ♥ 𩶘'></td>
<td id='xhr1bad-check'></td>
</tr>
<tr>
<th>2nd req should come from server</th>
<td id='xhr2bad-result' data-expected='from-server: I ½ ♥ 𩶘'></td>
<td id='xhr2bad-check'></td>
</tr>
</table>
<h2>Happy path <strong>application/json</strong> via onreadystatechange callback</h2>
<table class='pure-table pure-table-bordered'>
<tr>
<th>1st req should come from stub</th>
<td id='xhr1-result' data-expected='from-client: I ½ ♥ 𩶘'></td>
<td id='xhr1-check'></td>
</tr>
<tr>
<th>2nd req should come from server</th>
<td id='xhr2-result' data-expected='from-server: I ½ ♥ 𩶘'></td>
<td id='xhr2-check'></td>
</tr>
</table>
<h2>Happy path via window.fetch</h2>
<table class='pure-table pure-table-bordered'>
<tr>
<th>1st req should come from stub</th>
<td id='fetch1-result' data-expected='from-client: I ½ ♥ 𩶘'></td>
<td id='fetch1-check'></td>
</tr>
<tr>
<th>2nd req should come from server</th>
<td id='fetch2-result' data-expected='from-server: I ½ ♥ 𩶘'></td>
<td id='fetch2-check'></td>
</tr>
</table>
<h2>Happy path via window.fetch and passing Request obj</h2>
<table class='pure-table pure-table-bordered'>
<tr>
<th>1st req should come from stub</th>
<td id='fetch1RequestObj-result' data-expected='from-client: I ½ ♥ 𩶘'></td>
<td id='fetch1RequestObj-check'></td>
</tr>
<tr>
<th>2nd req should come from server</th>
<td id='fetch2RequestObj-result' data-expected='from-server: I ½ ♥ 𩶘'></td>
<td id='fetch2RequestObj-check'></td>
</tr>
</table>
<h2>Happy path <strong>text/plain</strong> via onreadystatechange callback</h2>
<table class='pure-table pure-table-bordered'>
<tr>
<th>1st req should come from stub</th>
<td id='xhr1text-result' data-expected='from-client: I ½ ♥ 𩶘'></td>
<td id='xhr1text-check'></td>
</tr>
<tr>
<th>2nd req should come from server</th>
<td id='xhr2text-result' data-expected='from-server: I ½ ♥ 𩶘'></td>
<td id='xhr2text-check'></td>
</tr>
</table>
<h2>Happy path text/plain via event <strong>listeners</strong></h2>
<table class='pure-table pure-table-bordered'>
<tr>
<th>1st req should come from stub</th>
<td id='xhr1textEv-result' data-expected='from-client: I ½ ♥ 𩶘'></td>
<td id='xhr1textEv-check'></td>
</tr>
<tr>
<th>2nd req should come from server</th>
<td id='xhr2textEv-result' data-expected='from-server: I ½ ♥ 𩶘'></td>
<td id='xhr2textEv-check'></td>
</tr>
</table>
<h2>Happy path text/plain via <strong>angular helper</strong></h2>
<p>fails if the onload or onreadystatechange handlers are called twice</p>
<table class='pure-table pure-table-bordered'>
<tr>
<th>1st req should come from stub</th>
<td id='xhr1textAngular-result' data-expected='from-client: I ½ ♥ 𩶘'></td>
<td id='xhr1textAngular-check'></td>
</tr>
<tr>
<th>2nd req should come from server</th>
<td id='xhr2textAngular-result' data-expected='from-server: I ½ ♥ 𩶘'></td>
<td id='xhr2textAngular-check'></td>
</tr>
</table>
<h2>Happy path text/plain via <strong>reqwest helper</strong></h2>
<p>relies on content-type header (via getResponseHeader), and deferred execution of xhr.send</p>
<table class='pure-table pure-table-bordered'>
<tr>
<th>1st req should come from stub</th>
<td id='xhr1textReqwest-result' data-expected='from-client: I ½ ♥ 𩶘'></td>
<td id='xhr1textReqwest-check'></td>
</tr>
<tr>
<th>2nd req should come from server</th>
<td id='xhr2textReqwest-result' data-expected='from-server: I ½ ♥ 𩶘'></td>
<td id='xhr2textReqwest-check'></td>
</tr>
</table>
<h2>Happy path application/json via <strong>reqwest helper</strong></h2>
<p>relies on content-type header (via getResponseHeader), and deferred execution of xhr.send</p>
<table class='pure-table pure-table-bordered'>
<tr>
<th>1st req should come from stub</th>
<td id='xhr1JsonReqwest-result' data-expected='from-client: I ½ ♥ 𩶘'></td>
<td id='xhr1JsonReqwest-check'></td>
</tr>
<tr>
<th>2nd req should come from server</th>
<td id='xhr2JsonReqwest-result' data-expected='from-server: I ½ ♥ 𩶘'></td>
<td id='xhr2JsonReqwest-check'></td>
</tr>
</table>
<h2>Happy path application/json via <strong>jquery.ajax helper</strong></h2>
<p>relies on content-type header, via getAllResponseHeaders (not getResponseHeader)</p>
<table class='pure-table pure-table-bordered'>
<tr>
<th>1st req should come from stub</th>
<td id='xhr1JsonJquery-result' data-expected='from-client: I ½ ♥ 𩶘'></td>
<td id='xhr1JsonJquery-check'></td>
</tr>
<tr>
<th>2nd req should come from server</th>
<td id='xhr2JsonJquery-result' data-expected='from-server: I ½ ♥ 𩶘'></td>
<td id='xhr2JsonJquery-check'></td>
</tr>
</table>
<h2>Happy path text/plain POST via <strong>onreadystatechange callback</strong></h2>
<p>verifies we're passing arguments to original send method</p>
<table class='pure-table pure-table-bordered'>
<tr>
<th>the server just echos, so if it returns expected value, our args were sent</th>
<td id='xhr1PostTest-result' data-expected='{"working":"true"}'></td>
<td id='xhr1PostTest-check'></td>
</tr>
</table>
</div>
</div>
<script type='text/javascript'>
function runPostTest(ajaxImpl, el1, endpoint) {
ajaxImpl(endpoint, function(res) {
document.getElementById(el1 + '-result').innerHTML = res;
if (document.getElementById(el1 + '-result').innerHTML === document.getElementById(el1 + '-result').getAttribute('data-expected')) {
document.getElementById(el1 + '-check').innerHTML = "✅"
} else {
document.getElementById(el1 + '-check').innerHTML = "❌"
}
})
}
function runTests(ajaxImpl, el1, el2, endpoint, useJson, alreadyParsedAsJson) {
ajaxImpl(endpoint, function(res) {
document.getElementById(el1 + '-result').innerHTML = useJson ? (!alreadyParsedAsJson && JSON.parse(res) || res).text : res;
if (document.getElementById(el1 + '-result').innerHTML === document.getElementById(el1 + '-result').getAttribute('data-expected')) {
document.getElementById(el1 + '-check').innerHTML = "✅"
} else {
document.getElementById(el1 + '-check').innerHTML = "❌"
}
ajaxImpl(endpoint, function(res) {
document.getElementById(el2 + '-result').innerHTML = useJson ? (!alreadyParsedAsJson && JSON.parse(res) || res).text : res;
if (document.getElementById(el2 + '-result').innerHTML === document.getElementById(el2 + '-result').getAttribute('data-expected')) {
document.getElementById(el2 + '-check').innerHTML = "✅"
} else {
document.getElementById(el2 + '-check').innerHTML = "❌"
}
});
})
}
</script>
<script type='text/javascript'>
var originalXMLHttpRequestOpen = XMLHttpRequest.prototype.open;
var originalXMLHttpRequestSend = XMLHttpRequest.prototype.send;
var originalFetch = fetch;
function restore() {
XMLHttpRequest.prototype.open = originalXMLHttpRequestOpen;
XMLHttpRequest.prototype.send = originalXMLHttpRequestSend;
fetch = originalFetch;
}
var base64Expectations = {
dev: {
// Buffer.from(JSON.stringify({'/json1': ['application/json', JSON.stringify({text: 'from-client: I ½ ♥ 𩶘'})]})).toString('base64')
json: 'eyIvanNvbjEiOlsiYXBwbGljYXRpb24vanNvbiIsIntcInRleHRcIjpcImZyb20tY2xpZW50OiBJIMK9IOKZpSDwqbaYXCJ9Il19',
// Buffer.from(JSON.stringify({'/text1': ['text/plain', 'from-client: I ½ ♥ 𩶘']})).toString('base64')
text: 'eyIvdGV4dDEiOlsidGV4dC9wbGFpbiIsImZyb20tY2xpZW50OiBJIMK9IOKZpSDwqbaYIl19'
},
prod: {
// Buffer.from(JSON.stringify({'/json1': ['application/json', JSON.stringify({text: 'from-client: I ½ ♥ 𩶘'})]})).toString('base64')
json: 'eyIvanNvbjEiOlsiYXBwbGljYXRpb24vanNvbiIsIntcInRleHRcIjpcImZyb20tY2xpZW50OiBJIMK9IOKZpSDwqbaYXCJ9Il19',
// Buffer.from(JSON.stringify({'/text1': ['text/plain', 'from-client: I ½ ♥ 𩶘']})).toString('base64')
text: 'eyIvdGV4dDEiOlsidGV4dC9wbGFpbiIsImZyb20tY2xpZW50OiBJIMK9IOKZpSDwqbaYIl19'
}
};
var getEnv = function() { return window.location.origin.match(/localhost/) ? 'dev' : 'prod'; };
var getField = function(field) { return base64Expectations[getEnv()][field]; };
base64Expectations.json = function() { return getField('json'); };
base64Expectations.text = function() { return getField('text'); };
/**
* - ajaxMonkeyPatch stubs XHR GET requests with a url-> payload mapping from a base64 encoded JSON object
* once an endpoint is called, it is cleared from the stub
* - looks like:
* {
* 'http://example.org/ajax-endpoint': 'aGVsbG8gd29ybGQ='
* }
**/
(function(window){
var host = '/';
// when base64 is bad
restore();
ajaxMonkeyPatchForPreload(window, 'invalid-base64');
runTests(ajaxImpls.onReadyStatechange, 'xhr1bad','xhr2bad', host + 'json1', true);
// when base64 is valid stringified JSON payload:
restore();
ajaxMonkeyPatchForPreload(window, base64Expectations.json());
runTests(ajaxImpls.onReadyStatechange, 'xhr1','xhr2', host + 'json1', true);
// when base64 is valid stringified JSON payload:
restore();
ajaxMonkeyPatchForPreload(window, base64Expectations.json());
runTests(ajaxImpls.fetch, 'fetch1','fetch2', host + 'json1', true, true);
// when base64 is valid stringified JSON payload:
restore();
ajaxMonkeyPatchForPreload(window, base64Expectations.json());
runTests(ajaxImpls.fetchRequestObj, 'fetch1RequestObj','fetch2RequestObj', host + 'json1', true, true);
// when valid text payload
restore();
ajaxMonkeyPatchForPreload(window, base64Expectations.text());
runTests(ajaxImpls.onReadyStatechange, 'xhr1text','xhr2text', host + 'text1', false);
// when valid text payload using an event listener
restore();
ajaxMonkeyPatchForPreload(window, base64Expectations.text());
runTests(ajaxImpls.listener, 'xhr1textEv','xhr2textEv', host + 'text1', false);
// when valid text payload using angular's helper
restore();
ajaxMonkeyPatchForPreload(window, base64Expectations.text());
runTests(ajaxImpls.angular, 'xhr1textAngular','xhr2textAngular', host + 'text1', false);
// when valid text payload using reqwest
restore();
ajaxMonkeyPatchForPreload(window, base64Expectations.text());
runTests(ajaxImpls.reqwest, 'xhr1textReqwest','xhr2textReqwest', host + 'text1', false);
// when valid json payload using reqwest
restore();
ajaxMonkeyPatchForPreload(window, base64Expectations.json());
runTests(ajaxImpls.reqwest, 'xhr1JsonReqwest','xhr2JsonReqwest', host + 'json1', true, true);
// when valid json payload using jquery.ajax
restore();
ajaxMonkeyPatchForPreload(window, base64Expectations.json());
runTests(ajaxImpls.jQuery, 'xhr1JsonJquery','xhr2JsonJquery', host + 'json1', true, true);
// valid post request
restore();
runPostTest(ajaxImpls.onReadyStatechangePost.bind(undefined, 'working=true'), 'xhr1PostTest', 'echo');
})(window)
</script>
</body>
</html>