Browse Source

Polish "Replace `BaseUrl` with `LocalTestWebServer`"

See gh-47680
pull/47740/head
Stéphane Nicoll 2 months ago
parent
commit
aceaee89ee
  1. 7
      core/spring-boot-test/src/main/java/org/springframework/boot/test/http/server/LazyUriBuilderFactory.java
  2. 63
      core/spring-boot-test/src/main/java/org/springframework/boot/test/http/server/LocalTestWebServer.java
  3. 5
      core/spring-boot-test/src/main/java/org/springframework/boot/test/web/htmlunit/UriBuilderFactoryWebClient.java
  4. 3
      core/spring-boot-test/src/main/java/org/springframework/boot/test/web/htmlunit/UriBuilderFactoryWebConnectionHtmlUnitDriver.java
  5. 24
      core/spring-boot-test/src/test/java/org/springframework/boot/test/http/server/LocalTestWebServerTests.java
  6. 1
      module/spring-boot-graphql-test/src/test/java/org/springframework/boot/graphql/test/autoconfigure/tester/HttpGraphQlTesterAutoConfigurationTests.java
  7. 2
      module/spring-boot-resttestclient/src/main/java/org/springframework/boot/resttestclient/autoconfigure/TestRestTemplateAutoConfiguration.java
  8. 6
      module/spring-boot-resttestclient/src/test/java/org/springframework/boot/resttestclient/autoconfigure/RestTestClientAutoConfigurationTests.java
  9. 77
      module/spring-boot-resttestclient/src/test/java/org/springframework/boot/resttestclient/autoconfigure/TestRestTemplateAutoConfigurationTests.java
  10. 4
      module/spring-boot-web-server/src/main/java/org/springframework/boot/web/server/reactive/context/ReactiveWebServerApplicationContextLocalTestWebServerProvider.java
  11. 4
      module/spring-boot-web-server/src/main/java/org/springframework/boot/web/server/servlet/context/ServletWebServerApplicationContextLocalTestWebServerProvider.java
  12. 12
      module/spring-boot-webmvc-test/src/main/java/org/springframework/boot/webmvc/test/autoconfigure/MockMvcWebClientAutoConfiguration.java
  13. 14
      module/spring-boot-webmvc-test/src/main/java/org/springframework/boot/webmvc/test/autoconfigure/MockMvcWebDriverAutoConfiguration.java
  14. 1
      module/spring-boot-webtestclient/src/test/java/org/springframework/boot/webflux/test/autoconfigure/WebTestClientAutoConfigurationTests.java
  15. 2
      smoke-test/spring-boot-smoke-test-actuator-custom-security/src/test/java/smoketest/actuator/customsecurity/AbstractSampleActuatorCustomSecurityTests.java
  16. 2
      smoke-test/spring-boot-smoke-test-actuator-custom-security/src/test/java/smoketest/actuator/customsecurity/CorsSampleActuatorApplicationTests.java
  17. 2
      smoke-test/spring-boot-smoke-test-actuator-extension/src/test/java/smoketest/actuator/extension/SampleActuatorExtensionApplicationTests.java
  18. 2
      smoke-test/spring-boot-smoke-test-actuator/src/test/java/smoketest/actuator/CorsSampleActuatorApplicationTests.java

7
core/spring-boot-test/src/main/java/org/springframework/boot/test/http/server/LazyUriBuilderFactory.java

@ -30,13 +30,14 @@ import org.springframework.web.util.UriBuilderFactory;
* Lazy {@link UriBuilderFactory} that only obtains the delegate on first call. * Lazy {@link UriBuilderFactory} that only obtains the delegate on first call.
* *
* @author Phillip Webb * @author Phillip Webb
* @author Stephane Nicoll
*/ */
class LazyUriBuilderFactory implements UriBuilderFactory { class LazyUriBuilderFactory implements UriBuilderFactory {
private final Supplier<UriBuilderFactory> suppler; private final Supplier<UriBuilderFactory> supplier;
LazyUriBuilderFactory(Supplier<UriBuilderFactory> supplier) { LazyUriBuilderFactory(Supplier<UriBuilderFactory> supplier) {
this.suppler = SingletonSupplier.of(supplier); this.supplier = SingletonSupplier.of(supplier);
} }
@Override @Override
@ -60,7 +61,7 @@ class LazyUriBuilderFactory implements UriBuilderFactory {
} }
private UriBuilderFactory delegate() { private UriBuilderFactory delegate() {
return this.suppler.get(); return this.supplier.get();
} }
} }

63
core/spring-boot-test/src/main/java/org/springframework/boot/test/http/server/LocalTestWebServer.java

