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);
}
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);
}
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");
}
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();
}
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());
}
Aggregations