Search in sources :

Example 16 with MockHttpServletRequest

use of org.minijax.test.MockHttpServletRequest in project minijax by minijax.

the class ServletFilterTest method testServletFilterMissingContext.

@Test
public void testServletFilterMissingContext() throws ServletException, IOException {
    final Minijax minijax = new Minijax();
    final MinijaxApplication application = minijax.getDefaultApplication();
    final MinijaxServlet servlet = new MinijaxServlet(application);
    final MockFilterChain chain = new MockFilterChain(servlet);
    final MinijaxFilter filter = new MinijaxFilter(application);
    filter.init(null);
    final MockHttpServletRequest request = new MockHttpServletRequest("GET", URI.create("/"));
    final MockHttpServletResponse response = new MockHttpServletResponse();
    filter.doFilter(request, response, chain);
    assertTrue(chain.success);
}
Also used : MockHttpServletRequest(org.minijax.test.MockHttpServletRequest) MockHttpServletResponse(org.minijax.test.MockHttpServletResponse) Test(org.junit.Test)

Example 17 with MockHttpServletRequest

use of org.minijax.test.MockHttpServletRequest in project minijax by minijax.

the class ServletRequestContextTest method testFormMissingContentType.

@Test
public void testFormMissingContentType() throws IOException {
    final ByteArrayInputStream mockContentBody = new ByteArrayInputStream("a=b".getBytes(StandardCharsets.UTF_8));
    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("POST", URI.create("/"), null, mockContentBody, null);
    try (final MinijaxRequestContext context = new MinijaxRequestContext(null, mockRequest, null)) {
        assertNull(context.getForm());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) MockHttpServletRequest(org.minijax.test.MockHttpServletRequest) Test(org.junit.Test)

Example 18 with MockHttpServletRequest

use of org.minijax.test.MockHttpServletRequest in project minijax by minijax.

the class ServletRequestContextTest method testMultipartForm.

@Test
public void testMultipartForm() throws IOException {
    final MultivaluedMap<String, String> mockHeaders = new MultivaluedHashMap<>();
    mockHeaders.add("Content-Type", "multipart/form-data");
    final String mockContent = "------WebKitFormBoundarycTqA2AimXQHBAJbZ\n" + "Content-Disposition: form-data; name=\"a\"\n" + "\n" + "b\n" + "------WebKitFormBoundarycTqA2AimXQHBAJbZ";
    final ByteArrayInputStream mockContentBody = new ByteArrayInputStream(mockContent.getBytes(StandardCharsets.UTF_8));
    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("POST", URI.create("/"), mockHeaders, mockContentBody, null);
    try (final MinijaxRequestContext context = new MinijaxRequestContext(null, mockRequest, null)) {
        final MinijaxForm form = context.getForm();
        assertTrue(form instanceof MinijaxMultipartForm);
        assertEquals("b", form.getString("a"));
        // Assert that same cached object
        assertTrue(form == context.getForm());
    }
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) MockHttpServletRequest(org.minijax.test.MockHttpServletRequest) Test(org.junit.Test)

Example 19 with MockHttpServletRequest

use of org.minijax.test.MockHttpServletRequest in project minijax by minijax.

the class ServletRequestContextTest method testHeaders.

@Test
public void testHeaders() throws IOException {
    final MultivaluedMap<String, String> mockHeaders = new MultivaluedHashMap<>();
    mockHeaders.add("a", "b");
    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET", URI.create("/"), mockHeaders, null, null);
    try (final MinijaxRequestContext context = new MinijaxRequestContext(null, mockRequest, null)) {
        final MultivaluedMap<String, String> headers = context.getHeaders();
        assertEquals("b", headers.get("a").get(0));
        // Assert that same cached object
        assertTrue(headers == context.getHeaders());
    }
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) MockHttpServletRequest(org.minijax.test.MockHttpServletRequest) Test(org.junit.Test)

Example 20 with MockHttpServletRequest

use of org.minijax.test.MockHttpServletRequest in project minijax by minijax.

the class ServletRequestContextTest method testFormUnknownContentType.

@Test(expected = BadRequestException.class)
public void testFormUnknownContentType() throws IOException {
    final MultivaluedMap<String, String> mockHeaders = new MultivaluedHashMap<>();
    mockHeaders.add("Content-Type", "text/plain");
    final ByteArrayInputStream mockContentBody = new ByteArrayInputStream("a=b".getBytes(StandardCharsets.UTF_8));
    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("POST", URI.create("/"), mockHeaders, mockContentBody, null);
    try (final MinijaxRequestContext context = new MinijaxRequestContext(null, mockRequest, null)) {
        context.getForm();
    }
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) MockHttpServletRequest(org.minijax.test.MockHttpServletRequest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)23 MockHttpServletRequest (org.minijax.test.MockHttpServletRequest)23 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)16 ByteArrayInputStream (java.io.ByteArrayInputStream)4 Minijax (org.minijax.Minijax)3 MinijaxApplication (org.minijax.MinijaxApplication)3 MinijaxRequestContext (org.minijax.MinijaxRequestContext)3 MockHttpServletResponse (org.minijax.test.MockHttpServletResponse)3 Cookie (javax.servlet.http.Cookie)2 MinijaxTest (org.minijax.test.MinijaxTest)2 DeploymentInfo (io.undertow.servlet.api.DeploymentInfo)1 WebSocketDeploymentInfo (io.undertow.websockets.jsr.WebSocketDeploymentInfo)1 AnnotatedEndpoint (io.undertow.websockets.jsr.annotated.AnnotatedEndpoint)1 ArrayList (java.util.ArrayList)1 Locale (java.util.Locale)1 ServerEndpointConfig (javax.websocket.server.ServerEndpointConfig)1 Cookie (javax.ws.rs.core.Cookie)1 MediaType (javax.ws.rs.core.MediaType)1 Widget (org.minijax.db.test.Widget)1