Search in sources :

Example 21 with API

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

the class APIDispatcherTest method testBasicAPIDispatch.

public void testBasicAPIDispatch() throws Exception {
    API api = new API(TEST_API, "/test");
    SynapseConfiguration synapseConfig = new SynapseConfiguration();
    synapseConfig.addAPI(TEST_API, api);
    RESTRequestHandler handler = new RESTRequestHandler();
    // Messages with '/test' context should be dispatched
    MessageContext synCtx = getMessageContext(synapseConfig, false, "/test", "GET");
    handler.process(synCtx);
    assertEquals(TEST_API, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
    synCtx = getMessageContext(synapseConfig, false, "/test/", "GET");
    handler.process(synCtx);
    assertEquals(TEST_API, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
    synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar?a=5", "GET");
    handler.process(synCtx);
    assertEquals(TEST_API, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
    synCtx = getMessageContext(synapseConfig, false, "/test?a=5", "GET");
    handler.process(synCtx);
    assertEquals(TEST_API, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
    // Messages WITHOUT the '/test' context should NOT be dispatched
    synCtx = getMessageContext(synapseConfig, false, "/foo/test/bar?a=5", "GET");
    handler.process(synCtx);
    assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
    synCtx = getMessageContext(synapseConfig, false, "/test1/bar?a=5", "GET");
    handler.process(synCtx);
    assertNull(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 22 with API

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

the class APIDispatcherTest method testResponseDispatch.

public void testResponseDispatch() throws Exception {
    API api = new API(TEST_API, "/test");
    SynapseConfiguration synapseConfig = new SynapseConfiguration();
    synapseConfig.addAPI(TEST_API, api);
    RESTRequestHandler handler = new RESTRequestHandler();
    // Messages with '/test' context should ne dispatched
    MessageContext synCtx = getMessageContext(synapseConfig, false, "/test", "GET");
    synCtx.setResponse(true);
    assertFalse(handler.process(synCtx));
    synCtx.setProperty(RESTConstants.SYNAPSE_REST_API, TEST_API);
    synCtx.setProperty(SynapseConstants.ARTIFACT_NAME, SynapseConstants.FAIL_SAFE_MODE_API + TEST_API);
    assertTrue(handler.process(synCtx));
}
Also used : API(org.apache.synapse.api.API) MessageContext(org.apache.synapse.MessageContext) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration)

Example 23 with API

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

the class APIDispatcherTest method testAPIContextVersionBasedDispatchEndingWithVersion.

public void testAPIContextVersionBasedDispatchEndingWithVersion() throws Exception {
    API api = new API(TEST_API, "/test/{version}");
    api.setVersionStrategy(new ContextVersionStrategy(api, TEST_API_VERSION, null));
    SynapseConfiguration synapseConfig = new SynapseConfiguration();
    synapseConfig.addAPI(api.getName(), api);
    RESTRequestHandler handler = new RESTRequestHandler();
    // Messages with '/test' context should NOT be dispatched
    MessageContext synCtx = getMessageContext(synapseConfig, false, "/test/", "GET");
    handler.process(synCtx);
    assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
    assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
    synCtx = getMessageContext(synapseConfig, false, "/test/1.0.0", "GET");
    handler.process(synCtx);
    assertEquals(api.getName(), synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
    assertEquals(TEST_API_VERSION, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
    synCtx = getMessageContext(synapseConfig, false, "/test/1.0.0/", "GET");
    handler.process(synCtx);
    assertEquals(api.getName(), synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
    assertEquals(TEST_API_VERSION, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
    synCtx = getMessageContext(synapseConfig, false, "/test/1.0.0/foo/bar?a=5", "GET");
    handler.process(synCtx);
    assertEquals(api.getName(), synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
    assertEquals(TEST_API_VERSION, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
    synCtx = getMessageContext(synapseConfig, false, "/test/1.0.0?a=5", "GET");
    handler.process(synCtx);
    assertEquals(api.getName(), synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
    assertEquals(TEST_API_VERSION, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
    // Message with '/test' context & URL as a Query Parameter should be dispatched
    synCtx = getMessageContext(synapseConfig, false, "/test/1.0.0?a=http://localhost.com", "GET");
    handler.process(synCtx);
    assertEquals(api.getName(), synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
    assertEquals(TEST_API_VERSION, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
    // Messages WITHOUT the '/test' context should NOT be dispatched
    synCtx = getMessageContext(synapseConfig, false, "/foo/test/bar?a=5", "GET");
    handler.process(synCtx);
    assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
    assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
    // Messages WITHOUT the '/test' context and proper version should NOT be dispatched
    synCtx = getMessageContext(synapseConfig, false, "/test/1.0.1/foo/bar?a=5", "GET");
    handler.process(synCtx);
    assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
    assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
    synCtx = getMessageContext(synapseConfig, false, "/test/2.0/foo/bar?a=5", "GET");
    handler.process(synCtx);
    assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
    assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
    synCtx = getMessageContext(synapseConfig, false, "/test/2.0.0.0/foo/bar?a=5", "GET");
    handler.process(synCtx);
    assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
    assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
}
Also used : ContextVersionStrategy(org.apache.synapse.api.version.ContextVersionStrategy) API(org.apache.synapse.api.API) MessageContext(org.apache.synapse.MessageContext) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration)

Example 24 with API

use of org.apache.synapse.api.API 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));
}
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 25 with API

use of org.apache.synapse.api.API 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));
}
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