Skip to content

Commit 0f15482

Browse files
committed
Refactor makeRequest to prepare to be overridden
1 parent 4512216 commit 0f15482

File tree

2 files changed

+118
-54
lines changed

2 files changed

+118
-54
lines changed

src/raven.js

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -748,35 +748,41 @@ function send(data) {
748748
// Set lastEventId after we know the error should actually be sent
749749
lastEventId = data.event_id || (data.event_id = uuid4());
750750

751-
makeRequest(data);
752-
}
751+
logDebug('debug', 'Raven about to send:', data);
753752

753+
if (!isSetup()) return;
754754

755-
function makeRequest(data) {
756-
var img,
757-
src;
755+
var url = globalServer + authQueryString;
758756

759-
logDebug('debug', 'Raven about to send:', data);
757+
makeRequest({
758+
url: url,
759+
data: data,
760+
options: globalOptions,
761+
onSuccess: function success() {
762+
triggerEvent('success', {
763+
data: data,
764+
src: url
765+
});
766+
},
767+
onError: function failure() {
768+
triggerEvent('failure', {
769+
data: data,
770+
src: url
771+
});
772+
}
773+
});
774+
}
760775

761-
if (!isSetup()) return;
762776

763-
img = newImage();
764-
src = globalServer + authQueryString + '&sentry_data=' + encodeURIComponent(JSON.stringify(data));
765-
if (globalOptions.crossOrigin || globalOptions.crossOrigin === '') {
766-
img.crossOrigin = globalOptions.crossOrigin;
777+
function makeRequest(opts) {
778+
var img = newImage(),
779+
src = opts.url + '&sentry_data=' + encodeURIComponent(JSON.stringify(opts.data));
780+
781+
if (opts.options.crossOrigin || opts.options.crossOrigin === '') {
782+
img.crossOrigin = opts.options.crossOrigin;
767783
}
768-
img.onload = function success() {
769-
triggerEvent('success', {
770-
data: data,
771-
src: src
772-
});
773-
};
774-
img.onerror = img.onabort = function failure() {
775-
triggerEvent('failure', {
776-
data: data,
777-
src: src
778-
});
779-
};
784+
img.onload = opts.onSuccess;
785+
img.onerror = img.onabort = opts.onError;
780786
img.src = src;
781787
}
782788

test/raven.test.js

Lines changed: 89 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,7 @@ describe('globals', function() {
881881
};
882882

883883
send({foo: 'bar'});
884-
assert.deepEqual(window.makeRequest.lastCall.args[0], {
884+
assert.deepEqual(window.makeRequest.lastCall.args[0].data, {
885885
project: '2',
886886
logger: 'javascript',
887887
platform: 'javascript',
@@ -913,7 +913,7 @@ describe('globals', function() {
913913
globalUser = {name: 'Matt'};
914914

915915
send({foo: 'bar'});
916-
assert.deepEqual(window.makeRequest.lastCall.args, [{
916+
assert.deepEqual(window.makeRequest.lastCall.args[0].data, {
917917
project: '2',
918918
logger: 'javascript',
919919
platform: 'javascript',
@@ -929,7 +929,7 @@ describe('globals', function() {
929929
},
930930
foo: 'bar',
931931
extra: {'session:duration': 100}
932-
}]);
932+
});
933933
});
934934

935935
it('should merge in global tags', function() {
@@ -948,7 +948,7 @@ describe('globals', function() {
948948

949949

950950
send({tags: {tag2: 'value2'}});
951-
assert.deepEqual(window.makeRequest.lastCall.args, [{
951+
assert.deepEqual(window.makeRequest.lastCall.args[0].data, {
952952
project: '2',
953953
logger: 'javascript',
954954
platform: 'javascript',
@@ -961,7 +961,7 @@ describe('globals', function() {
961961
event_id: 'abc123',
962962
tags: {tag1: 'value1', tag2: 'value2'},
963963
extra: {'session:duration': 100}
964-
}]);
964+
});
965965
assert.deepEqual(globalOptions, {
966966
logger: 'javascript',
967967
tags: {tag1: 'value1'}
@@ -984,7 +984,7 @@ describe('globals', function() {
984984

985985

986986
send({extra: {key2: 'value2'}});
987-
assert.deepEqual(window.makeRequest.lastCall.args, [{
987+
assert.deepEqual(window.makeRequest.lastCall.args[0].data, {
988988
project: '2',
989989
logger: 'javascript',
990990
platform: 'javascript',
@@ -996,7 +996,7 @@ describe('globals', function() {
996996
},
997997
event_id: 'abc123',
998998
extra: {key1: 'value1', key2: 'value2', 'session:duration': 100}
999-
}]);
999+
});
10001000
assert.deepEqual(globalOptions, {
10011001
logger: 'javascript',
10021002
extra: {key1: 'value1'}
@@ -1018,10 +1018,10 @@ describe('globals', function() {
10181018
globalUser = {name: 'Matt'};
10191019

10201020
send({foo: 'bar'});
1021-
assert.deepEqual(window.makeRequest.lastCall.args, [{
1021+
assert.deepEqual(window.makeRequest.lastCall.args[0].data, {
10221022
lol: 'ibrokeit',
10231023
event_id: 'abc123',
1024-
}]);
1024+
});
10251025
});
10261026

10271027
it('should ignore dataCallback if it does not return anything', function() {
@@ -1041,7 +1041,7 @@ describe('globals', function() {
10411041
};
10421042

10431043
send({foo: 'bar'});
1044-
assert.deepEqual(window.makeRequest.lastCall.args[0], {
1044+
assert.deepEqual(window.makeRequest.lastCall.args[0].data, {
10451045
project: '2',
10461046
logger: 'javascript',
10471047
platform: 'javascript',
@@ -1072,7 +1072,7 @@ describe('globals', function() {
10721072
};
10731073

10741074
send({foo: 'bar', tags: {}, extra: {}});
1075-
assert.deepEqual(window.makeRequest.lastCall.args[0], {
1075+
assert.deepEqual(window.makeRequest.lastCall.args[0].data, {
10761076
project: '2',
10771077
logger: 'javascript',
10781078
platform: 'javascript',
@@ -1103,7 +1103,7 @@ describe('globals', function() {
11031103
};
11041104

11051105
send({foo: 'bar'});
1106-
assert.deepEqual(window.makeRequest.lastCall.args[0], {
1106+
assert.deepEqual(window.makeRequest.lastCall.args[0].data, {
11071107
project: '2',
11081108
release: 'abc123',
11091109
logger: 'javascript',
@@ -1119,40 +1119,86 @@ describe('globals', function() {
11191119
extra: {'session:duration': 100}
11201120
});
11211121
});
1122-
});
11231122

1124-
describe('makeRequest', function() {
1125-
var imageCache;
1123+
it('should pass correct opts to makeRequest', function() {
1124+
this.sinon.stub(window, 'isSetup').returns(true);
1125+
this.sinon.stub(window, 'makeRequest');
1126+
this.sinon.stub(window, 'getHttpData').returns({
1127+
url: 'http://localhost/?a=b',
1128+
headers: {'User-Agent': 'lolbrowser'}
1129+
});
11261130

1127-
beforeEach(function () {
1128-
imageCache = [];
1129-
this.sinon.stub(window, 'newImage', function(){ var img = {}; imageCache.push(img); return img; });
1130-
})
1131+
globalServer = 'http://localhost/store/';
1132+
authQueryString = '?lol'
1133+
1134+
globalOptions = {
1135+
projectId: 2,
1136+
logger: 'javascript',
1137+
release: 'abc123',
1138+
};
1139+
1140+
send({foo: 'bar'});
1141+
var args = window.makeRequest.lastCall.args;
1142+
assert.equal(args.length, 1);
1143+
var opts = args[0];
1144+
assert.equal(opts.url, 'http://localhost/store/?lol');
1145+
assert.deepEqual(opts.data, {
1146+
project: '2',
1147+
release: 'abc123',
1148+
logger: 'javascript',
1149+
platform: 'javascript',
1150+
request: {
1151+
url: 'http://localhost/?a=b',
1152+
headers: {
1153+
'User-Agent': 'lolbrowser'
1154+
}
1155+
},
1156+
event_id: 'abc123',
1157+
foo: 'bar',
1158+
extra: {'session:duration': 100},
1159+
});
1160+
assert.deepEqual(opts.options, globalOptions);
1161+
assert.isFunction(opts.onSuccess);
1162+
assert.isFunction(opts.onError);
1163+
});
11311164

11321165
it('should check `isSetup`', function() {
11331166
this.sinon.stub(window, 'isSetup').returns(false);
1134-
makeRequest({foo: 'bar'});
1167+
this.sinon.stub(window, 'makeRequest');
1168+
send({foo: 'bar'});
11351169
assert.isTrue(window.isSetup.called);
11361170
});
11371171

1138-
it('should not create the image if `isSetup` is false', function() {
1172+
it('should not makeRequest if `isSetup` is false', function() {
11391173
this.sinon.stub(window, 'isSetup').returns(false);
1140-
makeRequest({foo: 'bar'});
1141-
assert.isFalse(window.newImage.called);
1174+
this.sinon.stub(window, 'makeRequest');
1175+
send({foo: 'bar'});
1176+
assert.isFalse(window.makeRequest.called);
11421177
});
11431178

11441179
it('should log to console', function() {
11451180
this.sinon.stub(window, 'isSetup').returns(true);
11461181
this.sinon.stub(window, 'logDebug');
1147-
makeRequest({foo: 'bar'});
1182+
this.sinon.stub(window, 'makeRequest');
1183+
send({foo: 'bar'});
11481184
assert.isTrue(window.logDebug.called);
11491185
});
1186+
});
11501187

1151-
it('should load an Image', function() {
1152-
authQueryString = '?lol';
1153-
globalServer = 'http://localhost/';
1188+
describe('makeRequest', function() {
1189+
var imageCache;
11541190

1155-
makeRequest({foo: 'bar'});
1191+
beforeEach(function () {
1192+
imageCache = [];
1193+
this.sinon.stub(window, 'newImage', function(){ var img = {}; imageCache.push(img); return img; });
1194+
})
1195+
1196+
it('should load an Image', function() {
1197+
makeRequest({
1198+
url: 'http://localhost/?lol',
1199+
data: {foo: 'bar'},
1200+
options: globalOptions
1201+
});
11561202
assert.equal(imageCache.length, 1);
11571203
assert.equal(imageCache[0].src, 'http://localhost/?lol&sentry_data=%7B%22foo%22%3A%22bar%22%7D');
11581204
});
@@ -1161,7 +1207,11 @@ describe('globals', function() {
11611207
globalOptions = {
11621208
crossOrigin: 'something'
11631209
};
1164-
makeRequest({foo: 'bar'});
1210+
makeRequest({
1211+
url: globalServer,
1212+
data: {foo: 'bar'},
1213+
options: globalOptions
1214+
});
11651215
assert.equal(imageCache.length, 1);
11661216
assert.equal(imageCache[0].crossOrigin, 'something');
11671217
});
@@ -1170,7 +1220,11 @@ describe('globals', function() {
11701220
globalOptions = {
11711221
crossOrigin: ''
11721222
};
1173-
makeRequest({foo: 'bar'});
1223+
makeRequest({
1224+
url: globalServer,
1225+
data: {foo: 'bar'},
1226+
options: globalOptions
1227+
});
11741228
assert.equal(imageCache.length, 1);
11751229
assert.equal(imageCache[0].crossOrigin, '');
11761230
});
@@ -1179,7 +1233,11 @@ describe('globals', function() {
11791233
globalOptions = {
11801234
crossOrigin: false
11811235
};
1182-
makeRequest({foo: 'bar'});
1236+
makeRequest({
1237+
url: globalServer,
1238+
data: {foo: 'bar'},
1239+
options: globalOptions
1240+
});
11831241
assert.equal(imageCache.length, 1);
11841242
assert.isUndefined(imageCache[0].crossOrigin);
11851243
});

0 commit comments

Comments
 (0)