Search in sources :

Example 91 with Exchange

use of com.predic8.membrane.core.exchange.Exchange in project service-proxy by membrane.

the class ExceptionHandlingTest method setUp.

@Before
public void setUp() throws Exception {
    router = new HttpRouter();
    router.getTransport().setPrintStackTrace(printStackTrace);
    ServiceProxy sp2 = new ServiceProxy(new ServiceProxyKey("*", "*", ".*", getPort()), "", -1);
    sp2.getInterceptors().add(new AbstractInterceptor() {

        @Override
        public Outcome handleRequest(Exchange exc) throws Exception {
            throw new Exception("secret");
        }
    });
    router.getRuleManager().addProxyAndOpenPortIfNew(sp2);
    router.init();
}
Also used : Exchange(com.predic8.membrane.core.exchange.Exchange) ServiceProxyKey(com.predic8.membrane.core.rules.ServiceProxyKey) ServiceProxy(com.predic8.membrane.core.rules.ServiceProxy) Outcome(com.predic8.membrane.core.interceptor.Outcome) AbstractInterceptor(com.predic8.membrane.core.interceptor.AbstractInterceptor) HttpRouter(com.predic8.membrane.core.HttpRouter) ParseException(org.apache.http.ParseException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Before(org.junit.Before)

Example 92 with Exchange

use of com.predic8.membrane.core.exchange.Exchange in project service-proxy by membrane.

the class BoundConnectionTest method setUp.

@Before
public void setUp() throws Exception {
    router = new HttpRouter();
    ServiceProxy sp1 = new ServiceProxy(new ServiceProxyKey("*", "*", ".*", 3021), "localhost", 3022);
    router.getRuleManager().addProxyAndOpenPortIfNew(sp1);
    ServiceProxy sp2 = new ServiceProxy(new ServiceProxyKey("*", "*", ".*", 3022), "", -1);
    sp2.getInterceptors().add(new AbstractInterceptor() {

        @Override
        public Outcome handleRequest(Exchange exc) throws Exception {
            exc.getRequest().readBody();
            exc.setResponse(Response.ok("OK.").build());
            connectionHash = ((HttpServerHandler) exc.getHandler()).getSrcOut().hashCode();
            return Outcome.RETURN;
        }
    });
    router.getRuleManager().addProxyAndOpenPortIfNew(sp2);
    router.init();
}
Also used : Exchange(com.predic8.membrane.core.exchange.Exchange) ServiceProxyKey(com.predic8.membrane.core.rules.ServiceProxyKey) ServiceProxy(com.predic8.membrane.core.rules.ServiceProxy) Outcome(com.predic8.membrane.core.interceptor.Outcome) AbstractInterceptor(com.predic8.membrane.core.interceptor.AbstractInterceptor) HttpRouter(com.predic8.membrane.core.HttpRouter) IOException(java.io.IOException) EndOfStreamException(com.predic8.membrane.core.util.EndOfStreamException) Before(org.junit.Before)

Example 93 with Exchange

use of com.predic8.membrane.core.exchange.Exchange in project service-proxy by membrane.

the class HttpKeepAliveTest method testConnectionClose.

@Test
public void testConnectionClose() throws Exception {
    HttpClient client = createHttpClient(500);
    sp1.getInterceptors().add(0, new AbstractInterceptor() {

        @Override
        public Outcome handleResponse(Exchange exc) throws Exception {
            exc.getResponse().getHeader().add(Header.KEEP_ALIVE, "max=2");
            return Outcome.CONTINUE;
        }
    });
    // opens connection 1
    assertEquals(200, issueRequest(client));
    assertEquals(1, set.size());
    assertEquals(1, client.getConnectionManager().getNumberInPool());
    // connection closer did not yet run
    Thread.sleep(600);
    // connection 1 is now dead, but still in pool
    assertEquals(1, client.getConnectionManager().getNumberInPool());
    // opens connection 2
    assertEquals(200, issueRequest(client));
    assertEquals(2, set.size());
    // connection closer runs and closes both
    Thread.sleep(600);
    assertEquals(0, client.getConnectionManager().getNumberInPool());
}
Also used : Exchange(com.predic8.membrane.core.exchange.Exchange) Outcome(com.predic8.membrane.core.interceptor.Outcome) AbstractInterceptor(com.predic8.membrane.core.interceptor.AbstractInterceptor) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) Test(org.junit.Test)

Example 94 with Exchange

use of com.predic8.membrane.core.exchange.Exchange in project service-proxy by membrane.

the class HttpKeepAliveTest method issueRequest.

private int issueRequest(HttpClient client) throws IOException, Exception {
    Exchange exchange = createExchange();
    Response response = client.call(exchange).getResponse();
    response.readBody();
    return response.getStatusCode();
}
Also used : Exchange(com.predic8.membrane.core.exchange.Exchange) Response(com.predic8.membrane.core.http.Response)

Example 95 with Exchange

use of com.predic8.membrane.core.exchange.Exchange in project service-proxy by membrane.

the class HttpKeepAliveTest method testTimeoutDefault.

@Test
public void testTimeoutDefault() throws Exception {
    HttpClient client = createHttpClient(1000);
    sp1.getInterceptors().add(0, new AbstractInterceptor() {

        @Override
        public Outcome handleResponse(Exchange exc) throws Exception {
            exc.getResponse().getHeader().add(Header.KEEP_ALIVE, "max=2");
            return Outcome.CONTINUE;
        }
    });
    assertEquals(200, issueRequest(client));
    assertEquals(1, set.size());
    Thread.sleep(1500);
    assertEquals(200, issueRequest(client));
    assertEquals(2, set.size());
}
Also used : Exchange(com.predic8.membrane.core.exchange.Exchange) Outcome(com.predic8.membrane.core.interceptor.Outcome) AbstractInterceptor(com.predic8.membrane.core.interceptor.AbstractInterceptor) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

Exchange (com.predic8.membrane.core.exchange.Exchange)107 Test (org.junit.Test)39 IOException (java.io.IOException)32 Request (com.predic8.membrane.core.http.Request)25 Outcome (com.predic8.membrane.core.interceptor.Outcome)24 Response (com.predic8.membrane.core.http.Response)16 AbstractInterceptor (com.predic8.membrane.core.interceptor.AbstractInterceptor)16 ServiceProxy (com.predic8.membrane.core.rules.ServiceProxy)16 HttpRouter (com.predic8.membrane.core.HttpRouter)14 Before (org.junit.Before)13 ServiceProxyKey (com.predic8.membrane.core.rules.ServiceProxyKey)12 AbstractExchange (com.predic8.membrane.core.exchange.AbstractExchange)11 Header (com.predic8.membrane.core.http.Header)10 HttpClient (com.predic8.membrane.core.transport.http.HttpClient)10 CacheBuilder (com.google.common.cache.CacheBuilder)9 Rule (com.predic8.membrane.core.rules.Rule)6 URISyntaxException (java.net.URISyntaxException)6 UnknownHostException (java.net.UnknownHostException)6 ArrayList (java.util.ArrayList)6 Session (com.predic8.membrane.core.interceptor.authentication.session.SessionManager.Session)5