@@ -216,6 +216,54 @@ describe('Terminus', () => {
216216 expect ( loggerRan ) . to . eql ( true )
217217 } )
218218
219+ it ( 'exposes internal state (isShuttingDown: false) to health check' , async ( ) => {
220+ let onHealthCheckRan = false
221+ let exposedState
222+
223+ createTerminus ( server , {
224+ healthChecks : {
225+ '/health' : ( { state } ) => {
226+ onHealthCheckRan = true
227+ exposedState = state
228+ return Promise . resolve ( )
229+ }
230+ }
231+ } )
232+ server . listen ( 8000 )
233+
234+ const response = await fetch ( 'http://localhost:8000/health' )
235+ expect ( response . status ) . to . eql ( 200 )
236+ expect ( response . headers . has ( 'Content-Type' ) ) . to . eql ( true )
237+ expect ( response . headers . get ( 'Content-Type' ) ) . to . eql ( 'application/json' )
238+ expect ( onHealthCheckRan ) . to . eql ( true )
239+ expect ( exposedState ) . to . eql ( { isShuttingDown : false } )
240+ } )
241+
242+ it ( 'exposes internal state (isShuttingDown: true) when shutting down' , ( done ) => {
243+ let responseAssertionsComplete = false
244+ let exposedState
245+
246+ // We're only truly finished when the response has been analyzed and the forked http process has exited,
247+ // freeing up port 8000 for future tests
248+ execFile ( 'node' , [ 'lib/standalone-tests/terminus.onsignal.nofail.js' ] , ( error ) => {
249+ expect ( error . signal ) . to . eql ( 'SIGINT' )
250+ expect ( responseAssertionsComplete ) . to . eql ( true )
251+ expect ( exposedState ) . to . eql ( { isShuttingDown : true } )
252+ done ( )
253+ } )
254+
255+ // let the process start up
256+ setTimeout ( ( ) => {
257+ fetch ( 'http://localhost:8000/health' )
258+ . then ( async ( res ) => {
259+ expect ( res . status ) . to . eql ( 200 )
260+ responseAssertionsComplete = true
261+ const json = await res . json ( )
262+ exposedState = json . info . state
263+ } )
264+ } , 300 )
265+ } )
266+
219267 describe ( 'includes error on reject' , async ( ) => {
220268 const errors = [
221269 new Error ( 'test error 1' ) ,
0 commit comments