use of org.apache.synapse.api.Resource in project wso2-synapse by wso2.
the class URITemplateBasedDispatcherTest method testBasicTemplateDispatch2.
public void testBasicTemplateDispatch2() throws Exception {
API api = new API("TestAPI", "/");
Resource resource = new Resource();
resource.setDispatcherHelper(new URITemplateHelper("/dictionary/{char}/{word}"));
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, "/dictionary/c/cat", "GET");
handler.process(synCtx);
assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
assertEquals("c", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "char"));
assertEquals("cat", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "word"));
synCtx = getMessageContext(synapseConfig, false, "/dictionary/d/dog/", "GET");
handler.process(synCtx);
assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
assertEquals("d", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "char"));
assertEquals("dog", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "word"));
synCtx = getMessageContext(synapseConfig, false, "/test/c/cat", "GET");
handler.process(synCtx);
assertNull(synCtx.getProperty(PROP_NAME));
synCtx = getMessageContext(synapseConfig, false, "/dictionary/c", "GET");
handler.process(synCtx);
assertNull(synCtx.getProperty(PROP_NAME));
/* With ESBJAVA-4260 we now support optional query parameters for URITemplates */
synCtx = getMessageContext(synapseConfig, false, "/dictionary/c/cat?a=5", "GET");
handler.process(synCtx);
assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
assertEquals("c", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "char"));
assertEquals("cat", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "word"));
}
use of org.apache.synapse.api.Resource in project wso2-synapse by wso2.
the class URITemplateBasedDispatcherTest method testMultipleResourceDispatch.
public void testMultipleResourceDispatch() throws Exception {
API api = new API("TestAPI", "/");
Resource resource1 = new Resource();
resource1.setDispatcherHelper(new URITemplateHelper("/dictionary/{char}/{word}"));
resource1.setInSequence(getTestSequence(PROP_NAME, "r1"));
api.addResource(resource1);
Resource resource2 = new Resource();
resource2.setDispatcherHelper(new URITemplateHelper("/dictionary/{char}"));
resource2.setInSequence(getTestSequence(PROP_NAME, "r2"));
api.addResource(resource2);
Resource resource3 = new Resource();
resource3.setDispatcherHelper(new URITemplateHelper("/dictionary/{char}{#ref}"));
resource3.setInSequence(getTestSequence(PROP_NAME, "r3"));
api.addResource(resource3);
SynapseConfiguration synapseConfig = new SynapseConfiguration();
synapseConfig.addAPI(api.getName(), api);
RESTRequestHandler handler = new RESTRequestHandler();
MessageContext synCtx = getMessageContext(synapseConfig, false, "/dictionary/c/cat", "GET");
handler.process(synCtx);
assertEquals("r1", synCtx.getProperty(PROP_NAME));
assertEquals("c", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "char"));
assertEquals("cat", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "word"));
synCtx = getMessageContext(synapseConfig, false, "/dictionary/d", "GET");
handler.process(synCtx);
assertEquals("r2", synCtx.getProperty(PROP_NAME));
assertEquals("d", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "char"));
synCtx = getMessageContext(synapseConfig, false, "/dictionary/e#test", "GET");
handler.process(synCtx);
assertEquals("r3", synCtx.getProperty(PROP_NAME));
assertEquals("e", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "char"));
assertEquals("test", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "ref"));
synCtx = getMessageContext(synapseConfig, false, "/dictionary/c/cat/test", "GET");
handler.process(synCtx);
assertNull(synCtx.getProperty(PROP_NAME));
synCtx = getMessageContext(synapseConfig, false, "/dictionary/c/cat#ref", "GET");
handler.process(synCtx);
assertNull(synCtx.getProperty(PROP_NAME));
/* With ESBJAVA-4260 we now support optional query parameters for URITemplates */
synCtx = getMessageContext(synapseConfig, false, "/dictionary/c/cat?a=5", "GET");
handler.process(synCtx);
assertEquals("r1", synCtx.getProperty(PROP_NAME));
assertEquals("c", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "char"));
assertEquals("cat", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "word"));
}
use of org.apache.synapse.api.Resource 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.Resource 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.Resource 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));
}
Aggregations