Skip to content

Commit cfc4e8b

Browse files
author
William Armiros
committed
fixed no http callback provided bug
1 parent 09ef0dd commit cfc4e8b

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

packages/core/lib/patchers/http_p.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ function enableCapture(module, downstreamXRayEnabled, subsegmentCallback) {
142142
// Only need to remove our listener & re-emit if we're not listening using the errorMonitor,
143143
// e.g. the app is running on Node 10. Otherwise the errorMonitor will re-emit automatically.
144144
// See: https://github.com/aws/aws-xray-sdk-node/issues/318
145-
// TODO: Remove this logic when the 'error' listener is removed once node 10 support is deprecated
145+
// TODO: Remove this logic once node 12 support is deprecated
146146
if (!events.errorMonitor && this.listenerCount('error') <= 1) {
147147
this.removeListener('error', errorCapturer);
148148
this.emit('error', e);
@@ -179,14 +179,15 @@ function enableCapture(module, downstreamXRayEnabled, subsegmentCallback) {
179179
} else {
180180
callback(res);
181181
}
182-
// if no callback provided and there is only SDK added response listener,
183-
// we consume the response so the actual end can fire.
184-
} else if (res && res.listenerCount('end') === 1) {
182+
// if no callback provided by user application, then we consume the response
183+
// so the 'end' event fires
184+
// See: https://nodejs.org/api/http.html#http_class_http_clientrequest
185+
} else {
185186
res.resume();
186187
}
187188
});
188189

189-
// Use errorMonitor if available (in Node 12+), otherwise fall back to standard error listener
190+
// Use errorMonitor if available (in Node 12.17+), otherwise fall back to standard error listener
190191
// See: https://nodejs.org/dist/latest-v12.x/docs/api/events.html#events_eventemitter_errormonitor
191192
req.on(events.errorMonitor || 'error', errorCapturer);
192193

packages/core/test/unit/patchers/http_p.test.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ describe('HTTP/S', function() {
129129
});
130130

131131
describe('on invocation', function() {
132-
var capturedHttp, fakeRequest, fakeResponse, httpClient, requestSpy, sandbox;
132+
var capturedHttp, fakeRequest, fakeResponse, httpClient, requestSpy, resumeSpy, sandbox;
133133

134134
beforeEach(function() {
135135
sandbox = sinon.sandbox.create();
@@ -145,8 +145,10 @@ describe('HTTP/S', function() {
145145
}};
146146
httpClient.get = httpClient.request;
147147

148+
resumeSpy = sandbox.spy(fakeResponse, 'resume');
148149
requestSpy = sandbox.spy(httpClient, 'request');
149-
capturedHttp = captureHTTPs(httpClient, true); });
150+
capturedHttp = captureHTTPs(httpClient, true);
151+
});
150152

151153
afterEach(function() {
152154
sandbox.restore();
@@ -159,6 +161,16 @@ describe('HTTP/S', function() {
159161
resolveManualStub.should.have.been.calledWith(options);
160162
});
161163

164+
it('should consume the response if no callback is provided by user', function() {
165+
capturedHttp.request(httpOptions); // no callback
166+
resumeSpy.should.have.been.calledOnce;
167+
});
168+
169+
it('should not consume the response if a callback is provided by user', function() {
170+
capturedHttp.request(httpOptions, () => {});
171+
resumeSpy.should.not.have.been.called;
172+
});
173+
162174
it('should create a new subsegment with name as hostname', function() {
163175
var options = {hostname: 'hostname', path: '/'};
164176
capturedHttp.request(options);
@@ -385,7 +397,7 @@ describe('HTTP/S', function() {
385397
}, 50);
386398
});
387399

388-
if (process.version.startsWith('v') && process.version >= 'v12') {
400+
if (process.version.startsWith('v') && process.version >= 'v12.17') {
389401
it('should still re-emit if there are multiple errorMonitors attached', function() {
390402
fakeRequest.on(events.errorMonitor, function() {});
391403
fakeRequest.on(events.errorMonitor, function() {});

0 commit comments

Comments
 (0)