Skip to content

Commit c747c9c

Browse files
committed
Polish "Add configuration for HTML escaping configuration with WebFlux"
See gh-49791
1 parent 59f8dca commit c747c9c

4 files changed

Lines changed: 30 additions & 40 deletions

File tree

module/spring-boot-webflux/src/main/java/org/springframework/boot/webflux/autoconfigure/HttpHandlerAutoConfiguration.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,13 @@ static class AnnotationConfig {
6464
@Bean
6565
HttpHandler httpHandler(ObjectProvider<WebFluxProperties> propsProvider,
6666
ObjectProvider<WebHttpHandlerBuilderCustomizer> handlerBuilderCustomizers) {
67+
WebFluxProperties properties = propsProvider.getIfAvailable();
6768
WebHttpHandlerBuilder handlerBuilder = WebHttpHandlerBuilder.applicationContext(this.applicationContext);
69+
if (properties != null) {
70+
handlerBuilder.defaultHtmlEscape(properties.getDefaultHtmlEscape());
71+
}
6872
handlerBuilderCustomizers.orderedStream().forEach((customizer) -> customizer.customize(handlerBuilder));
6973
HttpHandler httpHandler = handlerBuilder.build();
70-
WebFluxProperties properties = propsProvider.getIfAvailable();
7174
if (properties != null && StringUtils.hasText(properties.getBasePath())) {
7275
Map<String, HttpHandler> handlersMap = Collections.singletonMap(properties.getBasePath(), httpHandler);
7376
return new ContextPathCompositeHandler(handlersMap);

module/spring-boot-webflux/src/main/java/org/springframework/boot/webflux/autoconfigure/WebFluxAutoConfiguration.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -304,11 +304,6 @@ private void configureApiVersioningUse(ApiVersionConfigurer configurer, Use use)
304304
.forEach((mediaType, parameterName) -> configurer.useMediaTypeParameter(mediaType, parameterName));
305305
}
306306

307-
@Bean
308-
WebHttpHandlerBuilderCustomizer defaultHtmlEscapeCustomizer() {
309-
return (builder) -> builder.defaultHtmlEscape(this.webFluxProperties.getDefaultHtmlEscape());
310-
}
311-
312307
}
313308

314309
/**

module/spring-boot-webflux/src/test/java/org/springframework/boot/webflux/autoconfigure/HttpHandlerAutoConfigurationTests.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import org.assertj.core.api.InstanceOfAssertFactories;
2020
import org.junit.jupiter.api.Test;
21+
import org.junit.jupiter.params.ParameterizedTest;
22+
import org.junit.jupiter.params.provider.ValueSource;
2123
import reactor.core.publisher.Mono;
2224

2325
import org.springframework.boot.autoconfigure.AutoConfigurations;
@@ -35,6 +37,7 @@
3537
import org.springframework.web.reactive.function.server.RouterFunction;
3638
import org.springframework.web.reactive.function.server.ServerResponse;
3739
import org.springframework.web.server.WebHandler;
40+
import org.springframework.web.server.adapter.HttpWebHandlerAdapter;
3841

3942
import static org.assertj.core.api.Assertions.assertThat;
4043
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
@@ -100,6 +103,29 @@ void shouldConfigureBasePathCompositeHandler() {
100103
});
101104
}
102105

106+
@ParameterizedTest
107+
@ValueSource(booleans = { true, false })
108+
void shouldConfigureDefaultHtmlEscape(boolean enabled) {
109+
this.contextRunner.withConfiguration(AutoConfigurations.of(WebFluxAutoConfiguration.class))
110+
.withPropertyValues("spring.webflux.default-html-escape=" + enabled)
111+
.run((context) -> {
112+
assertThat(context).hasSingleBean(HttpHandler.class);
113+
assertThat(context.getBean(HttpHandler.class)).isInstanceOfSatisfying(HttpWebHandlerAdapter.class,
114+
(adapter) -> assertThat(adapter.getDefaultHtmlEscape()).isEqualTo(enabled));
115+
});
116+
}
117+
118+
@Test
119+
void shouldNotConfigureDefaultHtmlEscaperWithoutWebFluxAutoConfiguration() {
120+
this.contextRunner.withUserConfiguration(CustomWebHandler.class)
121+
.withPropertyValues("spring.webflux.default-html-escape=true")
122+
.run((context) -> {
123+
assertThat(context).hasSingleBean(HttpHandler.class);
124+
assertThat(context.getBean(HttpHandler.class)).isInstanceOfSatisfying(HttpWebHandlerAdapter.class,
125+
(adapter) -> assertThat(adapter.getDefaultHtmlEscape()).isNull());
126+
});
127+
}
128+
103129
@Configuration(proxyBeanMethods = false)
104130
static class CustomHttpHandler {
105131

module/spring-boot-webflux/src/test/java/org/springframework/boot/webflux/autoconfigure/WebFluxAutoConfigurationTests.java

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@
120120
import org.springframework.web.reactive.result.view.ViewResolutionResultHandler;
121121
import org.springframework.web.reactive.result.view.ViewResolver;
122122
import org.springframework.web.server.ServerWebExchange;
123-
import org.springframework.web.server.WebHandler;
124123
import org.springframework.web.server.WebSession;
125124
import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
126125
import org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver;
@@ -462,39 +461,6 @@ void hiddenHttpMethodFilterCanBeEnabled() {
462461
.run((context) -> assertThat(context).hasSingleBean(OrderedHiddenHttpMethodFilter.class));
463462
}
464463

465-
@Test
466-
void defaultHtmlEscapeIsNotConfiguredByDefault() {
467-
this.contextRunner.run((context) -> {
468-
WebHttpHandlerBuilderCustomizer customizer = context.getBean("defaultHtmlEscapeCustomizer",
469-
WebHttpHandlerBuilderCustomizer.class);
470-
WebHttpHandlerBuilder builder = WebHttpHandlerBuilder.webHandler(mock(WebHandler.class));
471-
customizer.customize(builder);
472-
assertThat(builder.getDefaultHtmlEscape()).isNull();
473-
});
474-
}
475-
476-
@Test
477-
void defaultHtmlEscapeCanBeEnabled() {
478-
this.contextRunner.withPropertyValues("spring.webflux.default-html-escape=true").run((context) -> {
479-
WebHttpHandlerBuilderCustomizer customizer = context.getBean("defaultHtmlEscapeCustomizer",
480-
WebHttpHandlerBuilderCustomizer.class);
481-
WebHttpHandlerBuilder builder = WebHttpHandlerBuilder.webHandler(mock(WebHandler.class));
482-
customizer.customize(builder);
483-
assertThat(builder.getDefaultHtmlEscape()).isTrue();
484-
});
485-
}
486-
487-
@Test
488-
void defaultHtmlEscapeCanBeDisabled() {
489-
this.contextRunner.withPropertyValues("spring.webflux.default-html-escape=false").run((context) -> {
490-
WebHttpHandlerBuilderCustomizer customizer = context.getBean("defaultHtmlEscapeCustomizer",
491-
WebHttpHandlerBuilderCustomizer.class);
492-
WebHttpHandlerBuilder builder = WebHttpHandlerBuilder.webHandler(mock(WebHandler.class));
493-
customizer.customize(builder);
494-
assertThat(builder.getDefaultHtmlEscape()).isFalse();
495-
});
496-
}
497-
498464
@Test
499465
void customRequestMappingHandlerMapping() {
500466
this.contextRunner.withUserConfiguration(CustomRequestMappingHandlerMapping.class).run((context) -> {

0 commit comments

Comments
 (0)