diff --git a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/factory/SaveSessionGatewayFilterFactory.java b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/factory/SaveSessionGatewayFilterFactory.java index 3ad4b14a..77bf5e37 100644 --- a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/factory/SaveSessionGatewayFilterFactory.java +++ b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/factory/SaveSessionGatewayFilterFactory.java @@ -42,7 +42,7 @@ public class SaveSessionGatewayFilterFactory extends AbstractGatewayFilterFactor return new GatewayFilter() { @Override public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) { - return exchange.getSession().map(WebSession::save).then(chain.filter(exchange)); + return exchange.getSession().flatMap(WebSession::save).then(chain.filter(exchange)); } @Override diff --git a/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/factory/SaveSessionGatewayFilterFactoryTests.java b/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/factory/SaveSessionGatewayFilterFactoryTests.java index 74c7aab1..9da3aed9 100644 --- a/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/factory/SaveSessionGatewayFilterFactoryTests.java +++ b/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/factory/SaveSessionGatewayFilterFactoryTests.java @@ -19,6 +19,7 @@ package org.springframework.cloud.gateway.filter.factory; import java.time.Duration; import java.util.HashMap; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import org.junit.jupiter.api.Test; import reactor.core.publisher.Mono; @@ -52,11 +53,19 @@ public class SaveSessionGatewayFilterFactoryTests extends BaseWebClientTests { static WebSession mockWebSession = mock(WebSession.class); + private final Map fakeSessionStore = new ConcurrentHashMap<>(); + + private static final String SESSION_ID = "RANDOM_SESSION_ID"; + @Test public void webCallShouldTriggerWebSessionSaveAction() { when(mockWebSession.getAttributes()).thenReturn(new HashMap<>()); - when(mockWebSession.save()).thenReturn(Mono.empty()); + Mono doSaveSession = Mono.fromRunnable(() -> { + // Do save session. Need to make sure the Mono is subscribed + fakeSessionStore.put(SESSION_ID, mockWebSession); + }); + when(mockWebSession.save()).thenReturn(doSaveSession); Mono result = webClient.get().uri("/get").retrieve().bodyToMono(Map.class); @@ -65,6 +74,7 @@ public class SaveSessionGatewayFilterFactoryTests extends BaseWebClientTests { }).expectComplete().verify(Duration.ofMinutes(10)); verify(mockWebSession).save(); + assertThat(fakeSessionStore.get(SESSION_ID)).isEqualTo(mockWebSession); } @Test