Search in sources :

Example 6 with MockHttpServletRequest

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

the class ServletRequestContextTest method testProperties.

@Test
public void testProperties() throws IOException {
    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET", URI.create("/"));
    try (final MinijaxRequestContext context = new MinijaxRequestContext(null, mockRequest, null)) {
        context.setProperty("a", "b");
        assertEquals("b", context.getProperty("a"));
        assertEquals("a", new ArrayList<>(context.getPropertyNames()).get(0));
        context.removeProperty("a");
        assertTrue(context.getPropertyNames().isEmpty());
    }
}
Also used : MockHttpServletRequest(org.minijax.test.MockHttpServletRequest) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 7 with MockHttpServletRequest

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

the class ServletRequestContextTest method testAcceptableLanguages.

@Test
public void testAcceptableLanguages() throws IOException {
    final MultivaluedMap<String, String> mockHeaders = new MultivaluedHashMap<>();
    mockHeaders.add("Accept-Language", "en-US");
    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET", URI.create("/"), mockHeaders, null, null);
    try (final MinijaxRequestContext context = new MinijaxRequestContext(null, mockRequest, null)) {
        final List<Locale> locales = context.getAcceptableLanguages();
        assertEquals(1, locales.size());
        assertEquals("en-US", locales.get(0).toLanguageTag());
        // Assert that same cached object
        assertTrue(locales == context.getAcceptableLanguages());
    }
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) Locale(java.util.Locale) MockHttpServletRequest(org.minijax.test.MockHttpServletRequest) Test(org.junit.Test)

Example 8 with MockHttpServletRequest

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

the class ServletTest method testServletResponse.

@Test
public void testServletResponse() throws IOException {
    final MultivaluedMap<String, String> headers = new MultivaluedHashMap<>();
    final Cookie[] cookies = new Cookie[0];
    final MockHttpServletRequest servletRequest = new MockHttpServletRequest("GET", URI.create("/servlet"), headers, null, cookies);
    final MockHttpServletResponse servletResponse = new MockHttpServletResponse();
    final MinijaxRequestContext context = new MinijaxRequestContext(null, servletRequest, servletResponse);
    getServer().getDefaultApplication().handle(context, servletResponse);
    assertEquals(200, servletResponse.getStatus());
    assertEquals("text/plain", servletResponse.getContentType());
    assertEquals("Hello world!", servletResponse.getOutput().trim());
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) Cookie(javax.servlet.http.Cookie) MockHttpServletRequest(org.minijax.test.MockHttpServletRequest) MockHttpServletResponse(org.minijax.test.MockHttpServletResponse) MinijaxTest(org.minijax.test.MinijaxTest) Test(org.junit.Test)

Example 9 with MockHttpServletRequest

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

the class ServletTest method testNullResponse.

@Test
public void testNullResponse() throws IOException {
    final MultivaluedMap<String, String> headers = new MultivaluedHashMap<>();
    final Cookie[] cookies = new Cookie[0];
    final MockHttpServletRequest servletRequest = new MockHttpServletRequest("GET", URI.create("/null"), headers, null, cookies);
    final MockHttpServletResponse servletResponse = new MockHttpServletResponse();
    final MinijaxRequestContext context = new MinijaxRequestContext(null, servletRequest, servletResponse);
    getServer().getDefaultApplication().handle(context, servletResponse);
    assertEquals(404, servletResponse.getStatus());
    assertNull(servletResponse.getContentType());
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) Cookie(javax.servlet.http.Cookie) MockHttpServletRequest(org.minijax.test.MockHttpServletRequest) MockHttpServletResponse(org.minijax.test.MockHttpServletResponse) MinijaxTest(org.minijax.test.MinijaxTest) Test(org.junit.Test)

Example 10 with MockHttpServletRequest

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

the class HttpHeadersTest method testContentLength.

@Test
public void testContentLength() {
    final MultivaluedMap<String, String> headers = new MultivaluedHashMap<>();
    headers.add("Content-Length", "1024");
    final MockHttpServletRequest request = new MockHttpServletRequest("GET", URI.create("/"), headers, null, null);
    final MinijaxHttpHeaders httpHeaders = new MinijaxHttpHeaders(request);
    assertEquals(1024, httpHeaders.getLength());
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) 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