@@ -24,6 +24,124 @@ describe('Requestlog:', () => {
2424 app . stop ( ) ;
2525 done ( ) ;
2626 } ) ;
27+ it ( 'http POST with body /externalcall {} 200' , ( ) => {
28+ const logger = requestlogger ( { logResponsePayload : true , logRequestPayload : true } ) ;
29+ const logspy = sandbox . spy ( logger , 'log' ) ;
30+ const postData = JSON . stringify ( {
31+ 'msg' : 'Hello World!' ,
32+ } ) ;
33+
34+ const options = {
35+ hostname : `localhost` ,
36+ port : server . address ( ) . port ,
37+ path : '/externalcall' ,
38+ protocol : 'http:' ,
39+ method : 'POST' ,
40+ headers : {
41+ 'Content-Type' : 'application/json' ,
42+ 'Content-Length' : Buffer . byteLength ( postData ) ,
43+ } ,
44+ } ;
45+
46+ return new Promise ( ( resolve , reject ) => {
47+ const req = http . request ( options , ( res ) => {
48+ res . on ( 'data' , ( ) => {
49+ } ) ;
50+ res . on ( 'end' , ( ) => {
51+ console . log ( 'Response ended: ' ) ;
52+ sinon . assert . calledWith ( logspy , {
53+ request : {
54+ host : sinon . match ( / l o c a l h o s t : [ 0 - 9 ] + / gm) ,
55+ path : '/externalcall' ,
56+ method : 'POST' ,
57+ payload : '{"msg":"Hello World!"}'
58+ } ,
59+ response : {
60+ status : 200 ,
61+ duration : sinon . match . number ,
62+ payload : '{"ok":"ok"}'
63+ } ,
64+ protocol : 'http:' ,
65+ type : [ 'application' ] ,
66+ } ) ;
67+ resolve ( 'ok' ) ;
68+ } ) ;
69+ } ) . on ( 'error' , ( err ) => {
70+ console . log ( 'Error: ' , err . message ) ;
71+ reject ( err ) ;
72+ } ) ;
73+ req . write ( postData ) ;
74+ req . end ( ) ;
75+ } ) ;
76+ } ) ;
77+ it ( 'http fails' , ( ) => {
78+ const logger = requestlogger ( { logResponsePayload : true , log : true } ) ;
79+ const logspy = sandbox . spy ( logger , 'log' ) ;
80+ return new Promise ( ( resolve , reject ) => {
81+ http . get ( `http://localhost/externalcall` , ( res ) => {
82+ res . on ( 'data' , ( ) => {
83+ } ) ;
84+ res . on ( 'end' , ( ) => {
85+ reject ( 'should not end' ) ;
86+ } ) ;
87+ } ) . on ( 'error' , ( err ) => {
88+ console . log ( 'Error: ' , err . message ) ;
89+ sinon . assert . calledWith ( logspy , {
90+ request : {
91+ host : 'localhost' ,
92+ path : '/externalcall' ,
93+ } ,
94+ response : {
95+ status : sinon . match . any ,
96+ duration : sinon . match . any ,
97+ } ,
98+ protocol : 'http:' ,
99+ type : [ 'application' ] ,
100+ } ) ;
101+ resolve ( ) ;
102+ } ) ;
103+ } ) ;
104+ } ) ;
105+ it ( 'http fails with correlationid header' , ( ) => {
106+ const logger = requestlogger ( { logResponsePayload : true , log : true } ) ;
107+ const logspy = sandbox . spy ( logger , 'log' ) ;
108+ const options = {
109+ hostname : `localhost` ,
110+ path : '/externalcall' ,
111+ protocol : 'http:' ,
112+ method : 'POST' ,
113+ headers : {
114+ 'Content-Type' : 'application/json' ,
115+ 'dgp-correlation' : 'correlationid' ,
116+ } ,
117+ } ;
118+ return new Promise ( ( resolve , reject ) => {
119+ http . request ( options , ( res ) => {
120+ res . on ( 'data' , ( ) => {
121+ } ) ;
122+ res . on ( 'end' , ( ) => {
123+ reject ( 'should not end' ) ;
124+ } ) ;
125+ } ) . on ( 'error' , ( err ) => {
126+ console . log ( 'Error: ' , err . message ) ;
127+ sinon . assert . calledWith ( logspy , {
128+ correlationId : 'correlationid' ,
129+ request : {
130+ host : 'localhost' ,
131+ path : '/externalcall' ,
132+ method : 'POST' ,
133+ } ,
134+ response : {
135+ status : sinon . match . any ,
136+ duration : sinon . match . any ,
137+ } ,
138+ protocol : 'http:' ,
139+ type : [ 'application' ] ,
140+ } ) ;
141+ resolve ( ) ;
142+ } ) ;
143+ } ) ;
144+ } ) ;
27145 it ( 'GET /externalcall {} 200' , async ( ) => {
28146 const logger = requestlogger ( ) ;
29147 const logspy = sandbox . spy ( logger , 'log' ) ;
0 commit comments