Search in sources :

Example 11 with MinijaxRequestContext

use of org.minijax.rs.MinijaxRequestContext in project minijax by minijax.

the class RolesAllowedTest method testLoginIncorrectPassword.

@Test
void testLoginIncorrectPassword() throws Exception {
    try (final MinijaxRequestContext ctx = createRequestContext()) {
        final LoginResult result = ctx.getResource(Security.class).login("alice@example.com", "wrong_password");
        assertEquals(LoginResult.Status.INCORRECT, result.getStatus());
    }
}
Also used : MinijaxRequestContext(org.minijax.rs.MinijaxRequestContext) MinijaxTest(org.minijax.rs.test.MinijaxTest) Test(org.junit.jupiter.api.Test)

Example 12 with MinijaxRequestContext

use of org.minijax.rs.MinijaxRequestContext in project minijax by minijax.

the class PersistenceContextTest method testPersistenceContextInject.

@Test
void testPersistenceContextInject() throws IOException {
    final Minijax container = new Minijax().register(PersistenceFeature.class).register(PersistenceContextDao.class);
    final MinijaxApplication application = container.getDefaultApplication();
    try (MinijaxRequestContext context = new MinijaxTestRequestContext(application, GET, "/")) {
        final PersistenceContextDao dao = context.getResource(PersistenceContextDao.class);
        final Widget widget = new Widget();
        widget.setId(123);
        widget.setName("test");
        final Widget result = dao.create(widget);
        assertNotNull(result);
        assertEquals(123, result.getId());
    }
    container.getInjector().close();
}
Also used : MinijaxApplication(org.minijax.rs.MinijaxApplication) MinijaxRequestContext(org.minijax.rs.MinijaxRequestContext) Minijax(org.minijax.Minijax) MinijaxTestRequestContext(org.minijax.rs.test.MinijaxTestRequestContext) Test(org.junit.jupiter.api.Test)

Example 13 with MinijaxRequestContext

use of org.minijax.rs.MinijaxRequestContext in project minijax by minijax.

the class ListenerTest method testHappyPath.

@Test
void testHappyPath() throws IOException {
    final Minijax minijax = new Minijax().register(TestSocket.class);
    final MinijaxApplication application = minijax.getDefaultApplication();
    try (final MinijaxRequestContext ctx = createRequestContext()) {
        final MinijaxUndertowWebSocketAdapter adapter = new MinijaxUndertowWebSocketAdapter(ctx, TestSocket.class);
        final WebSocketHttpExchange exchange = mock(WebSocketHttpExchange.class);
        final MinijaxUndertowWebSocketListener listener = new MinijaxUndertowWebSocketListener(application, adapter, exchange);
        final WebSocketChannel channel = mock(WebSocketChannel.class);
        final BufferedTextMessage message = mock(BufferedTextMessage.class);
        listener.onFullTextMessage(channel, message);
    }
}
Also used : WebSocketHttpExchange(io.undertow.websockets.spi.WebSocketHttpExchange) MinijaxApplication(org.minijax.rs.MinijaxApplication) MinijaxRequestContext(org.minijax.rs.MinijaxRequestContext) Minijax(org.minijax.Minijax) WebSocketChannel(io.undertow.websockets.core.WebSocketChannel) BufferedTextMessage(io.undertow.websockets.core.BufferedTextMessage) Test(org.junit.jupiter.api.Test) MinijaxTest(org.minijax.rs.test.MinijaxTest)

Example 14 with MinijaxRequestContext

use of org.minijax.rs.MinijaxRequestContext in project minijax by minijax.

the class MinijaxServlet method service.

@Override
protected void service(final HttpServletRequest servletRequest, final HttpServletResponse servletResponse) {
    final MinijaxApplication application = minijax.getDefaultApplication();
    try (final MinijaxRequestContext ctx = new MinijaxServletRequestContext(application, servletRequest)) {
        final Response response = application.handle(ctx);
        servletResponse.setStatus(response.getStatus());
        for (final Entry<String, List<Object>> entry : response.getHeaders().entrySet()) {
            final String name = entry.getKey();
            for (final Object value : entry.getValue()) {
                servletResponse.addHeader(name, value.toString());
            }
        }
        final MediaType mediaType = response.getMediaType();
        if (mediaType != null) {
            servletResponse.setContentType(mediaType.toString());
        }
        EntityUtils.writeEntity(response.getEntity(), mediaType, ctx.getProviders(), servletResponse.getOutputStream());
    } catch (final IOException ex) {
        LOG.error("Unhandled exception: {}", ex.getMessage(), ex);
    }
}
Also used : Response(jakarta.ws.rs.core.Response) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) MinijaxApplication(org.minijax.rs.MinijaxApplication) MinijaxRequestContext(org.minijax.rs.MinijaxRequestContext) MediaType(jakarta.ws.rs.core.MediaType) List(java.util.List) IOException(java.io.IOException)

Example 15 with MinijaxRequestContext

use of org.minijax.rs.MinijaxRequestContext in project minijax by minijax.

the class MinijaxUndertowServer method handleRequest.

@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
    final MinijaxApplication application = minijax.getDefaultApplication();
    try (final MinijaxRequestContext ctx = new MinijaxUndertowRequestContext(application, exchange)) {
        final Response response = application.handle(ctx);
        exchange.setStatusCode(response.getStatus());
        for (final Entry<String, List<Object>> entry : response.getHeaders().entrySet()) {
            final String name = entry.getKey();
            for (final Object value : entry.getValue()) {
                HttpString headerString = Headers.fromCache(name);
                if (headerString == null) {
                    headerString = HttpString.tryFromString(name);
                }
                exchange.getResponseHeaders().add(headerString, value.toString());
            }
        }
        final MediaType mediaType = response.getMediaType();
        if (mediaType != null) {
            exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, mediaType.toString());
        }
        EntityUtils.writeEntity(response.getEntity(), mediaType, ctx.getProviders(), exchange.getOutputStream());
    } catch (final Exception ex) {
        // NOSONAR
        LOG.error("Unhandled exception: {}", ex.getMessage(), ex);
        // NOSONAR
        throw ex;
    }
}
Also used : Response(jakarta.ws.rs.core.Response) MinijaxApplication(org.minijax.rs.MinijaxApplication) MinijaxRequestContext(org.minijax.rs.MinijaxRequestContext) MediaType(jakarta.ws.rs.core.MediaType) List(java.util.List) HttpString(io.undertow.util.HttpString) HttpString(io.undertow.util.HttpString)

Aggregations

MinijaxRequestContext (org.minijax.rs.MinijaxRequestContext)33 Test (org.junit.jupiter.api.Test)23 MinijaxTest (org.minijax.rs.test.MinijaxTest)21 Response (jakarta.ws.rs.core.Response)13 Form (jakarta.ws.rs.core.Form)8 MinijaxApplication (org.minijax.rs.MinijaxApplication)7 Cookie (jakarta.ws.rs.core.Cookie)6 MediaType (jakarta.ws.rs.core.MediaType)4 Minijax (org.minijax.Minijax)4 IOException (java.io.IOException)3 List (java.util.List)3 NotFoundException (jakarta.ws.rs.NotFoundException)2 InputStream (java.io.InputStream)2 BeforeAll (org.junit.jupiter.api.BeforeAll)2 MinijaxTestRequestContext (org.minijax.rs.test.MinijaxTestRequestContext)2 ByteBuf (io.netty.buffer.ByteBuf)1 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)1 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)1 AsciiString (io.netty.util.AsciiString)1 HttpString (io.undertow.util.HttpString)1