Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GuillotineApiWebHandler must support only POST, GET and OPTIONS #944 #945

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() );
}
}
Loading