import sinon , { SinonFakeServer } from 'sinon' ; import { API_BASE_PATH } from '../../../common' ; type HttpResponse = Record < string , any > | any [ ] ; const mockResponse = ( defaultResponse , response ? ) => [ 200 , { 'Content-Type' : 'application/json' } , JSON . stringify ( { ... defaultResponse , ... response } ) , ] ; const registerHttpRequestMockHelpers = ( server ) => { const setLoadRepositoriesResponse = ( response = { } ) => { const defaultResponse = { repositories : [ ] } ; server . respondWith ( 'GET' , `${ API_BASE_PATH }repositories` , mockResponse ( defaultResponse , response ) ) ; } ; const setLoadRepositoryTypesResponse = ( response = [ ] ) => { server . respondWith ( 'GET' , `${ API_BASE_PATH }repository_types` , JSON . stringify ( response ) ) ; } ; const setGetRepositoryResponse = ( response ? , delay = 0 ) => { const defaultResponse = { } ; server . respondWith ( 'GET' , /api\/snapshot_restore\/repositories\/.+/ , mockResponse ( defaultResponse , response ) ) ; } ; const setSaveRepositoryResponse = ( response ? , error ? ) => { const status = error ? error . status || 400 : 200 ; const body = error ? JSON . stringify ( error . body ) : JSON . stringify ( response ) ; server . respondWith ( 'PUT' , `${ API_BASE_PATH }repositories` , [ status , { 'Content-Type' : 'application/json' } , body , ] ) ; } ; const setLoadSnapshotsResponse = ( response = { } ) => { const defaultResponse = { errors : { } , snapshots : [ ] , repositories : [ ] , total : 0 } ; server . respondWith ( 'GET' , `${ API_BASE_PATH }snapshots` , mockResponse ( defaultResponse , response ) ) ; } ; const setGetSnapshotResponse = ( response ? ) => { const defaultResponse = { } ; server . respondWith ( 'GET' , /\/api\/snapshot_restore\/snapshots\/.+/ , mockResponse ( defaultResponse , response ) ) ; } ; const setLoadIndicesResponse = ( response = { } ) => { const defaultResponse = { indices : [ ] } ; server . respondWith ( 'GET' , `${ API_BASE_PATH }policies/indices` , mockResponse ( defaultResponse , response ) ) ; } ; const setAddPolicyResponse = ( response ? , error ? ) => { const status = error ? error . status || 400 : 200 ; const body = error ? JSON . stringify ( error . body ) : JSON . stringify ( response ) ; server . respondWith ( 'POST' , `${ API_BASE_PATH }policies` , [ status , { 'Content-Type' : 'application/json' } , body , ] ) ; } ; const setCleanupRepositoryResponse = ( response ? , error ? ) => { const status = error ? error . status || 503 : 200 ; const body = error ? JSON . stringify ( error ) : JSON . stringify ( response ) ; server . respondWith ( 'POST' , `${ API_BASE_PATH }repositories/:name/cleanup` , [ status , { 'Content-Type' : 'application/json' } , body , ] ) ; } ; const setGetPolicyResponse = ( response ? ) => { server . respondWith ( 'GET' , `${ API_BASE_PATH }policy/:name` , [ 200 , { 'Content-Type' : 'application/json' } , JSON . stringify ( response ) , ] ) ; } ; const setRestoreSnapshotResponse = ( response ? ) => { server . respondWith ( 'POST' , `${ API_BASE_PATH }restore/:repository/:snapshot` , [ 200 , { 'Content-Type' : 'application/json' } , JSON . stringify ( response ) , ] ) ; } ; return { setLoadRepositoriesResponse , setLoadRepositoryTypesResponse , setGetRepositoryResponse , setSaveRepositoryResponse , setLoadSnapshotsResponse , setGetSnapshotResponse , setLoadIndicesResponse , setAddPolicyResponse , setGetPolicyResponse , setCleanupRepositoryResponse , setRestoreSnapshotResponse , } ; } ; export const init = ( ) => { const server = sinon . fakeServer . create ( ) ; server . respondImmediately = true ; server . respondWith ( [ 200 , { } , 'DefaultResponse' ] ) ; const httpRequestsMockHelpers = registerHttpRequestMockHelpers ( server ) ; return { server , httpRequestsMockHelpers , } ; } ;