Search in sources :

Example 1 with MockHttpServletRequest

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

the class PersistenceContextTest method testPersistenceContextInject.

@Test
public void testPersistenceContextInject() throws IOException {
    final Minijax container = new Minijax().register(PersistenceFeature.class).register(PersistenceContextDao.class);
    final MinijaxApplication application = container.getDefaultApplication();
    final MockHttpServletRequest request = new MockHttpServletRequest("GET", URI.create("/"));
    try (MinijaxRequestContext context = new MinijaxRequestContext(application, request, null)) {
        final PersistenceContextDao dao = container.getResource(PersistenceContextDao.class);
        final Widget widget = new Widget();
        widget.setName("test");
        final Widget result = dao.create(widget);
        assertNotNull(result);
        assertNotNull(result.getId());
    }
    container.getInjector().close();
}
Also used : MinijaxApplication(org.minijax.MinijaxApplication) MinijaxRequestContext(org.minijax.MinijaxRequestContext) Minijax(org.minijax.Minijax) MockHttpServletRequest(org.minijax.test.MockHttpServletRequest) Widget(org.minijax.db.test.Widget) Test(org.junit.Test)

Example 2 with MockHttpServletRequest

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

the class WebSocketTest method testRun.

@Test
public void testRun() throws Exception {
    final Minijax minijax = createMinijax();
    minijax.register(WebSocketResource.class);
    minijax.start();
    final MinijaxApplication application = minijax.getDefaultApplication();
    final DeploymentInfo deploymentInfo = new DeploymentInfo();
    MinijaxWebSocketUtils.init(deploymentInfo, application);
    final WebSocketDeploymentInfo webSocketDeploymentInfo = (WebSocketDeploymentInfo) deploymentInfo.getServletContextAttributes().get(WebSocketDeploymentInfo.ATTRIBUTE_NAME);
    final ServerEndpointConfig endpointConfig = webSocketDeploymentInfo.getProgramaticEndpoints().get(0);
    final MinijaxWebSocketConfigurator configurator = (MinijaxWebSocketConfigurator) endpointConfig.getConfigurator();
    final MockHttpServletRequest request = new MockHttpServletRequest("GET", URI.create("/echo"));
    try (MinijaxRequestContext context = new MinijaxRequestContext(application, request, null)) {
        configurator.modifyHandshake(endpointConfig, null, null);
        final AnnotatedEndpoint endpoint = configurator.getEndpointInstance(AnnotatedEndpoint.class);
        assertNotNull(endpoint);
    }
}
Also used : MinijaxApplication(org.minijax.MinijaxApplication) MinijaxRequestContext(org.minijax.MinijaxRequestContext) ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) Minijax(org.minijax.Minijax) MockHttpServletRequest(org.minijax.test.MockHttpServletRequest) AnnotatedEndpoint(io.undertow.websockets.jsr.annotated.AnnotatedEndpoint) WebSocketDeploymentInfo(io.undertow.websockets.jsr.WebSocketDeploymentInfo) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) WebSocketDeploymentInfo(io.undertow.websockets.jsr.WebSocketDeploymentInfo) Test(org.junit.Test)

Example 3 with MockHttpServletRequest

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

the class ServletRequestContextTest method testAcceptableMediaTypes.

@Test
public void testAcceptableMediaTypes() throws IOException {
    final MultivaluedMap<String, String> mockHeaders = new MultivaluedHashMap<>();
    mockHeaders.add("Accept", "text/plain");
    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET", URI.create("/"), mockHeaders, null, null);
    try (final MinijaxRequestContext context = new MinijaxRequestContext(null, mockRequest, null)) {
        final List<MediaType> mediaTypes = context.getAcceptableMediaTypes();
        assertEquals(1, mediaTypes.size());
        assertEquals("text/plain", mediaTypes.get(0).toString());
        // Assert that same cached object
        assertTrue(mediaTypes == context.getAcceptableMediaTypes());
    }
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) MockHttpServletRequest(org.minijax.test.MockHttpServletRequest) MediaType(javax.ws.rs.core.MediaType) Test(org.junit.Test)

Example 4 with MockHttpServletRequest

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

the class ServletRequestContextTest method testCookies.

@Test
public void testCookies() throws IOException {
    final javax.servlet.http.Cookie[] mockCookies = new javax.servlet.http.Cookie[] { new javax.servlet.http.Cookie("a", "b") };
    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET", URI.create("/"), null, null, mockCookies);
    try (final MinijaxRequestContext context = new MinijaxRequestContext(null, mockRequest, null)) {
        final Map<String, Cookie> cookies = context.getCookies();
        assertEquals("b", cookies.get("a").getValue());
        // Assert that same cached object
        assertTrue(cookies == context.getCookies());
    }
}
Also used : Cookie(javax.ws.rs.core.Cookie) MockHttpServletRequest(org.minijax.test.MockHttpServletRequest) Test(org.junit.Test)

Example 5 with MockHttpServletRequest

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

the class ServletRequestContextTest method testUrlEncodedForm.

@Test
public void testUrlEncodedForm() throws IOException {
    final MultivaluedMap<String, String> mockHeaders = new MultivaluedHashMap<>();
    mockHeaders.add("Content-Type", "application/x-www-form-urlencoded");
    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)) {
        final MinijaxForm form = context.getForm();
        assertTrue(form instanceof MinijaxUrlEncodedForm);
        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)

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