Skip to content

Commit

Permalink
GuillotineApiWebHandler must support only POST, GET and OPTIONS #944
Browse files Browse the repository at this point in the history
  • Loading branch information
anatol-sialitski committed Oct 21, 2024
1 parent fbfc434 commit c0ae37d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.enonic.app.guillotine.handler;

import java.util.EnumSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -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 )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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() );
}
}

0 comments on commit c0ae37d

Please sign in to comment.