use of com.enonic.xp.portal.PortalResponse in project xp by enonic.
the class WebAppHandlerTest method handle_executeController.
@Test
public void handle_executeController() throws Exception {
mockResource("myapp:/assets/a.txt", null);
this.request.setApplicationKey(ApplicationKey.from("myapp"));
this.request.setBaseUri("/webapp/myapp");
this.request.setRawPath("/webapp/myapp/a.txt");
final ControllerScript script = Mockito.mock(ControllerScript.class);
Mockito.when(this.controllerScriptFactory.fromScript(ResourceKey.from("myapp:/webapp/webapp.js"))).thenReturn(script);
final PortalResponse response = PortalResponse.create().build();
Mockito.when(script.execute(Mockito.any())).thenReturn(response);
assertSame(response, this.handler.doHandle(this.request, null, this.chain));
assertEquals("/webapp/myapp", this.request.getContextPath());
}
use of com.enonic.xp.portal.PortalResponse 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.portal.PortalResponse in project xp by enonic.
the class ExceptionRendererImplTest method render_html.
@Test
void render_html() {
this.request.getHeaders().put(HttpHeaders.ACCEPT, "text/html,text/*");
final PortalResponse res = this.renderer.render(this.request, new WebException(HttpStatus.NOT_FOUND, "Custom message"));
assertEquals(HttpStatus.NOT_FOUND, res.getStatus());
assertEquals(MediaType.HTML_UTF_8.withoutParameters(), res.getContentType());
final String body = res.getBody().toString();
assertTrue(body.contains("404 Not Found"));
assertTrue(body.contains("Custom message"));
// Should not show exception
assertTrue(body.contains(ExceptionRendererImplTest.class.getName()));
}
use of com.enonic.xp.portal.PortalResponse in project xp by enonic.
the class ExceptionRendererImplTest method render_json_withCause.
@Test
void render_json_withCause() {
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(MediaType.JSON_UTF_8.withoutParameters(), res.getContentType());
MediaType.create("", "");
final String body = res.getBody().toString();
assertEquals("{\"status\":400,\"message\":\"Custom message (java.lang.RuntimeException)\"}", body);
}
use of com.enonic.xp.portal.PortalResponse 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());
}
Aggregations