Polishing contribution
Closes gh-29691
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
* Copyright 2002-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -22,7 +22,6 @@ import java.util.concurrent.atomic.AtomicReference;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.http.server.reactive.HttpHandler;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
|
||||
import org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest;
|
||||
@@ -36,16 +35,16 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
*/
|
||||
class ServerWebExchangeContextFilterTests {
|
||||
|
||||
|
||||
@Test
|
||||
void extractServerWebExchangeFromContext() {
|
||||
MyService service = new MyService();
|
||||
|
||||
HttpHandler httpHandler = WebHttpHandlerBuilder
|
||||
WebHttpHandlerBuilder
|
||||
.webHandler(exchange -> service.service().then())
|
||||
.filter(new ServerWebExchangeContextFilter())
|
||||
.build();
|
||||
|
||||
httpHandler.handle(MockServerHttpRequest.get("/path").build(), new MockServerHttpResponse())
|
||||
.build()
|
||||
.handle(MockServerHttpRequest.get("/path").build(), new MockServerHttpResponse())
|
||||
.block(Duration.ofSeconds(5));
|
||||
|
||||
assertThat(service.getExchange()).isNotNull();
|
||||
@@ -56,16 +55,16 @@ class ServerWebExchangeContextFilterTests {
|
||||
|
||||
private final AtomicReference<ServerWebExchange> exchangeRef = new AtomicReference<>();
|
||||
|
||||
|
||||
public ServerWebExchange getExchange() {
|
||||
return this.exchangeRef.get();
|
||||
}
|
||||
|
||||
public Mono<String> service() {
|
||||
return Mono.just("result").contextWrite(context -> {
|
||||
ServerWebExchangeContextFilter.getExchange(context).ifPresent(exchangeRef::set);
|
||||
return context;
|
||||
});
|
||||
return Mono.just("result")
|
||||
.transformDeferredContextual((mono, contextView) -> {
|
||||
ServerWebExchangeContextFilter.getExchange(contextView).ifPresent(exchangeRef::set);
|
||||
return mono;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user