Search in sources :

Example 31 with WebSession

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

the class WebSessionIntegrationTests method expiredSession.

@Test
public void expiredSession() throws Exception {
    // First request: no session yet, new session created
    RequestEntity<Void> request = RequestEntity.get(createUri("/")).build();
    ResponseEntity<Void> response = this.restTemplate.exchange(request, Void.class);
    assertEquals(HttpStatus.OK, response.getStatusCode());
    String id = extractSessionId(response.getHeaders());
    assertNotNull(id);
    assertEquals(1, this.handler.getCount());
    // Second request: same session
    request = RequestEntity.get(createUri("/")).header("Cookie", "SESSION=" + id).build();
    response = this.restTemplate.exchange(request, Void.class);
    assertEquals(HttpStatus.OK, response.getStatusCode());
    assertNull(response.getHeaders().get("Set-Cookie"));
    assertEquals(2, this.handler.getCount());
    // Update lastAccessTime of the created session to -31 min
    WebSession session = this.sessionManager.getSessionStore().retrieveSession(id).block();
    ((DefaultWebSession) session).setLastAccessTime(Clock.offset(this.sessionManager.getClock(), Duration.ofMinutes(-31)).instant());
    // Third request: expired session, new session created
    request = RequestEntity.get(createUri("/")).header("Cookie", "SESSION=" + id).build();
    response = this.restTemplate.exchange(request, Void.class);
    assertEquals(HttpStatus.OK, response.getStatusCode());
    id = extractSessionId(response.getHeaders());
    assertNotNull("Expected new session id", id);
    assertEquals("Expected new session attribute", 1, this.handler.getCount());
}
Also used : WebSession(org.springframework.web.server.WebSession) Test(org.junit.Test)

Example 32 with WebSession

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

the class SpringSessionWebSessionStoreTests method createSessionWhenGetAttributesAndEntrySetThenDelegatesToCreateSession.

@Test
public void createSessionWhenGetAttributesAndEntrySetThenDelegatesToCreateSession() {
    String attrName = "attrName";
    given(this.createSession.getAttributeNames()).willReturn(Collections.singleton(attrName));
    String attrValue = "attrValue";
    given(this.createSession.getAttribute(attrName)).willReturn(attrValue);
    WebSession createdWebSession = this.webSessionStore.createWebSession().block();
    Map<String, Object> attributes = createdWebSession.getAttributes();
    Set<Map.Entry<String, Object>> entries = attributes.entrySet();
    assertThat(entries).containsExactly(new AbstractMap.SimpleEntry<>(attrName, attrValue));
}
Also used : AbstractMap(java.util.AbstractMap) WebSession(org.springframework.web.server.WebSession) Test(org.junit.Test)

Example 33 with WebSession

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

the class SpringSessionWebSessionStoreTests method createSessionWhenGetAttributesAndContainsKeyAndFoundThenTrue.

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

Example 34 with WebSession

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

the class SpringSessionWebSessionStoreTests method createSessionWhenAddAttributeThenStarted.

@Test
public void createSessionWhenAddAttributeThenStarted() {
    given(this.createSession.getAttributeNames()).willReturn(Collections.singleton("a"));
    WebSession createdWebSession = this.webSessionStore.createWebSession().block();
    assertThat(createdWebSession.isStarted()).isTrue();
}
Also used : WebSession(org.springframework.web.server.WebSession) Test(org.junit.Test)

Example 35 with WebSession

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

the class SpringSessionWebSessionStoreTests method createSessionWhenGetAttributesAndContainsKeyAndNotStringThenFalse.

@Test
public void createSessionWhenGetAttributesAndContainsKeyAndNotStringThenFalse() {
    WebSession createdWebSession = this.webSessionStore.createWebSession().block();
    Map<String, Object> attributes = createdWebSession.getAttributes();
    assertThat(attributes.containsKey(1L)).isFalse();
}
Also used : WebSession(org.springframework.web.server.WebSession) Test(org.junit.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