Search in sources :

Example 21 with WebSession

use of org.springframework.web.server.WebSession in project spring-framework by spring-projects.

the class WebSessionIntegrationTests method expiredSessionIsRecreated.

@ParameterizedHttpServerTest
public void expiredSessionIsRecreated(HttpServer httpServer) throws Exception {
    startServer(httpServer);
    // First request: no session yet, new session created
    RequestEntity<Void> request = RequestEntity.get(createUri()).build();
    ResponseEntity<Void> response = this.restTemplate.exchange(request, Void.class);
    assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
    String id = extractSessionId(response.getHeaders());
    assertThat(id).isNotNull();
    assertThat(this.handler.getSessionRequestCount()).isEqualTo(1);
    // Second request: same session
    request = RequestEntity.get(createUri()).header("Cookie", "SESSION=" + id).build();
    response = this.restTemplate.exchange(request, Void.class);
    assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
    assertThat(response.getHeaders().get("Set-Cookie")).isNull();
    assertThat(this.handler.getSessionRequestCount()).isEqualTo(2);
    // Now fast-forward by 31 minutes
    InMemoryWebSessionStore store = (InMemoryWebSessionStore) this.sessionManager.getSessionStore();
    WebSession session = store.retrieveSession(id).block();
    assertThat(session).isNotNull();
    store.setClock(Clock.offset(store.getClock(), Duration.ofMinutes(31)));
    // Third request: expired session, new session created
    request = RequestEntity.get(createUri()).header("Cookie", "SESSION=" + id).build();
    response = this.restTemplate.exchange(request, Void.class);
    assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
    id = extractSessionId(response.getHeaders());
    assertThat(id).as("Expected new session id").isNotNull();
    assertThat(this.handler.getSessionRequestCount()).isEqualTo(1);
}
Also used : WebSession(org.springframework.web.server.WebSession)

Example 22 with WebSession

use of org.springframework.web.server.WebSession in project spring-framework by spring-projects.

the class ModelInitializerTests method clearModelAttributeFromSession.

@Test
public void clearModelAttributeFromSession() {
    WebSession session = this.exchange.getSession().block(TIMEOUT);
    assertThat(session).isNotNull();
    TestBean testBean = new TestBean("Session Bean");
    session.getAttributes().put("bean", testBean);
    TestController controller = new TestController();
    InitBinderBindingContext context = getBindingContext(controller);
    Method method = ResolvableMethod.on(TestController.class).annotPresent(GetMapping.class).resolveMethod();
    HandlerMethod handlerMethod = new HandlerMethod(controller, method);
    this.modelInitializer.initModel(handlerMethod, context, this.exchange).block(TIMEOUT);
    context.getSessionStatus().setComplete();
    context.saveModel();
    assertThat(session.getAttributes().size()).isEqualTo(0);
}
Also used : WebSession(org.springframework.web.server.WebSession) GetMapping(org.springframework.web.bind.annotation.GetMapping) HandlerMethod(org.springframework.web.method.HandlerMethod) Method(java.lang.reflect.Method) ResolvableMethod(org.springframework.web.testfixture.method.ResolvableMethod) SyncInvocableHandlerMethod(org.springframework.web.reactive.result.method.SyncInvocableHandlerMethod) HandlerMethod(org.springframework.web.method.HandlerMethod) SyncInvocableHandlerMethod(org.springframework.web.reactive.result.method.SyncInvocableHandlerMethod) Test(org.junit.jupiter.api.Test)

Example 23 with WebSession

use of org.springframework.web.server.WebSession in project spring-framework by spring-projects.

the class ModelInitializerTests method retrieveModelAttributeFromSession.

