Search in sources :

Example 1 with SimpleRequestArguments

use of com.tvd12.ezyhttp.server.core.request.SimpleRequestArguments 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 2 with SimpleRequestArguments

use of com.tvd12.ezyhttp.server.core.request.SimpleRequestArguments in project ezyhttp by youngmonkeys.

the class SimpleRequestArgumentsTest method setRedirectionAttributesFromCookieTest.

@Test
public void setRedirectionAttributesFromCookieTest() throws IOException {
    // given
    ObjectMapper objectMapper = new ObjectMapper();
    SimpleRequestArguments sut = new SimpleRequestArguments();
    sut.setObjectMapper(objectMapper);
    sut.setResponse(mock(HttpServletResponse.class));
    Map<String, Map<String, Object>> data = new HashMap<>();
    data.put("hello", Collections.singletonMap("foo", true));
    data.put("world", Collections.singletonMap("bar", 10));
    String dataString = EzyBase64.encodeUtf(objectMapper.writeValueAsString(data));
    Cookie cookie = new Cookie(CoreConstants.COOKIE_REDIRECT_ATTRIBUTES_NAME, dataString);
    sut.setCookies(new Cookie[] { cookie });
    // when
    sut.setRedirectionAttributesFromCookie();
    // then
    Map<String, Object> actualAttributes = sut.getRedirectionAttributes();
    Asserts.assertEquals(actualAttributes, data, false);
    Asserts.assertEquals(sut.getRedirectionAttribute("hello"), data.get("hello"), false);
    Asserts.assertEquals(sut.getRedirectionAttribute("world", Map.class), data.get("world"), false);
    Asserts.assertEquals(sut.getRedirectionAttribute("world", Map.class, Collections.emptyMap()), data.get("world"), false);
    Asserts.assertEquals(sut.getRedirectionAttribute("hello", "world"), data.get("hello"), false);
    Asserts.assertEquals(sut.getRedirectionAttribute("hello"), data.get("hello"), false);
    Asserts.assertEquals(sut.getRedirectionAttribute("not found", true), true);
    Asserts.assertEquals(sut.getRedirectionAttribute("not found", int.class, 0), 0);
    sut.release();
}
Also used : Cookie(javax.servlet.http.Cookie) HashMap(java.util.HashMap) HttpServletResponse(javax.servlet.http.HttpServletResponse) SimpleRequestArguments(com.tvd12.ezyhttp.server.core.request.SimpleRequestArguments) HashMap(java.util.HashMap) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.testng.annotations.Test)

Example 3 with SimpleRequestArguments

use of com.tvd12.ezyhttp.server.core.request.SimpleRequestArguments in project ezyhttp by youngmonkeys.

the class SimpleRequestArgumentsTest method getByDefaultTest.

@Test
public void getByDefaultTest() {
    // given
    SimpleRequestArguments sut = new SimpleRequestArguments();
    // when
    // then
    Asserts.assertEquals("paramValue", sut.getParameter(1, "paramValue"));
    Asserts.assertEquals("paramValue", sut.getParameter("key", "paramValue"));
    Asserts.assertEquals("headerValue", sut.getHeader(1, "headerValue"));
    Asserts.assertEquals("headerValue", sut.getHeader("key", "headerValue"));
    Asserts.assertEquals("cookieValue", sut.getCookieValue(1, "cookieValue"));
    Asserts.assertEquals("cookieValue", sut.getCookieValue("key", "cookieValue"));
    sut.release();
}
Also used : SimpleRequestArguments(com.tvd12.ezyhttp.server.core.request.SimpleRequestArguments) Test(org.testng.annotations.Test)

Example 4 with SimpleRequestArguments

use of com.tvd12.ezyhttp.server.core.request.SimpleRequestArguments in project ezyhttp by youngmonkeys.

the class SimpleRequestArgumentsTest method argumentTest.

@Test
public void argumentTest() {
    // given
    SimpleRequestArguments sut = new SimpleRequestArguments();
    // when
    // then
    Asserts.assertNull(sut.getArgument("unknown"));
    sut.release();
}
Also used : SimpleRequestArguments(com.tvd12.ezyhttp.server.core.request.SimpleRequestArguments) Test(org.testng.annotations.Test)

Example 5 with SimpleRequestArguments

use of com.tvd12.ezyhttp.server.core.request.SimpleRequestArguments in project ezyhttp by youngmonkeys.

the class SimpleRequestArgumentsTest method parameterTest.

@SuppressWarnings("unchecked")
@Test
public void parameterTest() {
    // given
    SimpleRequestArguments sut = new SimpleRequestArguments();
    sut.setParameter("hello", new String[] { "world" });
    sut.setParameter("foo", new String[] { "bar" });
    // when
    String paramOverSize = sut.getParameter(3);
    String paramByName = sut.getParameter("hello");
    Map<String, String> params = sut.getParameters();
    // then
    Asserts.assertNull(paramOverSize);
    Asserts.assertEquals("world", paramByName);
    Map<String, String> expectedParams = EzyMapBuilder.mapBuilder().put("hello", "world").put("foo", "bar").build();
    Asserts.assertEquals(expectedParams, params);
    sut.release();
}
Also used : SimpleRequestArguments(com.tvd12.ezyhttp.server.core.request.SimpleRequestArguments) Test(org.testng.annotations.Test)

Aggregations

SimpleRequestArguments (com.tvd12.ezyhttp.server.core.request.SimpleRequestArguments)16 Test (org.testng.annotations.Test)15 Cookie (javax.servlet.http.Cookie)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 AsyncContext (javax.servlet.AsyncContext)1 ServletInputStream (javax.servlet.ServletInputStream)1