import sinon , { SinonFakeServer } from 'sinon' ; import { API_BASE_PATH } from '../../../common/constants' ; import { CloudBackupStatus , ESUpgradeStatus , DeprecationLoggingStatus , ResponseError , } from '../../../common/types' ; const registerHttpRequestMockHelpers = ( server ) => { const setLoadCloudBackupStatusResponse = ( response ? , error ? ) => { const status = error ? error . statusCode || 400 : 200 ; const body = error ? error : response ; server . respondWith ( 'GET' , `${ API_BASE_PATH }/cloud_backup_status` , [ status , { 'Content-Type' : 'application/json' } , JSON . stringify ( body ) , ] ) ; } ; const setLoadEsDeprecationsResponse = ( response ? , error ? ) => { const status = error ? error . statusCode || 400 : 200 ; const body = error ? error : response ; server . respondWith ( 'GET' , `${ API_BASE_PATH }/es_deprecations` , [ status , { 'Content-Type' : 'application/json' } , JSON . stringify ( body ) , ] ) ; } ; const setLoadDeprecationLoggingResponse = ( response ? , error ? ) => { const status = error ? error . statusCode || 400 : 200 ; const body = error ? error : response ; server . respondWith ( 'GET' , `${ API_BASE_PATH }/deprecation_logging` , [ status , { 'Content-Type' : 'application/json' } , JSON . stringify ( body ) , ] ) ; } ; const setLoadDeprecationLogsCountResponse = ( response ? , error ? ) => { const status = error ? error . statusCode || 400 : 200 ; const body = error ? error : response ; server . respondWith ( 'GET' , `${ API_BASE_PATH }/deprecation_logging/count` , [ status , { 'Content-Type' : 'application/json' } , JSON . stringify ( body ) , ] ) ; } ; const setDeleteLogsCacheResponse = ( response ? , error ? ) => { const status = error ? error . statusCode || 400 : 200 ; const body = error ? error : response ; server . respondWith ( 'DELETE' , `${ API_BASE_PATH }/deprecation_logging/cache` , [ status , { 'Content-Type' : 'application/json' } , JSON . stringify ( body ) , ] ) ; } ; const setUpdateDeprecationLoggingResponse = ( response ? , error ? ) => { const status = error ? error . statusCode || 400 : 200 ; const body = error ? error : response ; server . respondWith ( 'PUT' , `${ API_BASE_PATH }/deprecation_logging` , [ status , { 'Content-Type' : 'application/json' } , JSON . stringify ( body ) , ] ) ; } ; const setUpdateIndexSettingsResponse = ( response ? , error ? ) => { const status = error ? error . statusCode || 400 : 200 ; const body = error ? error : response ; server . respondWith ( 'POST' , `${ API_BASE_PATH }/:indexName/index_settings` , [ status , { 'Content-Type' : 'application/json' } , JSON . stringify ( body ) , ] ) ; } ; const setUpgradeMlSnapshotResponse = ( response ? , error ? ) => { const status = error ? error . statusCode || 400 : 200 ; const body = error ? error : response ; server . respondWith ( 'POST' , `${ API_BASE_PATH }/ml_snapshots` , [ status , { 'Content-Type' : 'application/json' } , JSON . stringify ( body ) , ] ) ; } ; const setUpgradeMlSnapshotStatusResponse = ( response ? , error ? ) => { const status = error ? error . statusCode || 400 : 200 ; const body = error ? error : response ; server . respondWith ( 'GET' , `${ API_BASE_PATH }/ml_snapshots/:jobId/:snapshotId` , [ status , { 'Content-Type' : 'application/json' } , JSON . stringify ( body ) , ] ) ; } ; const setReindexStatusResponse = ( response ? , error ? ) => { const status = error ? error . statusCode || 400 : 200 ; const body = error ? error : response ; server . respondWith ( 'GET' , `${ API_BASE_PATH }/reindex/:indexName` , [ status , { 'Content-Type' : 'application/json' } , JSON . stringify ( body ) , ] ) ; } ; const setStartReindexingResponse = ( response ? , error ? ) => { const status = error ? error . statusCode || 400 : 200 ; const body = error ? error : response ; server . respondWith ( 'POST' , `${ API_BASE_PATH }/reindex/:indexName` , [ status , { 'Content-Type' : 'application/json' } , JSON . stringify ( body ) , ] ) ; } ; const setDeleteMlSnapshotResponse = ( response ? , error ? ) => { const status = error ? error . statusCode || 400 : 200 ; const body = error ? error : response ; server . respondWith ( 'DELETE' , `${ API_BASE_PATH }/ml_snapshots/:jobId/:snapshotId` , [ status , { 'Content-Type' : 'application/json' } , JSON . stringify ( body ) , ] ) ; } ; const setLoadSystemIndicesMigrationStatus = ( response ? , error ? ) => { const status = error ? error . statusCode || 400 : 200 ; const body = error ? error : response ; server . respondWith ( 'GET' , `${ API_BASE_PATH }/system_indices_migration` , [ status , { 'Content-Type' : 'application/json' } , JSON . stringify ( body ) , ] ) ; } ; const setLoadMlUpgradeModeResponse = ( response ? , error ? ) => { const status = error ? error . statusCode || 400 : 200 ; const body = error ? error : response ; server . respondWith ( 'GET' , `${ API_BASE_PATH }/ml_upgrade_mode` , [ status , { 'Content-Type' : 'application/json' } , JSON . stringify ( body ) , ] ) ; } ; const setSystemIndicesMigrationResponse = ( response ? , error ? ) => { const status = error ? error . statusCode || 400 : 200 ; const body = error ? error : response ; server . respondWith ( 'POST' , `${ API_BASE_PATH }/system_indices_migration` , [ status , { 'Content-Type' : 'application/json' } , JSON . stringify ( body ) , ] ) ; } ; const setGetUpgradeStatusResponse = ( response ? , error ? ) => { const status = error ? error . statusCode || 400 : 200 ; const body = error ? error : response ; server . respondWith ( 'GET' , `${ API_BASE_PATH }/status` , [ status , { 'Content-Type' : 'application/json' } , JSON . stringify ( body ) , ] ) ; } ; return { setLoadCloudBackupStatusResponse , setLoadEsDeprecationsResponse , setLoadDeprecationLoggingResponse , setUpdateDeprecationLoggingResponse , setUpdateIndexSettingsResponse , setUpgradeMlSnapshotResponse , setDeleteMlSnapshotResponse , setUpgradeMlSnapshotStatusResponse , setLoadDeprecationLogsCountResponse , setLoadSystemIndicesMigrationStatus , setSystemIndicesMigrationResponse , setDeleteLogsCacheResponse , setStartReindexingResponse , setReindexStatusResponse , setLoadMlUpgradeModeResponse , setGetUpgradeStatusResponse , } ; } ; export const init = ( ) => { const server = sinon . fakeServer . create ( ) ; server . respondImmediately = true ; server . respondWith ( [ 200 , { } , 'DefaultMockedResponse' ] ) ; const httpRequestsMockHelpers = registerHttpRequestMockHelpers ( server ) ; const setServerAsync = ( isAsync , timeout = 200 ) => { if ( isAsync ) { server . autoRespond = true ; server . autoRespondAfter = 1000 ; server . respondImmediately = false ; } else { server . respondImmediately = true ; } } ; return { server , setServerAsync , httpRequestsMockHelpers , } ; } ;