Search in sources :

Example 1 with ResponseEntity

use of com.tvd12.ezyhttp.core.response.ResponseEntity in project ezyhttp by youngmonkeys.

the class HttpClientProxyTest method executeJsonTest.

@Test
public void executeJsonTest() throws Exception {
    // given
    HttpClientProxy sut = newClientProxy();
    GetRequest request = new GetRequest().setConnectTimeout(15000).setResponseType(TestResponse.class).setResponseType(StatusCodes.OK, TestResponse.class).setURL("http://127.0.0.1:18081/greet?who=Monkey");
    // when
    CountDownLatch countDownLatch = new CountDownLatch(1);
    EzyWrap<TestResponse> wrap = new EzyWrap<>();
    sut.execute(request, new RequestCallback<ResponseEntity>() {

        @Override
        public void onResponse(ResponseEntity response) {
            wrap.setValue(response.getBody());
            countDownLatch.countDown();
        }

        @Override
        public void onException(Exception e) {
        }
    });
    countDownLatch.await();
    // then
    TestResponse expectation = new TestResponse("Greet Monkey!");
    Asserts.assertEquals(expectation, wrap.getValue());
    sut.close();
    sut.stop();
}
Also used : EzyWrap(com.tvd12.ezyfox.util.EzyWrap) ResponseEntity(com.tvd12.ezyhttp.core.response.ResponseEntity) HttpClientProxy(com.tvd12.ezyhttp.client.HttpClientProxy) CountDownLatch(java.util.concurrent.CountDownLatch) DownloadCancelledException(com.tvd12.ezyhttp.client.exception.DownloadCancelledException) RequestQueueFullException(com.tvd12.ezyhttp.client.exception.RequestQueueFullException) UnknownHostException(java.net.UnknownHostException) BadRequestException(com.tvd12.ezyfox.exception.BadRequestException) ClientNotActiveException(com.tvd12.ezyhttp.client.exception.ClientNotActiveException) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) BaseTest(com.tvd12.test.base.BaseTest)

Example 2 with ResponseEntity

use of com.tvd12.ezyhttp.core.response.ResponseEntity in project ezyhttp by youngmonkeys.

the class HttpClientProxy method execute.

public void execute(Request request, RequestCallback<ResponseEntity> callback) {
    EzyFuture future = new RequestFutureTask(callback);
    futures.addFuture(request, future);
    try {
        addRequest(request);
    } catch (Exception e) {
        futures.removeFuture(request);
        throw e;
    }
}
Also used : EzyFuture(com.tvd12.ezyfox.concurrent.EzyFuture) RequestFutureTask(com.tvd12.ezyhttp.client.concurrent.RequestFutureTask) EzyProcessor.processWithException(com.tvd12.ezyfox.util.EzyProcessor.processWithException) RequestQueueFullException(com.tvd12.ezyhttp.client.exception.RequestQueueFullException) IOException(java.io.IOException) ClientNotActiveException(com.tvd12.ezyhttp.client.exception.ClientNotActiveException)

Example 3 with ResponseEntity

use of com.tvd12.ezyhttp.core.response.ResponseEntity in project ezyhttp by youngmonkeys.

the class HttpClientProxy method handleRequests.

protected void handleRequests() {
    Request request = null;
    EzyFuture future = null;
    Exception exception = null;
    ResponseEntity response = null;
    try {
        request = requestQueue.take();
        future = futures.removeFuture(request);
        response = client.request(request);
    } catch (Exception e) {
        exception = e;
    }
    try {
        if (future != null) {
            if (exception != null) {
                future.setException(exception);
            } else {
                future.setResult(response);
            }
        } else {
            if (exception != null) {
                logger.info("handled request: {} with exception, but there is no future", request, exception);
            } else {
                logger.info("handled request: {} with response: {}, but there is no future", request, response);
            }
        }
    } catch (Throwable e) {
        logger.info("handle request result error", e);
    }
}
Also used : ResponseEntity(com.tvd12.ezyhttp.core.response.ResponseEntity) Request(com.tvd12.ezyhttp.client.request.Request) DownloadRequest(com.tvd12.ezyhttp.client.request.DownloadRequest) EzyFuture(com.tvd12.ezyfox.concurrent.EzyFuture) EzyProcessor.processWithException(com.tvd12.ezyfox.util.EzyProcessor.processWithException) RequestQueueFullException(com.tvd12.ezyhttp.client.exception.RequestQueueFullException) IOException(java.io.IOException) ClientNotActiveException(com.tvd12.ezyhttp.client.exception.ClientNotActiveException)

Example 4 with ResponseEntity

use of com.tvd12.ezyhttp.core.response.ResponseEntity in project ezyhttp by youngmonkeys.

