use of ninja.Cookie in project ninja by ninjaframework.
the class FlashScopeTest method testThatCookieUsesContextPath.
@Test
public void testThatCookieUsesContextPath() {
Mockito.when(context.getContextPath()).thenReturn("/my_context");
FlashScope flashScope = new FlashScopeImpl(ninjaProperties);
flashScope.put("anykey", "anyvalue");
flashScope.init(context);
flashScope.save(context);
verify(context).addCookie(cookieCaptor.capture());
Cookie cookie = cookieCaptor.getValue();
Assert.assertThat(cookie.getPath(), CoreMatchers.equalTo("/my_context/"));
}
use of ninja.Cookie in project ninja by ninjaframework.
the class SessionImpl method init.
@Override
public void init(Context context) {
try {
// get the cookie that contains session information:
Cookie cookie = context.getCookie(sessionCookieName);
// check that the cookie is not empty:
if (cookie != null && cookie.getValue() != null && !cookie.getValue().trim().isEmpty()) {
String value = cookie.getValue();
// the first substring until "-" is the sign
String sign = value.substring(0, value.indexOf("-"));
// rest from "-" until the end is the payload of the cookie
String payload = value.substring(value.indexOf("-") + 1);
// check if payload is valid:
if (CookieDataCodec.safeEquals(sign, crypto.signHmacSha1(payload))) {
payload = encryption.decrypt(payload);
CookieDataCodec.decode(data, payload);
}
// default session expire time.
if (data.containsKey(EXPIRY_TIME_KEY)) {
Long expiryTime = Long.parseLong(data.get(EXPIRY_TIME_KEY));
if (expiryTime >= 0) {
sessionExpireTimeInMs = expiryTime;
}
}
checkExpire();
}
} catch (UnsupportedEncodingException unsupportedEncodingException) {
logger.error("Encoding exception - this must not happen", unsupportedEncodingException);
}
}
use of ninja.Cookie in project ninja by ninjaframework.
the class AbstractContext method finalizeHeaders.
protected ResponseStreams finalizeHeaders(Result result, Boolean handleFlashAndSessionCookie) {
// copy ninja flash and session data directory to this context
if (handleFlashAndSessionCookie) {
flashScope.save(this);
session.save(this);
}
// copy any cookies from result
for (ninja.Cookie cookie : result.getCookies()) {
addCookie(cookie);
}
// subclasses responsible for creating the ResponseStreams instance
return null;
}
use of ninja.Cookie in project ninja by ninjaframework.
the class LangImplTest method testChangeLanguage.
@Test
public void testChangeLanguage() {
Cookie cookie = Cookie.builder("NINJA_TEST" + NinjaConstant.LANG_COOKIE_SUFFIX, "de").build();
when(ninjaProperties.getOrDie(NinjaConstant.applicationCookiePrefix)).thenReturn("NINJA_TEST");
Lang lang = new LangImpl(ninjaProperties);
// test with result
Result result = Results.noContent();
result = lang.setLanguage("to", result);
assertEquals("to", result.getCookie(cookie.getName()).getValue());
assertEquals(Result.SC_204_NO_CONTENT, result.getStatusCode());
}
use of ninja.Cookie in project ninja by ninjaframework.
the class LangImplTest method testGetLanguage.
@Test
public void testGetLanguage() {
Cookie cookie = Cookie.builder("NINJA_TEST" + NinjaConstant.LANG_COOKIE_SUFFIX, "de").build();
when(ninjaProperties.getOrDie(NinjaConstant.applicationCookiePrefix)).thenReturn("NINJA_TEST");
when(context.getCookie("NINJA_TEST" + NinjaConstant.LANG_COOKIE_SUFFIX)).thenReturn(cookie);
Lang lang = new LangImpl(ninjaProperties);
// 1) with context and result => but result does not have a default lang
Result result = Results.ok();
Optional<String> language = lang.getLanguage(context, Optional.of(result));
assertEquals("de", language.get());
// 2) with context and result => result has already new lang set...
result = Results.ok();
cookie = Cookie.builder("NINJA_TEST" + NinjaConstant.LANG_COOKIE_SUFFIX, "en").build();
result.addCookie(cookie);
language = lang.getLanguage(context, Optional.of(result));
assertEquals("en", language.get());
}
Aggregations