Skip to content

Commit 343da4a

Browse files
committed
Rewrote tests and simplified implementation
1 parent c5eec04 commit 343da4a

File tree

4 files changed

+128
-90
lines changed

4 files changed

+128
-90
lines changed

lib/client.js

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -230,25 +230,13 @@ extend(Raven.prototype, {
230230
so we only parse a `req` property if the `request` property is absent/empty (and hence we won't clobber)
231231
parseUser returns a partial kwargs object with a `request` property and possibly a `user` property
232232
*/
233-
var initialRequest = utils.prepareInitialRequest(
234-
this._globalContext.request,
235-
domainContext.request,
236-
kwargs.request
237-
);
238-
kwargs.request = extend(
239-
initialRequest,
233+
kwargs.request = this._createRequestObject(
240234
this._globalContext.request,
241235
domainContext.request,
242236
kwargs.request
243237
);
244238
if (Object.keys(kwargs.request).length === 0) {
245-
var initialReq = utils.prepareInitialRequest(
246-
this._globalContext.req,
247-
domainContext.req,
248-
kwargs.req
249-
);
250-
var req = extend(
251-
initialReq,
239+
var req = this._createRequestObject(
252240
this._globalContext.req,
253241
domainContext.req,
254242
kwargs.req
@@ -529,6 +517,36 @@ extend(Raven.prototype, {
529517
currCtx.breadcrumbs.shift();
530518
}
531519
this.setContext(currCtx);
520+
},
521+
522+
_createRequestObject: function() {
523+
/**
524+
* When using proxy, some of the attributes of req/request objects are non-enumerable.
525+
* To make sure, that they are still available to us after we consolidate our sources
526+
* (eg. globalContext.request + domainContext.request + kwargs.request),
527+
* we manually pull them out from original objects.
528+
*
529+
* We don't use Object.assign/extend as it's only merging over objects own properties,
530+
* and we don't want to go through all of the properties as well, as we simply don't
531+
* need all of them.
532+
*
533+
* So far the only missing piece is `ip`, but we can specify what properties should
534+
* be pulled by extending `nonEnumerables` array.
535+
**/
536+
var sources = Array.from(arguments).filter(function(source) {
537+
return Object.prototype.toString.call(source) === '[object Object]';
538+
});
539+
sources = [{}].concat(sources);
540+
var request = extend.apply(null, sources);
541+
var nonEnumberables = ['ip'];
542+
543+
nonEnumberables.forEach(function(key) {
544+
sources.forEach(function(source) {
545+
if (source[key]) request[key] = source[key];
546+
});
547+
});
548+
549+
return request;
532550
}
533551
});
534552

lib/utils.js

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,6 @@ module.exports.getAuthHeader = function getAuthHeader(timestamp, apiKey, apiSecr
5656
return header.join(', ');
5757
};
5858

59-
module.exports.prepareInitialRequest = function prepareInitialRequest() {
60-
var request = {};
61-
var nonEnumberables = ['ip'];
62-
var sources = Array.from(arguments).filter(function(source) {
63-
return Object.prototype.toString.call(source) === '[object Object]';
64-
});
65-
66-
nonEnumberables.forEach(function(key) {
67-
sources.forEach(function(source) {
68-
if (source[key]) request[key] = source[key];
69-
});
70-
});
71-
72-
return request;
73-
};
74-
7559
module.exports.parseDSN = function parseDSN(dsn) {
7660
if (!dsn) {
7761
// Let a falsey value return false explicitly

test/raven.client.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,6 +1369,102 @@ describe('raven.Client', function() {
13691369
});
13701370
});
13711371
});
1372+
1373+
describe('#_createRequestObject', function() {
1374+
it('should merge together all sources', function() {
1375+
var req = client._createRequestObject(
1376+
{
1377+
foo: 123
1378+
},
1379+
{
1380+
bar: 42
1381+
}
1382+
);
1383+
var expected = {
1384+
foo: 123,
1385+
bar: 42
1386+
};
1387+
req.should.eql(expected);
1388+
});
1389+
1390+
it('should preserve extend-like order', function() {
1391+
var req = client._createRequestObject(
1392+
{
1393+
foo: 111
1394+
},
1395+
{
1396+
foo: 222
1397+
},
1398+
{
1399+
foo: 333
1400+
}
1401+
);
1402+
var expected = {
1403+
foo: 333
1404+
};
1405+
req.should.eql(expected);
1406+
});
1407+
1408+
it('should filter incorrect sources', function() {
1409+
var req = client._createRequestObject(
1410+
{
1411+
foo: 111
1412+
},
1413+
[42],
1414+
null,
1415+
'hello',
1416+
{
1417+
foo: 222
1418+
}
1419+
);
1420+
var expected = {
1421+
foo: 222
1422+
};
1423+
req.should.eql(expected);
1424+
});
1425+
1426+
it('should extract specified non-enumerables', function() {
1427+
var foo = {};
1428+
Object.defineProperty(foo, 'ip', {
1429+
value: '127.0.0.1',
1430+
enumerable: false
1431+
});
1432+
var bar = {
1433+
foo: 222
1434+
};
1435+
var req = client._createRequestObject(foo, bar);
1436+
var expected = {
1437+
foo: 222,
1438+
ip: '127.0.0.1'
1439+
};
1440+
req.should.eql(expected);
1441+
});
1442+
1443+
it('should skip all remaining non-enumerables', function() {
1444+
var foo = {};
1445+
Object.defineProperty(foo, 'ip', {
1446+
value: '127.0.0.1',
1447+
enumerable: false
1448+
});
1449+
Object.defineProperty(foo, 'pickle', {
1450+
value: 'rick',
1451+
enumerable: false
1452+
});
1453+
var bar = {
1454+
dont: 'skip'
1455+
};
1456+
Object.defineProperty(bar, 'evil', {
1457+
value: 'morty',
1458+
enumerable: false
1459+
});
1460+
var req = client._createRequestObject(foo, bar);
1461+
var expected = {
1462+
ip: '127.0.0.1',
1463+
dont: 'skip'
1464+
};
1465+
req.should.eql(expected);
1466+
});
1467+
});
13721468
});
13731469

13741470
describe('raven requestHandler/errorHandler middleware', function() {

test/raven.utils.js

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -98,66 +98,6 @@ describe('raven.utils', function() {
9898
});
9999
});
100100

101-
describe('#prepareInitialRequest', function() {
102-
it('should extract specific non-enumerables and return an object containing them', function() {
103-
var req = raven.utils.prepareInitialRequest({
104-
ip: '127.0.0.1'
105-
});
106-
var expected = {
107-
ip: '127.0.0.1'
108-
};
109-
req.should.eql(expected);
110-
});
111-
112-
it('should skip non-listed properties', function() {
113-
var req = raven.utils.prepareInitialRequest({
114-
ip: '127.0.0.1',
115-
pickle: 'Rick',
116-
evil: 'Morty'
117-
});
118-
var expected = {
119-
ip: '127.0.0.1'
120-
};
121-
req.should.eql(expected);
122-
});
123-
124-
it('should preserve extend-like order', function() {
125-
var req = raven.utils.prepareInitialRequest(
126-
{
127-
ip: '127.0.0.1'
128-
},
129-
{
130-
ip: '123.123.123.123'
131-
},
132-
{
133-
ip: '42.42.42.42'
134-
}
135-
);
136-
var expected = {
137-
ip: '42.42.42.42'
138-
};
139-
req.should.eql(expected);
140-
});
141-
142-
it('should filter incorrect sources', function() {
143-
var req = raven.utils.prepareInitialRequest(
144-
{
145-
ip: '127.0.0.1'
146-
},
147-
[42],
148-
null,
149-
'hello',
150-
{
151-
ip: '123.123.123.123'
152-
}
153-
);
154-
var expected = {
155-
ip: '123.123.123.123'
156-
};
157-
req.should.eql(expected);
158-
});
159-
});
160-
161101
describe('#parseAuthHeader()', function() {
162102
it('should parse all parameters', function() {
163103
var timestamp = 12345,

0 commit comments

Comments
 (0)