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());
}
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();
}
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());
}
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());
}
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());
}
Aggregations