@Test
public void retrieveModelAttributeFromSession() {
    WebSession session = this.exchange.getSession().block(TIMEOUT);
    assertThat(session).isNotNull();
    TestBean testBean = new TestBean("Session Bean");
    session.getAttributes().put("bean", testBean);
    TestController controller = new TestController();
    InitBinderBindingContext context = getBindingContext(controller);
    Method method = ResolvableMethod.on(TestController.class).annotPresent(GetMapping.class).resolveMethod();
    HandlerMethod handlerMethod = new HandlerMethod(controller, method);
    this.modelInitializer.initModel(handlerMethod, context, this.exchange).block(TIMEOUT);
    context.saveModel();
    assertThat(session.getAttributes().size()).isEqualTo(1);
    assertThat(((TestBean) session.getRequiredAttribute("bean")).getName()).isEqualTo("Session Bean");
}
Also used : WebSession(org.springframework.web.server.WebSession) GetMapping(org.springframework.web.bind.annotation.GetMapping) HandlerMethod(org.springframework.web.method.HandlerMethod) Method(java.lang.reflect.Method) ResolvableMethod(org.springframework.web.testfixture.method.ResolvableMethod) SyncInvocableHandlerMethod(org.springframework.web.reactive.result.method.SyncInvocableHandlerMethod) HandlerMethod(org.springframework.web.method.HandlerMethod) SyncInvocableHandlerMethod(org.springframework.web.reactive.result.method.SyncInvocableHandlerMethod) Test(org.junit.jupiter.api.Test)

Example 24 with WebSession

use of org.springframework.web.server.WebSession in project spring-framework by spring-projects.

the class SessionAttributesHandlerTests method storeAttributes.

@Test
public void storeAttributes() {
    ModelMap model = new ModelMap();
    model.put("attr1", "value1");
    model.put("attr2", "value2");
    model.put("attr3", new TestBean());
    WebSession session = new MockWebSession();
    sessionAttributesHandler.storeAttributes(session, model);
    assertThat(session.getAttributes().get("attr1")).isEqualTo("value1");
    assertThat(session.getAttributes().get("attr2")).isEqualTo("value2");
    boolean condition = session.getAttributes().get("attr3") instanceof TestBean;
    assertThat(condition).isTrue();
}
Also used : MockWebSession(org.springframework.web.testfixture.server.MockWebSession) WebSession(org.springframework.web.server.WebSession) TestBean(org.springframework.beans.testfixture.beans.TestBean) ModelMap(org.springframework.ui.ModelMap) MockWebSession(org.springframework.web.testfixture.server.MockWebSession) Test(org.junit.jupiter.api.Test)

Example 25 with WebSession

use of org.springframework.web.server.WebSession in project spring-framework by spring-projects.

the class DefaultWebSessionManagerTests method getSessionSaveWhenCreatedAndNotStartedThenNotSaved.

@Test
void getSessionSaveWhenCreatedAndNotStartedThenNotSaved() {
    given(this.sessionIdResolver.resolveSessionIds(this.exchange)).willReturn(Collections.emptyList());
    WebSession session = this.sessionManager.getSession(this.exchange).block();
    this.exchange.getResponse().setComplete().block();
    assertThat(session).isSameAs(this.createSession);
    assertThat(session.isStarted()).isFalse();
    assertThat(session.isExpired()).isFalse();
    verify(this.createSession, never()).save();
    verify(this.sessionIdResolver, never()).setSessionId(any(), any());
}
Also used : WebSession(org.springframework.web.server.WebSession) Test(org.junit.jupiter.api.Test)

Aggregations

WebSession (org.springframework.web.server.WebSession)53 Test (org.junit.Test)24 Test (org.junit.jupiter.api.Test)24 Method (java.lang.reflect.Method)3 TestBean (org.springframework.beans.testfixture.beans.TestBean)3 GetMapping (org.springframework.web.bind.annotation.GetMapping)3 HandlerMethod (org.springframework.web.method.HandlerMethod)3 SyncInvocableHandlerMethod (org.springframework.web.reactive.result.method.SyncInvocableHandlerMethod)3 ServerWebExchange (org.springframework.web.server.ServerWebExchange)3 Mono (reactor.core.publisher.Mono)3 Instant (java.time.Instant)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)2 BDDMockito.given (org.mockito.BDDMockito.given)2 Mockito.mock (org.mockito.Mockito.mock)2 Mockito.spy (org.mockito.Mockito.spy)2 Mockito.times (org.mockito.Mockito.times)2 Mockito.verify (org.mockito.Mockito.verify)2