Skip to content

Commit c05f073

Browse files
committed
Merge pull request mozilla#2454 from yurydelendik/worker-xhr-response
Tests presence of the xhr-response in the worker
2 parents fe3697a + 0e70aac commit c05f073

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

src/worker.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,19 @@ var WorkerMessageHandler = {
152152
}
153153

154154
handler.on('test', function wphSetupTest(data) {
155-
handler.send('test', data instanceof Uint8Array);
155+
// check if Uint8Array can be sent to worker
156+
if (!(data instanceof Uint8Array)) {
157+
handler.send('test', false);
158+
return;
159+
}
160+
// check if the response property is supported by xhr
161+
var xhr = new XMLHttpRequest();
162+
if (!('response' in xhr || 'mozResponse' in xhr ||
163+
'responseArrayBuffer' in xhr || 'mozResponseArrayBuffer' in xhr)) {
164+
handler.send('test', false);
165+
return;
166+
}
167+
handler.send('test', true);
156168
});
157169

158170
handler.on('GetDocRequest', function wphSetupDoc(data) {

test/features/tests.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,37 @@ var tests = [
512512
},
513513
impact: 'Important',
514514
area: 'Core'
515+
},
516+
{
517+
id: 'Worker-xhr-response',
518+
name: 'XMLHttpRequest supports the reponse property in web workers',
519+
run: function () {
520+
if (typeof Worker == 'undefined')
521+
return { output: 'Skipped', emulated: '' };
522+
523+
try {
524+
var worker = new Worker('worker-stub.js');
525+
526+
var promise = new Promise();
527+
var timeout = setTimeout(function () {
528+
promise.resolve({ output: 'Failed', emulated: '?' });
529+
}, 5000);
530+
531+
worker.addEventListener('message', function (e) {
532+
var data = e.data;
533+
if (data.action == 'xhr' && data.result)
534+
promise.resolve({ output: 'Success', emulated: '' });
535+
else
536+
promise.resolve({ output: 'Failed', emulated: 'Yes' });
537+
});
538+
worker.postMessage({action: 'xhr'});
539+
return promise;
540+
} catch (e) {
541+
return { output: 'Failed', emulated: 'Yes' };
542+
}
543+
},
544+
impact: 'Important',
545+
area: 'Core'
515546
}
516547
];
517548

test/features/worker-stub.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,16 @@
1717

1818
onmessage = function (e) {
1919
var data = e.data;
20-
postMessage({action: 'test', result: data.action == 'test' &&
21-
data.data instanceof Uint8Array});
20+
switch (data.action) {
21+
case 'test':
22+
postMessage({action: 'test', result: data.data instanceof Uint8Array});
23+
break;
24+
case 'xhr':
25+
var xhr = new XMLHttpRequest();
26+
var responseExists = 'response' in xhr || 'mozResponse' in xhr ||
27+
'responseArrayBuffer' in xhr || 'mozResponseArrayBuffer' in xhr;
28+
postMessage({action: 'xhr', result: responseExists});
29+
break;
30+
}
2231
};
2332

0 commit comments

Comments
 (0)