Search in sources :

Example 61 with API

use of org.apache.synapse.api.API in project wso2-synapse by wso2.

the class APIDispatcherTest method testHostBasedAPIDispatch.

public void testHostBasedAPIDispatch() throws Exception {
    API api = new API(TEST_API, "/test");
    api.setHost("synapse.apache.org");
    SynapseConfiguration synapseConfig = new SynapseConfiguration();
    synapseConfig.addAPI(TEST_API, api);
    RESTRequestHandler handler = new RESTRequestHandler();
    // Messages that don't have the proper host set should not be dispatched
    MessageContext synCtx = getMessageContext(synapseConfig, false, "/test", "GET");
    handler.process(synCtx);
    assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
    // Messages with the correct host should be dispatched
    synCtx = getMessageContext(synapseConfig, false, "/test/", "GET");
    addHttpHeader(HTTP.TARGET_HOST, "synapse.apache.org", synCtx);
    handler.process(synCtx);
    assertEquals(TEST_API, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
    // API should be able to infer the default HTTP port
    api.setPort(80);
    handler.process(synCtx);
    assertEquals(TEST_API, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
    // Messages with an incorrect port number should not be dispatched
    synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar?a=5", "GET");
    addHttpHeader(HTTP.TARGET_HOST, "synapse.apache.org:8280", synCtx);
    handler.process(synCtx);
    assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
    // Messages with the correct port number should be dispatched
    api.setPort(8280);
    handler.process(synCtx);
    assertEquals(TEST_API, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
    api.setPort(443);
    synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar?a=5", "GET");
    addHttpHeader(HTTP.TARGET_HOST, "synapse.apache.org", synCtx);
    handler.process(synCtx);
    assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
    // API should accurately infer the default HTTPS port
    synCtx = getMessageContext(synapseConfig, true, "/test/foo/bar?a=5", "GET");
    addHttpHeader(HTTP.TARGET_HOST, "synapse.apache.org", synCtx);
    handler.process(synCtx);
    assertEquals(TEST_API, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
}
Also used : API(org.apache.synapse.api.API) MessageContext(org.apache.synapse.MessageContext) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration)

Example 62 with API

use of org.apache.synapse.api.API in project wso2-synapse by wso2.

the class URLMappingBasedDispatcherTest method testExactMatchBasedDispatch.

public void testExactMatchBasedDispatch() 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?a=5", "GET");
    handler.process(synCtx);
    assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
    synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar?a=5&name=test", "GET");
    handler.process(synCtx);
    assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
    synCtx = getMessageContext(synapseConfig, false, "/test/foo", "GET");
    handler.process(synCtx);
    assertNull(synCtx.getProperty(PROP_NAME));
    synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar/index.html", "GET");
    handler.process(synCtx);
    assertNull(synCtx.getProperty(PROP_NAME));
}
Also used : URLMappingHelper(org.apache.synapse.api.dispatch.URLMappingHelper) Resource(org.apache.synapse.api.Resource) API(org.apache.synapse.api.API) MessageContext(org.apache.synapse.MessageContext) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration)

Example 63 with API

use of org.apache.synapse.api.API in project wso2-synapse by wso2.

the class URLMappingBasedDispatcherTest method testMultipleResourceDispatch.

public void testMultipleResourceDispatch() throws Exception {
    API api = new API("TestAPI", "/test");
    Resource resource1 = new Resource();
    resource1.setDispatcherHelper(new URLMappingHelper("/foo/*"));
    resource1.setInSequence(getTestSequence(PROP_NAME, "resource1"));
    Resource resource2 = new Resource();
    resource2.setDispatcherHelper(new URLMappingHelper("/foo/bar/*"));
    resource2.setInSequence(getTestSequence(PROP_NAME, "resource2"));
    Resource resource3 = new Resource();
    resource3.setDispatcherHelper(new URLMappingHelper("*.jsp"));
    resource3.setInSequence(getTestSequence(PROP_NAME, "resource3"));
    api.addResource(resource1);
    api.addResource(resource2);
    api.addResource(resource3);
    SynapseConfiguration synapseConfig = new SynapseConfiguration();
    synapseConfig.addAPI(api.getName(), api);
    RESTRequestHandler handler = new RESTRequestHandler();
    MessageContext synCtx = getMessageContext(synapseConfig, false, "/test/foo/", "GET");
    handler.process(synCtx);
    assertEquals("resource1", synCtx.getProperty(PROP_NAME));
    synCtx = getMessageContext(synapseConfig, false, "/test/foo/index.html?a=5", "GET");
    handler.process(synCtx);
    assertEquals("resource1", synCtx.getProperty(PROP_NAME));
    synCtx = getMessageContext(synapseConfig, false, "/test/foo/bars", "GET");
    handler.process(synCtx);
    assertEquals("resource1", synCtx.getProperty(PROP_NAME));
    synCtx = getMessageContext(synapseConfig, false, "/test/foo/index.jsp", "GET");
    handler.process(synCtx);
    assertEquals("resource1", synCtx.getProperty(PROP_NAME));
    synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar/", "GET");
    handler.process(synCtx);
    assertEquals("resource2", synCtx.getProperty(PROP_NAME));
    synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar/index.html?a=5", "GET");
    handler.process(synCtx);
    assertEquals("resource2", synCtx.getProperty(PROP_NAME));
    synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar/hello", "GET");
    handler.process(synCtx);
    assertEquals("resource2", synCtx.getProperty(PROP_NAME));
    synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar/index.jsp", "GET");
    handler.process(synCtx);
    assertEquals("resource2", synCtx.getProperty(PROP_NAME));
    synCtx = getMessageContext(synapseConfig, false, "/test/index.jsp", "GET");
    handler.process(synCtx);
    assertEquals("resource3", synCtx.getProperty(PROP_NAME));
    synCtx = getMessageContext(synapseConfig, false, "/test/hello/index.jsp?a=5", "GET");
    handler.process(synCtx);
    assertEquals("resource3", synCtx.getProperty(PROP_NAME));
    synCtx = getMessageContext(synapseConfig, false, "/test/foolish/bars/index.jsp", "GET");
    handler.process(synCtx);
    assertEquals("resource3", synCtx.getProperty(PROP_NAME));
    synCtx = getMessageContext(synapseConfig, false, "/test/foolish/index.html", "GET");
    handler.process(synCtx);
    assertNull(synCtx.getProperty(PROP_NAME));
}
Also used : URLMappingHelper(org.apache.synapse.api.dispatch.URLMappingHelper) Resource(org.apache.synapse.api.Resource) API(org.apache.synapse.api.API) MessageContext(org.apache.synapse.MessageContext) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration)

