jest . mock ( 'uuid' , ( ) => ( { v4 : jest . fn ( ) . mockReturnValue ( 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' ) , } ) ) ; import { RouteOptions } from '@hapi/hapi' ; import { OpenSearchDashboardsRequest } from './request' ; import { httpServerMock } from '../http_server.mocks' ; import { schema } from '@osd/config-schema' ; describe ( 'OpenSearchDashboardsRequest' , ( ) => { describe ( 'id property' , ( ) => { it ( 'uses the request.app.requestId property if present' , ( ) => { const request = httpServerMock . createRawRequest ( { app : { requestId : 'fakeId' } , } ) ; const opensearchDashboardsRequest = OpenSearchDashboardsRequest . from ( request ) ; expect ( opensearchDashboardsRequest . id ) . toEqual ( 'fakeId' ) ; } ) ; it ( 'generates a new UUID if request.app property is not present' , ( ) => { const request = httpServerMock . createRawRequest ( { app : undefined , } ) ; const opensearchDashboardsRequest = OpenSearchDashboardsRequest . from ( request ) ; expect ( opensearchDashboardsRequest . id ) . toEqual ( 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' ) ; } ) ; it ( 'generates a new UUID if request.app.requestId property is not present' , ( ) => { const request = httpServerMock . createRawRequest ( { app : { } , } ) ; const opensearchDashboardsRequest = OpenSearchDashboardsRequest . from ( request ) ; expect ( opensearchDashboardsRequest . id ) . toEqual ( 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' ) ; } ) ; } ) ; describe ( 'uuid property' , ( ) => { it ( 'uses the request.app.requestUuid property if present' , ( ) => { const request = httpServerMock . createRawRequest ( { app : { requestUuid : '123e4567-e89b-12d3-a456-426614174000' } , } ) ; const opensearchDashboardsRequest = OpenSearchDashboardsRequest . from ( request ) ; expect ( opensearchDashboardsRequest . uuid ) . toEqual ( '123e4567-e89b-12d3-a456-426614174000' ) ; } ) ; it ( 'generates a new UUID if request.app property is not present' , ( ) => { const request = httpServerMock . createRawRequest ( { app : undefined , } ) ; const opensearchDashboardsRequest = OpenSearchDashboardsRequest . from ( request ) ; expect ( opensearchDashboardsRequest . uuid ) . toEqual ( 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' ) ; } ) ; it ( 'generates a new UUID if request.app.requestUuid property is not present' , ( ) => { const request = httpServerMock . createRawRequest ( { app : { } , } ) ; const opensearchDashboardsRequest = OpenSearchDashboardsRequest . from ( request ) ; expect ( opensearchDashboardsRequest . uuid ) . toEqual ( 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' ) ; } ) ; } ) ; describe ( 'get all headers' , ( ) => { it ( 'returns all headers' , ( ) => { const request = httpServerMock . createRawRequest ( { headers : { custom : 'one' , authorization : 'token' } , } ) ; const opensearchDashboardsRequest = OpenSearchDashboardsRequest . from ( request ) ; expect ( opensearchDashboardsRequest . headers ) . toEqual ( { custom : 'one' , authorization : 'token' , } ) ; } ) ; } ) ; describe ( 'headers property' , ( ) => { it ( 'provides a frozen copy of request headers' , ( ) => { const rawRequestHeaders = { custom : 'one' } ; const request = httpServerMock . createRawRequest ( { headers : rawRequestHeaders , } ) ; const opensearchDashboardsRequest = OpenSearchDashboardsRequest . from ( request ) ; expect ( opensearchDashboardsRequest . headers ) . toEqual ( { custom : 'one' } ) ; expect ( opensearchDashboardsRequest . headers ) . not . toBe ( rawRequestHeaders ) ; expect ( Object . isFrozen ( opensearchDashboardsRequest . headers ) ) . toBe ( true ) ; } ) ; it . skip ( "doesn't expose authorization header by default" , ( ) => { const request = httpServerMock . createRawRequest ( { headers : { custom : 'one' , authorization : 'token' } , } ) ; const opensearchDashboardsRequest = OpenSearchDashboardsRequest . from ( request ) ; expect ( opensearchDashboardsRequest . headers ) . toEqual ( { custom : 'one' , } ) ; } ) ; it ( 'exposes authorization header if secured = false' , ( ) => { const request = httpServerMock . createRawRequest ( { headers : { custom : 'one' , authorization : 'token' } , } ) ; const opensearchDashboardsRequest = OpenSearchDashboardsRequest . from ( request , undefined , false ) ; expect ( opensearchDashboardsRequest . headers ) . toEqual ( { custom : 'one' , authorization : 'token' , } ) ; } ) ; } ) ; describe ( 'isSytemApi property' , ( ) => { it ( 'is false when no osd-system-request header is set' , ( ) => { const request = httpServerMock . createRawRequest ( { headers : { custom : 'one' } , } ) ; const opensearchDashboardsRequest = OpenSearchDashboardsRequest . from ( request ) ; expect ( opensearchDashboardsRequest . isSystemRequest ) . toBe ( false ) ; } ) ; it ( 'is true when osd-system-request header is set to true' , ( ) => { const request = httpServerMock . createRawRequest ( { headers : { custom : 'one' , 'osd-system-request' : 'true' } , } ) ; const opensearchDashboardsRequest = OpenSearchDashboardsRequest . from ( request ) ; expect ( opensearchDashboardsRequest . isSystemRequest ) . toBe ( true ) ; } ) ; it ( 'is false when osd-system-request header is set to false' , ( ) => { const request = httpServerMock . createRawRequest ( { headers : { custom : 'one' , 'osd-system-request' : 'false' } , } ) ; const opensearchDashboardsRequest = OpenSearchDashboardsRequest . from ( request ) ; expect ( opensearchDashboardsRequest . isSystemRequest ) . toBe ( false ) ; } ) ; it ( 'is false when no osd-system-api header is set' , ( ) => { const request = httpServerMock . createRawRequest ( { headers : { custom : 'one' } , } ) ; const opensearchDashboardsRequest = OpenSearchDashboardsRequest . from ( request ) ; expect ( opensearchDashboardsRequest . isSystemRequest ) . toBe ( false ) ; } ) ; it ( 'is true when osd-system-api header is set to true' , ( ) => { const request = httpServerMock . createRawRequest ( { headers : { custom : 'one' , 'osd-system-api' : 'true' } , } ) ; const opensearchDashboardsRequest = OpenSearchDashboardsRequest . from ( request ) ; expect ( opensearchDashboardsRequest . isSystemRequest ) . toBe ( true ) ; } ) ; it ( 'is false when osd-system-api header is set to false' , ( ) => { const request = httpServerMock . createRawRequest ( { headers : { custom : 'one' , 'osd-system-api' : 'false' } , } ) ; const opensearchDashboardsRequest = OpenSearchDashboardsRequest . from ( request ) ; expect ( opensearchDashboardsRequest . isSystemRequest ) . toBe ( false ) ; } ) ; } ) ; describe ( 'route.options.authRequired property' , ( ) => { it ( 'handles required auth: undefined' , ( ) => { const auth = undefined ; const request = httpServerMock . createRawRequest ( { route : { settings : { auth , } , } , } ) ; const opensearchDashboardsRequest = OpenSearchDashboardsRequest . from ( request ) ; expect ( opensearchDashboardsRequest . route . options . authRequired ) . toBe ( true ) ; } ) ; it ( 'handles required auth: false' , ( ) => { const auth = false ; const request = httpServerMock . createRawRequest ( { route : { settings : { auth , } , } , } ) ; const opensearchDashboardsRequest = OpenSearchDashboardsRequest . from ( request ) ; expect ( opensearchDashboardsRequest . route . options . authRequired ) . toBe ( false ) ; } ) ; it ( 'handles required auth: { mode: "required" }' , ( ) => { const request = httpServerMock . createRawRequest ( { route : { settings : { auth : { mode : 'required' } , } , } , } ) ; const opensearchDashboardsRequest = OpenSearchDashboardsRequest . from ( request ) ; expect ( opensearchDashboardsRequest . route . options . authRequired ) . toBe ( true ) ; } ) ; it ( 'handles required auth: { mode: "optional" }' , ( ) => { const request = httpServerMock . createRawRequest ( { route : { settings : { auth : { mode : 'optional' } , } , } , } ) ; const opensearchDashboardsRequest = OpenSearchDashboardsRequest . from ( request ) ; expect ( opensearchDashboardsRequest . route . options . authRequired ) . toBe ( 'optional' ) ; } ) ; it ( 'handles required auth: { mode: "try" } as "optional"' , ( ) => { const request = httpServerMock . createRawRequest ( { route : { settings : { auth : { mode : 'try' } , } , } , } ) ; const opensearchDashboardsRequest = OpenSearchDashboardsRequest . from ( request ) ; expect ( opensearchDashboardsRequest . route . options . authRequired ) . toBe ( 'optional' ) ; } ) ; it ( 'throws on auth: strategy name' , ( ) => { const request = httpServerMock . createRawRequest ( { route : { settings : { auth : { strategies : [ 'session' ] } , } , } , } ) ; expect ( ( ) => OpenSearchDashboardsRequest . from ( request ) ) . toThrowErrorMatchingInlineSnapshot ( `"unexpected authentication options: {\\"strategies\\":[\\"session\\"]} for route: /"` ) ; } ) ; it ( 'throws on auth: { mode: unexpected mode }' , ( ) => { const request = httpServerMock . createRawRequest ( { route : { settings : { auth : { mode : undefined } , } , } , } ) ; expect ( ( ) => OpenSearchDashboardsRequest . from ( request ) ) . toThrowErrorMatchingInlineSnapshot ( `"unexpected authentication options: {} for route: /"` ) ; } ) ; } ) ; describe ( 'RouteSchema type inferring' , ( ) => { it ( 'should work with config-schema' , ( ) => { const body = Buffer . from ( 'body!' ) ; const request = { ... httpServerMock . createRawRequest ( { params : { id : 'params' } , query : { search : 'query' } , } ) , payload : body , } as any ; const opensearchDashboardsRequest = OpenSearchDashboardsRequest . from ( request , { params : schema . object ( { id : schema . string ( ) } ) , query : schema . object ( { search : schema . string ( ) } ) , body : schema . buffer ( ) , } ) ; expect ( opensearchDashboardsRequest . params ) . toStrictEqual ( { id : 'params' } ) ; expect ( opensearchDashboardsRequest . params . id . toUpperCase ( ) ) . toEqual ( 'PARAMS' ) ; expect ( opensearchDashboardsRequest . query ) . toStrictEqual ( { search : 'query' } ) ; expect ( opensearchDashboardsRequest . query . search . toUpperCase ( ) ) . toEqual ( 'QUERY' ) ; expect ( opensearchDashboardsRequest . body ) . toEqual ( body ) ; expect ( opensearchDashboardsRequest . body . byteLength ) . toBeGreaterThan ( 0 ) ; } ) ; it ( 'should work with ValidationFunction' , ( ) => { const body = Buffer . from ( 'body!' ) ; const request = { ... httpServerMock . createRawRequest ( { params : { id : 'params' } , query : { search : 'query' } , } ) , payload : body , } as any ; const opensearchDashboardsRequest = OpenSearchDashboardsRequest . from ( request , { params : schema . object ( { id : schema . string ( ) } ) , query : schema . object ( { search : schema . string ( ) } ) , body : ( data , { ok , badRequest } ) => { if ( Buffer . isBuffer ( data ) ) { return ok ( data ) ; } else { return badRequest ( 'It should be a Buffer' , [ ] ) ; } } , } ) ; expect ( opensearchDashboardsRequest . params ) . toStrictEqual ( { id : 'params' } ) ; expect ( opensearchDashboardsRequest . params . id . toUpperCase ( ) ) . toEqual ( 'PARAMS' ) ; expect ( opensearchDashboardsRequest . query ) . toStrictEqual ( { search : 'query' } ) ; expect ( opensearchDashboardsRequest . query . search . toUpperCase ( ) ) . toEqual ( 'QUERY' ) ; expect ( opensearchDashboardsRequest . body ) . toEqual ( body ) ; expect ( opensearchDashboardsRequest . body . byteLength ) . toBeGreaterThan ( 0 ) ; } ) ; } ) ; } ) ;