Search in sources :

Example 11 with GetRequest

use of com.tvd12.ezyhttp.client.request.GetRequest in project ezyhttp by youngmonkeys.

the class AsyncCallbackTest method onTimeoutFailed.

@Test
public void onTimeoutFailed() {
    // given
    AsyncCallback callback = event -> {
    };
    AsyncContext asyncContext = mock(AsyncContext.class);
    doThrow(RuntimeException.class).when(asyncContext).complete();
    ServletRequest request = mock(ServletRequest.class);
    when(asyncContext.getRequest()).thenReturn(request);
    HttpServletResponse response = mock(HttpServletResponse.class);
    when(asyncContext.getResponse()).thenReturn(response);
    AsyncEvent event = new AsyncEvent(asyncContext);
    // when
    callback.onTimeout(event);
    // then
    verify(asyncContext, times(1)).getRequest();
    verify(asyncContext, times(2)).getResponse();
    verify(response, times(1)).setStatus(StatusCodes.REQUEST_TIMEOUT);
}
Also used : AsyncContext(javax.servlet.AsyncContext) Mockito(org.mockito.Mockito) ServletRequest(javax.servlet.ServletRequest) AsyncCallback(com.tvd12.ezyhttp.server.core.servlet.AsyncCallback) StatusCodes(com.tvd12.ezyhttp.core.constant.StatusCodes) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.testng.annotations.Test) AsyncEvent(javax.servlet.AsyncEvent) ServletRequest(javax.servlet.ServletRequest) AsyncCallback(com.tvd12.ezyhttp.server.core.servlet.AsyncCallback) HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncContext(javax.servlet.AsyncContext) AsyncEvent(javax.servlet.AsyncEvent) Test(org.testng.annotations.Test)

Example 12 with GetRequest

use of com.tvd12.ezyhttp.client.request.GetRequest in project ezyhttp by youngmonkeys.

the class AsyncCallbackTest method onErrorWithHttpServletResponse.

@Test
public void onErrorWithHttpServletResponse() {
    // given
    AsyncCallback callback = event -> {
    };
    AsyncContext asyncContext = mock(AsyncContext.class);
    ServletRequest request = mock(ServletRequest.class);
    when(asyncContext.getRequest()).thenReturn(request);
    HttpServletResponse response = mock(HttpServletResponse.class);
    when(asyncContext.getResponse()).thenReturn(response);
    AsyncEvent event = new AsyncEvent(asyncContext);
    // when
    callback.onError(event);
    // then
    verify(asyncContext, times(1)).getRequest();
    verify(asyncContext, times(2)).getResponse();
    verify(response, times(1)).setStatus(StatusCodes.INTERNAL_SERVER_ERROR);
}
Also used : AsyncContext(javax.servlet.AsyncContext) Mockito(org.mockito.Mockito) ServletRequest(javax.servlet.ServletRequest) AsyncCallback(com.tvd12.ezyhttp.server.core.servlet.AsyncCallback) StatusCodes(com.tvd12.ezyhttp.core.constant.StatusCodes) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.testng.annotations.Test) AsyncEvent(javax.servlet.AsyncEvent) ServletRequest(javax.servlet.ServletRequest) AsyncCallback(com.tvd12.ezyhttp.server.core.servlet.AsyncCallback) HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncContext(javax.servlet.AsyncContext) AsyncEvent(javax.servlet.AsyncEvent) Test(org.testng.annotations.Test)

Example 13 with GetRequest

use of com.tvd12.ezyhttp.client.request.GetRequest in project ezyhttp by youngmonkeys.

the class HttpClientProxyTest method fireExceptionTest.

@Test
public void fireExceptionTest() throws Exception {
    // given
    HttpClientProxy sut = newClientProxy();
    GetRequest request = new GetRequest().setConnectTimeout(15000).setResponseType(String.class).setResponseType(StatusCodes.OK, String.class).setURL("http://unknow-host:18081/greet");
    // when
    CountDownLatch countDownLatch = new CountDownLatch(1);
    EzyWrap<Exception> wrap = new EzyWrap<>();
    sut.fire(request, new RequestCallback<String>() {

        @Override
        public void onResponse(String response) {
        }

        @Override
        public void onException(Exception e) {
            wrap.setValue(e);
            countDownLatch.countDown();
        }
    });
    countDownLatch.await();
    // then
    Asserts.assertEquals(BadRequestException.class, wrap.getValue().getClass());
    sut.close();
    sut.stop();
}
Also used : EzyWrap(com.tvd12.ezyfox.util.EzyWrap) 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 14 with GetRequest

use of com.tvd12.ezyhttp.client.request.GetRequest in project ezyhttp by youngmonkeys.

the class HttpClientProxyTest method getJsonTest.

@Test
public void getJsonTest() 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").setURL(new URL("http://127.0.0.1:18081/greet?who=Monkey")).setURL(URI.create("http://127.0.0.1:18081/greet?who=Monkey"));
    // when
    TestResponse actual = sut.call(request, 15000);
    // then
    TestResponse expectation = new TestResponse("Greet Monkey!");
    Asserts.assertEquals(expectation, actual);
    sut.close();
    sut.stop();
}
Also used : HttpClientProxy(com.tvd12.ezyhttp.client.HttpClientProxy) URL(java.net.URL) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) BaseTest(com.tvd12.test.base.BaseTest)

Example 15 with GetRequest

use of com.tvd12.ezyhttp.client.request.GetRequest in project ezyhttp by youngmonkeys.

the class HttpClientProxyTest method fireJsonTest.

@Test
public void fireJsonTest() 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.fire(request, new RequestCallback<TestResponse>() {

        @Override
        public void onResponse(TestResponse response) {
            wrap.setValue(response);
            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) 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)

Aggregations

Test (org.testng.annotations.Test)14 HttpClientProxy (com.tvd12.ezyhttp.client.HttpClientProxy)8 BaseTest (com.tvd12.test.base.BaseTest)7 AsyncContext (javax.servlet.AsyncContext)7 HttpServletResponse (javax.servlet.http.HttpServletResponse)7 BeforeTest (org.testng.annotations.BeforeTest)7 BadRequestException (com.tvd12.ezyfox.exception.BadRequestException)5 EzyWrap (com.tvd12.ezyfox.util.EzyWrap)5 ClientNotActiveException (com.tvd12.ezyhttp.client.exception.ClientNotActiveException)5 DownloadCancelledException (com.tvd12.ezyhttp.client.exception.DownloadCancelledException)5 RequestQueueFullException (com.tvd12.ezyhttp.client.exception.RequestQueueFullException)5 UnknownHostException (java.net.UnknownHostException)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 GetRequest (com.tvd12.ezyhttp.client.request.GetRequest)4 StatusCodes (com.tvd12.ezyhttp.core.constant.StatusCodes)4 AsyncCallback (com.tvd12.ezyhttp.server.core.servlet.AsyncCallback)4 AsyncEvent (javax.servlet.AsyncEvent)4 ServletRequest (javax.servlet.ServletRequest)4 Mockito (org.mockito.Mockito)4 EzyExceptionVoid (com.tvd12.ezyfox.function.EzyExceptionVoid)3