Skip to content

Commit 998caef

Browse files
committed
Merge pull request #427 from lgustafson/lgustafson/fix-cross-domain-ie7
Fix for #426 along with updated tests
2 parents c5ee333 + 3d58ddd commit 998caef

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

src/rails.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,14 @@
191191
// This is a workaround to a IE bug.
192192
urlAnchor.href = urlAnchor.href;
193193

194-
// Make sure that the browser parses the URL and that the protocols and hosts match.
195-
return !urlAnchor.protocol || !urlAnchor.host ||
196-
(originAnchor.protocol + '//' + originAnchor.host !==
197-
urlAnchor.protocol + '//' + urlAnchor.host);
194+
// If URL protocol is false or is a string containing a single colon
195+
// *and* host are false, assume it is not a cross-domain request
196+
// (should only be the case for IE7 and IE compatibility mode).
197+
// Otherwise, evaluate protocol and host of the URL against the origin
198+
// protocol and host.
199+
return !(((!urlAnchor.protocol || urlAnchor.protocol === ':') && !urlAnchor.host) ||
200+
(originAnchor.protocol + '//' + originAnchor.host ===
201+
urlAnchor.protocol + '//' + urlAnchor.host));
198202
} catch (e) {
199203
// If there is an error parsing the URL, assume it is crossDomain.
200204
return true;

test/public/test/call-remote.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ asyncTest('sends CSRF token in custom header', 1, function() {
122122
});
123123
});
124124

125-
asyncTest('intelligently guesses crossDomain behavior when target URL is a different domain', 1, function(e, xhr) {
125+
asyncTest('intelligently guesses crossDomain behavior when target URL has a different protocol and/or hostname', 1, function(e, xhr) {
126126

127127
// Don't set data-cross-domain here, just set action to be a different domain than localhost
128128
buildForm({ action: 'http://www.alfajango.com' });
@@ -140,4 +140,23 @@ asyncTest('intelligently guesses crossDomain behavior when target URL is a diffe
140140

141141
setTimeout(function() { start(); }, 13);
142142
});
143+
144+
asyncTest('intelligently guesses crossDomain behavior when target URL consists of only a path', 1, function(e, xhr) {
145+
146+
// Don't set data-cross-domain here, just set action to be a different domain than localhost
147+
buildForm({ action: '/just/a/path' });
148+
$('#qunit-fixture').append('<meta name="csrf-token" content="cf50faa3fe97702ca1ae" />');
149+
150+
$('#qunit-fixture').find('form')
151+
.bind('ajax:beforeSend', function(e, xhr, settings) {
152+
153+
equal(settings.crossDomain, false, 'crossDomain should be set to false');
154+
155+
// prevent request from actually getting sent off-domain
156+
return false;
157+
})
158+
.trigger('submit');
159+
160+
setTimeout(function() { start(); }, 13);
161+
});
143162
})();

0 commit comments

Comments
 (0)