Example 64 with API

use of org.apache.synapse.api.API in project wso2-synapse by wso2.

the class URLMappingBasedDispatcherTest method testResponseDispatch.

public void testResponseDispatch() throws Exception {
    API api = new API("TestAPI", "/test");
    Resource resource = new Resource();
    resource.setDispatcherHelper(new URLMappingHelper("/foo/bar/*"));
    resource.setOutSequence(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");
    synCtx.setProperty(RESTConstants.SYNAPSE_REST_API, api.getName());
    synCtx.setProperty(SynapseConstants.ARTIFACT_NAME, SynapseConstants.FAIL_SAFE_MODE_API + api.getName());
    synCtx.setResponse(true);
    handler.process(synCtx);
    assertNull(synCtx.getProperty(PROP_NAME));
    synCtx.setProperty(RESTConstants.SYNAPSE_RESOURCE, resource.getName());
    handler.process(synCtx);
    assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
}
Also used : URLMappingHelper(org.apache.synapse.api.dispatch.URLMappingHelper) Resource(org.apache.synapse.api.Resource) API(org.apache.synapse.api.API) MessageContext(org.apache.synapse.MessageContext) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration)

Aggregations

API (org.apache.synapse.api.API)64 MessageContext (org.apache.synapse.MessageContext)33 SynapseConfiguration (org.apache.synapse.config.SynapseConfiguration)33 Resource (org.apache.synapse.api.Resource)23 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)14 OMElement (org.apache.axiom.om.OMElement)10 URLMappingHelper (org.apache.synapse.api.dispatch.URLMappingHelper)7 Test (org.junit.Test)6 URITemplateHelper (org.apache.synapse.api.dispatch.URITemplateHelper)5 Cache (javax.cache.Cache)4 DeploymentException (org.apache.axis2.deployment.DeploymentException)4 SynapseException (org.apache.synapse.SynapseException)4 ContextVersionStrategy (org.apache.synapse.api.version.ContextVersionStrategy)4 ProxyService (org.apache.synapse.core.axis2.ProxyService)4 InboundEndpoint (org.apache.synapse.inbound.InboundEndpoint)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 APIManagerConfiguration (org.wso2.carbon.apimgt.impl.APIManagerConfiguration)4 APIManagerConfigurationService (org.wso2.carbon.apimgt.impl.APIManagerConfigurationService)4 CacheProvider (org.wso2.carbon.apimgt.impl.caching.CacheProvider)4 ArrayList (java.util.ArrayList)3