Search in sources :

Example 1 with MockRequestContext

use of cn.taketoday.web.MockRequestContext in project today-framework by TAKETODAY.

the class DefaultWebSessionManagerTests method testWebSession.

@Test
public void testWebSession() {
    try (StandardApplicationContext applicationContext = new StandardApplicationContext()) {
        applicationContext.register(AppConfig.class);
        applicationContext.refresh();
        SessionManager sessionManager = applicationContext.getBean(SessionManager.class);
        MockRequestContext context = new MockRequestContext();
        WebSession noneExistingSession = sessionManager.getSession(context, false);
        assertThat(noneExistingSession).isNull();
        WebSession createdSession = sessionManager.getSession(context);
        assertThat(createdSession).isNotNull();
        // CookieTokenResolver
        CookieSessionIdResolver cookieTokenResolver = applicationContext.getBean(CookieSessionIdResolver.class);
        List<HttpCookie> responseCookies = context.responseCookies();
        String sessionId = createdSession.getId();
        HttpCookie sessionCookie = cookieTokenResolver.createCookie(sessionId);
        assertThat(responseCookies).hasSize(1);
        assertThat(responseCookies.get(0)).isEqualTo(sessionCookie);
        // WebSessionStorage
        SessionRepository sessionStorage = applicationContext.getBean(SessionRepository.class);
        WebSession webSession = sessionStorage.retrieveSession(sessionId);
        assertThat(webSession).isEqualTo(createdSession);
        assertThat(sessionStorage.contains(sessionId)).isTrue();
        sessionStorage.removeSession(sessionId);
        assertThat(sessionStorage.contains(sessionId)).isFalse();
    }
}
Also used : StandardApplicationContext(cn.taketoday.context.support.StandardApplicationContext) EnableWebSession(cn.taketoday.framework.web.session.EnableWebSession) MockRequestContext(cn.taketoday.web.MockRequestContext) HttpCookie(cn.taketoday.http.HttpCookie) Test(org.junit.jupiter.api.Test)

Example 2 with MockRequestContext

use of cn.taketoday.web.MockRequestContext in project today-infrastructure by TAKETODAY.

the class DefaultWebSessionManagerTests method testWebSession.

@Test
public void testWebSession() {
    try (StandardApplicationContext applicationContext = new StandardApplicationContext()) {
        applicationContext.register(AppConfig.class);
        applicationContext.refresh();
        SessionManager sessionManager = applicationContext.getBean(SessionManager.class);
        MockRequestContext context = new MockRequestContext();
        WebSession noneExistingSession = sessionManager.getSession(context, false);
        assertThat(noneExistingSession).isNull();
        WebSession createdSession = sessionManager.getSession(context);
        assertThat(createdSession).isNotNull();
        // CookieTokenResolver
        CookieSessionIdResolver cookieTokenResolver = applicationContext.getBean(CookieSessionIdResolver.class);
        List<HttpCookie> responseCookies = context.responseCookies();
        String sessionId = createdSession.getId();
        HttpCookie sessionCookie = cookieTokenResolver.createCookie(sessionId);
        assertThat(responseCookies).hasSize(1);
        assertThat(responseCookies.get(0)).isEqualTo(sessionCookie);
        // WebSessionStorage
        SessionRepository sessionStorage = applicationContext.getBean(SessionRepository.class);
        WebSession webSession = sessionStorage.retrieveSession(sessionId);
        assertThat(webSession).isEqualTo(createdSession);
        assertThat(sessionStorage.contains(sessionId)).isTrue();
        sessionStorage.removeSession(sessionId);
        assertThat(sessionStorage.contains(sessionId)).isFalse();
    }
}
Also used : StandardApplicationContext(cn.taketoday.context.support.StandardApplicationContext) MockRequestContext(cn.taketoday.web.MockRequestContext) HttpCookie(cn.taketoday.http.HttpCookie) Test(org.junit.jupiter.api.Test)

Aggregations

StandardApplicationContext (cn.taketoday.context.support.StandardApplicationContext)2 HttpCookie (cn.taketoday.http.HttpCookie)2 MockRequestContext (cn.taketoday.web.MockRequestContext)2 Test (org.junit.jupiter.api.Test)2 EnableWebSession (cn.taketoday.framework.web.session.EnableWebSession)1