mirror of
https://github.com/spring-projects/spring-boot.git
synced 2026-09-27 04:19:03 +00:00
Polish "Replace BaseUrl with LocalTestWebServer"
See gh-47680
This commit is contained in:
+4
-3
@@ -30,13 +30,14 @@ import org.springframework.web.util.UriBuilderFactory;
|
||||
* Lazy {@link UriBuilderFactory} that only obtains the delegate on first call.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
class LazyUriBuilderFactory implements UriBuilderFactory {
|
||||
|
||||
private final Supplier<UriBuilderFactory> suppler;
|
||||
private final Supplier<UriBuilderFactory> supplier;
|
||||
|
||||
LazyUriBuilderFactory(Supplier<UriBuilderFactory> supplier) {
|
||||
this.suppler = SingletonSupplier.of(supplier);
|
||||
this.supplier = SingletonSupplier.of(supplier);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -60,7 +61,7 @@ class LazyUriBuilderFactory implements UriBuilderFactory {
|
||||
}
|
||||
|
||||
private UriBuilderFactory delegate() {
|
||||
return this.suppler.get();
|
||||
return this.supplier.get();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+28
-35
@@ -43,18 +43,22 @@ public final class LocalTestWebServer {
|
||||
|
||||
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(connectionSupplier, "'connectionSupplier' must not be null");
|
||||
Assert.notNull(baseUriDetailsSupplier, "'baseUriDetailsSupplier' must not be null");
|
||||
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
|
||||
* before the local test server is fully running.
|
||||
* Return if URI scheme used for the request. This method can be safely called before
|
||||
* the local test server is fully running.
|
||||
* @return if the web server uses an HTTPS address
|
||||
*/
|
||||
public Scheme scheme() {
|
||||
@@ -100,7 +104,7 @@ public final class LocalTestWebServer {
|
||||
* @return a new {@link 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
|
||||
*/
|
||||
public LocalTestWebServer withPath(String path) {
|
||||
return of(this.scheme, () -> getConnection().withPath(path));
|
||||
}
|
||||
|
||||
private Connection getConnection() {
|
||||
Connection connection = this.connection.get();
|
||||
Assert.state(connection != null, "No local test web server connection supplied");
|
||||
return connection;
|
||||
return of(this.scheme, () -> this.baseUriDetails.obtain().withPath(path));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -137,33 +135,33 @@ public final class LocalTestWebServer {
|
||||
* @return a new {@link LocalTestWebServer} instance
|
||||
*/
|
||||
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.
|
||||
* @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
|
||||
*/
|
||||
public static LocalTestWebServer of(Scheme scheme, Supplier<Connection> connectionSupplier) {
|
||||
return new LocalTestWebServer(scheme, connectionSupplier);
|
||||
public static LocalTestWebServer of(Scheme scheme, Supplier<BaseUriDetails> baseUriDetailsSupplier) {
|
||||
return new LocalTestWebServer(scheme, baseUriDetailsSupplier);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a {@link LocalTestWebServer} instance provided from the
|
||||
* Obtain the {@link LocalTestWebServer} instance provided from the
|
||||
* {@link ApplicationContext}.
|
||||
* @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);
|
||||
Assert.state(localTestWebServer != null, "No local test web server available");
|
||||
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
|
||||
* be provided.
|
||||
* @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 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) {
|
||||
StringBuilder uri = new StringBuilder(scheme.name().toLowerCase(Locale.getDefault()));
|
||||
uri.append("://localhost:");
|
||||
uri.append(port());
|
||||
uri.append(path());
|
||||
return uri.toString();
|
||||
return scheme.name().toLowerCase(Locale.ROOT) + "://localhost:" + port() + path();
|
||||
}
|
||||
|
||||
Connection withPath(String path) {
|
||||
return new Connection(port(), path() + path);
|
||||
BaseUriDetails withPath(String path) {
|
||||
return new BaseUriDetails(port(), path() + path);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -221,10 +215,9 @@ public final class LocalTestWebServer {
|
||||
}
|
||||
|
||||
/**
|
||||
* Strategy used to provide the running {@link LocalTestWebServer}. Implementations
|
||||
* can be registered in {@code spring.factories} and may accept an
|
||||
* Internal strategy used to provide the running {@link LocalTestWebServer}.
|
||||
* Implementations can be registered in {@code spring.factories} and may accept an
|
||||
* {@link ApplicationContext} constructor argument.
|
||||
*
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface Provider {
|
||||
|
||||
+2
-3
@@ -34,7 +34,7 @@ import org.springframework.web.util.UriBuilderFactory;
|
||||
*/
|
||||
public class UriBuilderFactoryWebClient extends WebClient {
|
||||
|
||||
private UriBuilderFactory uriBuilderFactory;
|
||||
private final UriBuilderFactory uriBuilderFactory;
|
||||
|
||||
public UriBuilderFactoryWebClient(UriBuilderFactory uriBuilderFactory) {
|
||||
Assert.notNull(uriBuilderFactory, "'uriBuilderFactory' must not be null");
|
||||
@@ -43,8 +43,7 @@ public class UriBuilderFactoryWebClient extends WebClient {
|
||||
|
||||
@Override
|
||||
public <P extends Page> P getPage(String url) throws IOException, FailingHttpStatusCodeException {
|
||||
return super.getPage(
|
||||
(this.uriBuilderFactory != null) ? this.uriBuilderFactory.uriString(url).toUriString() : url);
|
||||
return super.getPage(this.uriBuilderFactory.uriString(url).toUriString());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+2
-1
@@ -32,7 +32,7 @@ import org.springframework.web.util.UriBuilderFactory;
|
||||
*/
|
||||
public class UriBuilderFactoryWebConnectionHtmlUnitDriver extends WebConnectionHtmlUnitDriver {
|
||||
|
||||
private UriBuilderFactory uriBuilderFactory;
|
||||
private final UriBuilderFactory uriBuilderFactory;
|
||||
|
||||
public UriBuilderFactoryWebConnectionHtmlUnitDriver(UriBuilderFactory uriBuilderFactory) {
|
||||
Assert.notNull(uriBuilderFactory, "'uriBuilderFactory' must not be null");
|
||||
@@ -60,6 +60,7 @@ public class UriBuilderFactoryWebConnectionHtmlUnitDriver extends WebConnectionH
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("ConstantValue") // default constructor calls this method
|
||||
public void get(String url) {
|
||||
super.get((this.uriBuilderFactory != null) ? this.uriBuilderFactory.uriString(url).toUriString() : url);
|
||||
}
|
||||
|
||||
+17
-7
@@ -23,7 +23,7 @@ import java.util.concurrent.atomic.AtomicInteger;
|
||||
import org.jspecify.annotations.Nullable;
|
||||
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.testsupport.classpath.resources.WithResource;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
@@ -129,14 +129,21 @@ class LocalTestWebServerTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
void uriUsesSingletonConnection() {
|
||||
void uriUsesSingletonBaseUriDetails() {
|
||||
AtomicInteger counter = new AtomicInteger();
|
||||
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");
|
||||
}
|
||||
|
||||
@Test
|
||||
void uriBuilderFactoryUsesSingletonUriBuilderFactory() {
|
||||
LocalTestWebServer server = LocalTestWebServer.of(Scheme.HTTPS, () -> new BaseUriDetails(8080, "/"));
|
||||
UriBuilderFactory uriBuilderFactory = server.uriBuilderFactory();
|
||||
assertThat(server.uriBuilderFactory()).isSameAs(uriBuilderFactory);
|
||||
}
|
||||
|
||||
@Test
|
||||
void withPathCreatedNewInstance() {
|
||||
assertThat(LocalTestWebServer.of(Scheme.HTTPS, 8080, "/path").withPath("/other").uri())
|
||||
@@ -145,9 +152,9 @@ class LocalTestWebServerTests {
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("NullAway") // Test null check
|
||||
void ofWhenConnectionSupplierIsNull() {
|
||||
void ofWhenBaseUriDetailsSupplierIsNull() {
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> LocalTestWebServer.of(Scheme.HTTPS, null))
|
||||
.withMessage("'connectionSupplier' must not be null");
|
||||
.withMessage("'baseUriDetailsSupplier' must not be null");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -180,12 +187,13 @@ class LocalTestWebServerTests {
|
||||
org.springframework.boot.test.http.server.LocalTestWebServer$Provider=\
|
||||
org.springframework.boot.test.http.server.LocalTestWebServerTests$Provider1
|
||||
""")
|
||||
void getRequiredWhenNoneProvidedThrowsException() {
|
||||
void obtainWhenNoneProvidedThrowsException() {
|
||||
ApplicationContext applicationContext = new GenericApplicationContext();
|
||||
assertThatIllegalStateException().isThrownBy(() -> LocalTestWebServer.getRequired(applicationContext))
|
||||
assertThatIllegalStateException().isThrownBy(() -> LocalTestWebServer.obtain(applicationContext))
|
||||
.withMessage("No local test web server available");
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
static class Provider1 implements LocalTestWebServer.Provider {
|
||||
|
||||
@Override
|
||||
@@ -195,6 +203,7 @@ class LocalTestWebServerTests {
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
static class Provider2 implements LocalTestWebServer.Provider {
|
||||
|
||||
@Override
|
||||
@@ -204,6 +213,7 @@ class LocalTestWebServerTests {
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
static class Provider3 implements LocalTestWebServer.Provider {
|
||||
|
||||
@Override
|
||||
|
||||
+1
@@ -102,6 +102,7 @@ class HttpGraphQlTesterAutoConfigurationTests {
|
||||
});
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
static class TestLocalTestWebServerProvider implements LocalTestWebServer.Provider {
|
||||
|
||||
@Override
|
||||
|
||||
+1
-1
@@ -41,7 +41,7 @@ public final class TestRestTemplateAutoConfiguration {
|
||||
TestRestTemplate testRestTemplate(ObjectProvider<RestTemplateBuilder> builderProvider,
|
||||
ApplicationContext applicationContext) {
|
||||
RestTemplateBuilder builder = builderProvider.getIfAvailable(RestTemplateBuilder::new);
|
||||
LocalTestWebServer localTestWebServer = LocalTestWebServer.getRequired(applicationContext);
|
||||
LocalTestWebServer localTestWebServer = LocalTestWebServer.obtain(applicationContext);
|
||||
TestRestTemplate template = new TestRestTemplate(builder, null, null,
|
||||
httpClientOptions(localTestWebServer.scheme()));
|
||||
template.setUriTemplateHandler(localTestWebServer.uriBuilderFactory());
|
||||
|
||||
+3
-3
@@ -75,10 +75,9 @@ class RestTestClientAutoConfigurationTests {
|
||||
org.springframework.boot.test.http.server.LocalTestWebServer$Provider=\
|
||||
org.springframework.boot.resttestclient.autoconfigure.RestTestClientAutoConfigurationTests$TestLocalTestWebServerProvider
|
||||
""")
|
||||
void shouldDefineWebTestClientBoundToWebServer() {
|
||||
void shouldDefineRestTestClientBoundToWebServer() {
|
||||
this.contextRunner.run((context) -> {
|
||||
assertThat(context).hasSingleBean(RestTestClient.class);
|
||||
assertThat(context).hasBean("restTestClient");
|
||||
assertThat(context).hasSingleBean(RestTestClient.class).hasBean("restTestClient");
|
||||
RestTestClient client = context.getBean(RestTestClient.class);
|
||||
UriBuilderFactory uiBuilderFactory = (UriBuilderFactory) Extractors
|
||||
.byName("restTestClientBuilder.restClientBuilder.uriBuilderFactory")
|
||||
@@ -97,6 +96,7 @@ class RestTestClientAutoConfigurationTests {
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
static class TestLocalTestWebServerProvider implements LocalTestWebServer.Provider {
|
||||
|
||||
@Override
|
||||
|
||||
+77
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+2
-2
@@ -20,7 +20,7 @@ import org.jspecify.annotations.Nullable;
|
||||
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
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.web.server.AbstractConfigurableWebServerFactory;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
@@ -56,7 +56,7 @@ class ReactiveWebServerApplicationContextLocalTestWebServerProvider implements L
|
||||
return LocalTestWebServer.of((isSslEnabled(this.context)) ? Scheme.HTTPS : Scheme.HTTP, () -> {
|
||||
int port = this.context.getEnvironment().getProperty("local.server.port", Integer.class, 8080);
|
||||
String path = this.context.getEnvironment().getProperty("spring.webflux.base-path", "");
|
||||
return new Connection(port, path);
|
||||
return new BaseUriDetails(port, path);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -20,7 +20,7 @@ import org.jspecify.annotations.Nullable;
|
||||
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
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.web.server.AbstractConfigurableWebServerFactory;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
@@ -56,7 +56,7 @@ class ServletWebServerApplicationContextLocalTestWebServerProvider implements Lo
|
||||
return LocalTestWebServer.of((isSslEnabled(this.context)) ? Scheme.HTTPS : Scheme.HTTP, () -> {
|
||||
int port = this.context.getEnvironment().getProperty("local.server.port", Integer.class, 8080);
|
||||
String path = this.context.getEnvironment().getProperty("server.servlet.context-path", "");
|
||||
return new Connection(port, path);
|
||||
return new BaseUriDetails(port, path);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
+9
-3
@@ -24,11 +24,11 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnBooleanProp
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.test.web.htmlunit.UriBuilderFactoryWebClient;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.htmlunit.MockMvcWebClientBuilder;
|
||||
import org.springframework.web.util.DefaultUriBuilderFactory;
|
||||
import org.springframework.web.util.UriBuilderFactory;
|
||||
|
||||
/**
|
||||
* 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)
|
||||
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
|
||||
@ConditionalOnMissingBean({ WebClient.class, MockMvcWebClientBuilder.class })
|
||||
@ConditionalOnBean(MockMvc.class)
|
||||
MockMvcWebClientBuilder mockMvcWebClientBuilder(MockMvc mockMvc, ApplicationContext applicationContext) {
|
||||
MockMvcWebClientBuilder mockMvcWebClientBuilder(MockMvc mockMvc) {
|
||||
return MockMvcWebClientBuilder.mockMvcSetup(mockMvc)
|
||||
.withDelegate(new UriBuilderFactoryWebClient(new DefaultUriBuilderFactory("http://localhost")));
|
||||
.withDelegate(new UriBuilderFactoryWebClient(MOCK_URI_BUILDER_FACTORY));
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
+10
-4
@@ -27,11 +27,11 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnBooleanProp
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.test.web.htmlunit.UriBuilderFactoryWebConnectionHtmlUnitDriver;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.htmlunit.webdriver.MockMvcHtmlUnitDriverBuilder;
|
||||
import org.springframework.web.util.DefaultUriBuilderFactory;
|
||||
import org.springframework.web.util.UriBuilderFactory;
|
||||
|
||||
/**
|
||||
* 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)
|
||||
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
|
||||
@ConditionalOnMissingBean({ WebDriver.class, MockMvcHtmlUnitDriverBuilder.class })
|
||||
@ConditionalOnBean(MockMvc.class)
|
||||
MockMvcHtmlUnitDriverBuilder mockMvcHtmlUnitDriverBuilder(MockMvc mockMvc, ApplicationContext applicationContext) {
|
||||
MockMvcHtmlUnitDriverBuilder mockMvcHtmlUnitDriverBuilder(MockMvc mockMvc) {
|
||||
return MockMvcHtmlUnitDriverBuilder.mockMvcSetup(mockMvc)
|
||||
.withDelegate(new UriBuilderFactoryWebConnectionHtmlUnitDriver(
|
||||
new DefaultUriBuilderFactory("http://localhost"), BrowserVersion.CHROME));
|
||||
.withDelegate(
|
||||
new UriBuilderFactoryWebConnectionHtmlUnitDriver(MOCK_URI_BUILDER_FACTORY, BrowserVersion.CHROME));
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
+1
@@ -225,6 +225,7 @@ class WebTestClientAutoConfigurationTests {
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
static class TestLocalTestWebServerProvider implements LocalTestWebServer.Provider {
|
||||
|
||||
@Override
|
||||
|
||||
+1
-1
@@ -200,7 +200,7 @@ abstract class AbstractSampleActuatorCustomSecurityTests {
|
||||
}
|
||||
|
||||
private TestRestTemplate configure(TestRestTemplate restTemplate) {
|
||||
LocalTestWebServer localTestWebServer = LocalTestWebServer.getRequired(getApplicationContext());
|
||||
LocalTestWebServer localTestWebServer = LocalTestWebServer.obtain(getApplicationContext());
|
||||
restTemplate.setUriTemplateHandler(localTestWebServer.uriBuilderFactory());
|
||||
return restTemplate;
|
||||
}
|
||||
|
||||
+1
-1
@@ -54,7 +54,7 @@ class CorsSampleActuatorApplicationTests {
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
RestTemplateBuilder builder = new RestTemplateBuilder();
|
||||
LocalTestWebServer localTestWebServer = LocalTestWebServer.getRequired(this.applicationContext);
|
||||
LocalTestWebServer localTestWebServer = LocalTestWebServer.obtain(this.applicationContext);
|
||||
builder = builder.uriTemplateHandler(localTestWebServer.uriBuilderFactory());
|
||||
this.testRestTemplate = new TestRestTemplate(builder);
|
||||
}
|
||||
|
||||
+1
-1
@@ -65,7 +65,7 @@ class SampleActuatorExtensionApplicationTests {
|
||||
void healthExtensionWithAuthHeader() {
|
||||
TestRestTemplate restTemplate = new TestRestTemplate(
|
||||
this.restTemplateBuilder.defaultHeader("Authorization", "Bearer secret"));
|
||||
LocalTestWebServer localTestWebServer = LocalTestWebServer.getRequired(this.applicationContext);
|
||||
LocalTestWebServer localTestWebServer = LocalTestWebServer.obtain(this.applicationContext);
|
||||
restTemplate.setUriTemplateHandler(localTestWebServer.uriBuilderFactory());
|
||||
ResponseEntity<Map> entity = restTemplate.getForEntity("/myextension/health", Map.class);
|
||||
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
|
||||
+1
-1
@@ -54,7 +54,7 @@ class CorsSampleActuatorApplicationTests {
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
RestTemplateBuilder builder = new RestTemplateBuilder();
|
||||
LocalTestWebServer localTestWebServer = LocalTestWebServer.getRequired(this.applicationContext);
|
||||
LocalTestWebServer localTestWebServer = LocalTestWebServer.obtain(this.applicationContext);
|
||||
builder = builder.uriTemplateHandler(localTestWebServer.uriBuilderFactory());
|
||||
this.testRestTemplate = new TestRestTemplate(builder);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user