Skip to content

Commit

Permalink
Merge pull request #346 from rollbar/fix-server-transforms
Browse files Browse the repository at this point in the history
Allow more flexibility of request objects
  • Loading branch information
rokob authored Jul 28, 2017
2 parents e3a75df + fc27580 commit 0a47eb1
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 9 deletions.
28 changes: 19 additions & 9 deletions src/server/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,27 +187,37 @@ function _buildRequestData(req) {
var headers = req.headers || {};
var host = headers.host || '<no host>';
var proto = req.protocol || ((req.socket && req.socket.encrypted) ? 'https' : 'http' );
var reqUrl = proto + '://' + host + (req.url || '');
var parsedUrl = url.parse(reqUrl, true);
var parsedUrl;
if (_.isType(req.url, 'string')) {
parsedUrl = url.parse(req.url, true);
} else {
parsedUrl = req.url || {};
}
parsedUrl.protocol = parsedUrl.protocol || proto;
parsedUrl.host = parsedUrl.host || host;
var reqUrl = url.format(parsedUrl);
var data = {
url: reqUrl,
GET: parsedUrl.query,
user_ip: _extractIp(req),
headers: headers,
method: req.method
};
if (parsedUrl.search && parsedUrl.search.length > 0) {
data.GET = parsedUrl.query;
}

if (req.body) {
var body = req.body || req.payload;
if (body) {
var bodyParams = {};
if (_.isIterable(req.body)) {
for (var k in req.body) {
if (Object.prototype.hasOwnProperty.call(req.body, k)) {
bodyParams[k] = req.body[k];
if (_.isIterable(body)) {
for (var k in body) {
if (Object.prototype.hasOwnProperty.call(body, k)) {
bodyParams[k] = body[k];
}
}
data[req.method] = bodyParams;
} else {
data.body = req.body;
data.body = body;
}
}
return data;
Expand Down
60 changes: 60 additions & 0 deletions test/server.transforms.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,66 @@ vows.describe('transforms')
assert.equal(item.data.context, '/api/:bork');
},
},
'with a request like from hapi': {
topic: function(options) {
var item = {
request: {
headers: {
host: 'example.com',
'x-auth-token': '12345'
},
protocol: 'https',
url: {
protocol: null,
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: null,
search: '',
query: {},
pathname: '/some/endpoint',
path: '/some/endpoint',
href: '/some/endpoint'
},
ip: '192.192.192.1',
method: 'POST',
payload: {
token: 'abc123',
something: 'else'
},
route: { path: '/api/:bork' },
user: {
id: 42,
email: '[email protected]'
}
},
stuff: 'hey',
data: {other: 'thing'}
};
t.addRequestData(item, options, this.callback);
},
'should not error': function(err, item) {
assert.ifError(err);
},
'should have a request object inside data': function(err, item) {
assert.ok(item.data.request);
},
'should set a person based on request user': function(err, item) {
assert.equal(item.data.person.id, 42);
assert.equal(item.data.person.email, '[email protected]');
},
'should set some fields based on request data': function(err, item) {
var r = item.data.request;
assert.equal(r.url, 'https://example.com/some/endpoint');
assert.equal(r.user_ip, '192.192.192.1');
assert.ok(!r.GET);
assert.ok(r.POST);

assert.equal(item.data.context, '/api/:bork');
},
},
'with a request with an array body': {
topic: function(options) {
var item = {
Expand Down

0 comments on commit 0a47eb1

Please sign in to comment.