Skip to content

Commit

Permalink
Make _getHttpData still return user-agent when document is absent (#855)
Browse files Browse the repository at this point in the history
  • Loading branch information
LewisJEllis authored and benvinegar committed Feb 15, 2017
1 parent 07ce675 commit 2c38c31
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 22 deletions.
27 changes: 16 additions & 11 deletions src/raven.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var _window = typeof window !== 'undefined' ? window
: typeof self !== 'undefined' ? self
: {};
var _document = _window.document;
var _navigator = _window.navigator;

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

_getHttpData: function() {
if (!this._hasDocument || !_document.location || !_document.location.href) {
return;
if (!this._hasNavigator && !this._hasDocument) return;
var httpData = {};

if (this._hasNavigator && _navigator.userAgent) {
httpData.headers = {
'User-Agent': navigator.userAgent
};
}

var httpData = {
headers: {
'User-Agent': navigator.userAgent
if (this._hasDocument) {
if (_document.location && _document.location.href) {
httpData.url = _document.location.href;
}
if (_document.referrer) {
if (!httpData.headers) httpData.headers = {};
httpData.headers.Referer = _document.referrer;
}
};

httpData.url = _document.location.href;

if (_document.referrer) {
httpData.headers.Referer = _document.referrer;
}

return httpData;
Expand Down
26 changes: 15 additions & 11 deletions test/raven.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ describe('globals', function() {
describe('getHttpData', function() {
var data;

beforeEach(function () {
data = Raven._getHttpData();
});
describe('with document and navigator', function() {
beforeEach(function () {
data = Raven._getHttpData();
});

describe('with document', function() {
it('should have a url', function() {
assert.equal(data.url, window.location.href);
});
Expand All @@ -71,13 +71,17 @@ describe('globals', function() {
});
});

// describe('without document', function () {
// it('should return undefined if no document', function () {
// hasDocument = false;
// var data = getHttpData();
// assert.isUndefined(data);
// });
// });
it('without document but with navigator should return user-agent', function () {
Raven._hasDocument = false;
data = Raven._getHttpData();
assert.equal(data.headers['User-Agent'], navigator.userAgent);
});

it('with neither document nor navigator should return undefined', function () {
Raven._hasDocument = false;
Raven._hasNavigator = false;
assert.isUndefined(Raven._getHttpData());
});
});

describe('trimPacket', function() {
Expand Down

0 comments on commit 2c38c31

Please sign in to comment.