-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1a48ac1
commit 385446d
Showing
17 changed files
with
748 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
.../admin-impl/src/test/java/com/enonic/xp/admin/impl/portal/AdminToolPortalHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package com.enonic.xp.admin.impl.portal; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import com.google.common.net.HttpHeaders; | ||
|
||
import com.enonic.xp.web.HttpMethod; | ||
import com.enonic.xp.web.HttpStatus; | ||
import com.enonic.xp.web.WebRequest; | ||
import com.enonic.xp.web.WebResponse; | ||
import com.enonic.xp.web.exception.ExceptionMapper; | ||
import com.enonic.xp.web.exception.ExceptionRenderer; | ||
import com.enonic.xp.web.handler.WebHandlerChain; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
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; | ||
|
||
public class AdminToolPortalHandlerTest | ||
{ | ||
private WebRequest request; | ||
|
||
private WebResponse response; | ||
|
||
private AdminToolPortalHandler handler; | ||
|
||
private WebHandlerChain chain; | ||
|
||
@BeforeEach | ||
public void setup() | ||
throws Exception | ||
{ | ||
final ExceptionMapper exceptionMapper = mock( ExceptionMapper.class ); | ||
final ExceptionRenderer exceptionRenderer = mock( ExceptionRenderer.class ); | ||
|
||
this.handler = new AdminToolPortalHandler(); | ||
this.handler.setWebExceptionMapper( exceptionMapper ); | ||
this.handler.setExceptionRenderer( exceptionRenderer ); | ||
|
||
final HttpServletRequest rawRequest = mock( HttpServletRequest.class ); | ||
|
||
this.request = new WebRequest(); | ||
this.request.setMethod( HttpMethod.GET ); | ||
this.request.setRawRequest( rawRequest ); | ||
|
||
this.response = WebResponse.create().build(); | ||
|
||
this.chain = mock( WebHandlerChain.class ); | ||
when( chain.handle( any( WebRequest.class ), any( WebResponse.class ) ) ).thenReturn( this.response ); | ||
} | ||
|
||
@Test | ||
void testCanHandle() | ||
{ | ||
this.request.setRawPath( "/admin" ); | ||
assertTrue( this.handler.canHandle( request ) ); | ||
|
||
this.request.setRawPath( "/admin/app/toolname" ); | ||
assertTrue( this.handler.canHandle( request ) ); | ||
|
||
this.request.setRawPath( "/path" ); | ||
assertFalse( this.handler.canHandle( request ) ); | ||
} | ||
|
||
@Test | ||
void testDoHandleRedirect() | ||
{ | ||
this.request.setRawPath( "/admin/" ); | ||
|
||
final WebResponse webResponse = this.handler.doHandle( this.request, response, null ); | ||
assertEquals( HttpStatus.TEMPORARY_REDIRECT, webResponse.getStatus() ); | ||
assertEquals( "/admin", webResponse.getHeaders().get( HttpHeaders.LOCATION ) ); | ||
} | ||
|
||
@Test | ||
void testDoHandleOnSlashAdmin() | ||
{ | ||
this.request.setRawPath( "/admin" ); | ||
|
||
final WebResponse webResponse = this.handler.doHandle( this.request, response, chain ); | ||
assertEquals( this.response, webResponse ); | ||
} | ||
|
||
@Test | ||
void testDoHandle() | ||
{ | ||
this.request.setRawPath( "/admin/app/toolname" ); | ||
|
||
final WebResponse webResponse = this.handler.doHandle( this.request, response, chain ); | ||
assertEquals( this.response, webResponse ); | ||
} | ||
|
||
@Test | ||
void testDoHandleDescriptorNotFoundFallbackToSlashAdmin() | ||
{ | ||
this.request.setRawPath( "/admin/app" ); | ||
|
||
final WebResponse webResponse = this.handler.doHandle( this.request, response, chain ); | ||
assertEquals( this.response, webResponse ); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
modules/lib/lib-portal/src/main/java/com/enonic/xp/lib/portal/url/ApiUrlHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.enonic.xp.lib.portal.url; | ||
|
||
import java.util.Set; | ||
|
||
import com.google.common.collect.Multimap; | ||
|
||
import com.enonic.xp.portal.url.ApiUrlParams; | ||
|
||
public final class ApiUrlHandler | ||
extends AbstractUrlHandler | ||
{ | ||
private static final Set<String> VALID_URL_PROPERTY_KEYS = Set.of( "application", "api", "type", "params" ); | ||
|
||
@Override | ||
protected String buildUrl( final Multimap<String, String> map ) | ||
{ | ||
final ApiUrlParams params = new ApiUrlParams().portalRequest( this.request ).setAsMap( map ); | ||
return this.urlService.apiUrl( params ); | ||
} | ||
|
||
@Override | ||
protected boolean isValidParam( final String param ) | ||
{ | ||
return VALID_URL_PROPERTY_KEYS.contains( param ); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
modules/lib/lib-portal/src/main/resources/lib/xp/examples/portal/apiUrl.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const portalLib = require('/lib/xp/portal'); | ||
const assert = require('/lib/xp/testing'); | ||
|
||
// BEGIN | ||
const url = portalLib.apiUrl({ | ||
application: 'com.enonic.app.myapp', | ||
api: 'myapi', | ||
params: { | ||
a: 1, | ||
b: 2 | ||
} | ||
}); | ||
|
||
const unnamedApiUrl = portalLib.apiUrl({ | ||
application: 'com.enonic.app.myapp', | ||
}); | ||
|
||
// END | ||
|
||
assert.assertEquals('ApiUrlParams{type=server, params={a=[1], b=[2]}, api=myapi, application=com.enonic.app.myapp}', url); | ||
assert.assertEquals('ApiUrlParams{type=server, params={}, application=com.enonic.app.myapp}', unnamedApiUrl); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
modules/portal/portal-api/src/main/java/com/enonic/xp/portal/url/ApiUrlParams.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.enonic.xp.portal.url; | ||
|
||
import java.util.Objects; | ||
|
||
import com.google.common.base.MoreObjects; | ||
import com.google.common.base.Strings; | ||
import com.google.common.collect.Multimap; | ||
|
||
import com.enonic.xp.annotation.PublicApi; | ||
|
||
@PublicApi | ||
public final class ApiUrlParams | ||
extends AbstractUrlParams<ApiUrlParams> | ||
{ | ||
private String application; | ||
|
||
private String api; | ||
|
||
public String getApplication() | ||
{ | ||
return application; | ||
} | ||
|
||
public String getApi() | ||
{ | ||
return api; | ||
} | ||
|
||
public ApiUrlParams application( final String value ) | ||
{ | ||
this.application = Objects.requireNonNull( value ); | ||
return this; | ||
} | ||
|
||
public ApiUrlParams api( final String value ) | ||
{ | ||
this.api = Strings.emptyToNull( value ); | ||
return this; | ||
} | ||
|
||
@Override | ||
public ApiUrlParams setAsMap( final Multimap<String, String> map ) | ||
{ | ||
super.setAsMap( map ); | ||
api( singleValue( map, "_api" ) ); | ||
application( singleValue( map, "_application" ) ); | ||
getParams().putAll( map ); | ||
return this; | ||
} | ||
|
||
@Override | ||
protected void buildToString( final MoreObjects.ToStringHelper helper ) | ||
{ | ||
super.buildToString( helper ); | ||
helper.add( "api", this.api ); | ||
helper.add( "application", this.application ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.