import { Client , createRestAppClient , expect , givenHttpServerConfig , } from '@loopback/testlab' ; import { promisify } from 'util' ; import { GreetingApplication } from '../..' ; import { CACHING_SERVICE } from '../../keys' ; describe ( 'GreetingApplication' , ( ) => { let app ; let client ; before ( givenRunningApplicationWithCustomConfiguration ) ; after ( ( ) => app . stop ( ) ) ; before ( ( ) => { client = createRestAppClient ( app ) ; } ) ; it ( 'gets a greeting in English' , async function ( ) { const response = await client . get ( '/greet/Raymond' ) . set ( 'Accept-Language' , 'en' ) . expect ( 200 ) ; expect ( response . body ) . to . containEql ( { language : 'en' , greeting : 'Hello, Raymond!' , } ) ; } ) ; it ( 'gets a greeting in Chinese' , async function ( ) { const response = await client . get ( '/greet/Raymond' ) . set ( 'Accept-Language' , 'zh' ) . expect ( 200 ) ; expect ( response . body ) . to . containEql ( { language : 'zh' , greeting : 'Raymond，你好！' , } ) ; } ) ; it ( 'gets a greeting from cache' , async function ( ) { app . configure ( CACHING_SERVICE ) . to ( { ttl : 100 } ) ; let response = await client . get ( '/greet/Raymond' ) . set ( 'Accept-Language' , 'en' ) . expect ( 200 ) ; const msg1 = response . body ; response = await client . get ( '/greet/Raymond' ) . set ( 'Accept-Language' , 'en' ) . expect ( 200 ) ; expect ( response . body ) . to . eql ( msg1 ) ; await promisify ( setTimeout ) ( 200 ) ; response = await client . get ( '/greet/Raymond' ) . set ( 'Accept-Language' , 'en' ) . expect ( 200 ) ; expect ( response . body . timestamp ) . to . not . eql ( msg1 . timestamp ) ; } ) ; async function givenRunningApplicationWithCustomConfiguration ( ) { app = new GreetingApplication ( { rest : givenHttpServerConfig ( ) , } ) ; await app . main ( ) ; } } ) ;