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();
}
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;
}
}
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);
}
}
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();
}
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);
}
Aggregations