Skip to content

Commit 2c38c31

Browse files
LewisJEllisbenvinegar
authored andcommitted
Make _getHttpData still return user-agent when document is absent (#855)
1 parent 07ce675 commit 2c38c31

File tree

2 files changed

+31
-22
lines changed

2 files changed

+31
-22
lines changed

src/raven.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ var _window = typeof window !== 'undefined' ? window
2020
: typeof self !== 'undefined' ? self
2121
: {};
2222
var _document = _window.document;
23+
var _navigator = _window.navigator;
2324

2425
// First, check for JSON support
2526
// If there is no JSON, we no-op the core features of Raven
@@ -28,6 +29,7 @@ function Raven() {
2829
this._hasJSON = !!(typeof JSON === 'object' && JSON.stringify);
2930
// Raven can run in contexts where there's no document (react-native)
3031
this._hasDocument = !isUndefined(_document);
32+
this._hasNavigator = !isUndefined(_navigator);
3133
this._lastCapturedException = null;
3234
this._lastEventId = null;
3335
this._globalServer = null;
@@ -1301,20 +1303,23 @@ Raven.prototype = {
13011303
},
13021304

13031305
_getHttpData: function() {
1304-
if (!this._hasDocument || !_document.location || !_document.location.href) {
1305-
return;
1306+
if (!this._hasNavigator && !this._hasDocument) return;
1307+
var httpData = {};
1308+
1309+
if (this._hasNavigator && _navigator.userAgent) {
1310+
httpData.headers = {
1311+
'User-Agent': navigator.userAgent
1312+
};
13061313
}
13071314

1308-
var httpData = {
1309-
headers: {
1310-
'User-Agent': navigator.userAgent
1315+
if (this._hasDocument) {
1316+
if (_document.location && _document.location.href) {
1317+
httpData.url = _document.location.href;
1318+
}
1319+
if (_document.referrer) {
1320+
if (!httpData.headers) httpData.headers = {};
1321+
httpData.headers.Referer = _document.referrer;
13111322
}
1312-
};
1313-
1314-
httpData.url = _document.location.href;
1315-
1316-
if (_document.referrer) {
1317-
httpData.headers.Referer = _document.referrer;
13181323
}
13191324

13201325
return httpData;

test/raven.test.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ describe('globals', function() {
4848
describe('getHttpData', function() {
4949
var data;
5050

51-
beforeEach(function () {
52-
data = Raven._getHttpData();
53-
});
51+
describe('with document and navigator', function() {
52+
beforeEach(function () {
53+
data = Raven._getHttpData();
54+
});
5455

55-
describe('with document', function() {
5656
it('should have a url', function() {
5757
assert.equal(data.url, window.location.href);
5858
});
@@ -71,13 +71,17 @@ describe('globals', function() {
7171
});
7272
});
7373

74-
// describe('without document', function () {
75-
// it('should return undefined if no document', function () {
76-
// hasDocument = false;
77-
// var data = getHttpData();
78-
// assert.isUndefined(data);
79-
// });
80-
// });
74+
it('without document but with navigator should return user-agent', function () {
75+
Raven._hasDocument = false;
76+
data = Raven._getHttpData();
77+
assert.equal(data.headers['User-Agent'], navigator.userAgent);
78+
});
79+
80+
it('with neither document nor navigator should return undefined', function () {
81+
Raven._hasDocument = false;
82+
Raven._hasNavigator = false;
83+
assert.isUndefined(Raven._getHttpData());
84+
});
8185
});
8286

8387
describe('trimPacket', function() {

0 commit comments

Comments
 (0)