use of org.apache.synapse.api.dispatch.URLMappingHelper in project wso2-synapse by wso2.
the class URLMappingBasedDispatcherTest method testDefaultResourceDispatch.
public void testDefaultResourceDispatch() throws Exception {
API api = new API("TestAPI", "/test");
Resource resource = new Resource();
resource.setDispatcherHelper(new URLMappingHelper("/"));
resource.setInSequence(getTestSequence(PROP_NAME, PROP_VALUE));
api.addResource(resource);
SynapseConfiguration synapseConfig = new SynapseConfiguration();
synapseConfig.addAPI(api.getName(), api);
RESTRequestHandler handler = new RESTRequestHandler();
MessageContext synCtx = getMessageContext(synapseConfig, false, "/test", "GET");
handler.process(synCtx);
assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
}
use of org.apache.synapse.api.dispatch.URLMappingHelper in project wso2-synapse by wso2.
the class URLMappingBasedDispatcherTest method testPathBasedDispatch.
public void testPathBasedDispatch() throws Exception {
API api = new API("TestAPI", "/test");
Resource resource = new Resource();
resource.setDispatcherHelper(new URLMappingHelper("/foo/bar/*"));
resource.setInSequence(getTestSequence(PROP_NAME, PROP_VALUE));
api.addResource(resource);
SynapseConfiguration synapseConfig = new SynapseConfiguration();
synapseConfig.addAPI(api.getName(), api);
RESTRequestHandler handler = new RESTRequestHandler();
MessageContext synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar", "GET");
handler.process(synCtx);
assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar/", "GET");
handler.process(synCtx);
assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar/index.jsp", "GET");
handler.process(synCtx);
assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar?a=b", "GET");
handler.process(synCtx);
assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar/baz?a=b", "GET");
handler.process(synCtx);
assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar/?a=b", "GET");
handler.process(synCtx);
assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
synCtx = getMessageContext(synapseConfig, false, "/test/foo/bars?a=b", "GET");
handler.process(synCtx);
assertNull(synCtx.getProperty(PROP_NAME));
synCtx = getMessageContext(synapseConfig, false, "/test/foo/", "GET");
handler.process(synCtx);
assertNull(synCtx.getProperty(PROP_NAME));
}
use of org.apache.synapse.api.dispatch.URLMappingHelper in project wso2-synapse by wso2.
the class URLMappingBasedDispatcherTest method testExtensionBasedDispatch.
public void testExtensionBasedDispatch() throws Exception {
API api = new API("TestAPI", "/test");
Resource resource = new Resource();
resource.setDispatcherHelper(new URLMappingHelper("*.jsp"));
resource.setInSequence(getTestSequence(PROP_NAME, PROP_VALUE));
api.addResource(resource);
SynapseConfiguration synapseConfig = new SynapseConfiguration();
synapseConfig.addAPI(api.getName(), api);
RESTRequestHandler handler = new RESTRequestHandler();
MessageContext synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar/index.jsp", "GET");
handler.process(synCtx);
assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
synCtx = getMessageContext(synapseConfig, false, "/test/welcome.jsp", "GET");
handler.process(synCtx);
assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
synCtx = getMessageContext(synapseConfig, false, "/test/index.jsp?a=5&b=10", "GET");
handler.process(synCtx);
assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
synCtx = getMessageContext(synapseConfig, false, "/test/foo/index.html", "GET");
handler.process(synCtx);
assertNull(synCtx.getProperty(PROP_NAME));
}
use of org.apache.synapse.api.dispatch.URLMappingHelper in project wso2-synapse by wso2.
the class ResourceFactory method configureURLMappings.
private static void configureURLMappings(Resource resource, OMElement resourceElt) {
OMAttribute urlMappingAtt = resourceElt.getAttribute(new QName("url-mapping"));
OMAttribute uriTemplateAtt = resourceElt.getAttribute(new QName("uri-template"));
if (urlMappingAtt != null && !"".equals(urlMappingAtt.getAttributeValue())) {
resource.setDispatcherHelper(new URLMappingHelper(urlMappingAtt.getAttributeValue()));
} else if (uriTemplateAtt != null && !"".equals(uriTemplateAtt.getAttributeValue())) {
resource.setDispatcherHelper(new URITemplateHelper(uriTemplateAtt.getAttributeValue()));
}
}
use of org.apache.synapse.api.dispatch.URLMappingHelper in project carbon-apimgt by wso2.
the class CORSRequestHandlerTestCase method testHandleRequest.
@Test
public void testHandleRequest() throws Exception {
PowerMockito.mockStatic(Utils.class);
SynapseEnvironment synapseEnvironment = Mockito.mock(SynapseEnvironment.class);
MessageContext messageContext = Mockito.mock(Axis2MessageContext.class);
org.apache.axis2.context.MessageContext axis2MsgCntxt = Mockito.mock(org.apache.axis2.context.MessageContext.class);
Mockito.when(axis2MsgCntxt.getProperty(Constants.Configuration.HTTP_METHOD)).thenReturn("GET");
Map<String, String> headers = new HashMap<>();
Mockito.when(axis2MsgCntxt.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS)).thenReturn(headers);
Mockito.when(((Axis2MessageContext) messageContext).getAxis2MessageContext()).thenReturn(axis2MsgCntxt);
Mockito.when(messageContext.getProperty(RESTConstants.REST_API_CONTEXT)).thenReturn("/ishara");
Mockito.when(messageContext.getProperty(RESTConstants.SYNAPSE_REST_API)).thenReturn("admin-AT-wso2.com--PizzaShackAPI");
Mockito.when(messageContext.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION)).thenReturn("1.0");
SynapseConfiguration synapseConfiguration = Mockito.mock(SynapseConfiguration.class);
API api = Mockito.mock(API.class);
Mockito.when(api.getContext()).thenReturn("/ishara");
Resource resource = Mockito.mock(Resource.class);
Resource[] resources = new Resource[1];
resources[0] = resource;
Mockito.when(api.getResources()).thenReturn(resources);
VersionStrategy versionStrategy = Mockito.mock(VersionStrategy.class);
Mockito.when(versionStrategy.getVersionType()).thenReturn("url");
Mockito.when(versionStrategy.getVersion()).thenReturn("1.0");
Mockito.when(api.getVersionStrategy()).thenReturn(versionStrategy);
Mockito.when(Utils.getSelectedAPI(messageContext)).thenReturn(api);
Mockito.when(synapseConfiguration.getAPI("admin-AT-wso2.com--PizzaShackAPI")).thenReturn(api);
Mockito.when(messageContext.getConfiguration()).thenReturn(synapseConfiguration);
CORSRequestHandler corsRequestHandler = createCORSRequestHandler();
corsRequestHandler.init(synapseEnvironment);
// test ResourceNotFound path
Assert.assertFalse(corsRequestHandler.handleRequest(messageContext));
// test for resource that is found
String[] methods = { "GET", "POST" };
Mockito.when(resource.getMethods()).thenReturn(methods);
DispatcherHelper dispatcherHelper = new URLMappingHelper("/ishara/1.0") {
@Override
public String getString() {
return "/xx";
}
};
Mockito.when(resource.getDispatcherHelper()).thenReturn(dispatcherHelper);
Mockito.when(messageContext.getProperty("REST_SUB_REQUEST_PATH")).thenReturn("/ishara/1.0");
TreeMap transportHeaders = new TreeMap();
transportHeaders.put("Origin", "");
Mockito.when(axis2MsgCntxt.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS)).thenReturn(transportHeaders);
Assert.assertTrue(corsRequestHandler.handleRequest(messageContext));
DispatcherHelper dispatcherHelper1 = new URLMappingHelper("/ishara/1.0") {
@Override
public String getString() {
return "/xx";
}
};
Mockito.when(resource.getDispatcherHelper()).thenReturn(dispatcherHelper1);
Mockito.when(axis2MsgCntxt.getProperty(Constants.Configuration.HTTP_METHOD)).thenReturn("OPTIONS");
// test for OPTIONS request when OPTIONS is not supported by SupportedHTTPVerbs
Assert.assertFalse(corsRequestHandler.handleRequest(messageContext));
// test for OPTIONS request when OPTIONS is supported by SupportedHTTPVerbs
String[] methodsWithOptions = { "GET", "POST", "OPTIONS" };
Mockito.when(resource.getMethods()).thenReturn(methodsWithOptions);
Assert.assertTrue(corsRequestHandler.handleRequest(messageContext));
}
Aggregations