Skip to content

Commit 7c4dee0

Browse files
committed
Fix for #136 (cross domain requests)
As far as I can tell the hack originally suggested by @dcneiner is the best way to do this. The problem is that we still pass our mock XHR object to jQuery and it still sees the mismatched domains and forces an actual HTTP request using a script tag. Instead, we have to explicitly tell jQuery not to do so. We can safely set this all the time, because if we're mocking, we never want to send a cross-domain request. Thanks to @LeopoldoFu and @r4j4h for keeping on this one.
1 parent 9376b68 commit 7c4dee0

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

src/jquery.mockjax.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,11 @@
585585
}
586586
}
587587

588-
588+
// We are mocking, so there will be no cross domain request, however, jQuery
589+
// aggressively pursues this if the domains don't match, so we need to
590+
// explicitly disallow it. (See #136)
591+
origSettings.crossDomain = false;
592+
589593
// Removed to fix #54 - keep the mocking data object intact
590594
//mockHandler.data = requestSettings.data;
591595

test/test.js

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
qunit.begin(function() {
77

8-
qunit.noErrorCallbackExpected = function noErrorCallbackExpected() {
9-
qunit.assert.ok(false, 'Error callback executed');
8+
qunit.noErrorCallbackExpected = function noErrorCallbackExpected(xhr) {
9+
qunit.assert.ok(false, 'Error callback executed: ' + xhr.status, xhr.responseText);
1010
};
1111

1212
// Speed up our tests
@@ -2443,6 +2443,48 @@
24432443
});
24442444
});
24452445

2446+
t('Bug #136: cross domain script requests - GET', function(assert) {
2447+
var done = assert.async();
2448+
2449+
$.mockjax({
2450+
type: 'GET',
2451+
url: 'http://jquery-mockjax-foobar.com/somefile.js',
2452+
responseText: '(window.mockjaxCrossDomain=true)'
2453+
});
2454+
2455+
$.ajax({
2456+
type: 'GET',
2457+
dataType: 'script',
2458+
url: 'http://jquery-mockjax-foobar.com/somefile.js',
2459+
error: qunit.noErrorCallbackExpected,
2460+
success: function(data) {
2461+
assert.strictEqual(window.mockjaxCrossDomain, true, 'mockjax call for script was mocked');
2462+
},
2463+
complete: done
2464+
});
2465+
});
2466+
2467+
t('Bug #136: cross domain script requests - POST', function(assert) {
2468+
var done = assert.async();
2469+
2470+
$.mockjax({
2471+
type: 'POST',
2472+
url: 'http://jquery-mockjax-foobar.com/somefile.js',
2473+
responseText: '(window.mockjaxCrossDomain=true)'
2474+
});
2475+
2476+
$.ajax({
2477+
type: 'POST',
2478+
dataType: 'script',
2479+
url: 'http://jquery-mockjax-foobar.com/somefile.js',
2480+
error: qunit.noErrorCallbackExpected,
2481+
success: function(data) {
2482+
assert.strictEqual(window.mockjaxCrossDomain, true, 'mockjax call for script was mocked');
2483+
},
2484+
complete: done
2485+
});
2486+
});
2487+
24462488

24472489
/* -------------------- */
24482490
qunit.module('namespace');

0 commit comments

Comments
 (0)