Skip to content

Commit 8be5cef

Browse files
committed
Merge pull request #545 from getsentry/remove-image-transport
Remove image transport
2 parents a895d35 + 8c33296 commit 8be5cef

File tree

2 files changed

+16
-120
lines changed

2 files changed

+16
-120
lines changed

src/raven.js

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,24 +1162,15 @@ Raven.prototype = {
11621162
});
11631163
},
11641164

1165-
_makeImageRequest: function(opts) {
1166-
// Tack on sentry_data to auth options, which get urlencoded
1167-
opts.auth.sentry_data = JSON.stringify(opts.data);
1168-
1169-
var img = this._newImage(),
1170-
src = opts.url + '?' + urlencode(opts.auth),
1171-
crossOrigin = opts.options.crossOrigin;
1165+
_makeRequest: function(opts) {
1166+
var request = new XMLHttpRequest();
11721167

1173-
if (crossOrigin || crossOrigin === '') {
1174-
img.crossOrigin = crossOrigin;
1175-
}
1176-
img.onload = opts.onSuccess;
1177-
img.onerror = img.onabort = opts.onError;
1178-
img.src = src;
1179-
},
1168+
// if browser doesn't support CORS (e.g. IE7), we are out of luck
1169+
var hasCORS =
1170+
'withCredentials' in request ||
1171+
typeof XDomainRequest !== 'undefined';
11801172

1181-
_makeXhrRequest: function(opts) {
1182-
var request;
1173+
if (!hasCORS) return;
11831174

11841175
var url = opts.url;
11851176
function handler() {
@@ -1192,7 +1183,6 @@ Raven.prototype = {
11921183
}
11931184
}
11941185

1195-
request = new XMLHttpRequest();
11961186
if ('withCredentials' in request) {
11971187
request.onreadystatechange = function () {
11981188
if (request.readyState !== 4) {
@@ -1216,14 +1206,6 @@ Raven.prototype = {
12161206
request.send(JSON.stringify(opts.data));
12171207
},
12181208

1219-
_makeRequest: function(opts) {
1220-
var hasCORS =
1221-
'withCredentials' in new XMLHttpRequest() ||
1222-
typeof XDomainRequest !== 'undefined';
1223-
1224-
return (hasCORS ? this._makeXhrRequest : this._makeImageRequest)(opts);
1225-
},
1226-
12271209
// Note: this is shitty, but I can't figure out how to get
12281210
// sinon to stub document.createElement without breaking everything
12291211
// so this wrapper is just so I can stub it for tests.

test/raven.test.js

Lines changed: 9 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,52 +1188,6 @@ describe('globals', function() {
11881188
});
11891189

11901190
describe('makeRequest', function() {
1191-
beforeEach(function() {
1192-
// use fake xml http request so we can muck w/ its prototype
1193-
this.xhr = sinon.useFakeXMLHttpRequest();
1194-
this.sinon.stub(Raven, '_makeImageRequest');
1195-
this.sinon.stub(Raven, '_makeXhrRequest');
1196-
});
1197-
1198-
afterEach(function() {
1199-
this.xhr.restore();
1200-
});
1201-
1202-
it('should call makeXhrRequest if CORS is supported', function () {
1203-
XMLHttpRequest.prototype.withCredentials = true;
1204-
1205-
Raven._makeRequest({
1206-
url: 'http://localhost/',
1207-
auth: {a: '1', b: '2'},
1208-
data: {foo: 'bar'},
1209-
options: Raven._globalOptions
1210-
});
1211-
1212-
assert.isTrue(Raven._makeImageRequest.notCalled);
1213-
assert.isTrue(Raven._makeXhrRequest.calledOnce);
1214-
});
1215-
1216-
it('should call makeImageRequest if CORS is NOT supported', function () {
1217-
delete XMLHttpRequest.prototype.withCredentials;
1218-
1219-
var oldXDR = window.XDomainRequest;
1220-
window.XDomainRequest = undefined;
1221-
1222-
Raven._makeRequest({
1223-
url: 'http://localhost/',
1224-
auth: {a: '1', b: '2'},
1225-
data: {foo: 'bar'},
1226-
options: Raven._globalOptions
1227-
});
1228-
1229-
assert.isTrue(Raven._makeImageRequest.calledOnce);
1230-
assert.isTrue(Raven._makeXhrRequest.notCalled);
1231-
1232-
window.XDomainRequest = oldXDR;
1233-
});
1234-
});
1235-
1236-
describe('makeXhrRequest', function() {
12371191
beforeEach(function() {
12381192
// NOTE: can't seem to call useFakeXMLHttpRequest via sandbox; must
12391193
// restore manually
@@ -1252,7 +1206,7 @@ describe('globals', function() {
12521206
it('should create an XMLHttpRequest object with body as JSON payload', function() {
12531207
XMLHttpRequest.prototype.withCredentials = true;
12541208

1255-
Raven._makeXhrRequest({
1209+
Raven._makeRequest({
12561210
url: 'http://localhost/',
12571211
auth: {a: '1', b: '2'},
12581212
data: {foo: 'bar'},
@@ -1263,64 +1217,24 @@ describe('globals', function() {
12631217
assert.equal(lastXhr.requestBody, '{"foo":"bar"}');
12641218
assert.equal(lastXhr.url, 'http://localhost/?a=1&b=2');
12651219
});
1266-
});
12671220

1268-
describe('makeImageRequest', function() {
1269-
var imageCache;
1221+
it('should no-op if CORS is not supported', function () {
1222+
delete XMLHttpRequest.prototype.withCredentials;
12701223

1271-
beforeEach(function () {
1272-
imageCache = [];
1273-
this.sinon.stub(Raven, '_newImage', function(){ var img = {}; imageCache.push(img); return img; });
1274-
});
1224+
var oldXDR = window.XDomainRequest;
1225+
window.XDomainRequest = undefined;
12751226

1276-
it('should load an Image', function() {
1277-
Raven._makeImageRequest({
1227+
Raven._makeRequest({
12781228
url: 'http://localhost/',
12791229
auth: {a: '1', b: '2'},
12801230
data: {foo: 'bar'},
12811231
options: Raven._globalOptions
12821232
});
1283-
assert.equal(imageCache.length, 1);
1284-
assert.equal(imageCache[0].src, 'http://localhost/?a=1&b=2&sentry_data=%7B%22foo%22%3A%22bar%22%7D');
1285-
});
1286-
1287-
it('should populate crossOrigin based on options', function() {
1288-
Raven._makeImageRequest({
1289-
url: Raven._globalEndpoint,
1290-
auth: {lol: '1'},
1291-
data: {foo: 'bar'},
1292-
options: {
1293-
crossOrigin: 'something'
1294-
}
1295-
});
1296-
assert.equal(imageCache.length, 1);
1297-
assert.equal(imageCache[0].crossOrigin, 'something');
1298-
});
12991233

1300-
it('should populate crossOrigin if empty string', function() {
1301-
Raven._makeImageRequest({
1302-
url: Raven._globalEndpoint,
1303-
auth: {lol: '1'},
1304-
data: {foo: 'bar'},
1305-
options: {
1306-
crossOrigin: ''
1307-
}
1308-
});
1309-
assert.equal(imageCache.length, 1);
1310-
assert.equal(imageCache[0].crossOrigin, '');
1311-
});
1234+
assert.equal(this.requests.length, 1); // the "test" xhr
1235+
assert.equal(this.requests[0].readyState, 0);
13121236

1313-
it('should not populate crossOrigin if falsey', function() {
1314-
Raven._makeImageRequest({
1315-
url: Raven._globalEndpoint,
1316-
auth: {lol: '1'},
1317-
data: {foo: 'bar'},
1318-
options: {
1319-
crossOrigin: false
1320-
}
1321-
});
1322-
assert.equal(imageCache.length, 1);
1323-
assert.isUndefined(imageCache[0].crossOrigin);
1237+
window.XDomainRequest = oldXDR
13241238
});
13251239
});
13261240

0 commit comments

Comments
 (0)