@ -43,18 +43,22 @@ public final class LocalTestWebServer {
private final Scheme scheme; private final Scheme scheme;
private final SingletonSupplier<Connection> connection; private final SingletonSupplier<BaseUriDetails> baseUriDetails;
private LocalTestWebServer(Scheme scheme, Supplier<Connection> connectionSupplier) { private final UriBuilderFactory uriBuilderFactory;
private LocalTestWebServer(Scheme scheme, Supplier<BaseUriDetails> baseUriDetailsSupplier) {
Assert.notNull(scheme, "'scheme' must not be null"); Assert.notNull(scheme, "'scheme' must not be null");
Assert.notNull(connectionSupplier, "'connectionSupplier' must not be null"); Assert.notNull(baseUriDetailsSupplier, "'baseUriDetailsSupplier' must not be null");
this.scheme = scheme; this.scheme = scheme;
this.connection = SingletonSupplier.of(connectionSupplier); this.baseUriDetails = SingletonSupplier.of(baseUriDetailsSupplier);
this.uriBuilderFactory = new LazyUriBuilderFactory(
() -> new DefaultUriBuilderFactory(this.baseUriDetails.obtain().uri(scheme())));
} }
/** /**
* Return if URI scheme used for the connection. This method can be safely called * Return if URI scheme used for the request. This method can be safely called before
* before the local test server is fully running. * the local test server is fully running.
* @return if the web server uses an HTTPS address * @return if the web server uses an HTTPS address
*/ */
public Scheme scheme() { public Scheme scheme() {
@ -100,7 +104,7 @@ public final class LocalTestWebServer {
* @return a new {@link UriBuilderFactory} * @return a new {@link UriBuilderFactory}
*/ */
public UriBuilderFactory uriBuilderFactory() { public UriBuilderFactory uriBuilderFactory() {
return new LazyUriBuilderFactory(() -> new DefaultUriBuilderFactory(getConnection().uri(scheme()))); return this.uriBuilderFactory;
} }
/** /**
@ -110,13 +114,7 @@ public final class LocalTestWebServer {
* @return a new instance with the path added * @return a new instance with the path added
*/ */
public LocalTestWebServer withPath(String path) { public LocalTestWebServer withPath(String path) {
return of(this.scheme, () -> getConnection().withPath(path)); return of(this.scheme, () -> this.baseUriDetails.obtain().withPath(path));
}
private Connection getConnection() {
Connection connection = this.connection.get();
Assert.state(connection != null, "No local test web server connection supplied");
return connection;
} }
/** /**
@ -137,33 +135,33 @@ public final class LocalTestWebServer {
* @return a new {@link LocalTestWebServer} instance * @return a new {@link LocalTestWebServer} instance
*/ */
public static LocalTestWebServer of(Scheme scheme, int port, @Nullable String contextPath) { public static LocalTestWebServer of(Scheme scheme, int port, @Nullable String contextPath) {
return of(scheme, () -> new Connection(port, (contextPath != null) ? contextPath : "")); return of(scheme, () -> new BaseUriDetails(port, (contextPath != null) ? contextPath : ""));
} }
/** /**
* Factory method to create a new {@link LocalTestWebServer} instance. * Factory method to create a new {@link LocalTestWebServer} instance.
* @param scheme the URL scheme * @param scheme the URL scheme
* @param connectionSupplier a supplier to provide the server connection * @param baseUriDetailsSupplier a supplier to provide the details of the base URI
* @return a new {@link LocalTestWebServer} instance * @return a new {@link LocalTestWebServer} instance
*/ */
public static LocalTestWebServer of(Scheme scheme, Supplier<Connection> connectionSupplier) { public static LocalTestWebServer of(Scheme scheme, Supplier<BaseUriDetails> baseUriDetailsSupplier) {
return new LocalTestWebServer(scheme, connectionSupplier); return new LocalTestWebServer(scheme, baseUriDetailsSupplier);
} }
/** /**
* Return a {@link LocalTestWebServer} instance provided from the * Obtain the {@link LocalTestWebServer} instance provided from the
* {@link ApplicationContext}. * {@link ApplicationContext}.
* @param applicationContext the application context * @param applicationContext the application context
* @return the local test web server or {@code null} * @return the local test web server (never {@code null})
*/ */
public static LocalTestWebServer getRequired(ApplicationContext applicationContext) { public static LocalTestWebServer obtain(ApplicationContext applicationContext) {
LocalTestWebServer localTestWebServer = get(applicationContext); LocalTestWebServer localTestWebServer = get(applicationContext);
Assert.state(localTestWebServer != null, "No local test web server available"); Assert.state(localTestWebServer != null, "No local test web server available");
return localTestWebServer; return localTestWebServer;
} }
/** /**
* Return a {@link LocalTestWebServer} instance provided from the * Return the {@link LocalTestWebServer} instance provided from the
* {@link ApplicationContext} or {@code null} of no local server is started or could * {@link ApplicationContext} or {@code null} of no local server is started or could
* be provided. * be provided.
* @param applicationContext the application context * @param applicationContext the application context
@ -182,23 +180,19 @@ public final class LocalTestWebServer {
} }
/** /**
* Details of a connection to the local test web server. * Details of the base URI to the local test web server.
* *
* @param port the port of the running server * @param port the port of the running server
* @param path the path of the running server * @param path the path to use
*/ */
public record Connection(int port, String path) { public record BaseUriDetails(int port, String path) {
String uri(Scheme scheme) { String uri(Scheme scheme) {
StringBuilder uri = new StringBuilder(scheme.name().toLowerCase(Locale.getDefault())); return scheme.name().toLowerCase(Locale.ROOT) + "://localhost:" + port() + path();
uri.append("://localhost:");
uri.append(port());
uri.append(path());
return uri.toString();
} }
Connection withPath(String path) { BaseUriDetails withPath(String path) {
return new Connection(port(), path() + path); return new BaseUriDetails(port(), path() + path);
} }
} }
@ -221,10 +215,9 @@ public final class LocalTestWebServer {
} }
/** /**
* Strategy used to provide the running {@link LocalTestWebServer}. Implementations * Internal strategy used to provide the running {@link LocalTestWebServer}.
* can be registered in {@code spring.factories} and may accept an * Implementations can be registered in {@code spring.factories} and may accept an
* {@link ApplicationContext} constructor argument. * {@link ApplicationContext} constructor argument.
*
*/ */
@FunctionalInterface @FunctionalInterface
public interface Provider { public interface Provider {

5
core/spring-boot-test/src/main/java/org/springframework/boot/test/web/htmlunit/UriBuilderFactoryWebClient.java

@ -34,7 +34,7 @@ import org.springframework.web.util.UriBuilderFactory;
*/ */
public class UriBuilderFactoryWebClient extends WebClient { public class UriBuilderFactoryWebClient extends WebClient {
private UriBuilderFactory uriBuilderFactory; private final UriBuilderFactory uriBuilderFactory;
public UriBuilderFactoryWebClient(UriBuilderFactory uriBuilderFactory) { public UriBuilderFactoryWebClient(UriBuilderFactory uriBuilderFactory) {
Assert.notNull(uriBuilderFactory, "'uriBuilderFactory' must not be null"); Assert.notNull(uriBuilderFactory, "'uriBuilderFactory' must not be null");
@ -43,8 +43,7 @@ public class UriBuilderFactoryWebClient extends WebClient {
@Override @Override
public <P extends Page> P getPage(String url) throws IOException, FailingHttpStatusCodeException { public <P extends Page> P getPage(String url) throws IOException, FailingHttpStatusCodeException {
return super.getPage( return super.getPage(this.uriBuilderFactory.uriString(url).toUriString());
(this.uriBuilderFactory != null) ? this.uriBuilderFactory.uriString(url).toUriString() : url);
} }
} }

3
core/spring-boot-test/src/main/java/org/springframework/boot/test/web/htmlunit/UriBuilderFactoryWebConnectionHtmlUnitDriver.java

@ -32,7 +32,7 @@ import org.springframework.web.util.UriBuilderFactory;
*/ */
public class UriBuilderFactoryWebConnectionHtmlUnitDriver extends WebConnectionHtmlUnitDriver { public class UriBuilderFactoryWebConnectionHtmlUnitDriver extends WebConnectionHtmlUnitDriver {
private UriBuilderFactory uriBuilderFactory; private final UriBuilderFactory uriBuilderFactory;
public UriBuilderFactoryWebConnectionHtmlUnitDriver(UriBuilderFactory uriBuilderFactory) { public UriBuilderFactoryWebConnectionHtmlUnitDriver(UriBuilderFactory uriBuilderFactory) {
Assert.notNull(uriBuilderFactory, "'uriBuilderFactory' must not be null"); Assert.notNull(uriBuilderFactory, "'uriBuilderFactory' must not be null");
@ -60,6 +60,7 @@ public class UriBuilderFactoryWebConnectionHtmlUnitDriver extends WebConnectionH
} }
@Override @Override
@SuppressWarnings("ConstantValue") // default constructor calls this method
public void get(String url) { public void get(String url) {
super.get((this.uriBuilderFactory != null) ? this.uriBuilderFactory.uriString(url).toUriString() : url); super.get((this.uriBuilderFactory != null) ? this.uriBuilderFactory.uriString(url).toUriString() : url);
} }

24
core/spring-boot-test/src/test/java/org/springframework/boot/test/http/server/LocalTestWebServerTests.java

@ -23,7 +23,7 @@ import java.util.concurrent.atomic.AtomicInteger;
import org.jspecify.annotations.Nullable; import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.boot.test.http.server.LocalTestWebServer.Connection; import org.springframework.boot.test.http.server.LocalTestWebServer.BaseUriDetails;
import org.springframework.boot.test.http.server.LocalTestWebServer.Scheme; import org.springframework.boot.test.http.server.LocalTestWebServer.Scheme;
import org.springframework.boot.testsupport.classpath.resources.WithResource; import org.springframework.boot.testsupport.classpath.resources.WithResource;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
@ -129,14 +129,21 @@ class LocalTestWebServerTests {
} }
@Test @Test
void uriUsesSingletonConnection() { void uriUsesSingletonBaseUriDetails() {
AtomicInteger counter = new AtomicInteger(); AtomicInteger counter = new AtomicInteger();
LocalTestWebServer server = LocalTestWebServer.of(Scheme.HTTPS, LocalTestWebServer server = LocalTestWebServer.of(Scheme.HTTPS,
() -> new Connection(8080, "/" + counter.incrementAndGet())); () -> new BaseUriDetails(8080, "/" + counter.incrementAndGet()));
assertThat(server.uri()).isEqualTo("https://localhost:8080/1"); assertThat(server.uri()).isEqualTo("https://localhost:8080/1");
assertThat(server.uri()).isEqualTo("https://localhost:8080/1"); assertThat(server.uri()).isEqualTo("https://localhost:8080/1");
} }
@Test
void uriBuilderFactoryUsesSingletonUriBuilderFactory() {
LocalTestWebServer server = LocalTestWebServer.of(Scheme.HTTPS, () -> new BaseUriDetails(8080, "/"));
UriBuilderFactory uriBuilderFactory = server.uriBuilderFactory();
assertThat(server.uriBuilderFactory()).isSameAs(uriBuilderFactory);
}
@Test @Test
void withPathCreatedNewInstance() { void withPathCreatedNewInstance() {
assertThat(LocalTestWebServer.of(Scheme.HTTPS, 8080, "/path").withPath("/other").uri()) assertThat(LocalTestWebServer.of(Scheme.HTTPS, 8080, "/path").withPath("/other").uri())
@ -145,9 +152,9 @@ class LocalTestWebServerTests {
@Test @Test
@SuppressWarnings("NullAway") // Test null check @SuppressWarnings("NullAway") // Test null check
void ofWhenConnectionSupplierIsNull() { void ofWhenBaseUriDetailsSupplierIsNull() {
assertThatIllegalArgumentException().isThrownBy(() -> LocalTestWebServer.of(Scheme.HTTPS, null)) assertThatIllegalArgumentException().isThrownBy(() -> LocalTestWebServer.of(Scheme.HTTPS, null))
.withMessage("'connectionSupplier' must not be null"); .withMessage("'baseUriDetailsSupplier' must not be null");
} }
@Test @Test
@ -180,12 +187,13 @@ class LocalTestWebServerTests {
org.springframework.boot.test.http.server.LocalTestWebServer$Provider=\ org.springframework.boot.test.http.server.LocalTestWebServer$Provider=\
org.springframework.boot.test.http.server.LocalTestWebServerTests$Provider1 org.springframework.boot.test.http.server.LocalTestWebServerTests$Provider1
""") """)
void getRequiredWhenNoneProvidedThrowsException() { void obtainWhenNoneProvidedThrowsException() {
ApplicationContext applicationContext = new GenericApplicationContext(); ApplicationContext applicationContext = new GenericApplicationContext();
assertThatIllegalStateException().isThrownBy(() -> LocalTestWebServer.getRequired(applicationContext)) assertThatIllegalStateException().isThrownBy(() -> LocalTestWebServer.obtain(applicationContext))
.withMessage("No local test web server available"); .withMessage("No local test web server available");
} }
@SuppressWarnings("unused")
static class Provider1 implements LocalTestWebServer.Provider { static class Provider1 implements LocalTestWebServer.Provider {
@Override @Override
@ -195,6 +203,7 @@ class LocalTestWebServerTests {
} }
@SuppressWarnings("unused")
static class Provider2 implements LocalTestWebServer.Provider { static class Provider2 implements LocalTestWebServer.Provider {
@Override @Override
@ -204,6 +213,7 @@ class LocalTestWebServerTests {
} }
@SuppressWarnings("unused")
static class Provider3 implements LocalTestWebServer.Provider { static class Provider3 implements LocalTestWebServer.Provider {
@Override @Override

1
module/spring-boot-graphql-test/src/test/java/org/springframework/boot/graphql/test/autoconfigure/tester/HttpGraphQlTesterAutoConfigurationTests.java

@ -102,6 +102,7 @@ class HttpGraphQlTesterAutoConfigurationTests {
}); });
} }
@SuppressWarnings("unused")
static class TestLocalTestWebServerProvider implements LocalTestWebServer.Provider { static class TestLocalTestWebServerProvider implements LocalTestWebServer.Provider {
@Override @Override

2
module/spring-boot-resttestclient/src/main/java/org/springframework/boot/resttestclient/autoconfigure/TestRestTemplateAutoConfiguration.java

@ -41,7 +41,7 @@ public final class TestRestTemplateAutoConfiguration {
TestRestTemplate testRestTemplate(ObjectProvider<RestTemplateBuilder> builderProvider, TestRestTemplate testRestTemplate(ObjectProvider<RestTemplateBuilder> builderProvider,
ApplicationContext applicationContext) { ApplicationContext applicationContext) {
RestTemplateBuilder builder = builderProvider.getIfAvailable(RestTemplateBuilder::new); RestTemplateBuilder builder = builderProvider.getIfAvailable(RestTemplateBuilder::new);
LocalTestWebServer localTestWebServer = LocalTestWebServer.getRequired(applicationContext); LocalTestWebServer localTestWebServer = LocalTestWebServer.obtain(applicationContext);
TestRestTemplate template = new TestRestTemplate(builder, null, null, TestRestTemplate template = new TestRestTemplate(builder, null, null,
httpClientOptions(localTestWebServer.scheme())); httpClientOptions(localTestWebServer.scheme()));
template.setUriTemplateHandler(localTestWebServer.uriBuilderFactory()); template.setUriTemplateHandler(localTestWebServer.uriBuilderFactory());

6
module/spring-boot-resttestclient/src/test/java/org/springframework/boot/resttestclient/autoconfigure/RestTestClientAutoConfigurationTests.java

@ -75,10 +75,9 @@ class RestTestClientAutoConfigurationTests {
org.springframework.boot.test.http.server.LocalTestWebServer$Provider=\ org.springframework.boot.test.http.server.LocalTestWebServer$Provider=\
org.springframework.boot.resttestclient.autoconfigure.RestTestClientAutoConfigurationTests$TestLocalTestWebServerProvider org.springframework.boot.resttestclient.autoconfigure.RestTestClientAutoConfigurationTests$TestLocalTestWebServerProvider
""") """)
void shouldDefineWebTestClientBoundToWebServer() { void shouldDefineRestTestClientBoundToWebServer() {
this.contextRunner.run((context) -> { this.contextRunner.run((context) -> {
assertThat(context).hasSingleBean(RestTestClient.class); assertThat(context).hasSingleBean(RestTestClient.class).hasBean("restTestClient");
assertThat(context).hasBean("restTestClient");
RestTestClient client = context.getBean(RestTestClient.class); RestTestClient client = context.getBean(RestTestClient.class);
UriBuilderFactory uiBuilderFactory = (UriBuilderFactory) Extractors UriBuilderFactory uiBuilderFactory = (UriBuilderFactory) Extractors
.byName("restTestClientBuilder.restClientBuilder.uriBuilderFactory") .byName("restTestClientBuilder.restClientBuilder.uriBuilderFactory")
@ -97,6 +96,7 @@ class RestTestClientAutoConfigurationTests {
} }
@SuppressWarnings("unused")
static class TestLocalTestWebServerProvider implements LocalTestWebServer.Provider { static class TestLocalTestWebServerProvider implements LocalTestWebServer.Provider {
@Override @Override

77
module/spring-boot-resttestclient/src/test/java/org/springframework/boot/resttestclient/autoconfigure/TestRestTemplateAutoConfigurationTests.java

@ -0,0 +1,77 @@
/*
* Copyright 2012-present 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.resttestclient.autoconfigure;
import java.net.URI;
import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.resttestclient.TestRestTemplate;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.boot.test.http.server.LocalTestWebServer;
import org.springframework.boot.test.http.server.LocalTestWebServer.Scheme;
import org.springframework.boot.testsupport.classpath.resources.WithResource;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link TestRestTemplateAutoConfiguration}.
*
* @author Stephane Nicoll
*/
class TestRestTemplateAutoConfigurationTests {
private final WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(TestRestTemplateAutoConfiguration.class));
@Test
void shouldFailTotRegisterTestRestTemplateWithoutWebServer() {
this.contextRunner.run((context) -> assertThat(context).hasFailed()
.getFailure()
.hasMessageContaining(" No local test web server available"));
}
@Test
@WithResource(name = "META-INF/spring.factories",
content = """
org.springframework.boot.test.http.server.LocalTestWebServer$Provider=\
org.springframework.boot.resttestclient.autoconfigure.TestRestTemplateAutoConfigurationTests$TestLocalTestWebServerProvider
""")
void shouldDefineTestRestTemplateBoundToWebServer() {
this.contextRunner.run((context) -> {
assertThat(context).hasSingleBean(TestRestTemplate.class)
.hasBean("org.springframework.boot.resttestclient.TestRestTemplate");
TestRestTemplate testRestTemplate = context.getBean(TestRestTemplate.class);
assertThat(testRestTemplate.getRestTemplate().getUriTemplateHandler().expand("/"))
.isEqualTo(URI.create("https://localhost:8182/"));
});
}
@SuppressWarnings("unused")
static class TestLocalTestWebServerProvider implements LocalTestWebServer.Provider {
@Override
public @Nullable LocalTestWebServer getLocalTestWebServer() {
return LocalTestWebServer.of(Scheme.HTTPS, 8182);
}
}
}

4
module/spring-boot-web-server/src/main/java/org/springframework/boot/web/server/reactive/context/ReactiveWebServerApplicationContextLocalTestWebServerProvider.java

@ -20,7 +20,7 @@ import org.jspecify.annotations.Nullable;
import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.boot.test.http.server.LocalTestWebServer; import org.springframework.boot.test.http.server.LocalTestWebServer;
import org.springframework.boot.test.http.server.LocalTestWebServer.Connection; import org.springframework.boot.test.http.server.LocalTestWebServer.BaseUriDetails;
import org.springframework.boot.test.http.server.LocalTestWebServer.Scheme; import org.springframework.boot.test.http.server.LocalTestWebServer.Scheme;
import org.springframework.boot.web.server.AbstractConfigurableWebServerFactory; import org.springframework.boot.web.server.AbstractConfigurableWebServerFactory;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
@ -56,7 +56,7 @@ class ReactiveWebServerApplicationContextLocalTestWebServerProvider implements L
return LocalTestWebServer.of((isSslEnabled(this.context)) ? Scheme.HTTPS : Scheme.HTTP, () -> { return LocalTestWebServer.of((isSslEnabled(this.context)) ? Scheme.HTTPS : Scheme.HTTP, () -> {
int port = this.context.getEnvironment().getProperty("local.server.port", Integer.class, 8080); int port = this.context.getEnvironment().getProperty("local.server.port", Integer.class, 8080);
String path = this.context.getEnvironment().getProperty("spring.webflux.base-path", ""); String path = this.context.getEnvironment().getProperty("spring.webflux.base-path", "");
return new Connection(port, path); return new BaseUriDetails(port, path);
}); });
} }

4
module/spring-boot-web-server/src/main/java/org/springframework/boot/web/server/servlet/context/ServletWebServerApplicationContextLocalTestWebServerProvider.java

@ -20,7 +20,7 @@ import org.jspecify.annotations.Nullable;
import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.boot.test.http.server.LocalTestWebServer; import org.springframework.boot.test.http.server.LocalTestWebServer;
import org.springframework.boot.test.http.server.LocalTestWebServer.Connection; import org.springframework.boot.test.http.server.LocalTestWebServer.BaseUriDetails;
import org.springframework.boot.test.http.server.LocalTestWebServer.Scheme; import org.springframework.boot.test.http.server.LocalTestWebServer.Scheme;
import org.springframework.boot.web.server.AbstractConfigurableWebServerFactory; import org.springframework.boot.web.server.AbstractConfigurableWebServerFactory;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
@ -56,7 +56,7 @@ class ServletWebServerApplicationContextLocalTestWebServerProvider implements Lo
return LocalTestWebServer.of((isSslEnabled(this.context)) ? Scheme.HTTPS : Scheme.HTTP, () -> { return LocalTestWebServer.of((isSslEnabled(this.context)) ? Scheme.HTTPS : Scheme.HTTP, () -> {
int port = this.context.getEnvironment().getProperty("local.server.port", Integer.class, 8080); int port = this.context.getEnvironment().getProperty("local.server.port", Integer.class, 8080);
String path = this.context.getEnvironment().getProperty("server.servlet.context-path", ""); String path = this.context.getEnvironment().getProperty("server.servlet.context-path", "");
return new Connection(port, path); return new BaseUriDetails(port, path);
}); });
} }

12
module/spring-boot-webmvc-test/src/main/java/org/springframework/boot/webmvc/test/autoconfigure/MockMvcWebClientAutoConfiguration.java

@ -24,11 +24,11 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnBooleanProp
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.test.web.htmlunit.UriBuilderFactoryWebClient; import org.springframework.boot.test.web.htmlunit.UriBuilderFactoryWebClient;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.htmlunit.MockMvcWebClientBuilder; import org.springframework.test.web.servlet.htmlunit.MockMvcWebClientBuilder;
import org.springframework.web.util.DefaultUriBuilderFactory; import org.springframework.web.util.DefaultUriBuilderFactory;
import org.springframework.web.util.UriBuilderFactory;
/** /**
* Auto-configuration for HtmlUnit {@link WebClient} MockMVC integration. * Auto-configuration for HtmlUnit {@link WebClient} MockMVC integration.
@ -41,12 +41,18 @@ import org.springframework.web.util.DefaultUriBuilderFactory;
@ConditionalOnBooleanProperty(name = "spring.test.mockmvc.webclient.enabled", matchIfMissing = true) @ConditionalOnBooleanProperty(name = "spring.test.mockmvc.webclient.enabled", matchIfMissing = true)
public final class MockMvcWebClientAutoConfiguration { public final class MockMvcWebClientAutoConfiguration {
/**
* A {@link UriBuilderFactory} that is suitable for Mock access (i.e. without a
* running web server).
*/
private static final UriBuilderFactory MOCK_URI_BUILDER_FACTORY = new DefaultUriBuilderFactory("http://localhost");
@Bean @Bean
@ConditionalOnMissingBean({ WebClient.class, MockMvcWebClientBuilder.class }) @ConditionalOnMissingBean({ WebClient.class, MockMvcWebClientBuilder.class })
@ConditionalOnBean(MockMvc.class) @ConditionalOnBean(MockMvc.class)
MockMvcWebClientBuilder mockMvcWebClientBuilder(MockMvc mockMvc, ApplicationContext applicationContext) { MockMvcWebClientBuilder mockMvcWebClientBuilder(MockMvc mockMvc) {
return MockMvcWebClientBuilder.mockMvcSetup(mockMvc) return MockMvcWebClientBuilder.mockMvcSetup(mockMvc)
.withDelegate(new UriBuilderFactoryWebClient(new DefaultUriBuilderFactory("http://localhost"))); .withDelegate(new UriBuilderFactoryWebClient(MOCK_URI_BUILDER_FACTORY));
} }
@Bean @Bean

14
module/spring-boot-webmvc-test/src/main/java/org/springframework/boot/webmvc/test/autoconfigure/MockMvcWebDriverAutoConfiguration.java

@ -27,11 +27,11 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnBooleanProp
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.test.web.htmlunit.UriBuilderFactoryWebConnectionHtmlUnitDriver; import org.springframework.boot.test.web.htmlunit.UriBuilderFactoryWebConnectionHtmlUnitDriver;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.htmlunit.webdriver.MockMvcHtmlUnitDriverBuilder; import org.springframework.test.web.servlet.htmlunit.webdriver.MockMvcHtmlUnitDriverBuilder;
import org.springframework.web.util.DefaultUriBuilderFactory; import org.springframework.web.util.DefaultUriBuilderFactory;
import org.springframework.web.util.UriBuilderFactory;
/** /**
* Auto-configuration for Selenium {@link WebDriver} MockMVC integration. * Auto-configuration for Selenium {@link WebDriver} MockMVC integration.
@ -44,13 +44,19 @@ import org.springframework.web.util.DefaultUriBuilderFactory;
@ConditionalOnBooleanProperty(name = "spring.test.mockmvc.webdriver.enabled", matchIfMissing = true) @ConditionalOnBooleanProperty(name = "spring.test.mockmvc.webdriver.enabled", matchIfMissing = true)
public final class MockMvcWebDriverAutoConfiguration { public final class MockMvcWebDriverAutoConfiguration {
/**
* A {@link UriBuilderFactory} that is suitable for Mock access (i.e. without a
* running web server).
*/
private static final UriBuilderFactory MOCK_URI_BUILDER_FACTORY = new DefaultUriBuilderFactory("http://localhost");
@Bean @Bean
@ConditionalOnMissingBean({ WebDriver.class, MockMvcHtmlUnitDriverBuilder.class }) @ConditionalOnMissingBean({ WebDriver.class, MockMvcHtmlUnitDriverBuilder.class })
@ConditionalOnBean(MockMvc.class) @ConditionalOnBean(MockMvc.class)
MockMvcHtmlUnitDriverBuilder mockMvcHtmlUnitDriverBuilder(MockMvc mockMvc, ApplicationContext applicationContext) { MockMvcHtmlUnitDriverBuilder mockMvcHtmlUnitDriverBuilder(MockMvc mockMvc) {
return MockMvcHtmlUnitDriverBuilder.mockMvcSetup(mockMvc) return MockMvcHtmlUnitDriverBuilder.mockMvcSetup(mockMvc)
.withDelegate(new UriBuilderFactoryWebConnectionHtmlUnitDriver( .withDelegate(
new DefaultUriBuilderFactory("http://localhost"), BrowserVersion.CHROME)); new UriBuilderFactoryWebConnectionHtmlUnitDriver(MOCK_URI_BUILDER_FACTORY, BrowserVersion.CHROME));
} }
@Bean @Bean

1
module/spring-boot-webtestclient/src/test/java/org/springframework/boot/webflux/test/autoconfigure/WebTestClientAutoConfigurationTests.java

@ -225,6 +225,7 @@ class WebTestClientAutoConfigurationTests {
} }
@SuppressWarnings("unused")
static class TestLocalTestWebServerProvider implements LocalTestWebServer.Provider { static class TestLocalTestWebServerProvider implements LocalTestWebServer.Provider {
@Override @Override

2
smoke-test/spring-boot-smoke-test-actuator-custom-security/src/test/java/smoketest/actuator/customsecurity/AbstractSampleActuatorCustomSecurityTests.java

@ -200,7 +200,7 @@ abstract class AbstractSampleActuatorCustomSecurityTests {
} }
private TestRestTemplate configure(TestRestTemplate restTemplate) { private TestRestTemplate configure(TestRestTemplate restTemplate) {
LocalTestWebServer localTestWebServer = LocalTestWebServer.getRequired(getApplicationContext()); LocalTestWebServer localTestWebServer = LocalTestWebServer.obtain(getApplicationContext());
restTemplate.setUriTemplateHandler(localTestWebServer.uriBuilderFactory()); restTemplate.setUriTemplateHandler(localTestWebServer.uriBuilderFactory());
return restTemplate; return restTemplate;
} }

2
smoke-test/spring-boot-smoke-test-actuator-custom-security/src/test/java/smoketest/actuator/customsecurity/CorsSampleActuatorApplicationTests.java

@ -54,7 +54,7 @@ class CorsSampleActuatorApplicationTests {
@BeforeEach @BeforeEach
void setUp() { void setUp() {
RestTemplateBuilder builder = new RestTemplateBuilder(); RestTemplateBuilder builder = new RestTemplateBuilder();
LocalTestWebServer localTestWebServer = LocalTestWebServer.getRequired(this.applicationContext); LocalTestWebServer localTestWebServer = LocalTestWebServer.obtain(this.applicationContext);
builder = builder.uriTemplateHandler(localTestWebServer.uriBuilderFactory()); builder = builder.uriTemplateHandler(localTestWebServer.uriBuilderFactory());
this.testRestTemplate = new TestRestTemplate(builder); this.testRestTemplate = new TestRestTemplate(builder);
} }

2
smoke-test/spring-boot-smoke-test-actuator-extension/src/test/java/smoketest/actuator/extension/SampleActuatorExtensionApplicationTests.java

@ -65,7 +65,7 @@ class SampleActuatorExtensionApplicationTests {
void healthExtensionWithAuthHeader() { void healthExtensionWithAuthHeader() {
TestRestTemplate restTemplate = new TestRestTemplate( TestRestTemplate restTemplate = new TestRestTemplate(
this.restTemplateBuilder.defaultHeader("Authorization", "Bearer secret")); this.restTemplateBuilder.defaultHeader("Authorization", "Bearer secret"));
LocalTestWebServer localTestWebServer = LocalTestWebServer.getRequired(this.applicationContext); LocalTestWebServer localTestWebServer = LocalTestWebServer.obtain(this.applicationContext);
restTemplate.setUriTemplateHandler(localTestWebServer.uriBuilderFactory()); restTemplate.setUriTemplateHandler(localTestWebServer.uriBuilderFactory());
ResponseEntity<Map> entity = restTemplate.getForEntity("/myextension/health", Map.class); ResponseEntity<Map> entity = restTemplate.getForEntity("/myextension/health", Map.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);

2
smoke-test/spring-boot-smoke-test-actuator/src/test/java/smoketest/actuator/CorsSampleActuatorApplicationTests.java

@ -54,7 +54,7 @@ class CorsSampleActuatorApplicationTests {
@BeforeEach @BeforeEach
void setUp() { void setUp() {
RestTemplateBuilder builder = new RestTemplateBuilder(); RestTemplateBuilder builder = new RestTemplateBuilder();
LocalTestWebServer localTestWebServer = LocalTestWebServer.getRequired(this.applicationContext); LocalTestWebServer localTestWebServer = LocalTestWebServer.obtain(this.applicationContext);
builder = builder.uriTemplateHandler(localTestWebServer.uriBuilderFactory()); builder = builder.uriTemplateHandler(localTestWebServer.uriBuilderFactory());
this.testRestTemplate = new TestRestTemplate(builder); this.testRestTemplate = new TestRestTemplate(builder);
} }

Loading…
Cancel
Save