To fix GH-2304 SaveSessionGatewayFilterFactory didn't work correctly (#2997)

* The return value of WebSession.save was not passed correctly in SaveSessionGatewayFilterFactory

Fixes gh-2304

* The return value of WebSession.save was not passed correctly in SaveSessionGatewayFilterFactory

Fixes gh-2304
This commit is contained in:
zkq
2024-03-09 03:31:19 +08:00
committed by GitHub
parent c4e4d20d44
commit 7968b0da99
2 changed files with 12 additions and 2 deletions

View File

@@ -42,7 +42,7 @@ public class SaveSessionGatewayFilterFactory extends AbstractGatewayFilterFactor
return new GatewayFilter() {
@Override
public Mono<Void> 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

View File

@@ -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<String, WebSession> 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<Void> 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<Map> 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