Search in sources :

Example 21 with Cookie

use of ninja.Cookie in project ninja by ninjaframework.

the class SessionImplTest method testThatCookieSavingAndInitingWorks.

@Test
public void testThatCookieSavingAndInitingWorks() {
    Session sessionCookie = createNewSession();
    sessionCookie.init(context);
    sessionCookie.put("key1", "value1");
    sessionCookie.put("key2", "value2");
    sessionCookie.put("key3", "value3");
    // put nothing => intentionally to check if no session cookie will be
    // saved
    sessionCookie.save(context);
    // a cookie will be set
    verify(context).addCookie(cookieCaptor.capture());
    // now we simulate a new request => the session storage will generate a
    // new cookie:
    Cookie newSessionCookie = Cookie.builder(cookieCaptor.getValue().getName(), cookieCaptor.getValue().getValue()).build();
    // that will be returned by the httprequest...
    when(context.getCookie(cookieCaptor.getValue().getName())).thenReturn(newSessionCookie);
    // init new session from that cookie:
    Session sessionCookie2 = createNewSession();
    sessionCookie2.init(context);
    assertEquals("value1", sessionCookie2.get("key1"));
    assertEquals("value2", sessionCookie2.get("key2"));
    assertEquals("value3", sessionCookie2.get("key3"));
}
Also used : Cookie(ninja.Cookie) Test(org.junit.Test)

Example 22 with Cookie

use of ninja.Cookie in project ninja by ninjaframework.

the class SessionImplTest method testThatCookieDoesNotUseApplicationDomainWhenNotSet.

@Test
public void testThatCookieDoesNotUseApplicationDomainWhenNotSet() {
    when(ninjaProperties.get(NinjaConstant.applicationCookieDomain)).thenReturn(null);
    Session sessionCookie = createNewSession();
    sessionCookie.init(context);
    sessionCookie.put("anykey", "anyvalue");
    sessionCookie.save(context);
    verify(context).addCookie(cookieCaptor.capture());
    Cookie cookie = cookieCaptor.getValue();
    assertThat(cookie.getDomain(), CoreMatchers.equalTo(null));
}
Also used : Cookie(ninja.Cookie) Test(org.junit.Test)

Example 23 with Cookie

use of ninja.Cookie in project ninja by ninjaframework.

the class SessionImplTest method testSessionEncryptionKeysMismatch.

@Test
public void testSessionEncryptionKeysMismatch() {
    if (!encrypted) {
        assertTrue("N/A for plain session cookies without encryption", true);
        return;
    }
    // (1) create session with some data and save
    Session session_1 = createNewSession();
    session_1.init(context);
    session_1.put("key", "value");
    session_1.save(context);
    // (2) verify that cookie with our data is created and added to context
    verify(context).addCookie(cookieCaptor.capture());
    assertEquals("value", session_1.get("key"));
    // save reference to our cookie - we will use it to init sessions below
    Cookie cookie = cookieCaptor.getValue();
    // (3) create new session with the same cookie and assert that it still has our data
    Session session_2 = createNewSession();
    when(context.getCookie("NINJA_SESSION")).thenReturn(cookie);
    session_2.init(context);
    assertFalse(session_2.isEmpty());
    assertEquals("value", session_2.get("key"));
    // (4) now we change our application secret and thus our encryption key is modified
    when(ninjaProperties.getOrDie(NinjaConstant.applicationSecret)).thenReturn(SecretGenerator.generateSecret());
    encryption = new CookieEncryption(ninjaProperties);
    // (5) creating new session with the same cookie above would result in clean session
    // because that cookie was encrypted with another key and decryption with the new key
    // is not possible; usually such a case throws `javax.crypto.BadPaddingException`
    Session session_3 = createNewSession();
    session_3.init(context);
    assertTrue(session_3.isEmpty());
}
Also used : Cookie(ninja.Cookie) CookieEncryption(ninja.utils.CookieEncryption) Test(org.junit.Test)

Example 24 with Cookie

use of ninja.Cookie in project ninja by ninjaframework.

the class NinjaServletContextTest method getCookieTest.

@Test
public void getCookieTest() {
    javax.servlet.http.Cookie servletCookie1 = new javax.servlet.http.Cookie("contextCookie1", "theValue1");
    javax.servlet.http.Cookie servletCookie2 = new javax.servlet.http.Cookie("contextCookie2", "theValue2");
    javax.servlet.http.Cookie[] servletCookies = { servletCookie1, servletCookie2 };
    when(httpServletRequest.getCookies()).thenReturn(servletCookies);
    context.init(servletContext, httpServletRequest, httpServletResponse);
    // negative test:
    ninja.Cookie doesNotExist = context.getCookie("doesNotExist");
    assertNull(doesNotExist);
    // test  against cookie that is really there
    ninja.Cookie cookie1 = context.getCookie("contextCookie1");
    assertEquals(cookie1.getName(), "contextCookie1");
    assertEquals(cookie1.getValue(), "theValue1");
    // test 2 against cookie that is really there
    ninja.Cookie cookie2 = context.getCookie("contextCookie2");
    assertEquals(cookie2.getName(), "contextCookie2");
    assertEquals(cookie2.getValue(), "theValue2");
}
Also used : Cookie(ninja.Cookie) Cookie(ninja.Cookie) Test(org.junit.Test)

Example 25 with Cookie

use of ninja.Cookie in project ninja by ninjaframework.

the class LangImpl method clearLanguage.

@Override
public void clearLanguage(Result result) {
    Cookie defaultLangCookie = generateNinjaLanguageCookie();
    result.unsetCookie(defaultLangCookie.getName());
}
Also used : Cookie(ninja.Cookie)

Aggregations

Cookie (ninja.Cookie)27 Test (org.junit.Test)20 Result (ninja.Result)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Route (ninja.Route)1 CookieEncryption (ninja.utils.CookieEncryption)1