Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow captureIp without a window object #1008

Merged
merged 1 commit into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -222,9 +222,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 @@ -239,6 +240,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