@@ -4,6 +4,7 @@ var chai = require('chai');
44var sinon = require ( 'sinon' ) ;
55var sinonChai = require ( 'sinon-chai' ) ;
66var URL = require ( 'url' ) ;
7+ var events = require ( 'events' ) ;
78
89var captureHTTPs = require ( '../../../lib/patchers/http_p' ) . captureHTTPs ;
910var captureHTTPsGlobal = require ( '../../../lib/patchers/http_p' ) . captureHTTPsGlobal ;
@@ -128,14 +129,15 @@ describe('HTTP/S', function() {
128129 } ) ;
129130
130131 describe ( 'on invocation' , function ( ) {
131- var capturedHttp , fakeRequest , fakeResponse , httpClient , requestSpy , sandbox ;
132+ var capturedHttp , fakeRequest , fakeResponse , httpClient , requestSpy , resumeSpy , sandbox ;
132133
133134 beforeEach ( function ( ) {
134135 sandbox = sinon . sandbox . create ( ) ;
135136 segment = new Segment ( 'test' , traceId ) ;
136137
137138 fakeRequest = buildFakeRequest ( ) ;
138139 fakeResponse = buildFakeResponse ( ) ;
140+ fakeResponse . req = fakeRequest ;
139141
140142 httpClient = { request : function ( ...args ) {
141143 const callback = args [ typeof args [ 1 ] === 'object' ? 2 : 1 ] ;
@@ -144,8 +146,10 @@ describe('HTTP/S', function() {
144146 } } ;
145147 httpClient . get = httpClient . request ;
146148
149+ resumeSpy = sandbox . spy ( fakeResponse , 'resume' ) ;
147150 requestSpy = sandbox . spy ( httpClient , 'request' ) ;
148- capturedHttp = captureHTTPs ( httpClient , true ) ; } ) ;
151+ capturedHttp = captureHTTPs ( httpClient , true ) ;
152+ } ) ;
149153
150154 afterEach ( function ( ) {
151155 sandbox . restore ( ) ;
@@ -158,6 +162,22 @@ describe('HTTP/S', function() {
158162 resolveManualStub . should . have . been . calledWith ( options ) ;
159163 } ) ;
160164
165+ it ( 'should consume the response if no callback is provided by user' , function ( ) {
166+ capturedHttp . request ( httpOptions ) ; // no callback
167+ resumeSpy . should . have . been . calledOnce ;
168+ } ) ;
169+
170+ it ( 'should not consume the response if a callback is provided by user' , function ( ) {
171+ capturedHttp . request ( httpOptions , ( ) => { } ) ;
172+ resumeSpy . should . not . have . been . called ;
173+ } ) ;
174+
175+ it ( 'should not consume the response if a response listener is provided by user' , function ( ) {
176+ fakeRequest . on ( 'response' , ( ) => { } ) ;
177+ capturedHttp . request ( httpOptions ) ;
178+ resumeSpy . should . not . have . been . called ;
179+ } ) ;
180+
161181 it ( 'should create a new subsegment with name as hostname' , function ( ) {
162182 var options = { hostname : 'hostname' , path : '/' } ;
163183 capturedHttp . request ( options ) ;
@@ -206,16 +226,14 @@ describe('HTTP/S', function() {
206226 assert . match ( options . headers [ 'X-Amzn-Trace-Id' ] , xAmznTraceId ) ;
207227 } ) ;
208228
209- if ( process . version . startsWith ( 'v' ) && process . version >= 'v10' ) {
210- it ( 'should inject the tracing headers into the options if a URL is also provided' , function ( ) {
211- capturedHttp . request ( `http://${ httpOptions . host } ${ httpOptions . path } ` , httpOptions ) ;
212-
213- // example: 'Root=1-59138384-82ff54d5ba9282f0c680adb3;Parent=53af362e4e4efeb8;Sampled=1'
214- var xAmznTraceId = new RegExp ( '^Root=' + traceId + ';Parent=([a-f0-9]{16});Sampled=1$' ) ;
215- var options = requestSpy . firstCall . args [ 1 ] ;
216- assert . match ( options . headers [ 'X-Amzn-Trace-Id' ] , xAmznTraceId ) ;
217- } ) ;
218- }
229+ it ( 'should inject the tracing headers into the options if a URL is also provided' , function ( ) {
230+ capturedHttp . request ( `http://${ httpOptions . host } ${ httpOptions . path } ` , httpOptions ) ;
231+
232+ // example: 'Root=1-59138384-82ff54d5ba9282f0c680adb3;Parent=53af362e4e4efeb8;Sampled=1'
233+ var xAmznTraceId = new RegExp ( '^Root=' + traceId + ';Parent=([a-f0-9]{16});Sampled=1$' ) ;
234+ var options = requestSpy . firstCall . args [ 1 ] ;
235+ assert . match ( options . headers [ 'X-Amzn-Trace-Id' ] , xAmznTraceId ) ;
236+ } ) ;
219237
220238 it ( 'should return the request object' , function ( ) {
221239 var request = capturedHttp . request ( httpOptions ) ;
@@ -385,6 +403,15 @@ describe('HTTP/S', function() {
385403 done ( ) ;
386404 } , 50 ) ;
387405 } ) ;
406+
407+ if ( process . version . startsWith ( 'v' ) && process . version >= 'v12.17' ) {
408+ it ( 'should still re-emit if there are multiple errorMonitors attached' , function ( ) {
409+ fakeRequest . on ( events . errorMonitor , function ( ) { } ) ;
410+ fakeRequest . on ( events . errorMonitor , function ( ) { } ) ;
411+
412+ assert . throws ( function ( ) { fakeRequest . emitter . emit ( 'error' , error ) ; } ) ;
413+ } ) ;
414+ }
388415 } ) ;
389416 } ) ;
390417} ) ;
0 commit comments