Versioning support in WebTestClient controller setup

See gh-34919
This commit is contained in:
rstoyanchev 2025-05-19 09:31:37 +01:00
parent 3095219479
commit 094e653746
4 changed files with 62 additions and 17 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2025 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.
@ -31,6 +31,7 @@ import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.Validator;
import org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder;
import org.springframework.web.reactive.config.ApiVersionConfigurer;
import org.springframework.web.reactive.config.BlockingExecutionConfigurer;
import org.springframework.web.reactive.config.CorsRegistry;
import org.springframework.web.reactive.config.DelegatingWebFluxConfiguration;
@ -118,6 +119,12 @@ class DefaultControllerSpec extends AbstractMockServerSpec<WebTestClient.Control
return this;
}
@Override
public WebTestClient.ControllerSpec apiVersioning(Consumer<ApiVersionConfigurer> configurer) {
this.configurer.versionConsumer = configurer;
return this;
}
@Override
public DefaultControllerSpec viewResolvers(Consumer<ViewResolverRegistry> consumer) {
this.configurer.viewResolversConsumer = consumer;
@ -168,6 +175,8 @@ class DefaultControllerSpec extends AbstractMockServerSpec<WebTestClient.Control
private @Nullable Validator validator;
private @Nullable Consumer<ApiVersionConfigurer> versionConsumer;
private @Nullable Consumer<ViewResolverRegistry> viewResolversConsumer;
private @Nullable Consumer<BlockingExecutionConfigurer> executionConsumer;
@ -219,6 +228,13 @@ class DefaultControllerSpec extends AbstractMockServerSpec<WebTestClient.Control
return this.validator;
}
@Override
public void configureApiVersioning(ApiVersionConfigurer configurer) {
if (this.versionConsumer != null) {
this.versionConsumer.accept(configurer);
}
}
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
if (this.viewResolversConsumer != null) {

View File

@ -48,6 +48,7 @@ import org.springframework.validation.Validator;
import org.springframework.web.client.ApiVersionFormatter;
import org.springframework.web.client.ApiVersionInserter;
import org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder;
import org.springframework.web.reactive.config.ApiVersionConfigurer;
import org.springframework.web.reactive.config.BlockingExecutionConfigurer;
import org.springframework.web.reactive.config.CorsRegistry;
import org.springframework.web.reactive.config.PathMatchConfigurer;
@ -346,6 +347,12 @@ public interface WebTestClient {
*/
ControllerSpec validator(Validator validator);
/**
* Configure API versioning for mapping requests to controller methods.
* @since 7.0
*/
ControllerSpec apiVersioning(Consumer<ApiVersionConfigurer> configurer);
/**
* Configure view resolution.
* @see WebFluxConfigurer#configureViewResolvers

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 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.
@ -28,6 +28,7 @@ import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder;
import org.springframework.web.reactive.config.ApiVersionConfigurer;
import org.springframework.web.reactive.config.CorsRegistry;
import org.springframework.web.reactive.config.PathMatchConfigurer;
import org.springframework.web.reactive.config.ViewResolverRegistry;
@ -87,6 +88,7 @@ public class DefaultControllerSpecTests {
TestConsumer<FormatterRegistry> formatterConsumer = new TestConsumer<>();
TestConsumer<ServerCodecConfigurer> codecsConsumer = new TestConsumer<>();
TestConsumer<PathMatchConfigurer> pathMatchingConsumer = new TestConsumer<>();
TestConsumer<ApiVersionConfigurer> versionConsumer = new TestConsumer<>();
TestConsumer<ViewResolverRegistry> viewResolverConsumer = new TestConsumer<>();
new DefaultControllerSpec(new MyController())
@ -96,6 +98,7 @@ public class DefaultControllerSpecTests {
.formatters(formatterConsumer)
.httpMessageCodecs(codecsConsumer)
.pathMatching(pathMatchingConsumer)
.apiVersioning(versionConsumer)
.viewResolvers(viewResolverConsumer)
.build();
@ -105,6 +108,7 @@ public class DefaultControllerSpecTests {
assertThat(formatterConsumer.getValue()).isNotNull();
assertThat(codecsConsumer.getValue()).isNotNull();
assertThat(pathMatchingConsumer.getValue()).isNotNull();
assertThat(versionConsumer.getValue()).isNotNull();
assertThat(viewResolverConsumer.getValue()).isNotNull();
}

View File

@ -18,6 +18,7 @@ package org.springframework.test.web.reactive.server.samples;
import java.net.URI;
import java.util.Map;
import java.util.function.Consumer;
import org.junit.jupiter.api.Test;
@ -26,6 +27,7 @@ import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.ApiVersionInserter;
import org.springframework.web.reactive.config.ApiVersionConfigurer;
import static org.assertj.core.api.Assertions.assertThat;
@ -36,35 +38,49 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
public class ApiVersionTests {
private static final String HEADER_NAME = "X-API-Version";
@Test
void header() {
Map<String, String> result = performRequest(ApiVersionInserter.useHeader("X-API-Version"));
assertThat(result.get(HEADER_NAME)).isEqualTo("1.2");
String header = "X-API-Version";
Map<String, String> result = performRequest(
configurer -> configurer.useRequestHeader(header),
ApiVersionInserter.useHeader(header));
assertThat(result.get(header)).isEqualTo("1.2");
}
@Test
void queryParam() {
Map<String, String> result = performRequest(ApiVersionInserter.useQueryParam("api-version"));
assertThat(result.get("query")).isEqualTo("api-version=1.2");
String param = "api-version";
Map<String, String> result = performRequest(
configurer -> configurer.useRequestParam(param),
ApiVersionInserter.useQueryParam(param));
assertThat(result.get("query")).isEqualTo(param + "=1.2");
}
@Test
void pathSegment() {
Map<String, String> result = performRequest(ApiVersionInserter.usePathSegment(0));
Map<String, String> result = performRequest(
configurer -> configurer.usePathSegment(0),
ApiVersionInserter.usePathSegment(0));
assertThat(result.get("path")).isEqualTo("/1.2/path");
}
@SuppressWarnings("unchecked")
private Map<String, String> performRequest(ApiVersionInserter inserter) {
return WebTestClient.bindToController(new TestController())
private Map<String, String> performRequest(
Consumer<ApiVersionConfigurer> versionConfigurer, ApiVersionInserter inserter) {
WebTestClient client = WebTestClient.bindToController(new TestController())
.apiVersioning(versionConfigurer)
.configureClient()
.baseUrl("/path")
.apiVersionInserter(inserter)
.build()
.get()
.build();
return client.get()
.apiVersion(1.2)
.exchange()
.returnResult(Map.class)
@ -76,14 +92,16 @@ public class ApiVersionTests {
@RestController
static class TestController {
@GetMapping("/**")
private static final String HEADER = "X-API-Version";
@GetMapping(path = "/**", version = "1.2")
Map<String, String> handle(ServerHttpRequest request) {
URI uri = request.getURI();
String query = uri.getQuery();
String header = request.getHeaders().getFirst(HEADER_NAME);
String versionHeader = request.getHeaders().getFirst(HEADER);
return Map.of("path", uri.getRawPath(),
"query", (query != null ? query : ""),
HEADER_NAME, (header != null ? header : ""));
HEADER, (versionHeader != null ? versionHeader : ""));
}
}