Skip to content

Commit 2e4a92c

Browse files
committed
Get rid of setAuthQueryString and pass all the bits to makeRequest
1 parent 0f15482 commit 2e4a92c

File tree

2 files changed

+40
-32
lines changed

2 files changed

+40
-32
lines changed

src/raven.js

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ var _Raven = window.Raven,
2323
maxMessageLength: 100,
2424
extra: {}
2525
},
26-
authQueryString,
2726
isRavenInstalled = false,
2827
objectPrototype = Object.prototype,
2928
// capture references to window.console *and* all its methods first
@@ -114,8 +113,6 @@ var Raven = {
114113

115114
TraceKit.collectWindowErrors = !!globalOptions.collectWindowErrors;
116115

117-
setAuthQueryString();
118-
119116
// return for chaining
120117
return Raven;
121118
},
@@ -508,15 +505,6 @@ function each(obj, callback) {
508505
}
509506
}
510507

511-
512-
function setAuthQueryString() {
513-
authQueryString =
514-
'?sentry_version=4' +
515-
'&sentry_client=raven-js/' + Raven.VERSION +
516-
'&sentry_key=' + globalKey;
517-
}
518-
519-
520508
function handleStackInfo(stackInfo, options) {
521509
var frames = [];
522510

@@ -752,31 +740,36 @@ function send(data) {
752740

753741
if (!isSetup()) return;
754742

755-
var url = globalServer + authQueryString;
756-
757743
makeRequest({
758-
url: url,
744+
url: globalServer,
745+
auth: {
746+
sentry_version: '4',
747+
sentry_client: 'raven-js/' + Raven.VERSION,
748+
sentry_key: globalKey
749+
},
759750
data: data,
760751
options: globalOptions,
761752
onSuccess: function success() {
762753
triggerEvent('success', {
763754
data: data,
764-
src: url
755+
src: globalServer
765756
});
766757
},
767758
onError: function failure() {
768759
triggerEvent('failure', {
769760
data: data,
770-
src: url
761+
src: globalServer
771762
});
772763
}
773764
});
774765
}
775766

776-
777767
function makeRequest(opts) {
768+
// Tack on sentry_data to auth options, which get urlencoded
769+
opts.auth.sentry_data = JSON.stringify(opts.data);
770+
778771
var img = newImage(),
779-
src = opts.url + '&sentry_data=' + encodeURIComponent(JSON.stringify(opts.data));
772+
src = opts.url + '?' + urlencode(opts.auth);
780773

781774
if (opts.options.crossOrigin || opts.options.crossOrigin === '') {
782775
img.crossOrigin = opts.options.crossOrigin;
@@ -876,4 +869,13 @@ function afterLoad() {
876869
Raven.config(RavenConfig.dsn, RavenConfig.config).install();
877870
}
878871
}
872+
873+
function urlencode(o) {
874+
var pairs = [];
875+
each(o, function(key, value) {
876+
pairs.push(encodeURIComponent(key) + '=' + encodeURIComponent(value));
877+
});
878+
return pairs.join('&');
879+
}
880+
879881
afterLoad();

test/raven.test.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
function flushRavenState() {
2-
authQueryString = undefined;
32
hasJSON = !isUndefined(window.JSON);
43
lastCapturedException = undefined;
54
lastEventId = undefined;
@@ -360,14 +359,6 @@ describe('globals', function() {
360359
});
361360
});
362361

363-
describe('setAuthQueryString', function() {
364-
it('should return a properly formatted string and cache it', function() {
365-
var expected = '?sentry_version=4&sentry_client=raven-js/<%= pkg.version %>&sentry_key=abc';
366-
setAuthQueryString();
367-
assert.strictEqual(authQueryString, expected);
368-
});
369-
});
370-
371362
describe('parseDSN', function() {
372363
it('should do what it advertises', function() {
373364
var pieces = parseDSN('http://[email protected]:80/2');
@@ -1129,7 +1120,6 @@ describe('globals', function() {
11291120
});
11301121

11311122
globalServer = 'http://localhost/store/';
1132-
authQueryString = '?lol'
11331123

11341124
globalOptions = {
11351125
projectId: 2,
@@ -1141,7 +1131,7 @@ describe('globals', function() {
11411131
var args = window.makeRequest.lastCall.args;
11421132
assert.equal(args.length, 1);
11431133
var opts = args[0];
1144-
assert.equal(opts.url, 'http://localhost/store/?lol');
1134+
assert.equal(opts.url, 'http://localhost/store/');
11451135
assert.deepEqual(opts.data, {
11461136
project: '2',
11471137
release: 'abc123',
@@ -1157,6 +1147,11 @@ describe('globals', function() {
11571147
foo: 'bar',
11581148
extra: {'session:duration': 100},
11591149
});
1150+
assert.deepEqual(opts.auth, {
1151+
sentry_client: 'raven-js/<%= pkg.version %>',
1152+
sentry_key: 'abc',
1153+
sentry_version: '4'
1154+
});
11601155
assert.deepEqual(opts.options, globalOptions);
11611156
assert.isFunction(opts.onSuccess);
11621157
assert.isFunction(opts.onError);
@@ -1195,12 +1190,13 @@ describe('globals', function() {
11951190

11961191
it('should load an Image', function() {
11971192
makeRequest({
1198-
url: 'http://localhost/?lol',
1193+
url: 'http://localhost/',
1194+
auth: {a: '1', b: '2'},
11991195
data: {foo: 'bar'},
12001196
options: globalOptions
12011197
});
12021198
assert.equal(imageCache.length, 1);
1203-
assert.equal(imageCache[0].src, 'http://localhost/?lol&sentry_data=%7B%22foo%22%3A%22bar%22%7D');
1199+
assert.equal(imageCache[0].src, 'http://localhost/?a=1&b=2&sentry_data=%7B%22foo%22%3A%22bar%22%7D');
12041200
});
12051201

12061202
it('should populate crossOrigin based on globalOptions', function() {
@@ -1209,6 +1205,7 @@ describe('globals', function() {
12091205
};
12101206
makeRequest({
12111207
url: globalServer,
1208+
auth: {lol: '1'},
12121209
data: {foo: 'bar'},
12131210
options: globalOptions
12141211
});
@@ -1222,6 +1219,7 @@ describe('globals', function() {
12221219
};
12231220
makeRequest({
12241221
url: globalServer,
1222+
auth: {lol: '1'},
12251223
data: {foo: 'bar'},
12261224
options: globalOptions
12271225
});
@@ -1235,6 +1233,7 @@ describe('globals', function() {
12351233
};
12361234
makeRequest({
12371235
url: globalServer,
1236+
auth: {lol: '1'},
12381237
data: {foo: 'bar'},
12391238
options: globalOptions
12401239
});
@@ -1418,6 +1417,13 @@ describe('globals', function() {
14181417
]).source, 'a|b|a\\.b|d|[0-9]');
14191418
});
14201419
});
1420+
1421+
describe('urlencode', function() {
1422+
it('should work', function() {
1423+
assert.equal(urlencode({}), '');
1424+
assert.equal(urlencode({'foo': 'bar', 'baz': '1 2'}), 'foo=bar&baz=1%202');
1425+
});
1426+
});
14211427
});
14221428

14231429
describe('Raven (public API)', function() {

0 commit comments

Comments
 (0)