@@ -4,7 +4,7 @@ const Promise = require("bluebird");
44const request = require ( "request" ) ;
55const debug = require ( "debug" ) ( "yildiz:http:pr" ) ;
66
7- const reqProm = ( prefix , token , proto , host , port , path = "/" , options = { } , agent = undefined , expectedStatusCode = null ) => {
7+ const reqProm = ( prefix , token , proto , host , port , path = "/" , options = { } , agent = undefined , timings = false , expectedStatusCode = null ) => {
88
99 options . url = `${ proto } ://${ host } :${ port } ${ path } ` ;
1010
@@ -28,7 +28,13 @@ const reqProm = (prefix, token, proto, host, port, path = "/", options = {}, age
2828 }
2929
3030 //keep-alive
31- options . agent = agent ;
31+ if ( agent ) {
32+ options . agent = agent ;
33+ }
34+
35+ if ( timings ) {
36+ options . time = true ;
37+ }
3238
3339 return new Promise ( ( resolve , reject ) => {
3440 request ( options , ( error , response , body ) => {
@@ -46,6 +52,20 @@ const reqProm = (prefix, token, proto, host, port, path = "/", options = {}, age
4652 //empty
4753 }
4854
55+ const responseData = {
56+ time : null
57+ } ;
58+
59+ if ( timings ) {
60+ responseData . time = {
61+ elapsedTime : response . elapsedTime ,
62+ responseStartTime : response . responseStartTime ,
63+ timingStart : response . timingStart ,
64+ timings : response . timings ,
65+ timingPhases : response . timingPhases
66+ } ;
67+ }
68+
4969 if ( expectedStatusCode && response . statusCode !== expectedStatusCode ) {
5070
5171 let errorMessage = "No error message present in body" ;
@@ -58,19 +78,19 @@ const reqProm = (prefix, token, proto, host, port, path = "/", options = {}, age
5878 return reject ( new Error ( `Response status code: ${ response . statusCode } does match expected status code: ${ expectedStatusCode } . ${ errorMessage } .` ) ) ;
5979 }
6080
61- resolve ( {
81+ resolve ( Object . assign ( responseData , {
6282 status : response . statusCode ,
6383 headers : response . headers ,
6484 body : parsedBody
65- } ) ;
85+ } ) ) ;
6686 } ) ;
6787 } ) ;
6888} ;
6989
7090module . exports = {
71- getInstance : ( prefix , token , proto = "http" , host = "localhost" , port = 3058 , agent = undefined ) => {
91+ getInstance : ( prefix , token , proto = "http" , host = "localhost" , port = 3058 , agent = undefined , timings = false ) => {
7292 return ( path , options , expectedStatusCode ) => {
73- return reqProm ( prefix , token , proto , host , port , path , options , agent , expectedStatusCode ) ;
93+ return reqProm ( prefix , token , proto , host , port , path , options , agent , timings , expectedStatusCode ) ;
7494 } ;
7595 }
7696} ;
0 commit comments