Skip to content

Remove image transport #545

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

Merged
merged 1 commit into from
Apr 4, 2016
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
32 changes: 7 additions & 25 deletions src/raven.js
Original file line number Diff line number Diff line change
Expand Up @@ -1162,24 +1162,15 @@ Raven.prototype = {
});
},

_makeImageRequest: function(opts) {
// Tack on sentry_data to auth options, which get urlencoded
opts.auth.sentry_data = JSON.stringify(opts.data);

var img = this._newImage(),
src = opts.url + '?' + urlencode(opts.auth),
crossOrigin = opts.options.crossOrigin;
_makeRequest: function(opts) {
var request = new XMLHttpRequest();

if (crossOrigin || crossOrigin === '') {
img.crossOrigin = crossOrigin;
}
img.onload = opts.onSuccess;
img.onerror = img.onabort = opts.onError;
img.src = src;
},
// if browser doesn't support CORS (e.g. IE7), we are out of luck
var hasCORS =
'withCredentials' in request ||
typeof XDomainRequest !== 'undefined';

_makeXhrRequest: function(opts) {
var request;
if (!hasCORS) return;

var url = opts.url;
function handler() {
Expand All @@ -1192,7 +1183,6 @@ Raven.prototype = {
}
}

request = new XMLHttpRequest();
if ('withCredentials' in request) {
request.onreadystatechange = function () {
if (request.readyState !== 4) {
Expand All @@ -1216,14 +1206,6 @@ Raven.prototype = {
request.send(JSON.stringify(opts.data));
},

_makeRequest: function(opts) {
var hasCORS =
'withCredentials' in new XMLHttpRequest() ||
typeof XDomainRequest !== 'undefined';

return (hasCORS ? this._makeXhrRequest : this._makeImageRequest)(opts);
},

// Note: this is shitty, but I can't figure out how to get
// sinon to stub document.createElement without breaking everything
// so this wrapper is just so I can stub it for tests.
Expand Down
104 changes: 9 additions & 95 deletions test/raven.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1188,52 +1188,6 @@ describe('globals', function() {
});

describe('makeRequest', function() {
beforeEach(function() {
// use fake xml http request so we can muck w/ its prototype
this.xhr = sinon.useFakeXMLHttpRequest();
this.sinon.stub(Raven, '_makeImageRequest');
this.sinon.stub(Raven, '_makeXhrRequest');
});

afterEach(function() {
this.xhr.restore();
});

it('should call makeXhrRequest if CORS is supported', function () {
XMLHttpRequest.prototype.withCredentials = true;

Raven._makeRequest({
url: 'http://localhost/',
auth: {a: '1', b: '2'},
data: {foo: 'bar'},
options: Raven._globalOptions
});

assert.isTrue(Raven._makeImageRequest.notCalled);
assert.isTrue(Raven._makeXhrRequest.calledOnce);
});

it('should call makeImageRequest if CORS is NOT supported', function () {
delete XMLHttpRequest.prototype.withCredentials;

var oldXDR = window.XDomainRequest;
window.XDomainRequest = undefined;

Raven._makeRequest({
url: 'http://localhost/',
auth: {a: '1', b: '2'},
data: {foo: 'bar'},
options: Raven._globalOptions
});

assert.isTrue(Raven._makeImageRequest.calledOnce);
assert.isTrue(Raven._makeXhrRequest.notCalled);

window.XDomainRequest = oldXDR;
});
});

describe('makeXhrRequest', function() {
beforeEach(function() {
// NOTE: can't seem to call useFakeXMLHttpRequest via sandbox; must
// restore manually
Expand All @@ -1252,7 +1206,7 @@ describe('globals', function() {
it('should create an XMLHttpRequest object with body as JSON payload', function() {
XMLHttpRequest.prototype.withCredentials = true;

Raven._makeXhrRequest({
Raven._makeRequest({
url: 'http://localhost/',
auth: {a: '1', b: '2'},
data: {foo: 'bar'},
Expand All @@ -1263,64 +1217,24 @@ describe('globals', function() {
assert.equal(lastXhr.requestBody, '{"foo":"bar"}');
assert.equal(lastXhr.url, 'http://localhost/?a=1&b=2');
});
});

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

beforeEach(function () {
imageCache = [];
this.sinon.stub(Raven, '_newImage', function(){ var img = {}; imageCache.push(img); return img; });
});
var oldXDR = window.XDomainRequest;
window.XDomainRequest = undefined;

it('should load an Image', function() {
Raven._makeImageRequest({
Raven._makeRequest({
url: 'http://localhost/',
auth: {a: '1', b: '2'},
data: {foo: 'bar'},
options: Raven._globalOptions
});
assert.equal(imageCache.length, 1);
assert.equal(imageCache[0].src, 'http://localhost/?a=1&b=2&sentry_data=%7B%22foo%22%3A%22bar%22%7D');
});

it('should populate crossOrigin based on options', function() {
Raven._makeImageRequest({
url: Raven._globalEndpoint,
auth: {lol: '1'},
data: {foo: 'bar'},
options: {
crossOrigin: 'something'
}
});
assert.equal(imageCache.length, 1);
assert.equal(imageCache[0].crossOrigin, 'something');
});

it('should populate crossOrigin if empty string', function() {
Raven._makeImageRequest({
url: Raven._globalEndpoint,
auth: {lol: '1'},
data: {foo: 'bar'},
options: {
crossOrigin: ''
}
});
assert.equal(imageCache.length, 1);
assert.equal(imageCache[0].crossOrigin, '');
});
assert.equal(this.requests.length, 1); // the "test" xhr
assert.equal(this.requests[0].readyState, 0);

it('should not populate crossOrigin if falsey', function() {
Raven._makeImageRequest({
url: Raven._globalEndpoint,
auth: {lol: '1'},
data: {foo: 'bar'},
options: {
crossOrigin: false
}
});
assert.equal(imageCache.length, 1);
assert.isUndefined(imageCache[0].crossOrigin);
window.XDomainRequest = oldXDR
});
});

Expand Down