import { IRouter , RouteConfig } from '../http' ; import { coreMock } from '../mocks' ; import { mockCoreContext } from '../core_context.mock' ; import { httpServiceMock } from '../http/http_service.mock' ; import { httpServerMock } from '../http/http_server.mocks' ; import { renderingMock } from '../rendering/rendering_service.mock' ; import { HttpResourcesService , SetupDeps } from './http_resources_service' ; import { httpResourcesMock } from './http_resources_service.mock' ; const coreContext = mockCoreContext . create ( ) ; describe ( 'HttpResources service' , ( ) => { let service ; let setupDeps ; let router ; const opensearchDashboardsRequest = httpServerMock . createOpenSearchDashboardsRequest ( ) ; const context = { core : coreMock . createRequestHandlerContext ( ) } ; describe ( '#createRegistrar' , ( ) => { beforeEach ( ( ) => { setupDeps = { http : httpServiceMock . createInternalSetupContract ( ) , rendering : renderingMock . createSetupContract ( ) , } ; service = new HttpResourcesService ( coreContext ) ; router = httpServiceMock . createRouter ( ) ; } ) ; describe ( 'register' , ( ) => { describe ( 'renderCoreApp' , ( ) => { it ( 'formats successful response' , async ( ) => { const routeConfig = { path : '/' , validate : false } ; const { createRegistrar } = await service . setup ( setupDeps ) ; const { register } = createRegistrar ( router ) ; register ( routeConfig , async ( ctx , req , res ) => { return res . renderCoreApp ( ) ; } ) ; const [ [ , routeHandler ] ] = router . get . mock . calls ; const responseFactory = httpResourcesMock . createResponseFactory ( ) ; await routeHandler ( context , opensearchDashboardsRequest , responseFactory ) ; expect ( setupDeps . rendering . render ) . toHaveBeenCalledWith ( opensearchDashboardsRequest , context . core . uiSettings . client , { includeUserSettings : true , } ) ; } ) ; it ( 'can attach headers, except the CSP header' , async ( ) => { const routeConfig = { path : '/' , validate : false } ; const { createRegistrar } = await service . setup ( setupDeps ) ; const { register } = createRegistrar ( router ) ; register ( routeConfig , async ( ctx , req , res ) => { return res . renderCoreApp ( { headers : { 'content-security-policy' : "script-src 'unsafe-eval'" , 'x-opensearch-dashboards' : '42' , } , } ) ; } ) ; const [ [ , routeHandler ] ] = router . get . mock . calls ; const responseFactory = httpResourcesMock . createResponseFactory ( ) ; await routeHandler ( context , opensearchDashboardsRequest , responseFactory ) ; expect ( responseFactory . ok ) . toHaveBeenCalledWith ( { body : '<body />' , headers : { 'x-opensearch-dashboards' : '42' , 'content-security-policy' : "script-src 'unsafe-eval' 'self'; worker-src blob: 'self'; style-src 'unsafe-inline' 'self'" , } , } ) ; } ) ; } ) ; describe ( 'renderAnonymousCoreApp' , ( ) => { it ( 'formats successful response' , async ( ) => { const routeConfig = { path : '/' , validate : false } ; const { createRegistrar } = await service . setup ( setupDeps ) ; const { register } = createRegistrar ( router ) ; register ( routeConfig , async ( ctx , req , res ) => { return res . renderAnonymousCoreApp ( ) ; } ) ; const [ [ , routeHandler ] ] = router . get . mock . calls ; const responseFactory = httpResourcesMock . createResponseFactory ( ) ; await routeHandler ( context , opensearchDashboardsRequest , responseFactory ) ; expect ( setupDeps . rendering . render ) . toHaveBeenCalledWith ( opensearchDashboardsRequest , context . core . uiSettings . client , { includeUserSettings : false , } ) ; } ) ; it ( 'can attach headers, except the CSP header' , async ( ) => { const routeConfig = { path : '/' , validate : false } ; const { createRegistrar } = await service . setup ( setupDeps ) ; const { register } = createRegistrar ( router ) ; register ( routeConfig , async ( ctx , req , res ) => { return res . renderAnonymousCoreApp ( { headers : { 'content-security-policy' : "script-src 'unsafe-eval'" , 'x-opensearch-dashboards' : '42' , } , } ) ; } ) ; const [ [ , routeHandler ] ] = router . get . mock . calls ; const responseFactory = httpResourcesMock . createResponseFactory ( ) ; await routeHandler ( context , opensearchDashboardsRequest , responseFactory ) ; expect ( responseFactory . ok ) . toHaveBeenCalledWith ( { body : '<body />' , headers : { 'x-opensearch-dashboards' : '42' , 'content-security-policy' : "script-src 'unsafe-eval' 'self'; worker-src blob: 'self'; style-src 'unsafe-inline' 'self'" , } , } ) ; } ) ; } ) ; describe ( 'renderHtml' , ( ) => { it ( 'formats successful response' , async ( ) => { const htmlBody = '<html><body /></html>' ; const routeConfig = { path : '/' , validate : false } ; const { createRegistrar } = await service . setup ( setupDeps ) ; const { register } = createRegistrar ( router ) ; register ( routeConfig , async ( ctx , req , res ) => { return res . renderHtml ( { body : htmlBody } ) ; } ) ; const [ [ , routeHandler ] ] = router . get . mock . calls ; const responseFactory = httpResourcesMock . createResponseFactory ( ) ; await routeHandler ( context , opensearchDashboardsRequest , responseFactory ) ; expect ( responseFactory . ok ) . toHaveBeenCalledWith ( { body : htmlBody , headers : { 'content-type' : 'text/html' , 'content-security-policy' : "script-src 'unsafe-eval' 'self'; worker-src blob: 'self'; style-src 'unsafe-inline' 'self'" , } , } ) ; } ) ; it ( 'can attach headers, except the CSP & "content-type" headers' , async ( ) => { const htmlBody = '<html><body /></html>' ; const routeConfig = { path : '/' , validate : false } ; const { createRegistrar } = await service . setup ( setupDeps ) ; const { register } = createRegistrar ( router ) ; register ( routeConfig , async ( ctx , req , res ) => { return res . renderHtml ( { body : htmlBody , headers : { 'content-type' : 'text/html5' , 'content-security-policy' : "script-src 'unsafe-eval'" , 'x-opensearch-dashboards' : '42' , } , } ) ; } ) ; const [ [ , routeHandler ] ] = router . get . mock . calls ; const responseFactory = httpResourcesMock . createResponseFactory ( ) ; await routeHandler ( context , opensearchDashboardsRequest , responseFactory ) ; expect ( responseFactory . ok ) . toHaveBeenCalledWith ( { body : htmlBody , headers : { 'content-type' : 'text/html' , 'x-opensearch-dashboards' : '42' , 'content-security-policy' : "script-src 'unsafe-eval' 'self'; worker-src blob: 'self'; style-src 'unsafe-inline' 'self'" , } , } ) ; } ) ; } ) ; describe ( 'renderJs' , ( ) => { it ( 'formats successful response' , async ( ) => { const jsBody = 'alert(1);' ; const routeConfig = { path : '/' , validate : false } ; const { createRegistrar } = await service . setup ( setupDeps ) ; const { register } = createRegistrar ( router ) ; register ( routeConfig , async ( ctx , req , res ) => { return res . renderJs ( { body : jsBody } ) ; } ) ; const [ [ , routeHandler ] ] = router . get . mock . calls ; const responseFactory = httpResourcesMock . createResponseFactory ( ) ; await routeHandler ( context , opensearchDashboardsRequest , responseFactory ) ; expect ( responseFactory . ok ) . toHaveBeenCalledWith ( { body : jsBody , headers : { 'content-type' : 'text/javascript' , 'content-security-policy' : "script-src 'unsafe-eval' 'self'; worker-src blob: 'self'; style-src 'unsafe-inline' 'self'" , } , } ) ; } ) ; it ( 'can attach headers, except the CSP & "content-type" headers' , async ( ) => { const jsBody = 'alert(1);' ; const routeConfig = { path : '/' , validate : false } ; const { createRegistrar } = await service . setup ( setupDeps ) ; const { register } = createRegistrar ( router ) ; register ( routeConfig , async ( ctx , req , res ) => { return res . renderJs ( { body : jsBody , headers : { 'content-type' : 'text/html' , 'content-security-policy' : "script-src 'unsafe-eval'" , 'x-opensearch-dashboards' : '42' , } , } ) ; } ) ; const [ [ , routeHandler ] ] = router . get . mock . calls ; const responseFactory = httpResourcesMock . createResponseFactory ( ) ; await routeHandler ( context , opensearchDashboardsRequest , responseFactory ) ; expect ( responseFactory . ok ) . toHaveBeenCalledWith ( { body : jsBody , headers : { 'content-type' : 'text/javascript' , 'x-opensearch-dashboards' : '42' , 'content-security-policy' : "script-src 'unsafe-eval' 'self'; worker-src blob: 'self'; style-src 'unsafe-inline' 'self'" , } , } ) ; } ) ; } ) ; } ) ; } ) ; } ) ;