use of com.tvd12.ezyhttp.client.request.Request in project ezyhttp by youngmonkeys.
the class HttpClientTest method postWithNoBody.
@Test
public void postWithNoBody() {
// given
HttpClient sut = HttpClient.builder().build();
PostRequest request = new PostRequest().setResponseType(TestResponse.class).setResponseType(StatusCodes.OK, TestResponse.class).setURL(URI.create("http://127.0.0.1:18081/form"));
// when
Throwable e = Asserts.assertThrows(() -> sut.call(request));
// then
Asserts.assertThat(e).isEqualsType(HttpBadRequestException.class);
}
use of com.tvd12.ezyhttp.client.request.Request in project ezyhttp by youngmonkeys.
the class HttpClientTest method postMethodNotAcceptable.
@Test
public void postMethodNotAcceptable() {
// given
HttpClient sut = HttpClient.builder().build();
PostRequest request = new PostRequest().setResponseType(TestResponse.class).setResponseType(StatusCodes.OK, TestResponse.class).setURL(URI.create("http://127.0.0.1:18081/406"));
// when
Throwable e = Asserts.assertThrows(() -> sut.call(request));
// then
Asserts.assertThat(e).isEqualsType(HttpNotAcceptableException.class);
}
use of com.tvd12.ezyhttp.client.request.Request in project ezyhttp by youngmonkeys.
the class BlockingServlet method newRequestArguments.
protected RequestArguments newRequestArguments(HttpMethod method, String uriTemplate, HttpServletRequest request, HttpServletResponse response) {
SimpleRequestArguments arguments = new SimpleRequestArguments();
arguments.setDebug(debug);
arguments.setMethod(method);
arguments.setRequest(request);
arguments.setResponse(response);
arguments.setUriTemplate(uriTemplate);
arguments.setCookies(request.getCookies());
arguments.setObjectMapper(objectMapper);
arguments.setRedirectionAttributesFromCookie();
Enumeration<String> paramNames = request.getParameterNames();
while (paramNames.hasMoreElements()) {
String paramName = paramNames.nextElement();
String[] paramValues = request.getParameterValues(paramName);
arguments.setParameter(paramName, paramValues);
}
Enumeration<String> headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
String headerName = headerNames.nextElement();
String headerValue = request.getHeader(headerName);
arguments.setHeader(headerName, headerValue);
}
return arguments;
}
use of com.tvd12.ezyhttp.client.request.Request in project ezyhttp by youngmonkeys.
the class BlockingServlet method handleRequest.
@SuppressWarnings("MethodLength")
protected void handleRequest(HttpMethod method, HttpServletRequest request, HttpServletResponse response) throws IOException {
String requestURI = request.getRequestURI();
String matchedURI = requestHandlerManager.getMatchedURI(method, requestURI);
if (matchedURI == null) {
if (!handleError(method, request, response, HttpServletResponse.SC_NOT_FOUND)) {
responseString(response, "uri " + requestURI + " not found");
}
return;
}
request.setAttribute(CoreConstants.ATTRIBUTE_MATCHED_URI, matchedURI);
boolean isManagementURI = requestURIManager.isManagementURI(method, matchedURI);
if (isManagementURI && !exposeManagementURIs && request.getServerPort() != managementPort) {
handleError(method, request, response, HttpServletResponse.SC_NOT_FOUND);
logger.warn("a normal client's not allowed call to: {}, " + "please check your proxy configuration", requestURI);
return;
}
RequestHandler requestHandler = requestHandlerManager.getHandler(method, matchedURI, isManagementURI);
if (requestHandler == RequestHandler.EMPTY) {
if (!handleError(method, request, response, HttpServletResponse.SC_METHOD_NOT_ALLOWED)) {
responseString(response, "method " + method + " not allowed");
}
return;
}
boolean acceptableRequest = false;
boolean syncResponse = true;
String uriTemplate = requestHandler.getRequestURI();
RequestArguments arguments = newRequestArguments(method, uriTemplate, request, response);
try {
acceptableRequest = preHandleRequest(arguments, requestHandler);
if (acceptableRequest) {
if (requestHandler.isAsync()) {
syncResponse = false;
AsyncContext asyncContext = request.startAsync(request, response);
if (asyncDefaultTimeout > 0) {
asyncContext.setTimeout(asyncDefaultTimeout);
}
asyncContext.addListener(newAsyncListener(arguments, requestHandler));
requestHandler.handle(arguments);
} else {
Object responseData = requestHandler.handle(arguments);
String responseContentType = requestHandler.getResponseContentType();
if (responseContentType != null) {
response.setContentType(responseContentType);
}
if (responseData != null) {
handleResponseData(request, response, responseData);
} else {
response.setStatus(HttpServletResponse.SC_OK);
}
}
} else {
handleError(method, request, response, HttpServletResponse.SC_NOT_ACCEPTABLE);
}
} catch (Exception e) {
handleException(method, arguments, e);
} finally {
if (syncResponse) {
if (acceptableRequest) {
postHandleRequest(arguments, requestHandler);
}
arguments.release();
}
}
}
use of com.tvd12.ezyhttp.client.request.Request in project ezyhttp by youngmonkeys.
the class BlockingServletTest method doGetResponseContentTypeNull.
@Test
public void doGetResponseContentTypeNull() throws Exception {
// given
ComponentManager componentManager = ComponentManager.getInstance();
componentManager.setServerPort(PORT);
BlockingServlet sut = new BlockingServlet();
sut.init();
String requestURI = "/get";
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getMethod()).thenReturn(HttpMethod.GET.toString());
when(request.getRequestURI()).thenReturn(requestURI);
when(request.getServerPort()).thenReturn(PORT);
when(request.getParameterNames()).thenReturn(Collections.enumeration(Collections.singletonList("param")));
when(request.getParameter("param")).thenReturn("ParameterValue");
when(request.getParameterValues("param")).thenReturn(new String[] { "ParameterValue" });
when(request.getHeaderNames()).thenReturn(Collections.enumeration(Collections.singletonList("header")));
when(request.getHeader("header")).thenReturn("HeaderValue");
when(request.getCookies()).thenReturn(new Cookie[] { new Cookie("cookie", "CookieValue") });
HttpServletResponse response = mock(HttpServletResponse.class);
when(response.getContentType()).thenReturn(ContentTypes.APPLICATION_JSON);
ServletOutputStream outputStream = mock(ServletOutputStream.class);
when(response.getOutputStream()).thenReturn(outputStream);
AsyncContext asyncContext = mock(AsyncContext.class);
when(request.startAsync(request, response)).thenReturn(asyncContext);
when(request.isAsyncStarted()).thenReturn(true);
EzyWrap<AsyncListener> asyncListener = new EzyWrap<>();
doAnswer(it -> {
asyncListener.setValue(it.getArgumentAt(0, AsyncListener.class));
return null;
}).when(asyncContext).addListener(any(AsyncListener.class));
RequestHandlerManager requestHandlerManager = componentManager.getRequestHandlerManager();
GetRequestHandlerContentTypeNull requestHandler = new GetRequestHandlerContentTypeNull();
requestHandlerManager.addHandler(new RequestURI(HttpMethod.GET, requestURI, false), requestHandler);
// when
sut.service(request, response);
asyncListener.getValue().onComplete(new AsyncEvent(asyncContext));
// then
verify(request, times(1)).getMethod();
verify(request, times(1)).getRequestURI();
verify(asyncContext, times(1)).addListener(any(AsyncListener.class));
componentManager.destroy();
}
Aggregations