Skip to content

Commit 0590805

Browse files
authored
guard against undefined req.agent (#313)
* Update remote_request_data.js * suggested change * Add unit test for request agent validation
1 parent 88e3eac commit 0590805

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

packages/core/lib/segments/attributes/remote_request_data.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function RemoteRequestData(req, res, downstreamXRayEnabled) {
1414

1515
RemoteRequestData.prototype.init = function init(req, res, downstreamXRayEnabled) {
1616
this.request = {
17-
url: (req.agent.protocol + '//' + req.getHeader('host') + stripQueryStringFromPath(req.path)) || '',
17+
url: (req.agent && req.agent.protocol) ? (req.agent.protocol + '//' + req.getHeader('host') + stripQueryStringFromPath(req.path)) : '',
1818
method: req.method || '',
1919
};
2020

packages/core/test/unit/segments/attributes/remote_request_data.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,14 @@ describe('RemoteRequestData', function() {
4141
'https://host.com/path/to/resource'
4242
);
4343
});
44+
it('should return empty url if request agent is missing', function() {
45+
const requestWithoutAgent = {};
46+
47+
assert.propertyVal(
48+
new RemoteRequestData(requestWithoutAgent, response, true).request,
49+
'url',
50+
''
51+
);
52+
});
4453
});
4554
});

0 commit comments

Comments
 (0)