Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix The XhrSandbox.openNativeXhr function calls non native the xhr.setRequestHeader function (close #1252) #1254

Merged
merged 3 commits into from
Aug 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/client/sandbox/native-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ class NativeMethods {
this.xhrGetResponseHeader = win.XMLHttpRequest.prototype.getResponseHeader;
this.xhrGetAllResponseHeaders = win.XMLHttpRequest.prototype.getAllResponseHeaders;
this.xhrSetRequestHeader = win.XMLHttpRequest.prototype.setRequestHeader;
this.xhrOverrideMimeType = win.XMLHttpRequest.prototype.overrideMimeType;
this.xhrDispatchEvent = win.XMLHttpRequest.prototype.dispatchEvent;

try {
this.registerServiceWorker = win.navigator.serviceWorker.register;
Expand Down
15 changes: 10 additions & 5 deletions src/client/sandbox/xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,16 @@ export default class XhrSandbox extends SandboxBase {
static createNativeXHR () {
const xhr = new nativeMethods.XMLHttpRequest();

xhr.open = nativeMethods.xhrOpen;
xhr.abort = nativeMethods.xhrAbort;
xhr.send = nativeMethods.xhrSend;
xhr.addEventListener = nativeMethods.xhrAddEventListener;
xhr.removeEventListener = nativeMethods.xhrRemoveEventListener;
xhr.open = nativeMethods.xhrOpen;
xhr.abort = nativeMethods.xhrAbort;
xhr.send = nativeMethods.xhrSend;
xhr.addEventListener = nativeMethods.xhrAddEventListener;
xhr.removeEventListener = nativeMethods.xhrRemoveEventListener;
xhr.setRequestHeader = nativeMethods.xhrSetRequestHeader;
xhr.getResponseHeader = nativeMethods.xhrGetResponseHeader;
xhr.getAllResponseHeaders = nativeMethods.xhrGetAllResponseHeaders;
xhr.overrideMimeType = nativeMethods.xhrOverrideMimeType;
xhr.dispatchEvent = nativeMethods.xhrDispatchEvent;

return xhr;
}
Expand Down
43 changes: 24 additions & 19 deletions test/client/fixtures/sandbox/xhr-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ QUnit.testDone(function () {
iframeSandbox.off(iframeSandbox.RUN_TASK_SCRIPT_EVENT, initIframeTestHandler);
});

function getPrototypeFromChainContainsProp (obj, prop) {
while (obj && !obj.hasOwnProperty(prop))
obj = Object.getPrototypeOf(obj);

return obj;
}

test('redirect requests to proxy', function () {
jQuery.ajaxSetup({ async: false });

Expand All @@ -43,6 +50,23 @@ test('createNativeXHR', function () {
ok(xhr instanceof nativeMethods.XMLHttpRequest);

window.XMLHttpRequest = nativeMethods.XMLHttpRequest;

var isWrappedFunctionRE = /return 'function is wrapped'/;

for (var prop in xhr) {
if (typeof xhr[prop] === 'function' && prop !== 'msCachingEnabled') {
var prototype = getPrototypeFromChainContainsProp(window.XMLHttpRequest.prototype, prop);
var storedFn = prototype[prop];

prototype[prop] = function () {
return 'function is wrapped';
};

ok(!isWrappedFunctionRE.test(xhr[prop]), prop);

prototype[prop] = storedFn;
}
}
});

module('regression');
Expand Down Expand Up @@ -86,25 +110,6 @@ asyncTest('parameters must pass correctly in xhr event handlers (T239198)', func
request.send(null);
});


test('createNativeXHR returns an xhr with the native "open" method (GH-492)', function () {
var storedNativeOpen = nativeMethods.xhrOpen;
var storedPrototypeOpen = XMLHttpRequest.prototype.open;

expect(1);

nativeMethods.xhrOpen = function () {
nativeMethods.xhrOpen = storedNativeOpen;
XMLHttpRequest.prototype.open = storedPrototypeOpen;
ok(true);
};

XMLHttpRequest.prototype.open = function () {
};

XhrSandbox.createNativeXHR().open();
});

test('the internal 222 status code should be replaced with 0 on the client side', function () {
var xhr = new XMLHttpRequest();

Expand Down