Search in sources :

Example 16 with Cookie

use of ninja.Cookie in project ninja by ninjaframework.

the class AbstractContextTest method finalizeInAbstractContextSavesFlashSessionCookies.

@Test
public void finalizeInAbstractContextSavesFlashSessionCookies() {
    AbstractContextImpl context = spy(abstractContext);
    Result result = Results.json();
    Cookie cookie = Cookie.builder("TEST", "value").build();
    result.addCookie(cookie);
    doNothing().when(context).addCookie(cookie);
    ResponseStreams streams = context.finalizeHeaders(result);
    // abstract finalizeHeaders does not return anything
    assertThat(streams, is(nullValue()));
    verify(flashCookie, times(1)).save(context);
    verify(sessionCookie, times(1)).save(context);
    verify(context, times(1)).addCookie(cookie);
}
Also used : Cookie(ninja.Cookie) Result(ninja.Result) Test(org.junit.Test)

Example 17 with Cookie

use of ninja.Cookie in project ninja by ninjaframework.

the class LangImplTest method testClearLanguage.

@Test
public void testClearLanguage() {
    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);
    Result result = Results.ok();
    lang.clearLanguage(result);
    Cookie returnCookie = result.getCookie(cookie.getName());
    assertEquals("", returnCookie.getValue());
    assertEquals(0, returnCookie.getMaxAge());
}
Also used : Cookie(ninja.Cookie) Result(ninja.Result) Test(org.junit.Test)

Example 18 with Cookie

use of ninja.Cookie in project ninja by ninjaframework.

the class FlashScopeTest method testThatFlashCookieClearOfOutgoingWorks.

@Test
public void testThatFlashCookieClearOfOutgoingWorks() {
    // setup this testmethod
    Cookie cookie = Cookie.builder("NINJA_FLASH", "hello=flashScope").build();
    when(context.getCookie("NINJA_FLASH")).thenReturn(cookie);
    FlashScope flashCookie = new FlashScopeImpl(ninjaProperties);
    flashCookie.init(context);
    // make sure the old cookue gets parsed:
    assertEquals("flashScope", flashCookie.get("hello"));
    flashCookie.put("funny new flash message", "is there...");
    // now test clearCurrentFlashCookieData
    flashCookie.discard();
    assertEquals(2, ((FlashScopeImpl) flashCookie).getCurrentFlashCookieData().size());
    assertEquals(0, ((FlashScopeImpl) flashCookie).getOutgoingFlashCookieData().size());
}
Also used : Cookie(ninja.Cookie) Test(org.junit.Test)

Example 19 with Cookie

use of ninja.Cookie in project ninja by ninjaframework.

the class SessionImplTest method testThatCookieUsesContextPath.

@Test
public void testThatCookieUsesContextPath() {
    Mockito.when(context.getContextPath()).thenReturn("/my_context");
    Session sessionCookie = createNewSession();
    sessionCookie.init(context);
    sessionCookie.put("anykey", "anyvalue");
    sessionCookie.save(context);
    verify(context).addCookie(cookieCaptor.capture());
    Cookie cookie = cookieCaptor.getValue();
    assertThat(cookie.getPath(), CoreMatchers.equalTo("/my_context/"));
}
Also used : Cookie(ninja.Cookie) Test(org.junit.Test)

Example 20 with Cookie

use of ninja.Cookie in project ninja by ninjaframework.

the class SessionImplTest method testThatCookieUseApplicationDomain.

@Test
public void testThatCookieUseApplicationDomain() {
    when(ninjaProperties.get(NinjaConstant.applicationCookieDomain)).thenReturn("domain.com");
    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("domain.com"));
}
Also used : Cookie(ninja.Cookie) Test(org.junit.Test)

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