Skip to content

Commit

Permalink
fix: allow captureIp without a window object (#1008)
Browse files Browse the repository at this point in the history
  • Loading branch information
waltjones authored Sep 13, 2022
1 parent acf2dfe commit 87484fb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
19 changes: 12 additions & 7 deletions src/browser/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,25 @@ function addBaseInfo(item, options, callback) {

function addRequestInfo(window) {
return function(item, options, callback) {
if (!window || !window.location) {
return callback(null, item);
var requestInfo = {};

if (window && window.location) {
requestInfo.url = window.location.href;
requestInfo.query_string = window.location.search;
}

var remoteString = '$remote_ip';
if (!options.captureIp) {
remoteString = null;
} else if (options.captureIp !== true) {
remoteString += '_anonymize';
}
_.set(item, 'data.request', {
url: window.location.href,
query_string: window.location.search,
user_ip: remoteString
});
if (remoteString) requestInfo.user_ip = remoteString;

if (Object.keys(requestInfo).length > 0) {
_.set(item, 'data.request', requestInfo);
}

callback(null, item);
};
}
Expand Down
16 changes: 15 additions & 1 deletion test/browser.transforms.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,10 @@ describe('addRequestInfo', function() {
it('should use window info to set request properties', function(done) {
var args = ['a message'];
var item = itemFromArgs(args);
var options = {};
var options = { captureIp: 'anonymize' };
t.addRequestInfo(window)(item, options, function(e, i) {
expect(i.data.request).to.be.ok();
expect(i.data.request.user_ip).to.eql('$remote_ip_anonymize');
done(e);
});
});
Expand All @@ -243,6 +244,19 @@ describe('addRequestInfo', function() {
done(e);
});
});
it('should honor captureIp without window', function(done) {
var args = ['a message'];
var item = itemFromArgs(args);
item.data = {};
var options = { captureIp: true };
var w = null;
t.addRequestInfo(w)(item, options, function(e, i) {
expect(i.data.request.url).to.not.be.ok();
expect(i.data.request.query_string).to.not.be.ok();
expect(i.data.request.user_ip).to.eql('$remote_ip');
done(e);
});
});
});

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

0 comments on commit 87484fb

Please sign in to comment.