Search in sources :

Example 41 with WebSession

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

the class InMemoryWebSessionStoreTests method startsSessionExplicitly.

@Test
public void startsSessionExplicitly() {
    WebSession session = this.store.createWebSession().block();
    assertThat(session).isNotNull();
    session.start();
    assertThat(session.isStarted()).isTrue();
}
Also used : WebSession(org.springframework.web.server.WebSession) Test(org.junit.jupiter.api.Test)

Example 42 with WebSession

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

the class InMemoryWebSessionStoreTests method lastAccessTimeIsUpdatedOnRetrieve.

@Test
public void lastAccessTimeIsUpdatedOnRetrieve() {
    WebSession session1 = this.store.createWebSession().block();
    assertThat(session1).isNotNull();
    String id = session1.getId();
    Instant time1 = session1.getLastAccessTime();
    session1.start();
    session1.save().block();
    // Fast-forward a few seconds
    this.store.setClock(Clock.offset(this.store.getClock(), Duration.ofSeconds(5)));
    WebSession session2 = this.store.retrieveSession(id).block();
    assertThat(session2).isNotNull();
    assertThat(session2).isSameAs(session1);
    Instant time2 = session2.getLastAccessTime();
    assertThat(time1.isBefore(time2)).isTrue();
}
Also used : WebSession(org.springframework.web.server.WebSession) Instant(java.time.Instant) Test(org.junit.jupiter.api.Test)

Example 43 with WebSession

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

the class InMemoryWebSessionStore method createWebSession.

@Override
public Mono<WebSession> createWebSession() {
    // Opportunity to clean expired sessions
    Instant now = this.clock.instant();
    this.expiredSessionChecker.checkIfNecessary(now);
    return Mono.<WebSession>fromSupplier(() -> new InMemoryWebSession(now)).subscribeOn(Schedulers.boundedElastic()).publishOn(Schedulers.parallel());
}
Also used : WebSession(org.springframework.web.server.WebSession) Instant(java.time.Instant)

Example 44 with WebSession

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

the class WebSessionMethodArgumentResolver method resolveArgument.

@Override
public Mono<Object> resolveArgument(MethodParameter parameter, BindingContext context, ServerWebExchange exchange) {
    Mono<WebSession> session = exchange.getSession();
    ReactiveAdapter adapter = getAdapterRegistry().getAdapter(parameter.getParameterType());
    return (adapter != null ? Mono.just(adapter.fromPublisher(session)) : Mono.from(session));
}
Also used : WebSession(org.springframework.web.server.WebSession) ReactiveAdapter(org.springframework.core.ReactiveAdapter)

Example 45 with WebSession

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

the class WebSessionMethodArgumentResolverTests method resolverArgument.

@Test
public void resolverArgument() {
    BindingContext context = new BindingContext();
    WebSession session = mock(WebSession.class);
    MockServerHttpRequest request = MockServerHttpRequest.get("/").build();
    ServerWebExchange exchange = MockServerWebExchange.builder(request).session(session).build();
    MethodParameter param = this.testMethod.arg(WebSession.class);
    Object actual = this.resolver.resolveArgument(param, context, exchange).block();
    assertThat(actual).isSameAs(session);
    param = this.testMethod.arg(Mono.class, WebSession.class);
    actual = this.resolver.resolveArgument(param, context, exchange).block();
    assertThat(actual).isNotNull();
    assertThat(Mono.class.isAssignableFrom(actual.getClass())).isTrue();
    assertThat(((Mono<?>) actual).block()).isSameAs(session);
    param = this.testMethod.arg(Single.class, WebSession.class);
    actual = this.resolver.resolveArgument(param, context, exchange).block();
    assertThat(actual).isNotNull();
    assertThat(Single.class.isAssignableFrom(actual.getClass())).isTrue();
    assertThat(((Single<?>) actual).blockingGet()).isSameAs(session);
}
Also used : MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) ServerWebExchange(org.springframework.web.server.ServerWebExchange) WebSession(org.springframework.web.server.WebSession) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) Single(io.reactivex.rxjava3.core.Single) Mono(reactor.core.publisher.Mono) BindingContext(org.springframework.web.reactive.BindingContext) MethodParameter(org.springframework.core.MethodParameter) 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