import 'mocha' ; import HttpStatus from '@xpring-eng/http-status' ; import * as request from 'supertest' ; import App from '../../../../src/app' ; import config , { payIdServerVersions } from '../../../../src/config' ; import { appSetup , appCleanup } from '../../../helpers/helpers' ; let app ; const earliestPayIdServerVersion = payIdServerVersions [ 0 ] ; const payIdServerVersion = config . app . payIdVersion ; describe ( 'E2E - publicAPIRouter - Version Headers' , function ( ) : void { before ( async function ( ) { app = await appSetup ( ) ; } ) ; it ( 'Returns a 400 - Bad Request when we omit a PayID-Version header' , function ( done ) : void { const payId = '/alice' ; const acceptHeader = 'application/xrpl-mainnet+json' ; const expectedErrorResponse = { statusCode : 400 , error : 'Bad Request' , message : "A PayID-Version header is required in the request, of the form 'PayID-Version: {major}.{minor}'." , } ; request ( app . publicApiExpress ) . get ( payId ) . set ( 'Accept' , acceptHeader ) . expect ( 'PayID-Server-Version' , payIdServerVersion ) . expect ( HttpStatus . BadRequest , expectedErrorResponse , done ) ; } ) ; it ( 'Returns a 400 - Bad Request when we provide a malformed PayID-Version header' , function ( done ) : void { const payId = '/alice' ; const acceptHeader = 'application/xrpl-mainnet+json' ; const expectedErrorResponse = { statusCode : 400 , error : 'Bad Request' , message : "A PayID-Version header must be in the form 'PayID-Version: {major}.{minor}'." , } ; request ( app . publicApiExpress ) . get ( payId ) . set ( 'PayID-Version' , 'abc' ) . set ( 'Accept' , acceptHeader ) . expect ( 'PayID-Server-Version' , payIdServerVersion ) . expect ( HttpStatus . BadRequest , expectedErrorResponse , done ) ; } ) ; it ( 'Returns a 400 - Bad Request when we provide an unsupported PayID-Version header (greater than PayID server)' , function ( done ) : void { const payId = '/alice' ; const acceptHeader = 'application/xrpl-mainnet+json' ; const payIdVersion = '100.0' ; const expectedErrorResponse = { statusCode : 400 , error : 'Bad Request' , message : `The PayID-Version ${ payIdVersion } is not supported, please try downgrading your request to PayID-Version ${ payIdServerVersion }` , } ; request ( app . publicApiExpress ) . get ( payId ) . set ( 'PayID-Version' , payIdVersion ) . set ( 'Accept' , acceptHeader ) . expect ( 'PayID-Server-Version' , payIdServerVersion ) . expect ( HttpStatus . BadRequest , expectedErrorResponse , done ) ; } ) ; it ( 'Returns a 400 - Bad Request when we provide an unknown PayID-Version header' , function ( done ) : void { const payId = '/alice' ; const acceptHeader = 'application/xrpl-mainnet+json' ; const payIdVersion = '0.1' ; const expectedErrorResponse = { statusCode : 400 , error : 'Bad Request' , message : `The PayID Version ${ payIdVersion } is not supported, try something in the range ${ earliestPayIdServerVersion } - ${ payIdServerVersion }` , } ; request ( app . publicApiExpress ) . get ( payId ) . set ( 'PayID-Version' , payIdVersion ) . set ( 'Accept' , acceptHeader ) . expect ( 'PayID-Server-Version' , payIdServerVersion ) . expect ( HttpStatus . BadRequest , expectedErrorResponse , done ) ; } ) ; it ( 'Returns a 200 - OK when we provide a valid PayID-Version header' , function ( done ) : void { const payId = '/alice' ; const acceptHeader = 'application/xrpl-mainnet+json' ; const payIdVersion = payIdServerVersion ; request ( app . publicApiExpress ) . get ( payId ) . set ( 'PayID-Version' , payIdVersion ) . set ( 'Accept' , acceptHeader ) . expect ( 'PayID-Server-Version' , payIdServerVersion ) . expect ( 'PayID-Version' , payIdVersion ) . expect ( HttpStatus . OK , done ) ; } ) ; after ( function ( ) { appCleanup ( app ) ; } ) ; } ) ;