use of com.predic8.membrane.core.HttpRouter in project service-proxy by membrane.
the class FormValidationInterceptorTest method testValidation.
@Test
public void testValidation() throws Exception {
FormValidationInterceptor interceptor = new FormValidationInterceptor();
interceptor.init(new HttpRouter());
Field article = new Field();
article.setName("article");
article.setRegex("banana|apple");
Field amount = new Field();
amount.setName("amount");
amount.setRegex("\\d+");
List<Field> fields = new ArrayList<Field>();
fields.add(amount);
fields.add(article);
interceptor.setFields(fields);
Exchange exc = getExchange("/buy?article=pizza&amount=five");
assertEquals(Outcome.ABORT, interceptor.handleRequest(exc));
assertEquals(400, exc.getResponse().getStatusCode());
exc = getExchange("/buy?article=pizza&amount=2");
assertEquals(Outcome.ABORT, interceptor.handleRequest(exc));
assertEquals(400, exc.getResponse().getStatusCode());
exc = getExchange("/buy?article=banana&amount=five");
assertEquals(Outcome.ABORT, interceptor.handleRequest(exc));
assertEquals(400, exc.getResponse().getStatusCode());
exc = getExchange("/buy?article=banana&amount=5");
assertEquals(Outcome.CONTINUE, interceptor.handleRequest(exc));
}
use of com.predic8.membrane.core.HttpRouter in project service-proxy by membrane.
the class LoadBalancingWithClusterManagerTest method startLB.
private void startLB() throws Exception {
LoadBalancingInterceptor lbi = new LoadBalancingInterceptor();
lbi.setName("Default");
XMLElementSessionIdExtractor extractor = new XMLElementSessionIdExtractor();
extractor.setLocalName("session");
extractor.setNamespace("http://predic8.com/session/");
lbi.setSessionIdExtractor(extractor);
ServiceProxy lbiRule = new ServiceProxy(new ServiceProxyKey("localhost", "*", ".*", 3017), "thomas-bayer.com", 80);
lbiRule.getInterceptors().add(lbi);
ClusterNotificationInterceptor cni = new ClusterNotificationInterceptor();
ServiceProxy cniRule = new ServiceProxy(new ServiceProxyKey("localhost", "*", ".*", 3012), "thomas-bayer.com", 80);
cniRule.getInterceptors().add(cni);
lb = new HttpRouter();
lb.getRuleManager().addProxyAndOpenPortIfNew(lbiRule);
lb.getRuleManager().addProxyAndOpenPortIfNew(cniRule);
lb.init();
}
use of com.predic8.membrane.core.HttpRouter in project service-proxy by membrane.
the class LoadBalancingWithClusterManagerTest method startNode.
private DummyWebServiceInterceptor startNode(HttpRouter node, int port) throws Exception {
DummyWebServiceInterceptor service1 = new DummyWebServiceInterceptor();
node.addUserFeatureInterceptor(service1);
node.getRuleManager().addProxyAndOpenPortIfNew(new ServiceProxy(new ServiceProxyKey("localhost", "POST", ".*", port), "thomas-bayer.com", 80));
node.init();
return service1;
}
use of com.predic8.membrane.core.HttpRouter in project service-proxy by membrane.
the class LoadBalancingWithClusterManagerTest method nodesTest.
@Test
public void nodesTest() throws Exception {
node1 = new HttpRouter();
node2 = new HttpRouter();
node3 = new HttpRouter();
DummyWebServiceInterceptor service1 = startNode(node1, 2000);
DummyWebServiceInterceptor service2 = startNode(node2, 3000);
DummyWebServiceInterceptor service3 = startNode(node3, 4000);
startLB();
sendNotification("up", 2000);
sendNotification("up", 3000);
// goes to service one
assertEquals(200, post("/getBankwithSession555555.xml"));
assertEquals(1, service1.getCount());
assertEquals(0, service2.getCount());
// goes to service 1 again
assertEquals(200, post("/getBankwithSession555555.xml"));
assertEquals(2, service1.getCount());
assertEquals(0, service2.getCount());
// goes to service 2
assertEquals(200, post("/getBankwithSession444444.xml"));
assertEquals(2, service1.getCount());
assertEquals(1, service2.getCount());
sendNotification("down", 2000);
// goes to service 2 because service 1 is down
assertEquals(200, post("/getBankwithSession555555.xml"));
assertEquals(2, service1.getCount());
assertEquals(2, service2.getCount());
sendNotification("up", 4000);
assertEquals(0, service3.getCount());
// goes to service 3
assertEquals(200, post("/getBankwithSession666666.xml"));
assertEquals(2, service1.getCount());
assertEquals(2, service2.getCount());
assertEquals(1, service3.getCount());
// goes to service 2
assertEquals(200, post("/getBankwithSession555555.xml"));
// goes to service 2
assertEquals(200, post("/getBankwithSession444444.xml"));
// goes to service 3
assertEquals(200, post("/getBankwithSession666666.xml"));
assertEquals(2, service1.getCount());
assertEquals(4, service2.getCount());
assertEquals(2, service3.getCount());
}
use of com.predic8.membrane.core.HttpRouter in project service-proxy by membrane.
the class GroovyInterceptorTest method testRequest.
@Test
public void testRequest() throws Exception {
HttpRouter r = new HttpRouter();
r.setApplicationContext(applicationContext);
when(applicationContext.getBean("abc")).thenReturn("OK");
Exchange exc = new Exchange(null);
exc.setRequest(new Request());
GroovyInterceptor i = new GroovyInterceptor();
i.setSrc("exc.setProperty('foo', 'bar')\n" + "def b = spring.getBean('abc')\n" + "CONTINUE");
i.init(r);
assertEquals(Outcome.CONTINUE, i.handleRequest(exc));
assertEquals("bar", exc.getProperty("foo"));
verify(applicationContext, times(1)).getBean("abc");
}
Aggregations