Search in sources :

Example 31 with HttpRouter

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));
}
Also used : Exchange(com.predic8.membrane.core.exchange.Exchange) Field(com.predic8.membrane.core.interceptor.formvalidation.FormValidationInterceptor.Field) HttpRouter(com.predic8.membrane.core.HttpRouter)

Example 32 with HttpRouter

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();
}
Also used : ServiceProxyKey(com.predic8.membrane.core.rules.ServiceProxyKey) ServiceProxy(com.predic8.membrane.core.rules.ServiceProxy) HttpRouter(com.predic8.membrane.core.HttpRouter)

Example 33 with HttpRouter

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;
}
Also used : ServiceProxyKey(com.predic8.membrane.core.rules.ServiceProxyKey) ServiceProxy(com.predic8.membrane.core.rules.ServiceProxy) DummyWebServiceInterceptor(com.predic8.membrane.core.services.DummyWebServiceInterceptor)

Example 34 with HttpRouter

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());
}
Also used : HttpRouter(com.predic8.membrane.core.HttpRouter) DummyWebServiceInterceptor(com.predic8.membrane.core.services.DummyWebServiceInterceptor) Test(org.junit.Test)

Example 35 with HttpRouter

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");
}
Also used : Exchange(com.predic8.membrane.core.exchange.Exchange) Request(com.predic8.membrane.core.http.Request) HttpRouter(com.predic8.membrane.core.HttpRouter) Test(org.junit.Test)

Aggregations

HttpRouter (com.predic8.membrane.core.HttpRouter)38 ServiceProxy (com.predic8.membrane.core.rules.ServiceProxy)24 ServiceProxyKey (com.predic8.membrane.core.rules.ServiceProxyKey)23 Before (org.junit.Before)19 Exchange (com.predic8.membrane.core.exchange.Exchange)14 Rule (com.predic8.membrane.core.rules.Rule)11 AbstractInterceptor (com.predic8.membrane.core.interceptor.AbstractInterceptor)8 Outcome (com.predic8.membrane.core.interceptor.Outcome)8 IOException (java.io.IOException)7 Test (org.junit.Test)6 Mapping (com.predic8.membrane.core.interceptor.rewrite.RewriteInterceptor.Mapping)3 DummyWebServiceInterceptor (com.predic8.membrane.core.services.DummyWebServiceInterceptor)3 Request (com.predic8.membrane.core.http.Request)2 RoundRobinStrategy (com.predic8.membrane.core.interceptor.balancer.RoundRobinStrategy)2 ProxyRule (com.predic8.membrane.core.rules.ProxyRule)2 ProxyRuleKey (com.predic8.membrane.core.rules.ProxyRuleKey)2 URISyntaxException (java.net.URISyntaxException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 AbstractExchange (com.predic8.membrane.core.exchange.AbstractExchange)1 MemoryExchangeStore (com.predic8.membrane.core.exchangestore.MemoryExchangeStore)1