import * as assert from "assert" ; import * as Azure from "azure-storage" ; import { configLogger } from "../../../src/common/Logger" ; import TableServer from "../../../src/table/TableServer" ; import { overrideRequest , restoreBuildRequestOptions } from "../../testutils" ; import { createConnectionStringForTest , createSecondaryConnectionStringForTest , createTableServerForTest } from "./table.entity.test.utils" ; import { getServicePropertiesForTest } from "./table.service.test.properties" ; configLogger ( false ) ; const testLocalAzuriteInstance = true ; describe ( "table APIs test" , ( ) => { let server ; const tableService = Azure . createTableService ( createConnectionStringForTest ( testLocalAzuriteInstance ) ) ; tableService . enableGlobalHttpAgent = true ; const requestOverride = { headers : { } } ; before ( async ( ) => { overrideRequest ( requestOverride , tableService ) ; server = createTableServerForTest ( ) ; await server . start ( ) ; } ) ; after ( async ( ) => { restoreBuildRequestOptions ( tableService ) ; tableService . removeAllListeners ( ) ; await server . close ( ) ; } ) ; it ( "GetServiceProperties @loki" , async ( ) => { const getServicePropsPromise = ( ) => { return new Promise < Azure . common . models . ServicePropertiesResult . ServiceProperties > ( ( resolve , reject ) => { tableService . getServiceProperties ( ( error , result , response ) => { if ( error ) return reject ( error ) ; assert . ok ( response . isSuccessful ) ; resolve ( result ) ; } ) ; } ) ; } ; await getServicePropsPromise ( ) . then ( ( result ) => { if ( result && result . Cors && result . Cors . CorsRule !== undefined ) { assert . ok ( result . Cors . CorsRule [ 0 ] . AllowedHeaders . length >= 0 ) ; assert . ok ( result . Cors . CorsRule [ 0 ] . AllowedMethods . length > 0 ) ; assert . ok ( result . Cors . CorsRule [ 0 ] . AllowedOrigins . length > 0 ) ; assert . ok ( result . Cors . CorsRule [ 0 ] . ExposedHeaders . length >= 0 ) ; assert . ok ( result . Cors . CorsRule [ 0 ] . MaxAgeInSeconds >= 0 ) ; } else { assert . notStrictEqual ( result , undefined ) ; } if ( result . HourMetrics ) { assert . strictEqual ( result . HourMetrics . Enabled , false ) ; } else { assert . notStrictEqual ( result . HourMetrics , undefined ) ; } } ) . catch ( ( err ) => { assert . ifError ( err ) ; } ) ; } ) ; it ( "GetServiceStats negative @loki" , async ( ) => { const getServiceStatsPromise = ( ) => { return new Promise < Azure . common . models . ServiceStats > ( ( resolve , reject ) => { tableService . getServiceStats ( ( error , result , response ) => { if ( error ) return reject ( error ) ; assert . ok ( response . isSuccessful ) ; resolve ( result ) ; } ) ; } ) ; } ; await getServiceStatsPromise ( ) . catch ( ( err ) => { assert . strictEqual ( err . statusCode , 400 ) ; assert . strictEqual ( err . code , "InvalidQueryParameterValue" ) ; assert . ok ( err ) ; } ) ; } ) ; it ( "SetServiceProperties @loki" , async ( ) => { const setServicePropsPromise = ( properties ) => { return new Promise < Azure . ServiceResponse > ( ( resolve , reject ) => { tableService . setServiceProperties ( properties , ( error , response ) => { if ( error ) return reject ( error ) ; assert . ok ( response . isSuccessful ) ; resolve ( response ) ; } ) ; } ) ; } ; const props = getServicePropertiesForTest ( ) ; await setServicePropsPromise ( props ) . then ( async ( setResponse ) => { assert . strictEqual ( setResponse . statusCode , 202 ) ; const getServicePropsPromise = ( ) => { return new Promise < Azure . common . models . ServicePropertiesResult . ServiceProperties > ( ( resolve , reject ) => { tableService . getServiceProperties ( ( error , result , getResponse ) => { if ( error ) return reject ( error ) ; assert . ok ( getResponse . isSuccessful ) ; resolve ( result ) ; } ) ; } ) ; } ; await getServicePropsPromise ( ) . then ( ( result ) => { if ( result && result . Cors && result . Cors . CorsRule !== undefined ) { assert . strictEqual ( result . Cors . CorsRule . length , 3 ) ; assert . strictEqual ( result . Cors . CorsRule [ 0 ] . AllowedHeaders . length , 3 ) ; assert . strictEqual ( result . Cors . CorsRule [ 0 ] . AllowedMethods . length , 6 ) ; assert . strictEqual ( result . Cors . CorsRule [ 0 ] . AllowedOrigins . length , 2 ) ; assert . strictEqual ( result . Cors . CorsRule [ 0 ] . ExposedHeaders . length , 1 ) ; assert . strictEqual ( result . Cors . CorsRule [ 0 ] . MaxAgeInSeconds , 100 ) ; } else { assert . notStrictEqual ( result , undefined ) ; } if ( result . HourMetrics ) { assert . strictEqual ( result . HourMetrics . Enabled , false ) ; } else { assert . notStrictEqual ( result . HourMetrics , undefined ) ; } } ) . catch ( ( err ) => { assert . ifError ( err ) ; } ) ; } ) . catch ( ( err ) => { assert . ifError ( err ) ; } ) ; } ) ; } ) ; describe ( "table APIs test - secondary location endpoint" , ( ) => { let server ; const tableService = Azure . createTableService ( createSecondaryConnectionStringForTest ( testLocalAzuriteInstance ) ) ; tableService . enableGlobalHttpAgent = true ; const requestOverride = { headers : { } } ; before ( async ( ) => { overrideRequest ( requestOverride , tableService ) ; server = createTableServerForTest ( ) ; await server . start ( ) ; } ) ; after ( async ( ) => { restoreBuildRequestOptions ( tableService ) ; tableService . removeAllListeners ( ) ; await server . close ( ) ; } ) ; it ( "GetServiceStats @loki" , async ( ) => { const getServiceStatsPromise = ( ) => { return new Promise < Azure . common . models . ServiceStats > ( ( resolve , reject ) => { tableService . getServiceStats ( ( error , result , response ) => { if ( error ) return reject ( error ) ; assert . ok ( response . isSuccessful ) ; resolve ( result ) ; } ) ; } ) ; } ; await getServiceStatsPromise ( ) . then ( ( result ) => { assert . strictEqual ( result . GeoReplication ?. Status , "live" ) ; } ) . catch ( ( err ) => { assert . ifError ( err ) ; } ) ; } ) ; } ) ;