the class BlockingServletTest method requestHandlerNullAndHasErrorHandler.

@Test
public void requestHandlerNullAndHasErrorHandler() throws Exception {
    // given
    ComponentManager componentManager = ComponentManager.getInstance();
    componentManager.setServerPort(PORT);
    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    UnhandledErrorHandler unhandledErrorHandler = mock(UnhandledErrorHandler.class);
    ResponseEntity responseEntity = ResponseEntity.ok();
    when(unhandledErrorHandler.handleError(HttpMethod.GET, request, response, HttpServletResponse.SC_NOT_FOUND, null)).thenReturn(responseEntity);
    componentManager.setUnhandledErrorHandler(Collections.singletonList(unhandledErrorHandler));
    BlockingServlet sut = new BlockingServlet();
    sut.init();
    String requestURI = "/get-handler-null";
    when(request.getMethod()).thenReturn(HttpMethod.GET.toString());
    when(request.getRequestURI()).thenReturn(requestURI);
    when(request.getServerPort()).thenReturn(PORT);
    when(response.getContentType()).thenReturn(ContentTypes.APPLICATION_JSON);
    ServletOutputStream outputStream = mock(ServletOutputStream.class);
    when(response.getOutputStream()).thenReturn(outputStream);
    // when
    sut.service(request, response);
    // then
    verify(request, times(1)).getMethod();
    verify(request, times(1)).getRequestURI();
    verify(response, times(1)).setStatus(StatusCodes.OK);
    componentManager.destroy();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ResponseEntity(com.tvd12.ezyhttp.core.response.ResponseEntity) ServletOutputStream(javax.servlet.ServletOutputStream) ComponentManager(com.tvd12.ezyhttp.server.core.manager.ComponentManager) UnhandledErrorHandler(com.tvd12.ezyhttp.server.core.handler.UnhandledErrorHandler) BlockingServlet(com.tvd12.ezyhttp.server.core.servlet.BlockingServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) ToString(lombok.ToString) Test(org.testng.annotations.Test)

Example 5 with ResponseEntity

use of com.tvd12.ezyhttp.core.response.ResponseEntity in project ezyhttp by youngmonkeys.

the class ResponseEntityTest method buildTest.

@Test
public void buildTest() {
    // given
    int status = StatusCodes.ACCEPTED;
    Object body = "Hello World";
    // when
    ResponseEntity sut = ResponseEntity.builder().textPlain().textPlain(body).header("hello", Arrays.asList("world", "galaxy")).header("foo", "bar").headers(Collections.singletonMap("yes", "no")).status(status).build();
    // then
    Asserts.assertEquals(status, sut.getStatus());
    Asserts.assertEquals("world", sut.getHeader("hello"));
    Asserts.assertEquals("bar", sut.getHeader("foo"));
    Asserts.assertEquals("no", sut.getHeader("yes"));
    Asserts.assertEquals(ContentTypes.TEXT_PLAIN, sut.getContentType());
    Asserts.assertNull(sut.getHeader("nothing"));
    Asserts.assertEquals(body, sut.getBody());
    System.out.println(sut);
}
Also used : ResponseEntity(com.tvd12.ezyhttp.core.response.ResponseEntity) Test(org.testng.annotations.Test)

Aggregations

ResponseEntity (com.tvd12.ezyhttp.core.response.ResponseEntity)24 Test (org.testng.annotations.Test)21 ClientNotActiveException (com.tvd12.ezyhttp.client.exception.ClientNotActiveException)6 RequestQueueFullException (com.tvd12.ezyhttp.client.exception.RequestQueueFullException)6 UnhandledErrorHandler (com.tvd12.ezyhttp.server.core.handler.UnhandledErrorHandler)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 EzyFuture (com.tvd12.ezyfox.concurrent.EzyFuture)3 BadRequestException (com.tvd12.ezyfox.exception.BadRequestException)3 EzyProcessor.processWithException (com.tvd12.ezyfox.util.EzyProcessor.processWithException)3 HttpClientProxy (com.tvd12.ezyhttp.client.HttpClientProxy)3 DownloadCancelledException (com.tvd12.ezyhttp.client.exception.DownloadCancelledException)3 MultiValueMap (com.tvd12.ezyhttp.core.data.MultiValueMap)3 ComponentManager (com.tvd12.ezyhttp.server.core.manager.ComponentManager)3 BlockingServlet (com.tvd12.ezyhttp.server.core.servlet.BlockingServlet)3 BaseTest (com.tvd12.test.base.BaseTest)3 IOException (java.io.IOException)3 UnknownHostException (java.net.UnknownHostException)3 ServletOutputStream (javax.servlet.ServletOutputStream)3 ToString (lombok.ToString)3