diff --git a/src/main/java/com/enonic/app/guillotine/handler/GuillotineApiWebHandler.java b/src/main/java/com/enonic/app/guillotine/handler/GuillotineApiWebHandler.java index 8b97dc9c..f5e849e1 100644 --- a/src/main/java/com/enonic/app/guillotine/handler/GuillotineApiWebHandler.java +++ b/src/main/java/com/enonic/app/guillotine/handler/GuillotineApiWebHandler.java @@ -1,5 +1,6 @@ package com.enonic.app.guillotine.handler; +import java.util.EnumSet; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -34,21 +35,18 @@ public class GuillotineApiWebHandler private final ControllerScriptFactory controllerScriptFactory; - @Activate - public GuillotineApiWebHandler( final @Reference ControllerScriptFactory controllerScriptFactory ) - { - super( -49 ); - this.controllerScriptFactory = controllerScriptFactory; - } + @Activate + public GuillotineApiWebHandler( final @Reference ControllerScriptFactory controllerScriptFactory ) + { + super( -49, EnumSet.of( HttpMethod.POST, HttpMethod.GET, HttpMethod.OPTIONS ) ); + this.controllerScriptFactory = controllerScriptFactory; + } - @Override - protected boolean canHandle( final WebRequest webRequest ) - { - final String path = webRequest.getRawPath(); - final Matcher matcher = URL_PATTERN.matcher( path ); - return ( webRequest.getMethod() == HttpMethod.POST || ( webRequest.getMethod() == HttpMethod.GET && webRequest.isWebSocket() ) ) && - matcher.matches(); - } + @Override + protected boolean canHandle( final WebRequest webRequest ) + { + return URL_PATTERN.matcher( webRequest.getRawPath() ).matches(); + } @Override protected WebResponse doHandle( final WebRequest webRequest, final WebResponse webResponse, final WebHandlerChain webHandlerChain ) diff --git a/src/test/java/com/enonic/app/guillotine/handler/GuillotineApiWebHandlerTest.java b/src/test/java/com/enonic/app/guillotine/handler/GuillotineApiWebHandlerTest.java index 62840ad5..d793fe1b 100644 --- a/src/test/java/com/enonic/app/guillotine/handler/GuillotineApiWebHandlerTest.java +++ b/src/test/java/com/enonic/app/guillotine/handler/GuillotineApiWebHandlerTest.java @@ -2,12 +2,22 @@ import org.junit.jupiter.api.Test; +import com.enonic.xp.portal.PortalRequest; +import com.enonic.xp.portal.PortalResponse; +import com.enonic.xp.portal.controller.ControllerScript; import com.enonic.xp.portal.controller.ControllerScriptFactory; +import com.enonic.xp.resource.ResourceKey; import com.enonic.xp.web.HttpMethod; +import com.enonic.xp.web.HttpStatus; +import com.enonic.xp.web.WebException; import com.enonic.xp.web.WebRequest; +import com.enonic.xp.web.WebResponse; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -45,4 +55,36 @@ public void testCanHandle() when( webRequest.getRawPath() ).thenReturn( "/something" ); assertFalse( instance.canHandle( webRequest ) ); } + + @Test + void testHandle() + throws Exception + { + ControllerScript controllerScript = mock( ControllerScript.class ); + when( controllerScript.execute( any( PortalRequest.class ) ) ).thenReturn( PortalResponse.create().build() ); + + ControllerScriptFactory scriptFactory = mock( ControllerScriptFactory.class ); + when( scriptFactory.fromScript( any( ResourceKey.class ) ) ).thenReturn( controllerScript ); + + GuillotineApiWebHandler instance = new GuillotineApiWebHandler( scriptFactory ); + + WebRequest webRequest = mock( WebRequest.class ); + when( webRequest.getRawPath() ).thenReturn( "/site/hmdb/draft" ); + + when( webRequest.getMethod() ).thenReturn( HttpMethod.OPTIONS ); + WebResponse webResponse = instance.handle( webRequest, null, null ); + assertTrue( webResponse.getStatus().is2xxSuccessful() ); + + when( webRequest.getMethod() ).thenReturn( HttpMethod.POST ); + webResponse = instance.handle( webRequest, null, null ); + assertTrue( webResponse.getStatus().is2xxSuccessful() ); + + when( webRequest.getMethod() ).thenReturn( HttpMethod.GET ); + webResponse = instance.handle( webRequest, null, null ); + assertTrue( webResponse.getStatus().is2xxSuccessful() ); + + when( webRequest.getMethod() ).thenReturn( HttpMethod.HEAD ); + WebException ex = assertThrows( WebException.class, () -> instance.handle( webRequest, null, null ) ); + assertEquals( HttpStatus.METHOD_NOT_ALLOWED, ex.getStatus() ); + } }