Search in sources :

Example 11 with SimpleRequestArguments

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

the class SimpleRequestArgumentsTest method setParameterEmpty.

@Test
public void setParameterEmpty() {
    // given
    SimpleRequestArguments sut = new SimpleRequestArguments();
    sut.setParameter("key", new String[0]);
    // when
    // then
    Asserts.assertEquals("", sut.getParameter(0, "paramValue"));
    Asserts.assertEquals("", sut.getParameter("key", "paramValue"));
    sut.release();
}
Also used : SimpleRequestArguments(com.tvd12.ezyhttp.server.core.request.SimpleRequestArguments) Test(org.testng.annotations.Test)

Example 12 with SimpleRequestArguments

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

the class SimpleRequestArgumentsTest method test.

@Test
public void test() throws Exception {
    // given
    SimpleRequestArguments sut = new SimpleRequestArguments();
    sut.setMethod(HttpMethod.HEAD);
    sut.setArgument("arg1", "argValue1");
    sut.setUriTemplate("/");
    ServletInputStream inputStream = mock(ServletInputStream.class);
    HttpServletRequest servletRequest = mock(HttpServletRequest.class);
    when(servletRequest.getInputStream()).thenReturn(inputStream);
    when(servletRequest.getRequestURI()).thenReturn("/");
    when(servletRequest.getContentType()).thenReturn(ContentTypes.TEXT_HTML_UTF8);
    sut.setRequest(servletRequest);
    AsyncContext asyncContext = mock(AsyncContext.class);
    when(servletRequest.getAsyncContext()).thenReturn(asyncContext);
    HttpServletResponse servletResponse = mock(HttpServletResponse.class);
    sut.setResponse(servletResponse);
    // when
    String arg1 = sut.getArgument("arg1");
    // then
    Asserts.assertEquals("argValue1", arg1);
    Asserts.assertNull(sut.getArgument("unknown"));
    Asserts.assertNull(sut.getParameter(0));
    Asserts.assertNull(sut.getParameter("unknown"));
    Asserts.assertNull(sut.getHeader(0));
    Asserts.assertNull(sut.getHeader("unknown"));
    Asserts.assertNull(sut.getPathVariable(0));
    Asserts.assertNull(sut.getPathVariable("unknown"));
    Asserts.assertEquals(0, sut.getContentLength());
    Asserts.assertEquals(ContentTypes.TEXT_HTML, sut.getContentType());
    Asserts.assertEquals(ContentTypes.TEXT_HTML_UTF8, sut.getRequestContentType());
    Asserts.assertEquals(inputStream, sut.getInputStream());
    Asserts.assertNull(sut.getCookie("unknown"));
    Asserts.assertNull(sut.getCookieValue(0));
    Asserts.assertNull(sut.getCookieValue("unknown"));
    Asserts.assertEquals(HttpMethod.HEAD, sut.getMethod());
    Asserts.assertEquals(sut.getAsyncContext(), asyncContext);
    Asserts.assertFalse(sut.isAsyncStarted());
    Asserts.assertEquals(sut.getUriTemplate(), "/");
    verify(servletRequest, times(1)).getRequestURI();
    verify(servletRequest, times(1)).getInputStream();
    sut.release();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletInputStream(javax.servlet.ServletInputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncContext(javax.servlet.AsyncContext) SimpleRequestArguments(com.tvd12.ezyhttp.server.core.request.SimpleRequestArguments) Test(org.testng.annotations.Test)

Example 13 with SimpleRequestArguments

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

the class SimpleRequestArgumentsTest method setRedirectionAttributesFromCookieExceptionTest.

@Test
public void setRedirectionAttributesFromCookieExceptionTest() {
    // given
    ObjectMapper objectMapper = new ObjectMapper();
    SimpleRequestArguments sut = new SimpleRequestArguments();
    sut.setObjectMapper(objectMapper);
    sut.setResponse(mock(HttpServletResponse.class));
    String dataString = EzyBase64.encodeUtf("hello world");
    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.assertNull(actualAttributes);
    Asserts.assertNull(sut.getRedirectionAttribute("hello"));
    Asserts.assertNull(sut.getRedirectionAttribute("hello", Map.class));
}
Also used : Cookie(javax.servlet.http.Cookie) 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 14 with SimpleRequestArguments

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

the class SimpleRequestArgumentsTest method cookieTest.

@Test
public void cookieTest() {
    // given
    SimpleRequestArguments sut = new SimpleRequestArguments();
    sut.setCookies(new Cookie[] { new Cookie("hello", ""), new Cookie("hello", "world"), new Cookie("hello", "") });
    // when
    // then
    Asserts.assertNull(sut.getCookieValue(3));
    Asserts.assertEquals("world", sut.getCookieValue(0));
    Asserts.assertEquals("world", sut.getCookie("hello").getValue());
    Asserts.assertEquals("world", sut.getCookieValue("hello"));
    Asserts.assertNull(sut.getCookie("unknown"));
    sut.release();
}
Also used : Cookie(javax.servlet.http.Cookie) SimpleRequestArguments(com.tvd12.ezyhttp.server.core.request.SimpleRequestArguments) Test(org.testng.annotations.Test)

Example 15 with SimpleRequestArguments

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

the class SimpleRequestArgumentsTest method argumentButDebugTest.

@Test
public void argumentButDebugTest() {
    // given
    SimpleRequestArguments sut = new SimpleRequestArguments();
    sut.setDebug(true);
    // when
    Throwable e = Asserts.assertThrows(() -> sut.getArgument("unknown"));
    // then
    Asserts.assertEqualsType(e, NullPointerException.class);
    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