use of org.apache.synapse.config.SynapseConfiguration in project wso2-synapse by wso2.
the class APIDispatcherTest method testAPIContextVersionBasedDispatchStartingWithVersion.
public void testAPIContextVersionBasedDispatchStartingWithVersion() throws Exception {
API api = new API(TEST_API, "/{version}/test");
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, "/1.0.0/test", "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, "/1.0.0/test/", "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, "/1.0.0/test/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, "/1.0.0/test?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, "/1.0.0/test?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, "/1.0.1/test/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, "/2.0/test/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, "/2.0.0.0/test/foo/bar?a=5", "GET");
handler.process(synCtx);
assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
}
use of org.apache.synapse.config.SynapseConfiguration 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));
}
use of org.apache.synapse.config.SynapseConfiguration in project wso2-synapse by wso2.
the class APIDispatcherTest method testAPIDefaultVersionBasedDispatch.
public void testAPIDefaultVersionBasedDispatch() throws Exception {
API api = new API(TEST_API, "/test");
api.setVersionStrategy(new DefaultStrategy(api));
SynapseConfiguration synapseConfig = new SynapseConfiguration();
synapseConfig.addAPI(api.getName(), api);
RESTRequestHandler handler = new RESTRequestHandler();
// Messages with '/test' context should ne dispatched
MessageContext synCtx = getMessageContext(synapseConfig, false, "/test", "GET");
handler.process(synCtx);
assertEquals(TEST_API, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
assertEquals("", synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
synCtx = getMessageContext(synapseConfig, false, "/test/", "GET");
handler.process(synCtx);
assertEquals(TEST_API, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
assertEquals("", synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar?a=5", "GET");
handler.process(synCtx);
assertEquals(TEST_API, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
assertEquals("", synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
synCtx = getMessageContext(synapseConfig, false, "/test?a=5", "GET");
handler.process(synCtx);
assertEquals(TEST_API, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
assertEquals("", 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));
synCtx = getMessageContext(synapseConfig, false, "/test1/bar?a=5", "GET");
handler.process(synCtx);
assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
}
use of org.apache.synapse.config.SynapseConfiguration in project wso2-synapse by wso2.
the class BasicAPIMediationTest method testRestURLPostfix3.
public void testRestURLPostfix3() throws Exception {
API api = new API(TEST_API, "/services/Foo");
SynapseConfiguration synapseConfig = new SynapseConfiguration();
synapseConfig.addAPI(TEST_API, api);
RESTRequestHandler handler = new RESTRequestHandler();
MessageContext synCtx = getMessageContext(synapseConfig, false, "/services/Foo/test", "GET");
// When the service path is in the URL, NHTTP transport removes that portion
// from the postfix
((Axis2MessageContext) synCtx).getAxis2MessageContext().setProperty(NhttpConstants.REST_URL_POSTFIX, "/test");
handler.process(synCtx);
checkRestURLPostfix(synCtx, "/test");
}
use of org.apache.synapse.config.SynapseConfiguration in project wso2-synapse by wso2.
the class BasicAPIMediationTest method testRestURLPostfix1.
public void testRestURLPostfix1() throws Exception {
API api = new API(TEST_API, "/test");
SynapseConfiguration synapseConfig = new SynapseConfiguration();
synapseConfig.addAPI(TEST_API, api);
RESTRequestHandler handler = new RESTRequestHandler();
MessageContext synCtx = getMessageContext(synapseConfig, false, "/test", "GET");
handler.process(synCtx);
checkRestURLPostfix(synCtx, "");
synCtx = getMessageContext(synapseConfig, false, "/test/me/now", "GET");
handler.process(synCtx);
checkRestURLPostfix(synCtx, "/me/now");
synCtx = getMessageContext(synapseConfig, false, "/test?a=5", "GET");
handler.process(synCtx);
checkRestURLPostfix(synCtx, "?a=5");
api.setVersionStrategy(new URLBasedVersionStrategy(api, "1.0.0", null));
synCtx = getMessageContext(synapseConfig, false, "/test/1.0.0?a=5", "GET");
handler.process(synCtx);
checkRestURLPostfix(synCtx, "?a=5");
synCtx = getMessageContext(synapseConfig, false, "/test/1.0.0/foo?a=5", "GET");
handler.process(synCtx);
checkRestURLPostfix(synCtx, "/foo?a=5");
}
Aggregations