Search in sources :

Example 16 with WebSession

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

the class SpringSessionWebSessionStoreTests method createSessionWhenGetAttributesAndSizeThenDelegatesToCreateSession.

@Test
public void createSessionWhenGetAttributesAndSizeThenDelegatesToCreateSession() {
    WebSession createdWebSession = this.webSessionStore.createWebSession().block();
    Map<String, Object> attributes = createdWebSession.getAttributes();
    assertThat(attributes.size()).isEqualTo(0);
    given(this.createSession.getAttributeNames()).willReturn(Collections.singleton("a"));
    assertThat(attributes.size()).isEqualTo(1);
}
Also used : WebSession(org.springframework.web.server.WebSession) Test(org.junit.Test)

Example 17 with WebSession

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

the class SpringSessionWebSessionStoreTests method createSessionWhenGetAttributesAndValuesThenDelegatesToCreateSession.

@Test
public void createSessionWhenGetAttributesAndValuesThenDelegatesToCreateSession() {
    given(this.createSession.getAttributeNames()).willReturn(Collections.singleton("a"));
    given(this.createSession.getAttribute("a")).willReturn("b");
    WebSession createdWebSession = this.webSessionStore.createWebSession().block();
    Map<String, Object> attributes = createdWebSession.getAttributes();
    assertThat(attributes.values()).containsExactly("b");
}
Also used : WebSession(org.springframework.web.server.WebSession) Test(org.junit.Test)

Example 18 with WebSession

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

the class WebSessionServerSecurityContextRepositoryTests method saveWhenNewContextThenChangeSessionId.

@Test
public void saveWhenNewContextThenChangeSessionId() {
    String originalSessionId = this.exchange.getSession().block().getId();
    this.repository.save(this.exchange, new SecurityContextImpl()).block();
    WebSession session = this.exchange.getSession().block();
    assertThat(session.getId()).isNotEqualTo(originalSessionId);
}
Also used : SecurityContextImpl(org.springframework.security.core.context.SecurityContextImpl) WebSession(org.springframework.web.server.WebSession) Test(org.junit.jupiter.api.Test)

Example 19 with WebSession

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

the class WebSessionServerCsrfTokenRepositoryTests method saveTokenWhenNullThenDeletes.

@Test
public void saveTokenWhenNullThenDeletes() {
    CsrfToken token = this.repository.generateToken(this.exchange).block();
    Mono<Void> result = this.repository.saveToken(this.exchange, null);
    StepVerifier.create(result).verifyComplete();
    WebSession session = this.exchange.getSession().block();
    assertThat(session.getAttributes()).isEmpty();
}
Also used : WebSession(org.springframework.web.server.WebSession) Test(org.junit.jupiter.api.Test)

Example 20 with WebSession

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

the class SessionAttributesHandlerTests method retrieveAttributes.

@Test
public void retrieveAttributes() {
    WebSession session = new MockWebSession();
    session.getAttributes().put("attr1", "value1");
    session.getAttributes().put("attr2", "value2");
    session.getAttributes().put("attr3", new TestBean());
    session.getAttributes().put("attr4", new TestBean());
    assertThat(sessionAttributesHandler.retrieveAttributes(session).keySet()).as("Named attributes (attr1, attr2) should be 'known' right away").isEqualTo(new HashSet<>(asList("attr1", "attr2")));
    // Resolve 'attr3' by type
    sessionAttributesHandler.isHandlerSessionAttribute("attr3", TestBean.class);
    assertThat(sessionAttributesHandler.retrieveAttributes(session).keySet()).as("Named attributes (attr1, attr2) and resolved attribute (att3) should be 'known'").isEqualTo(new HashSet<>(asList("attr1", "attr2", "attr3")));
}
Also used : MockWebSession(org.springframework.web.testfixture.server.MockWebSession) WebSession(org.springframework.web.server.WebSession) TestBean(org.springframework.beans.testfixture.beans.TestBean) MockWebSession(org.springframework.web.testfixture.server.MockWebSession) 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