use of com.enonic.xp.resource.ResourceKey in project xp by enonic.
the class WebAppHandlerTest method mockResource.
private Resource mockResource(final String uri, final byte[] data) {
final ResourceKey key = ResourceKey.from(uri);
final Resource resource = new MockResource(key, data, System.currentTimeMillis());
Mockito.when(this.resourceService.getResource(key)).thenReturn(resource);
return resource;
}
use of com.enonic.xp.resource.ResourceKey in project xp by enonic.
the class ExceptionRendererImplTest method render_custom_error_with_site_context.
@Test
void render_custom_error_with_site_context() {
this.request.getHeaders().put(HttpHeaders.ACCEPT, "text/html,text/*");
final Site site = newSite();
this.request.setSite(site);
final ResourceKey errorResource = ResourceKey.from(ApplicationKey.from("myapplication"), "site/error/error.js");
final ErrorHandlerScript errorHandlerScript = (portalError, handlerMethod) -> PortalResponse.create().body("Custom message page").status(HttpStatus.BAD_REQUEST).postProcess(false).build();
when(this.errorHandlerScriptFactory.errorScript(errorResource)).thenReturn(errorHandlerScript);
final Resource resource = mock(Resource.class);
when(resource.exists()).thenReturn(true);
when(this.resourceService.getResource(errorResource)).thenReturn(resource);
final RuntimeException cause = new RuntimeException("Custom message");
final PortalResponse res = this.renderer.render(this.request, new WebException(HttpStatus.BAD_REQUEST, cause));
assertEquals(HttpStatus.BAD_REQUEST, res.getStatus());
assertEquals("Custom message page", res.getBody().toString());
assertFalse(postProcessor.isExecuted());
}
use of com.enonic.xp.resource.ResourceKey in project xp by enonic.
the class ExceptionRendererImplTest method customErrorWithPostProcessing.
@Test
void customErrorWithPostProcessing() {
this.request.getHeaders().put(HttpHeaders.ACCEPT, "text/html,text/*");
final Site site = newSite();
this.request.setSite(site);
final ResourceKey errorResource = ResourceKey.from(ApplicationKey.from("myapplication"), "site/error/error.js");
final ErrorHandlerScript errorHandlerScript = (portalError, handlerMethod) -> PortalResponse.create().body("<h1>Custom message page</h1><!--#COMPONENT module:myPart -->").status(HttpStatus.BAD_REQUEST).postProcess(true).build();
when(this.errorHandlerScriptFactory.errorScript(errorResource)).thenReturn(errorHandlerScript);
final Resource resource = mock(Resource.class);
when(resource.exists()).thenReturn(true);
when(this.resourceService.getResource(errorResource)).thenReturn(resource);
final RuntimeException cause = new RuntimeException("Custom message");
final PortalResponse res = this.renderer.render(this.request, new WebException(HttpStatus.BAD_REQUEST, cause));
assertEquals(HttpStatus.BAD_REQUEST, res.getStatus());
assertEquals("<h1>Custom message page</h1><h3>My Part</h3>", res.getBody().toString());
assertTrue(postProcessor.isExecuted());
}
use of com.enonic.xp.resource.ResourceKey in project xp by enonic.
the class AssetHandlerTest method testCacheHeader.
@Test
public void testCacheHeader() throws Exception {
addResource("demo:/assets/css/main.css");
this.request.setEndpointPath("/_/asset/demo:0000000000000001/css/main.css");
final ResourceKey resourceKey = ResourceKey.from(ApplicationKey.from("demo"), "META-INF/MANIFEST.MF");
when(this.resourceService.getResource(resourceKey)).thenReturn(MockResource.empty(resourceKey, 1));
final WebResponse res = this.handler.handle(this.request, PortalResponse.create().build(), null);
assertNotNull(res);
assertEquals(HttpStatus.OK, res.getStatus());
assertEquals("public, max-age=31536000, immutable", res.getHeaders().get("Cache-Control"));
}
use of com.enonic.xp.resource.ResourceKey in project xp by enonic.
the class MappingHandlerTest method executeScript.
@Test
public void executeScript() throws Exception {
final ResourceKey controller = ResourceKey.from("demo:/services/test");
final ControllerMappingDescriptor mapping = ControllerMappingDescriptor.create().controller(controller).pattern(".*/content").build();
setupContentAndSite(mapping);
this.request.setBaseUri("/site");
this.request.setContentPath(ContentPath.from("/site/somesite/content"));
when(rendererDelegate.render(isA(ControllerMappingDescriptor.class), same(request))).thenReturn(PortalResponse.create().body("Ok").build());
final WebResponse response = this.handler.handle(this.request, PortalResponse.create().build(), null);
assertEquals(HttpStatus.OK, response.getStatus());
assertNotNull(this.request.getApplicationKey());
assertNotNull(this.request.getSite());
assertNotNull(this.request.getContent());
}
Aggregations