Search in sources :

Example 6 with MinijaxRequestContext

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

the class FormParamProvider method get.

@Override
@SuppressWarnings("unchecked")
public T get() {
    final MinijaxRequestContext context = MinijaxRequestContext.getThreadLocal();
    final Class<?> c = key.getType();
    final MinijaxForm form = context.getForm();
    final String name = key.getName();
    if (c == InputStream.class) {
        return form == null ? null : (T) form.getInputStream(name);
    }
    if (c == Part.class) {
        return form == null ? null : (T) form.getPart(name);
    }
    String value = form == null ? null : form.getString(name);
    final DefaultValue defaultValue = key.getDefaultValue();
    if (value == null && defaultValue != null) {
        value = defaultValue.value();
    }
    return (T) context.getApplication().convertParamToType(value, c, key.getAnnotations());
}
Also used : DefaultValue(javax.ws.rs.DefaultValue) MinijaxRequestContext(org.minijax.MinijaxRequestContext) MinijaxForm(org.minijax.MinijaxForm)

Example 7 with MinijaxRequestContext

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

the class RequestScopedTest method testRequestScoped.

@Test
public void testRequestScoped() throws IOException {
    final Minijax container = new Minijax();
    final MinijaxApplication application = container.getDefaultApplication();
    final MockHttpServletRequest r1 = new MockHttpServletRequest("GET", URI.create("/"));
    A a1;
    A a2;
    try (MinijaxRequestContext context = new MinijaxRequestContext(application, r1, null)) {
        a1 = container.getResource(A.class);
        assertNotNull(a1);
        a2 = container.getResource(A.class);
        assertEquals(a1, a2);
        assertTrue(a1 == a2);
    }
    final MockHttpServletRequest r2 = new MockHttpServletRequest("GET", URI.create("/"));
    A a3;
    A a4;
    try (MinijaxRequestContext context = new MinijaxRequestContext(application, r2, null)) {
        a3 = container.getResource(A.class);
        assertNotNull(a3);
        a4 = container.getResource(A.class);
        assertEquals(a3, a4);
        assertTrue(a3 == a4);
    }
    assertNotEquals(a1, a3);
    assertTrue(a1 != a3);
    container.getInjector().close();
}
Also used : MinijaxApplication(org.minijax.MinijaxApplication) MinijaxRequestContext(org.minijax.MinijaxRequestContext) Minijax(org.minijax.Minijax) MockHttpServletRequest(org.minijax.test.MockHttpServletRequest) Test(org.junit.Test)

Example 8 with MinijaxRequestContext

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

the class ChangePasswordTest method testUserWithoutPassword.

@Test
public void testUserWithoutPassword() throws IOException {
    final User user = new User();
    user.setName("Example 2");
    user.setEmail("pwd-2@example.com");
    user.setRoles("user");
    Cookie cookie = null;
    try (MinijaxRequestContext ctx = createRequestContext()) {
        ctx.get(Dao.class).create(user);
        cookie = ctx.get(Security.class).loginAs(user);
    }
    final Form form = new Form();
    form.param("csrf", cookie.getValue());
    form.param("oldPassword", "my-old-password");
    form.param("newPassword", "my-new-password");
    form.param("confirmNewPassword", "my-new-password");
    final Response r = target("/changepassword").request().cookie(cookie).post(Entity.form(form));
    assertNotNull(r);
    assertEquals(400, r.getStatus());
}
Also used : Cookie(javax.ws.rs.core.Cookie) Response(javax.ws.rs.core.Response) MinijaxRequestContext(org.minijax.MinijaxRequestContext) Form(javax.ws.rs.core.Form) MinijaxTest(org.minijax.test.MinijaxTest) Test(org.junit.Test)

Example 9 with MinijaxRequestContext

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

the class ChangePasswordTest method testPasswordTooShort.

@Test
public void testPasswordTooShort() throws IOException {
    final User user = new User();
    user.setName("Example 5");
    user.setEmail("pwd-5@example.com");
    user.setRoles("user");
    user.setPassword("my-old-password");
    Cookie cookie = null;
    try (MinijaxRequestContext ctx = createRequestContext()) {
        ctx.get(Dao.class).create(user);
        cookie = ctx.get(Security.class).loginAs(user);
    }
    final Form form = new Form();
    form.param("csrf", cookie.getValue());
    form.param("oldPassword", "my-old-password");
    form.param("newPassword", "foo");
    form.param("confirmNewPassword", "foo");
    final Response r = target("/changepassword").request().cookie(cookie).post(Entity.form(form));
    assertNotNull(r);
    assertEquals(400, r.getStatus());
}
Also used : Cookie(javax.ws.rs.core.Cookie) Response(javax.ws.rs.core.Response) MinijaxRequestContext(org.minijax.MinijaxRequestContext) Form(javax.ws.rs.core.Form) MinijaxTest(org.minijax.test.MinijaxTest) Test(org.junit.Test)

Example 10 with MinijaxRequestContext

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

the class ChangePasswordTest method testMismatchedPasswords.

@Test
public void testMismatchedPasswords() throws IOException {
    final User user = new User();
    user.setName("Example 4");
    user.setEmail("pwd-4@example.com");
    user.setRoles("user");
    user.setPassword("my-old-password");
    Cookie cookie = null;
    try (MinijaxRequestContext ctx = createRequestContext()) {
        ctx.get(Dao.class).create(user);
        cookie = ctx.get(Security.class).loginAs(user);
    }
    final Form form = new Form();
    form.param("csrf", cookie.getValue());
    form.param("oldPassword", "my-old-password");
    form.param("newPassword", "my-new-password");
    form.param("confirmNewPassword", "different-password");
    final Response r = target("/changepassword").request().cookie(cookie).post(Entity.form(form));
    assertNotNull(r);
    assertEquals(400, r.getStatus());
}
Also used : Cookie(javax.ws.rs.core.Cookie) Response(javax.ws.rs.core.Response) MinijaxRequestContext(org.minijax.MinijaxRequestContext) Form(javax.ws.rs.core.Form) MinijaxTest(org.minijax.test.MinijaxTest) Test(org.junit.Test)

Aggregations

MinijaxRequestContext (org.minijax.MinijaxRequestContext)25 Test (org.junit.Test)16 MinijaxTest (org.minijax.test.MinijaxTest)13 Response (javax.ws.rs.core.Response)10 Form (javax.ws.rs.core.Form)8 Cookie (javax.ws.rs.core.Cookie)7 Minijax (org.minijax.Minijax)4 MinijaxApplication (org.minijax.MinijaxApplication)4 MockHttpServletRequest (org.minijax.test.MockHttpServletRequest)3 IOException (java.io.IOException)2 NotFoundException (javax.ws.rs.NotFoundException)2 BeforeClass (org.junit.BeforeClass)2 DeploymentInfo (io.undertow.servlet.api.DeploymentInfo)1 WebSocketDeploymentInfo (io.undertow.websockets.jsr.WebSocketDeploymentInfo)1 AnnotatedEndpoint (io.undertow.websockets.jsr.annotated.AnnotatedEndpoint)1 InputStream (java.io.InputStream)1 InjectionException (javax.enterprise.inject.InjectionException)1 ServerEndpointConfig (javax.websocket.server.ServerEndpointConfig)1 DefaultValue (javax.ws.rs.DefaultValue)1 MediaType (javax.ws.rs.core.MediaType)1