Search in sources :

Example 56 with Request

use of com.predic8.membrane.core.http.Request in project service-proxy by membrane.

the class ClusterBalancerTest method getExchangeWithSession.

private Exchange getExchangeWithSession() throws IOException {
    Exchange exc = new Exchange(null);
    Request res = new Request();
    res.setHeader(getHeader());
    res.setBodyContent(getByteArrayData(getClass().getResourceAsStream("/getBankwithSession555555.xml")));
    exc.setRequest(res);
    exc.setOriginalRequestUri("/axis2/services/BLZService");
    return exc;
}
Also used : Exchange(com.predic8.membrane.core.exchange.Exchange)

Example 57 with Request

use of com.predic8.membrane.core.http.Request in project service-proxy by membrane.

the class AMQuotaInterceptorTest method testAMQuota.

@Test
public void testAMQuota() throws IOException, InterruptedException {
    final Exchange exc = new Exchange(null);
    exc.setRequest(new Request.Builder().header("Test", "test").body("hello").build());
    exc.setResponse(new Response.ResponseBuilder().header("Test2", "test2").body("Hello back!").build());
    exc.setProperty(Exchange.API_KEY, "junit");
    exc.setRule(new ServiceProxy());
    exc.getRule().setName("junit API");
    ApiManagementConfiguration amc = new ApiManagementConfiguration(System.getProperty("user.dir"), "src\\test\\resources\\apimanagement\\api.yaml");
    long reqSize = exc.getRequest().getHeader().toString().getBytes().length + exc.getRequest().getHeader().getContentLength();
    long respSize = exc.getResponse().getHeader().toString().getBytes().length + exc.getResponse().getHeader().getContentLength();
    assertEquals(31 + 5, reqSize);
    assertEquals(34 + 11, respSize);
    final AMQuota amq = new AMQuota();
    amq.setAmc(amc);
    ArrayList<Thread> threads = new ArrayList<Thread>();
    final AtomicInteger continues = new AtomicInteger();
    final AtomicInteger returns = new AtomicInteger();
    for (int i = 0; i < 1000; i++) {
        Thread t = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    Outcome out = amq.handleRequest(exc);
                    if (out == Outcome.CONTINUE) {
                        continues.incrementAndGet();
                    } else if (out == Outcome.RETURN) {
                        returns.incrementAndGet();
                    }
                    amq.handleResponse(exc);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        threads.add(t);
        // t.start();
        // doing sync because else we cant predictably count request/response pairs
        t.run();
    }
    for (Thread t : threads) {
        t.join();
    }
    // the limit is ( or should be ) 120B
    // 31+5 ( Req ) + 34+11 ( Resp ) = 81 for every completed exchange
    // the second request adds another 31+5 -> 81 + 36 = 117 < 120B -> after the second request it should block because the limit is 120b and the following response would bring it over the limit ( responses never block, only requests )
    assertEquals(2, continues.get());
    assertEquals(998, returns.get());
    Thread.sleep(2000);
    assertEquals(Outcome.CONTINUE, amq.handleRequest(exc));
}
Also used : ArrayList(java.util.ArrayList) AMQuota(com.predic8.membrane.core.interceptor.apimanagement.quota.AMQuota) IOException(java.io.IOException) Exchange(com.predic8.membrane.core.exchange.Exchange) ServiceProxy(com.predic8.membrane.core.rules.ServiceProxy) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Outcome(com.predic8.membrane.core.interceptor.Outcome) Test(org.junit.Test)

Example 58 with Request

use of com.predic8.membrane.core.http.Request in project service-proxy by membrane.

the class XPathCBRInterceptorTest method testRouting.

@Test
public void testRouting() throws Exception {
    exc = new Exchange(null);
    Request res = new Request();
    res.setBodyContent(getByteArrayData(getClass().getResourceAsStream("/customerFromBonn.xml")));
    exc.setRequest(res);
    XPathCBRInterceptor i = new XPathCBRInterceptor();
    i.setCases(getRouteList("//CITY[text()='England']", "http://www.host.uk/service", "//CITY[text()='Bonn']", "http://www.host.de/service"));
    i.handleRequest(exc);
    Assert.assertEquals("http://www.host.de/service", exc.getDestinations().get(0));
}
Also used : Exchange(com.predic8.membrane.core.exchange.Exchange) Request(com.predic8.membrane.core.http.Request) Test(org.junit.Test)

Example 59 with Request

use of com.predic8.membrane.core.http.Request in project service-proxy by membrane.

the class XPathCBRInterceptorTest method testRoutingNSAware.

@Test
public void testRoutingNSAware() throws Exception {
    exc = new Exchange(null);
    Request res = new Request();
    res.setBodyContent(getByteArrayData(getClass().getResourceAsStream("/customerFromBonnWithNS.xml")));
    exc.setRequest(res);
    XPathCBRInterceptor i = new XPathCBRInterceptor();
    i.setCases(getRouteList("//pre:CITY[text()='England']", "http://www.host.uk/service", "//pre:CITY[text()='Bonn']", "http://www.host.de/service"));
    i.setNamespaces(getNamespaceMap("pre", "http://predic8.de/customer/1"));
    i.handleRequest(exc);
    Assert.assertEquals("http://www.host.de/service", exc.getDestinations().get(0));
}
Also used : Exchange(com.predic8.membrane.core.exchange.Exchange) Request(com.predic8.membrane.core.http.Request) Test(org.junit.Test)

Example 60 with Request

use of com.predic8.membrane.core.http.Request 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

Request (com.predic8.membrane.core.http.Request)39 Exchange (com.predic8.membrane.core.exchange.Exchange)20 Test (org.junit.Test)12 IOException (java.io.IOException)11 Header (com.predic8.membrane.core.http.Header)8 Response (com.predic8.membrane.core.http.Response)8 AbstractExchange (com.predic8.membrane.core.exchange.AbstractExchange)5 Message (com.predic8.membrane.core.http.Message)5 Outcome (com.predic8.membrane.core.interceptor.Outcome)5 Body (com.predic8.membrane.core.http.Body)3 Request (com.predic8.membrane.core.http.xml.Request)3 EndOfStreamException (com.predic8.membrane.core.util.EndOfStreamException)3 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)2 CacheBuilder (com.google.common.cache.CacheBuilder)2 HeaderField (com.predic8.membrane.core.http.HeaderField)2 ResponseBuilder (com.predic8.membrane.core.http.Response.ResponseBuilder)2 ResolverMap (com.predic8.membrane.core.resolver.ResolverMap)2 HttpClient (com.predic8.membrane.core.transport.http.HttpClient)2 StringReader (java.io.StringReader)2 SocketException (java.net.SocketException)2