Search in sources :

Example 66 with Request

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);
}
Also used : PostRequest(com.tvd12.ezyhttp.client.request.PostRequest) HttpClient(com.tvd12.ezyhttp.client.HttpClient) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 67 with Request

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);
}
Also used : PostRequest(com.tvd12.ezyhttp.client.request.PostRequest) HttpClient(com.tvd12.ezyhttp.client.HttpClient) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 68 with Request

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;
}
Also used : SimpleRequestArguments(com.tvd12.ezyhttp.server.core.request.SimpleRequestArguments)

Example 69 with Request

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();
        }
    }
}
Also used : RequestHandler(com.tvd12.ezyhttp.server.core.handler.RequestHandler) AsyncContext(javax.servlet.AsyncContext) RequestArguments(com.tvd12.ezyhttp.server.core.request.RequestArguments) SimpleRequestArguments(com.tvd12.ezyhttp.server.core.request.SimpleRequestArguments) ServletException(javax.servlet.ServletException) DeserializeValueException(com.tvd12.ezyhttp.core.exception.DeserializeValueException) IOException(java.io.IOException) HttpRequestException(com.tvd12.ezyhttp.core.exception.HttpRequestException)

Example 70 with Request

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();
}
Also used : Cookie(javax.servlet.http.Cookie) RequestCookie(com.tvd12.ezyhttp.server.core.annotation.RequestCookie) EzyWrap(com.tvd12.ezyfox.util.EzyWrap) ServletOutputStream(javax.servlet.ServletOutputStream) BlockingServlet(com.tvd12.ezyhttp.server.core.servlet.BlockingServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncContext(javax.servlet.AsyncContext) ToString(lombok.ToString) AsyncEvent(javax.servlet.AsyncEvent) HttpServletRequest(javax.servlet.http.HttpServletRequest) AsyncListener(javax.servlet.AsyncListener) RequestHandlerManager(com.tvd12.ezyhttp.server.core.manager.RequestHandlerManager) ComponentManager(com.tvd12.ezyhttp.server.core.manager.ComponentManager) RequestURI(com.tvd12.ezyhttp.server.core.request.RequestURI) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)128 HttpServletResponse (javax.servlet.http.HttpServletResponse)48 HttpServletRequest (javax.servlet.http.HttpServletRequest)45 EzySession (com.tvd12.ezyfoxserver.entity.EzySession)37 BeforeTest (org.testng.annotations.BeforeTest)36 BaseTest (com.tvd12.test.base.BaseTest)31 EzyArray (com.tvd12.ezyfox.entity.EzyArray)30 ComponentManager (com.tvd12.ezyhttp.server.core.manager.ComponentManager)29 BlockingServlet (com.tvd12.ezyhttp.server.core.servlet.BlockingServlet)29 ToString (lombok.ToString)29 RequestURI (com.tvd12.ezyhttp.server.core.request.RequestURI)27 ServletOutputStream (javax.servlet.ServletOutputStream)25 EzyServerContext (com.tvd12.ezyfoxserver.context.EzyServerContext)24 RequestHandlerManager (com.tvd12.ezyhttp.server.core.manager.RequestHandlerManager)24 Cookie (javax.servlet.http.Cookie)24 HttpClientProxy (com.tvd12.ezyhttp.client.HttpClientProxy)22 RequestCookie (com.tvd12.ezyhttp.server.core.annotation.RequestCookie)22 EzyZoneContext (com.tvd12.ezyfoxserver.context.EzyZoneContext)21 HttpClient (com.tvd12.ezyhttp.client.HttpClient)21 PostRequest (com.tvd12.ezyhttp.client.request.PostRequest)21