Search in sources :

Example 16 with ResponseEntity

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

the class HttpClientProxy method request.

public ResponseEntity request(Request request, int timeout) throws Exception {
    EzyFuture future = new EzyFutureTask();
    futures.addFuture(request, future);
    try {
        addRequest(request);
    } catch (Exception e) {
        futures.removeFuture(request);
        throw e;
    }
    return future.get(timeout);
}
Also used : EzyFutureTask(com.tvd12.ezyfox.concurrent.EzyFutureTask) 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 17 with ResponseEntity

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

the class HttpClientProxyTest method executeExceptionTest.

@Test
public void executeExceptionTest() 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.0:18081/greet");
    // when
    CountDownLatch countDownLatch = new CountDownLatch(1);
    EzyWrap<Exception> wrap = new EzyWrap<>();
    sut.execute(request, new RequestCallback<ResponseEntity>() {

        @Override
        public void onResponse(ResponseEntity response) {
            System.out.println(response);
        }

        @Override
        public void onException(Exception e) {
            wrap.setValue(e);
            countDownLatch.countDown();
        }
    });
    countDownLatch.await();
    // then
    Asserts.assertEquals(UnknownHostException.class, wrap.getValue().getClass());
    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 18 with ResponseEntity

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

the class ResponseEntityTest method createByStatusAndBody.

@Test
public void createByStatusAndBody() {
    // given
    int status = StatusCodes.ACCEPTED;
    Object body = "Hello World";
    // when
    ResponseEntity sut = ResponseEntity.of(status, body).build();
    // then
    Asserts.assertEquals(status, sut.getStatus());
    Asserts.assertEquals(body, sut.getBody());
}
Also used : ResponseEntity(com.tvd12.ezyhttp.core.response.ResponseEntity) Test(org.testng.annotations.Test)

Example 19 with ResponseEntity

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

the class ResponseEntityTest method createByBadRequest.

@Test
public void createByBadRequest() {
    // given
    // when
    ResponseEntity sut = ResponseEntity.badRequest();
    // then
    Asserts.assertEquals(StatusCodes.BAD_REQUEST, sut.getStatus());
}
Also used : ResponseEntity(com.tvd12.ezyhttp.core.response.ResponseEntity) Test(org.testng.annotations.Test)

Example 20 with ResponseEntity

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

the class ResponseEntityTest method createByStatusHeadersAndBody.

@Test
public void createByStatusHeadersAndBody() {
    // given
    int status = StatusCodes.ACCEPTED;
    Map<String, List<String>> headers = new HashMap<>();
    headers.put("hello", Arrays.asList("world", "galaxy"));
    headers.put("foo", Arrays.asList("bar", "animal"));
    Object body = "Hello World";
    // when
    ResponseEntity sut = new ResponseEntity(status, headers, body);
    // then
    Asserts.assertEquals(status, sut.getStatus());
    Asserts.assertEquals(new MultiValueMap(headers), sut.getHeaders());
    Asserts.assertEquals("world", sut.getHeader("hello"));
    Asserts.assertEquals(body, sut.getBody());
}
Also used : ResponseEntity(com.tvd12.ezyhttp.core.response.ResponseEntity) HashMap(java.util.HashMap) List(java.util.List) MultiValueMap(com.tvd12.ezyhttp.core.data.MultiValueMap) 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