11import EventEmitter from 'events' ;
2- import http , { ClientRequest , IncomingMessage , RequestOptions } from 'http' ;
2+ import http , { ClientRequest , IncomingMessage } from 'http' ;
33import https from 'https' ;
44import { AddressInfo } from 'net' ;
5- import { parse as parseUrl , URL } from 'url' ; // eslint-disable-line node/no-deprecated-api
65import util from 'util' ;
76import pEvent from 'p-event' ;
87import test from 'ava' ;
@@ -52,9 +51,21 @@ test('by default everything is set to undefined', t => {
5251 t . is ( timings . socket , undefined ) ;
5352 t . is ( timings . lookup , undefined ) ;
5453 t . is ( timings . connect , undefined ) ;
54+ t . is ( timings . secureConnect , undefined ) ;
5555 t . is ( timings . response , undefined ) ;
5656 t . is ( timings . end , undefined ) ;
5757 t . is ( timings . error , undefined ) ;
58+
59+ t . deepEqual ( timings . phases , {
60+ wait : undefined ,
61+ dns : undefined ,
62+ tcp : undefined ,
63+ tls : undefined ,
64+ request : undefined ,
65+ firstByte : undefined ,
66+ download : undefined ,
67+ total : undefined
68+ } ) ;
5869} ) ;
5970
6071test ( 'timings' , async t => {
@@ -68,6 +79,7 @@ test('timings', async t => {
6879 t . is ( typeof timings . socket , 'number' ) ;
6980 t . is ( typeof timings . lookup , 'number' ) ;
7081 t . is ( typeof timings . connect , 'number' ) ;
82+ t . is ( typeof timings . secureConnect , 'number' ) ;
7183 t . is ( typeof timings . upload , 'number' ) ;
7284 t . is ( typeof timings . response , 'number' ) ;
7385 t . is ( typeof timings . end , 'number' ) ;
@@ -83,14 +95,16 @@ test('phases', async t => {
8395 t . is ( typeof timings . phases . wait , 'number' ) ;
8496 t . is ( typeof timings . phases . dns , 'number' ) ;
8597 t . is ( typeof timings . phases . tcp , 'number' ) ;
98+ t . is ( typeof timings . phases . tls , 'number' ) ;
8699 t . is ( typeof timings . phases . firstByte , 'number' ) ;
87100 t . is ( typeof timings . phases . download , 'number' ) ;
88101 t . is ( typeof timings . phases . total , 'number' ) ;
89102
90103 t . is ( timings . phases . wait , timings . socket ! - timings . start ) ;
91104 t . is ( timings . phases . dns , timings . lookup ! - timings . socket ! ) ;
92105 t . is ( timings . phases . tcp , timings . connect ! - timings . lookup ! ) ;
93- t . is ( timings . phases . request , timings . upload ! - timings . connect ! ) ;
106+ t . is ( timings . phases . tls , timings . secureConnect ! - timings . connect ! ) ;
107+ t . is ( timings . phases . request , timings . upload ! - timings . secureConnect ! ) ;
94108 t . is ( timings . phases . firstByte , timings . response ! - timings . upload ! ) ;
95109 t . is ( timings . phases . download , timings . end ! - timings . response ! ) ;
96110 t . is ( timings . phases . total , timings . end ! - timings . start ) ;
@@ -105,10 +119,9 @@ test('no memory leak (`lookup` event)', async t => {
105119} ) ;
106120
107121test ( 'sets `total` on request error' , async t => {
108- const request = http . get ( {
109- ...parseUrl ( server . url ! ) ,
122+ const request = http . get ( server . url ! , {
110123 timeout : 1
111- } as RequestOptions ) ;
124+ } ) ;
112125 request . on ( 'timeout' , ( ) => {
113126 request . abort ( ) ;
114127 } ) ;
@@ -147,7 +160,7 @@ test('doesn\'t throw when someone used `.prependOnceListener()`', t => {
147160} ) ;
148161
149162test ( 'sensible timings' , async t => {
150- const { timings, request} = makeRequest ( 'http ://google.com' ) ;
163+ const { timings, request} = makeRequest ( 'https ://google.com' ) ;
151164 const now = Date . now ( ) ;
152165
153166 const response = await pEvent ( request , 'response' ) ;
@@ -157,12 +170,14 @@ test('sensible timings', async t => {
157170 t . true ( timings . socket ! >= now ) ;
158171 t . true ( timings . lookup ! >= now ) ;
159172 t . true ( timings . connect ! >= now ) ;
173+ t . true ( timings . secureConnect ! >= now ) ;
160174 t . true ( timings . response ! >= now ) ;
161175 t . true ( timings . end ! >= now ) ;
162176 t . is ( timings . error , undefined ) ;
163177 t . true ( timings . phases . wait ! < 1000 ) ;
164178 t . true ( timings . phases . dns ! < 1000 ) ;
165179 t . true ( timings . phases . tcp ! < 1000 ) ;
180+ t . true ( timings . phases . tls ! < 1000 ) ;
166181 t . true ( timings . phases . request ! < 1000 ) ;
167182 t . true ( timings . phases . firstByte ! < 1000 ) ;
168183 t . true ( timings . phases . download ! < 1000 ) ;
@@ -185,3 +200,25 @@ test('prepends once listeners', async t => {
185200 await promise ;
186201 request . abort ( ) ;
187202} ) ;
203+
204+ test ( '`tls` phase for https requests' , async t => {
205+ const { request, timings} = makeRequest ( 'https://google.com' ) ;
206+
207+ const response = await pEvent ( request , 'response' ) ;
208+ response . resume ( ) ;
209+ await pEvent ( response , 'end' ) ;
210+
211+ t . is ( typeof timings . secureConnect , 'number' ) ;
212+ t . is ( typeof timings . phases . tls , 'number' ) ;
213+ } ) ;
214+
215+ test ( 'no `tls` phase for http requests' , async t => {
216+ const { request, timings} = makeRequest ( server . url ) ;
217+
218+ const response = await pEvent ( request , 'response' ) ;
219+ response . resume ( ) ;
220+ await pEvent ( response , 'end' ) ;
221+
222+ t . is ( timings . secureConnect , undefined ) ;
223+ t . is ( timings . phases . tls , undefined ) ;
224+ } ) ;
0 commit comments