Search in sources :

Example 11 with WebException

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());
}
Also used : WebException(com.enonic.xp.web.WebException) ByteSource(com.google.common.io.ByteSource) BaseHandlerTest(com.enonic.xp.web.handler.BaseHandlerTest) Test(org.junit.jupiter.api.Test)

Example 12 with WebException

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());
}
Also used : Site(com.enonic.xp.site.Site) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) ContentService(com.enonic.xp.content.ContentService) WebException(com.enonic.xp.web.WebException) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) ErrorHandlerScript(com.enonic.xp.portal.impl.error.ErrorHandlerScript) ResourceKey(com.enonic.xp.resource.ResourceKey) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Branch(com.enonic.xp.branch.Branch) ContentId(com.enonic.xp.content.ContentId) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpHeaders(com.google.common.net.HttpHeaders) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) PortalRequest(com.enonic.xp.portal.PortalRequest) PortalResponse(com.enonic.xp.portal.PortalResponse) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) PropertyTree(com.enonic.xp.data.PropertyTree) MediaType(com.google.common.net.MediaType) ResourceService(com.enonic.xp.resource.ResourceService) ContentPath(com.enonic.xp.content.ContentPath) SiteConfig(com.enonic.xp.site.SiteConfig) ErrorHandlerScriptFactory(com.enonic.xp.portal.impl.error.ErrorHandlerScriptFactory) Mockito.when(org.mockito.Mockito.when) RenderMode(com.enonic.xp.portal.RenderMode) ApplicationKey(com.enonic.xp.app.ApplicationKey) Test(org.junit.jupiter.api.Test) Site(com.enonic.xp.site.Site) RunMode(com.enonic.xp.server.RunMode) Resource(com.enonic.xp.resource.Resource) SiteConfigs(com.enonic.xp.site.SiteConfigs) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) HttpStatus(com.enonic.xp.web.HttpStatus) AdditionalMatchers.not(org.mockito.AdditionalMatchers.not) Mockito.mock(org.mockito.Mockito.mock) PortalResponse(com.enonic.xp.portal.PortalResponse) WebException(com.enonic.xp.web.WebException) Resource(com.enonic.xp.resource.Resource) ErrorHandlerScript(com.enonic.xp.portal.impl.error.ErrorHandlerScript) ResourceKey(com.enonic.xp.resource.ResourceKey) Test(org.junit.jupiter.api.Test)

Example 13 with WebException

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()));
}
Also used : PortalResponse(com.enonic.xp.portal.PortalResponse) WebException(com.enonic.xp.web.WebException) Test(org.junit.jupiter.api.Test)

Example 14 with WebException

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());
}
Also used : Site(com.enonic.xp.site.Site) WebException(com.enonic.xp.web.WebException) ContentNotFoundException(com.enonic.xp.content.ContentNotFoundException) ContentPath(com.enonic.xp.content.ContentPath) PortalRequest(com.enonic.xp.portal.PortalRequest) Test(org.junit.jupiter.api.Test)

Example 15 with WebException

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);
}
Also used : PortalResponse(com.enonic.xp.portal.PortalResponse) WebException(com.enonic.xp.web.WebException) Test(org.junit.jupiter.api.Test)

Aggregations

WebException (com.enonic.xp.web.WebException)47 Test (org.junit.jupiter.api.Test)39 PortalResponse (com.enonic.xp.portal.PortalResponse)14 ContentPath (com.enonic.xp.content.ContentPath)12 Site (com.enonic.xp.site.Site)10 PortalRequest (com.enonic.xp.portal.PortalRequest)8 BaseHandlerTest (com.enonic.xp.web.handler.BaseHandlerTest)7 ContentNotFoundException (com.enonic.xp.content.ContentNotFoundException)6 HttpStatus (com.enonic.xp.web.HttpStatus)5 MediaType (com.google.common.net.MediaType)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 ApplicationKey (com.enonic.xp.app.ApplicationKey)4 Branch (com.enonic.xp.branch.Branch)4 Content (com.enonic.xp.content.Content)4 ContentId (com.enonic.xp.content.ContentId)4 ContentService (com.enonic.xp.content.ContentService)4 PropertyTree (com.enonic.xp.data.PropertyTree)4 RenderMode (com.enonic.xp.portal.RenderMode)4 ErrorHandlerScript (com.enonic.xp.portal.impl.error.ErrorHandlerScript)4 ErrorHandlerScriptFactory (com.enonic.xp.portal.impl.error.ErrorHandlerScriptFactory)4