use of com.enonic.xp.web.WebException in project xp by enonic.
the class ImageHandlerTest method invalidQuality.
@Test
void invalidQuality() throws Exception {
setupContent();
when(this.mediaInfoService.getImageOrientation(any(ByteSource.class))).thenReturn(ImageOrientation.LeftBottom);
this.request.setEndpointPath("/_/image/123456/scale-100-100/image-name.jpg");
this.request.getParams().put("quality", "-1");
final WebException webException = assertThrows(WebException.class, () -> this.handler.handle(this.request, PortalResponse.create().build(), null));
assertEquals(HttpStatus.BAD_REQUEST, webException.getStatus());
}
use of com.enonic.xp.web.WebException 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.web.WebException 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.web.WebException in project xp by enonic.
the class ContentResolverTest method resolve_existing_but_needs_authentication_in_live_mode.
@Test
void resolve_existing_but_needs_authentication_in_live_mode() {
final Site site = newSite();
final PortalRequest request = new PortalRequest();
final ContentPath contentPath = ContentPath.from("/mysite/landing-page/non-existing");
request.setContentPath(contentPath);
when(this.contentService.getByPath(contentPath)).thenThrow(new ContentNotFoundException(contentPath, null));
when(this.contentService.contentExists(contentPath)).thenReturn(true);
when(this.contentService.findNearestSiteByPath(contentPath)).thenReturn(site);
final ContentResolverResult result = new ContentResolver(contentService).resolve(request);
assertNull(result.getContent());
assertSame(site, result.getNearestSite());
assertEquals("/landing-page/non-existing", result.getSiteRelativePath());
final WebException e = assertThrows(WebException.class, result::getContentOrElseThrow);
assertEquals(HttpStatus.UNAUTHORIZED, e.getStatus());
}
use of com.enonic.xp.web.WebException 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);
}
Aggregations