Search in sources :

Example 46 with Response

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

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

the class WADLInterceptorTest method getExchange.

private Exchange getExchange() throws IOException {
    Exchange exc = new Exchange(new FakeHttpHandler(3011));
    exc.setRequest(MessageUtil.getGetRequest("/search?wadl"));
    InputStream resourceAsStream = this.getClass().getResourceAsStream("/wadls/search.wadl");
    Response okResponse = Response.ok().contentType("text/xml; charset=utf-8").body(resourceAsStream, true).build();
    exc.setResponse(okResponse);
    exc.setOriginalHostHeader("thomas-bayer.com:80");
    return exc;
}
Also used : Exchange(com.predic8.membrane.core.exchange.Exchange) Response(com.predic8.membrane.core.http.Response) InputStream(java.io.InputStream) FakeHttpHandler(com.predic8.membrane.core.transport.http.FakeHttpHandler)

Example 48 with Response

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

the class AbortExchangeTest method doit.

@Test
public void doit() throws Exception {
    Response response = performRequest();
    response.getBodyAsStream().read(new byte[4096]);
    assertExchangeStoreHas(router.getExchangeStore(), 1, 0);
    BodyUtil.closeConnection(response.getBody());
    Thread.sleep(100);
    assertExchangeStoreHas(router.getExchangeStore(), 0, 0);
}
Also used : Response(com.predic8.membrane.core.http.Response) Test(org.junit.Test)

Example 49 with Response

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

the class AbortExchangeTest method assertExchangeStoreHas.

private void assertExchangeStoreHas(ExchangeStore exchangeStore, int numberOfExchanges, int responsePresent) {
    List<AbstractExchange> list = exchangeStore.getAllExchangesAsList();
    Assert.assertEquals(numberOfExchanges, list.size());
    for (AbstractExchange e : list) {
        Assert.assertEquals("Exchange has " + (responsePresent == 1 ? "no " : "") + "response", responsePresent, e.getResponse() != null ? 1 : 0);
    }
}
Also used : AbstractExchange(com.predic8.membrane.core.exchange.AbstractExchange)

Example 50 with Response

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

the class HttpUtil method setHTMLErrorResponse.

public static Response setHTMLErrorResponse(ResponseBuilder responseBuilder, String message, String comment) {
    Response response = responseBuilder.build();
    response.setHeader(createHeaders(MimeType.TEXT_HTML_UTF8));
    response.setBodyContent(getHTMLErrorBody(message, comment).getBytes(Constants.UTF_8_CHARSET));
    return response;
}
Also used : Response(com.predic8.membrane.core.http.Response)

Aggregations

Response (com.predic8.membrane.core.http.Response)29 Exchange (com.predic8.membrane.core.exchange.Exchange)14 IOException (java.io.IOException)14 StringWriter (java.io.StringWriter)9 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)7 Request (com.predic8.membrane.core.http.Request)7 AbstractExchange (com.predic8.membrane.core.exchange.AbstractExchange)6 Header (com.predic8.membrane.core.http.Header)6 Test (org.junit.Test)6 JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)4 Message (com.predic8.membrane.core.http.Message)4 JSONContent (com.predic8.membrane.core.interceptor.rest.JSONContent)4 ProxyRule (com.predic8.membrane.core.rules.ProxyRule)4 HttpClient (com.predic8.membrane.core.transport.http.HttpClient)4 SQLException (java.sql.SQLException)4 Element (org.w3c.dom.Element)4 NodeList (org.w3c.dom.NodeList)4 JsonFactory (com.fasterxml.jackson.core.JsonFactory)3 MCElement (com.predic8.membrane.annot.MCElement)3 ResponseBuilder (com.predic8.membrane.core.http.Response.ResponseBuilder)3