Browse Source
Replace `BaseUrl` and `BaseUrlProvider` provider code with a more targeted `LocalTestWebServer` class. The `LocalTestWebServer` can be used to obtain the url of the locally running server, or provide `UriBuilderFactory` or `UriBuilder` instances base on it. This commit also updates the MockMVC HTML Unit auto-configuration to directly use `localhost` as the base URL. Closes gh-47680pull/47721/head
32 changed files with 735 additions and 765 deletions
@ -1,80 +0,0 @@
@@ -1,80 +0,0 @@
|
||||
/* |
||||
* 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.test.http.client; |
||||
|
||||
import java.net.URI; |
||||
import java.util.Map; |
||||
|
||||
import org.jspecify.annotations.Nullable; |
||||
|
||||
import org.springframework.boot.test.http.server.BaseUrl; |
||||
import org.springframework.util.Assert; |
||||
import org.springframework.web.util.DefaultUriBuilderFactory; |
||||
import org.springframework.web.util.UriBuilder; |
||||
import org.springframework.web.util.UriBuilderFactory; |
||||
import org.springframework.web.util.UriComponentsBuilder; |
||||
|
||||
/** |
||||
* {@link UriBuilderFactory} to support {@link BaseUrl}. |
||||
* |
||||
* @author Phillip Webb |
||||
* @author Stephane Nicoll |
||||
* @since 4.0.0 |
||||
*/ |
||||
public final class BaseUrlUriBuilderFactory implements UriBuilderFactory { |
||||
|
||||
private final BaseUrl baseUrl; |
||||
|
||||
/** |
||||
* Create a new {@link BaseUrlUriBuilderFactory} instance. |
||||
* @param baseUrl the base URL to use |
||||
*/ |
||||
BaseUrlUriBuilderFactory(BaseUrl baseUrl) { |
||||
Assert.notNull(baseUrl, "'baseUrl' must not be null"); |
||||
this.baseUrl = baseUrl; |
||||
} |
||||
|
||||
/** |
||||
* Get a {@link UriBuilderFactory} instance applying the given {@code baseUrl}. |
||||
* @param baseUrl the base URL to apply or {@code null} |
||||
* @return a factory for the given base URL |
||||
*/ |
||||
public static UriBuilderFactory get(@Nullable BaseUrl baseUrl) { |
||||
return (baseUrl != null) ? new BaseUrlUriBuilderFactory(baseUrl) : new DefaultUriBuilderFactory(); |
||||
} |
||||
|
||||
@Override |
||||
public UriBuilder uriString(String uriTemplate) { |
||||
return this.baseUrl.getUriBuilderFactory().uriString(uriTemplate); |
||||
} |
||||
|
||||
@Override |
||||
public UriBuilder builder() { |
||||
return UriComponentsBuilder.newInstance(); |
||||
} |
||||
|
||||
@Override |
||||
public URI expand(String uriTemplate, Map<String, ?> uriVariables) { |
||||
return this.baseUrl.getUriBuilderFactory().expand(uriTemplate, uriVariables); |
||||
} |
||||
|
||||
@Override |
||||
public URI expand(String uriTemplate, @Nullable Object... uriVariables) { |
||||
return this.baseUrl.getUriBuilderFactory().expand(uriTemplate, uriVariables); |
||||
} |
||||
|
||||
} |
||||
@ -1,96 +0,0 @@
@@ -1,96 +0,0 @@
|
||||
/* |
||||
* 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.test.http.server; |
||||
|
||||
import java.util.function.Supplier; |
||||
|
||||
import org.springframework.util.Assert; |
||||
import org.springframework.util.StringUtils; |
||||
import org.springframework.util.function.SingletonSupplier; |
||||
import org.springframework.web.util.DefaultUriBuilderFactory; |
||||
import org.springframework.web.util.UriBuilderFactory; |
||||
|
||||
/** |
||||
* A base URL that can be used to connect to the running server. |
||||
* |
||||
* @author Phillip Webb |
||||
* @author Stephane Nicoll |
||||
* @since 4.0.0 |
||||
*/ |
||||
public final class BaseUrl { |
||||
|
||||
/** |
||||
* {@link BaseUrl} that resolves to {@code http://localhost}.
|
||||
*/ |
||||
public static final BaseUrl LOCALHOST = BaseUrl.of("http://localhost"); |
||||
|
||||
private final boolean https; |
||||
|
||||
private final Supplier<String> resolver; |
||||
|
||||
private BaseUrl(boolean https, Supplier<String> resolver) { |
||||
this.https = https; |
||||
this.resolver = SingletonSupplier.of(resolver); |
||||
} |
||||
|
||||
/** |
||||
* Return if the URL will ultimately resolve to an HTTPS address. |
||||
* @return if the URL is HTTPS |
||||
*/ |
||||
public boolean isHttps() { |
||||
return this.https; |
||||
} |
||||
|
||||
/** |
||||
* Get a {@link UriBuilderFactory} that applies the base URL. |
||||
* @return a {@link UriBuilderFactory} |
||||
*/ |
||||
public UriBuilderFactory getUriBuilderFactory() { |
||||
return new DefaultUriBuilderFactory(this.resolver.get()); |
||||
} |
||||
|
||||
/** |
||||
* Return a new instance that applies the given {@code path}. |
||||
* @param path a path to append |
||||
* @return a new instance with the path added |
||||
*/ |
||||
public BaseUrl withPath(String path) { |
||||
return new BaseUrl(this.https, () -> this.resolver.get() + path); |
||||
} |
||||
|
||||
/** |
||||
* Factory method to create a new {@link BaseUrl}. |
||||
* @param url the URL to use |
||||
* @return a new {@link BaseUrl} instance |
||||
*/ |
||||
public static BaseUrl of(String url) { |
||||
Assert.notNull(url, "'url' must not be null"); |
||||
return of(StringUtils.startsWithIgnoreCase(url, "https"), () -> url); |
||||
} |
||||
|
||||
/** |
||||
* Factory method to create a new {@link BaseUrl}. |
||||
* @param https whether the base URL is https |
||||
* @param resolver the resolver used to supply the actual URL |
||||
* @return a new {@link BaseUrl} instance |
||||
*/ |
||||
public static BaseUrl of(boolean https, Supplier<String> resolver) { |
||||
Assert.notNull(resolver, "'resolver' must not be null"); |
||||
return new BaseUrl(https, resolver); |
||||
} |
||||
|
||||
} |
||||
@ -1,40 +0,0 @@
@@ -1,40 +0,0 @@
|
||||
/* |
||||
* 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.test.http.server; |
||||
|
||||
import org.jspecify.annotations.Nullable; |
||||
|
||||
import org.springframework.context.ApplicationContext; |
||||
|
||||
/** |
||||
* Strategy used to provide the base URL that can be used to connect to the running |
||||
* server. Implementations can be registered in {@code spring.factories} and may accept an |
||||
* {@link ApplicationContext} constructor argument. |
||||
* |
||||
* @author Phillip Webb |
||||
* @since 4.0.0 |
||||
*/ |
||||
@FunctionalInterface |
||||
public interface BaseUrlProvider { |
||||
|
||||
/** |
||||
* Return the base URL that can be used to connect to the running server. |
||||
* @return the base URL or {@code null} |
||||
*/ |
||||
@Nullable BaseUrl getBaseUrl(); |
||||
|
||||
} |
||||
@ -1,72 +0,0 @@
@@ -1,72 +0,0 @@
|
||||
/* |
||||
* 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.test.http.server; |
||||
|
||||
import java.util.List; |
||||
import java.util.Objects; |
||||
|
||||
import org.jspecify.annotations.Nullable; |
||||
|
||||
import org.springframework.context.ApplicationContext; |
||||
import org.springframework.core.io.support.SpringFactoriesLoader; |
||||
import org.springframework.core.io.support.SpringFactoriesLoader.ArgumentResolver; |
||||
import org.springframework.lang.Contract; |
||||
import org.springframework.util.Assert; |
||||
|
||||
/** |
||||
* A collection of {@link BaseUrlProvider} instances loaded from {@code spring.factories}. |
||||
* |
||||
* @author Phillip Webb |
||||
* @since 4.0.0 |
||||
*/ |
||||
public class BaseUrlProviders { |
||||
|
||||
private List<BaseUrlProvider> providers; |
||||
|
||||
public BaseUrlProviders(ApplicationContext applicationContext) { |
||||
Assert.notNull(applicationContext, "'applicationContext' must not be null"); |
||||
this.providers = SpringFactoriesLoader.forDefaultResourceLocation(applicationContext.getClassLoader()) |
||||
.load(BaseUrlProvider.class, ArgumentResolver.of(ApplicationContext.class, applicationContext)); |
||||
} |
||||
|
||||
BaseUrlProviders(List<BaseUrlProvider> providers) { |
||||
this.providers = providers; |
||||
} |
||||
|
||||
/** |
||||
* Return the provided {@link BaseUrl} or {@code null}. |
||||
* @return the base URL or {@code null} |
||||
*/ |
||||
public @Nullable BaseUrl getBaseUrl() { |
||||
return getBaseUrl(null); |
||||
} |
||||
|
||||
/** |
||||
* Return the provided {@link BaseUrl} or the given fallback. |
||||
* @param fallback the fallback |
||||
* @return the base URL or the fallback |
||||
*/ |
||||
@Contract("!null -> !null") |
||||
public @Nullable BaseUrl getBaseUrl(@Nullable BaseUrl fallback) { |
||||
return this.providers.stream() |
||||
.map(BaseUrlProvider::getBaseUrl) |
||||
.filter(Objects::nonNull) |
||||
.findFirst() |
||||
.orElse(fallback); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,66 @@
@@ -0,0 +1,66 @@
|
||||
/* |
||||
* 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.test.http.server; |
||||
|
||||
import java.net.URI; |
||||
import java.util.Map; |
||||
import java.util.function.Supplier; |
||||
|
||||
import org.jspecify.annotations.Nullable; |
||||
|
||||
import org.springframework.util.function.SingletonSupplier; |
||||
import org.springframework.web.util.UriBuilder; |
||||
import org.springframework.web.util.UriBuilderFactory; |
||||
|
||||
/** |
||||
* Lazy {@link UriBuilderFactory} that only obtains the delegate on first call. |
||||
* |
||||
* @author Phillip Webb |
||||
*/ |
||||
class LazyUriBuilderFactory implements UriBuilderFactory { |
||||
|
||||
private final Supplier<UriBuilderFactory> suppler; |
||||
|
||||
LazyUriBuilderFactory(Supplier<UriBuilderFactory> supplier) { |
||||
this.suppler = SingletonSupplier.of(supplier); |
||||
} |
||||
|
||||
@Override |
||||
public URI expand(String uriTemplate, Map<String, ? extends @Nullable Object> uriVariables) { |
||||
return delegate().expand(uriTemplate, uriVariables); |
||||
} |
||||
|
||||
@Override |
||||
public URI expand(String uriTemplate, @Nullable Object... uriVariables) { |
||||
return delegate().expand(uriTemplate, uriVariables); |
||||
} |
||||
|
||||
@Override |
||||
public UriBuilder uriString(String uriTemplate) { |
||||
return delegate().uriString(uriTemplate); |
||||
} |
||||
|
||||
@Override |
||||
public UriBuilder builder() { |
||||
return delegate().builder(); |
||||
} |
||||
|
||||
private UriBuilderFactory delegate() { |
||||
return this.suppler.get(); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,240 @@
@@ -0,0 +1,240 @@
|
||||
/* |
||||
* 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.test.http.server; |
||||
|
||||
import java.util.Locale; |
||||
import java.util.Objects; |
||||
import java.util.function.Supplier; |
||||
|
||||
import org.jspecify.annotations.Nullable; |
||||
|
||||
import org.springframework.context.ApplicationContext; |
||||
import org.springframework.core.io.support.SpringFactoriesLoader; |
||||
import org.springframework.core.io.support.SpringFactoriesLoader.ArgumentResolver; |
||||
import org.springframework.util.Assert; |
||||
import org.springframework.util.function.SingletonSupplier; |
||||
import org.springframework.web.util.DefaultUriBuilderFactory; |
||||
import org.springframework.web.util.UriBuilder; |
||||
import org.springframework.web.util.UriBuilderFactory; |
||||
|
||||
/** |
||||
* Provides details of a locally running test web server which may have been started on a |
||||
* dynamic port. |
||||
* |
||||
* @author Phillip Webb |
||||
* @author Stephane Nicoll |
||||
* @since 4.0.0 |
||||
*/ |
||||
public final class LocalTestWebServer { |
||||
|
||||
private final Scheme scheme; |
||||
|
||||
private final SingletonSupplier<Connection> connection; |
||||
|
||||
private LocalTestWebServer(Scheme scheme, Supplier<Connection> connectionSupplier) { |
||||
Assert.notNull(scheme, "'scheme' must not be null"); |
||||
Assert.notNull(connectionSupplier, "'connectionSupplier' must not be null"); |
||||
this.scheme = scheme; |
||||
this.connection = SingletonSupplier.of(connectionSupplier); |
||||
} |
||||
|
||||
/** |
||||
* Return if URI scheme used for the connection. 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() { |
||||
return this.scheme; |
||||
} |
||||
|
||||
/** |
||||
* Return the URI of the running local test server. This method should only be called |
||||
* once the local test server is fully running. |
||||
* @return the URI of the server |
||||
*/ |
||||
public String uri() { |
||||
return uri(null); |
||||
} |
||||
|
||||
/** |
||||
* Return the URI of the running local test server taking into account the given |
||||
* {@code uri}. This method should only be called once the local test server is fully |
||||
* running. |
||||
* @param uri a URI template for the builder or {@code null} |
||||
* @return the URI of the server |
||||
*/ |
||||
public String uri(@Nullable String uri) { |
||||
return uriBuilder(uri).toUriString(); |
||||
} |
||||
|
||||
/** |
||||
* Return a new {@link UriBuilder} with the base URI template initialized from the |
||||
* local server {@link #uri()}. This method should only be called once the local test |
||||
* server is fully running. |
||||
* @param uri a URI template for the builder or {@code null} |
||||
* @return a new {@link UriBuilder} instance |
||||
*/ |
||||
public UriBuilder uriBuilder(@Nullable String uri) { |
||||
UriBuilderFactory factory = uriBuilderFactory(); |
||||
return (uri != null) ? factory.uriString(uri) : factory.builder(); |
||||
} |
||||
|
||||
/** |
||||
* Return a new {@link UriBuilderFactory} with the base URI template initialized from |
||||
* the local server {@link #uri()}. Methods of the return UriBuilderFactory should |
||||
* only be called once the local test server is fully running. |
||||
* @return a new {@link UriBuilderFactory} |
||||
*/ |
||||
public UriBuilderFactory uriBuilderFactory() { |
||||
return new LazyUriBuilderFactory(() -> new DefaultUriBuilderFactory(getConnection().uri(scheme()))); |
||||
} |
||||
|
||||
/** |
||||
* Return a new {@link LocalTestWebServer} instance that applies the given |
||||
* {@code path}. |
||||
* @param path a path to append |
||||
* @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; |
||||
} |
||||
|
||||
/** |
||||
* Factory method to create a new {@link LocalTestWebServer} instance. |
||||
* @param scheme the URL scheme |
||||
* @param port the port of the running server |
||||
* @return a new {@link LocalTestWebServer} instance |
||||
*/ |
||||
public static LocalTestWebServer of(Scheme scheme, int port) { |
||||
return of(scheme, port, null); |
||||
} |
||||
|
||||
/** |
||||
* Factory method to create a new {@link LocalTestWebServer} instance. |
||||
* @param scheme the URL scheme |
||||
* @param port the port of the running server |
||||
* @param contextPath the context path of the running server |
||||
* @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 : "")); |
||||
} |
||||
|
||||
/** |
||||
* Factory method to create a new {@link LocalTestWebServer} instance. |
||||
* @param scheme the URL scheme |
||||
* @param connectionSupplier a supplier to provide the server connection |
||||
* @return a new {@link LocalTestWebServer} instance |
||||
*/ |
||||
public static LocalTestWebServer of(Scheme scheme, Supplier<Connection> connectionSupplier) { |
||||
return new LocalTestWebServer(scheme, connectionSupplier); |
||||
} |
||||
|
||||
/** |
||||
* Return a {@link LocalTestWebServer} instance provided from the |
||||
* {@link ApplicationContext}. |
||||
* @param applicationContext the application context |
||||
* @return the local test web server or {@code null} |
||||
*/ |
||||
public static LocalTestWebServer getRequired(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 |
||||
* {@link ApplicationContext} or {@code null} of no local server is started or could |
||||
* be provided. |
||||
* @param applicationContext the application context |
||||
* @return the local test web server or {@code null} |
||||
*/ |
||||
public static @Nullable LocalTestWebServer get(ApplicationContext applicationContext) { |
||||
Assert.notNull(applicationContext, "'applicationContext' must not be null"); |
||||
SpringFactoriesLoader loader = SpringFactoriesLoader |
||||
.forDefaultResourceLocation(applicationContext.getClassLoader()); |
||||
return loader.load(Provider.class, ArgumentResolver.of(ApplicationContext.class, applicationContext)) |
||||
.stream() |
||||
.map(Provider::getLocalTestWebServer) |
||||
.filter(Objects::nonNull) |
||||
.findFirst() |
||||
.orElse(null); |
||||
} |
||||
|
||||
/** |
||||
* Details of a connection to the local test web server. |
||||
* |
||||
* @param port the port of the running server |
||||
* @param path the path of the running server |
||||
*/ |
||||
public record Connection(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(); |
||||
} |
||||
|
||||
Connection withPath(String path) { |
||||
return new Connection(port(), path() + path); |
||||
} |
||||
|
||||
} |
||||
|
||||
/** |
||||
* Supported HTTP schemes. |
||||
*/ |
||||
public enum Scheme { |
||||
|
||||
/** |
||||
* HTTP scheme. |
||||
*/ |
||||
HTTP, |
||||
|
||||
/** |
||||
* HTTPS scheme. |
||||
*/ |
||||
HTTPS |
||||
|
||||
} |
||||
|
||||
/** |
||||
* 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 { |
||||
|
||||
/** |
||||
* Return the provided {@link LocalTestWebServer} or {@code null}. |
||||
* @return the local test web server |
||||
*/ |
||||
@Nullable LocalTestWebServer getLocalTestWebServer(); |
||||
|
||||
} |
||||
|
||||
} |
||||
@ -1,65 +0,0 @@
@@ -1,65 +0,0 @@
|
||||
/* |
||||
* 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.test.web.htmlunit; |
||||
|
||||
import org.htmlunit.BrowserVersion; |
||||
import org.jspecify.annotations.Nullable; |
||||
import org.openqa.selenium.Capabilities; |
||||
|
||||
import org.springframework.boot.test.http.server.BaseUrl; |
||||
import org.springframework.boot.test.http.server.BaseUrlProvider; |
||||
import org.springframework.test.web.servlet.htmlunit.webdriver.WebConnectionHtmlUnitDriver; |
||||
|
||||
/** |
||||
* HTML Unit {@link WebConnectionHtmlUnitDriver} that will automatically prefix relative |
||||
* URLs with a {@link BaseUrlProvider provided} {@link BaseUrl}. |
||||
* |
||||
* @author Phillip Webb |
||||
* @since 4.0.0 |
||||
*/ |
||||
public class BaseUrlWebConnectionHtmlUnitDriver extends WebConnectionHtmlUnitDriver { |
||||
|
||||
private @Nullable BaseUrl baseUrl; |
||||
|
||||
public BaseUrlWebConnectionHtmlUnitDriver(@Nullable BaseUrl baseUrl) { |
||||
this.baseUrl = baseUrl; |
||||
} |
||||
|
||||
public BaseUrlWebConnectionHtmlUnitDriver(@Nullable BaseUrl baseUrl, boolean enableJavascript) { |
||||
super(enableJavascript); |
||||
this.baseUrl = baseUrl; |
||||
} |
||||
|
||||
public BaseUrlWebConnectionHtmlUnitDriver(@Nullable BaseUrl baseUrl, BrowserVersion browserVersion) { |
||||
super(browserVersion); |
||||
this.baseUrl = baseUrl; |
||||
} |
||||
|
||||
public BaseUrlWebConnectionHtmlUnitDriver(@Nullable BaseUrl baseUrl, Capabilities capabilities) { |
||||
super(capabilities); |
||||
this.baseUrl = baseUrl; |
||||
} |
||||
|
||||
@Override |
||||
public void get(String url) { |
||||
if (this.baseUrl != null) { |
||||
url = this.baseUrl.getUriBuilderFactory().uriString(url).toUriString(); |
||||
} |
||||
super.get(url); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,67 @@
@@ -0,0 +1,67 @@
|
||||
/* |
||||
* 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.test.web.htmlunit; |
||||
|
||||
import org.htmlunit.BrowserVersion; |
||||
import org.openqa.selenium.Capabilities; |
||||
|
||||
import org.springframework.test.web.servlet.htmlunit.webdriver.WebConnectionHtmlUnitDriver; |
||||
import org.springframework.util.Assert; |
||||
import org.springframework.web.util.UriBuilderFactory; |
||||
|
||||
/** |
||||
* HTML Unit {@link WebConnectionHtmlUnitDriver} supported by a {@link UriBuilderFactory}. |
||||
* |
||||
* @author Phillip Webb |
||||
* @author Stephane Nicoll |
||||
* @since 4.0.0 |
||||
*/ |
||||
public class UriBuilderFactoryWebConnectionHtmlUnitDriver extends WebConnectionHtmlUnitDriver { |
||||
|
||||
private UriBuilderFactory uriBuilderFactory; |
||||
|
||||
public UriBuilderFactoryWebConnectionHtmlUnitDriver(UriBuilderFactory uriBuilderFactory) { |
||||
Assert.notNull(uriBuilderFactory, "'uriBuilderFactory' must not be null"); |
||||
this.uriBuilderFactory = uriBuilderFactory; |
||||
} |
||||
|
||||
public UriBuilderFactoryWebConnectionHtmlUnitDriver(UriBuilderFactory uriBuilderFactory, boolean enableJavascript) { |
||||
super(enableJavascript); |
||||
Assert.notNull(uriBuilderFactory, "'uriBuilderFactory' must not be null"); |
||||
this.uriBuilderFactory = uriBuilderFactory; |
||||
} |
||||
|
||||
public UriBuilderFactoryWebConnectionHtmlUnitDriver(UriBuilderFactory uriBuilderFactory, |
||||
BrowserVersion browserVersion) { |
||||
super(browserVersion); |
||||
Assert.notNull(uriBuilderFactory, "'uriBuilderFactory' must not be null"); |
||||
this.uriBuilderFactory = uriBuilderFactory; |
||||
} |
||||
|
||||
public UriBuilderFactoryWebConnectionHtmlUnitDriver(UriBuilderFactory uriBuilderFactory, |
||||
Capabilities capabilities) { |
||||
super(capabilities); |
||||
Assert.notNull(uriBuilderFactory, "'uriBuilderFactory' must not be null"); |
||||
this.uriBuilderFactory = uriBuilderFactory; |
||||
} |
||||
|
||||
@Override |
||||
public void get(String url) { |
||||
super.get((this.uriBuilderFactory != null) ? this.uriBuilderFactory.uriString(url).toUriString() : url); |
||||
} |
||||
|
||||
} |
||||
@ -1,68 +0,0 @@
@@ -1,68 +0,0 @@
|
||||
/* |
||||
* 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.test.http.client; |
||||
|
||||
import java.net.URI; |
||||
import java.util.Map; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import org.springframework.boot.test.http.server.BaseUrl; |
||||
import org.springframework.web.util.UriBuilderFactory; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
/** |
||||
* Tests for {@link BaseUrlUriBuilderFactory}. |
||||
* |
||||
* @author Stephane Nicoll |
||||
*/ |
||||
class BaseUrlUriBuilderFactoryTests { |
||||
|
||||
@Test |
||||
void uriWithRootSlashAddsBaseUrl() { |
||||
UriBuilderFactory factory = BaseUrlUriBuilderFactory.get(BaseUrl.of("https://example.com")); |
||||
assertThat(factory.uriString("/").build()).isEqualTo(URI.create("https://example.com/")); |
||||
} |
||||
|
||||
@Test |
||||
void uriWithEmptyAddsBaseUrl() { |
||||
UriBuilderFactory factory = BaseUrlUriBuilderFactory.get(BaseUrl.of("https://example.com")); |
||||
assertThat(factory.uriString("").build()).isEqualTo(URI.create("https://example.com")); |
||||
} |
||||
|
||||
@Test |
||||
void uriWithMapVariablesAddsBaseUrl() { |
||||
UriBuilderFactory factory = BaseUrlUriBuilderFactory.get(BaseUrl.of("https://example.com")); |
||||
assertThat(factory.expand("/test/{name}", Map.of("name", "value"))) |
||||
.isEqualTo(URI.create("https://example.com/test/value")); |
||||
} |
||||
|
||||
@Test |
||||
void uriWithVariablesAddsBaseUrl() { |
||||
UriBuilderFactory factory = BaseUrlUriBuilderFactory.get(BaseUrl.of("https://example.com")); |
||||
assertThat(factory.expand("/test/{name}", "value")).isEqualTo(URI.create("https://example.com/test/value")); |
||||
} |
||||
|
||||
@Test |
||||
void uriWithHostDoesNotExpandBaseUrl() { |
||||
UriBuilderFactory factory = BaseUrlUriBuilderFactory.get(BaseUrl.of("https://example.com")); |
||||
assertThat(factory.uriString("https://sub.example.com").build()) |
||||
.isEqualTo(URI.create("https://sub.example.com")); |
||||
} |
||||
|
||||
} |
||||
@ -1,57 +0,0 @@
@@ -1,57 +0,0 @@
|
||||
/* |
||||
* 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.test.http.server; |
||||
|
||||
import java.util.Collections; |
||||
import java.util.List; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.mockito.BDDMockito.given; |
||||
import static org.mockito.BDDMockito.then; |
||||
import static org.mockito.Mockito.mock; |
||||
|
||||
/** |
||||
* Tests for {@link BaseUrlProviders}. |
||||
* |
||||
* @author Phillip Webb |
||||
*/ |
||||
class BaseUrlProvidersTests { |
||||
|
||||
@Test |
||||
void getBaseUrlWhenNoProvidedBaseUrlReturnsNull() { |
||||
assertThat(new BaseUrlProviders(Collections.emptyList()).getBaseUrl()).isNull(); |
||||
} |
||||
|
||||
@Test |
||||
void getBaseUrlWithFallbackWhenNoProvidedBaseUrlReturnsFallback() { |
||||
BaseUrl fallback = BaseUrl.of("https://example.com"); |
||||
assertThat(new BaseUrlProviders(Collections.emptyList()).getBaseUrl(fallback)).isSameAs(fallback); |
||||
} |
||||
|
||||
@Test |
||||
void getBaseUrlReturnsFirstProvidedBaseUrl() { |
||||
BaseUrlProvider p1 = mock(); |
||||
BaseUrlProvider p2 = mock(); |
||||
BaseUrl baseUrl = BaseUrl.of("https://example.com"); |
||||
given(p1.getBaseUrl()).willReturn(baseUrl); |
||||
assertThat(new BaseUrlProviders(List.of(p1, p2)).getBaseUrl()).isSameAs(baseUrl); |
||||
then(p2).shouldHaveNoInteractions(); |
||||
} |
||||
|
||||
} |
||||
@ -1,104 +0,0 @@
@@ -1,104 +0,0 @@
|
||||
/* |
||||
* 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.test.http.server; |
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; |
||||
|
||||
/** |
||||
* Tests for {@link BaseUrl}. |
||||
* |
||||
* @author Phillip Webb |
||||
*/ |
||||
class BaseUrlTests { |
||||
|
||||
@Test |
||||
void resolveWithString() { |
||||
assertThat(resolve(BaseUrl.of("http://localhost"), "")).isEqualTo("http://localhost"); |
||||
assertThat(resolve(BaseUrl.of("http://localhost"), "/path")).isEqualTo("http://localhost/path"); |
||||
assertThat(resolve(BaseUrl.of("http://localhost/"), "/path")).isEqualTo("http://localhost/path"); |
||||
} |
||||
|
||||
@Test |
||||
void ofWhenHttp() { |
||||
BaseUrl baseUrl = BaseUrl.of("http://localhost:8080/context"); |
||||
assertThat(baseUrl.isHttps()).isFalse(); |
||||
assertThat(resolve(baseUrl, "")).isEqualTo("http://localhost:8080/context"); |
||||
} |
||||
|
||||
@Test |
||||
void ofWhenHttps() { |
||||
BaseUrl baseUrl = BaseUrl.of("https://localhost:8080/context"); |
||||
assertThat(baseUrl.isHttps()).isTrue(); |
||||
assertThat(resolve(baseUrl, "")).isEqualTo("https://localhost:8080/context"); |
||||
} |
||||
|
||||
@Test |
||||
void ofWhenUppercaseHttps() { |
||||
BaseUrl baseUrl = BaseUrl.of("HTTPS://localhost:8080/context"); |
||||
assertThat(baseUrl.isHttps()).isTrue(); |
||||
} |
||||
|
||||
@Test |
||||
@SuppressWarnings("NullAway") // Test null check
|
||||
void ofWhenUrlIssNull() { |
||||
assertThatIllegalArgumentException().isThrownBy(() -> BaseUrl.of(null)).withMessage("'url' must not be null"); |
||||
} |
||||
|
||||
@Test |
||||
void of() { |
||||
AtomicInteger atomicInteger = new AtomicInteger(); |
||||
BaseUrl baseUrl = BaseUrl.of(true, () -> String.valueOf(atomicInteger.incrementAndGet())); |
||||
assertThat(atomicInteger.get()).isZero(); |
||||
assertThat(baseUrl.isHttps()).isTrue(); |
||||
assertThat(resolve(baseUrl, "")).isEqualTo("1"); |
||||
assertThat(resolve(baseUrl, "")).isEqualTo("1"); |
||||
} |
||||
|
||||
@Test |
||||
@SuppressWarnings("NullAway") // Test null check
|
||||
void ofWhenResolverIsNull() { |
||||
assertThatIllegalArgumentException().isThrownBy(() -> BaseUrl.of(true, null)) |
||||
.withMessage("'resolver' must not be null"); |
||||
} |
||||
|
||||
@Test |
||||
void withPath() { |
||||
BaseUrl baseUrl = BaseUrl.of("http://localhost"); |
||||
assertThat(resolve(baseUrl.withPath("/context"), "")).isEqualTo("http://localhost/context"); |
||||
assertThat(resolve(baseUrl.withPath("/context").withPath("/test"), "/path")) |
||||
.isEqualTo("http://localhost/context/test/path"); |
||||
} |
||||
|
||||
@Test |
||||
void withPathInvokesParentResolver() { |
||||
AtomicInteger atomicInteger = new AtomicInteger(); |
||||
BaseUrl baseUrl = BaseUrl.of(true, () -> "https://example.com/" + atomicInteger.incrementAndGet()); |
||||
assertThat(resolve(baseUrl.withPath("/context"), "")).isEqualTo("https://example.com/1/context"); |
||||
assertThat(resolve(baseUrl.withPath("/context").withPath("/test"), "/path")) |
||||
.isEqualTo("https://example.com/1/context/test/path"); |
||||
} |
||||
|
||||
private String resolve(BaseUrl baseUrl, String path) { |
||||
return baseUrl.getUriBuilderFactory().uriString(path).toUriString(); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,216 @@
@@ -0,0 +1,216 @@
|
||||
/* |
||||
* 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.test.http.server; |
||||
|
||||
import java.net.URI; |
||||
import java.util.Map; |
||||
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.Scheme; |
||||
import org.springframework.boot.testsupport.classpath.resources.WithResource; |
||||
import org.springframework.context.ApplicationContext; |
||||
import org.springframework.context.support.GenericApplicationContext; |
||||
import org.springframework.web.util.UriBuilder; |
||||
import org.springframework.web.util.UriBuilderFactory; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; |
||||
import static org.assertj.core.api.Assertions.assertThatIllegalStateException; |
||||
|
||||
/** |
||||
* Tests for {@link LocalTestWebServer}. |
||||
* |
||||
* @author Phillip Webb |
||||
* @author Stephane Nicoll |
||||
*/ |
||||
class LocalTestWebServerTests { |
||||
|
||||
private final LocalTestWebServer server = LocalTestWebServer.of(Scheme.HTTPS, 8080, ""); |
||||
|
||||
@Test |
||||
void schemeWhenHttpsSchemeReturnsHttpsScheme() { |
||||
assertThat(LocalTestWebServer.of(Scheme.HTTPS, 8080, "").scheme()).isEqualTo(Scheme.HTTPS); |
||||
} |
||||
|
||||
@Test |
||||
void schemeWhenHttpSchemeReturnsHttpScheme() { |
||||
assertThat(LocalTestWebServer.of(Scheme.HTTP, 8080, "").scheme()).isEqualTo(Scheme.HTTP); |
||||
} |
||||
|
||||
@Test |
||||
void uriBuilderWhenHasSlashUriUsesLocalServer() { |
||||
UriBuilder builder = this.server.uriBuilder("/"); |
||||
assertThat(builder.toUriString()).isEqualTo("https://localhost:8080/"); |
||||
} |
||||
|
||||
@Test |
||||
void uriBuilderWhenHasEmptyUriUsesLocalServer() { |
||||
UriBuilder builder = this.server.uriBuilder(""); |
||||
assertThat(builder.toUriString()).isEqualTo("https://localhost:8080"); |
||||
} |
||||
|
||||
@Test |
||||
void uriBuilderWhenHasNestedPathUsesLocalServer() { |
||||
UriBuilder builder = this.server.uriBuilder("/foo/bar"); |
||||
assertThat(builder.toUriString()).isEqualTo("https://localhost:8080/foo/bar"); |
||||
} |
||||
|
||||
@Test |
||||
void uriBuilderWhenHasPathNoStartingWithSlashUsesLocalServer() { |
||||
UriBuilder builder = this.server.uriBuilder("foo/bar"); |
||||
assertThat(builder.toUriString()).isEqualTo("https://localhost:8080/foo/bar"); |
||||
} |
||||
|
||||
@Test |
||||
void uriBuilderWhenHasFullUriDoesNotUseLocalServer() { |
||||
UriBuilder builder = this.server.uriBuilder("https://sub.example.com"); |
||||
assertThat(builder.toUriString()).isEqualTo("https://sub.example.com"); |
||||
} |
||||
|
||||
@Test |
||||
void uriBuilderFactoryExpandWithMap() { |
||||
UriBuilderFactory factory = this.server.uriBuilderFactory(); |
||||
assertThat(factory.expand("/test/{name}", Map.of("name", "value"))) |
||||
.isEqualTo(URI.create("https://localhost:8080/test/value")); |
||||
} |
||||
|
||||
@Test |
||||
void uriBuilderFactoryExpandsWithMap() { |
||||
UriBuilderFactory factory = this.server.uriBuilderFactory(); |
||||
assertThat(factory.expand("/test/{name}", "value")).isEqualTo(URI.create("https://localhost:8080/test/value")); |
||||
} |
||||
|
||||
@Test |
||||
void uriBuilderFactoryExpandsWithVariables() { |
||||
UriBuilderFactory factory = this.server.uriBuilderFactory(); |
||||
assertThat(factory.uriString("https://example.com").build()).isEqualTo(URI.create("https://example.com")); |
||||
} |
||||
|
||||
@Test |
||||
void uriWhenHttp() { |
||||
assertThat(LocalTestWebServer.of(Scheme.HTTP, 8080, "").uri()).isEqualTo("http://localhost:8080"); |
||||
} |
||||
|
||||
@Test |
||||
void uriWhenHttps() { |
||||
assertThat(LocalTestWebServer.of(Scheme.HTTPS, 4343, "").uri()).isEqualTo("https://localhost:4343"); |
||||
} |
||||
|
||||
@Test |
||||
void uriWhenHasPath() { |
||||
assertThat(LocalTestWebServer.of(Scheme.HTTPS, 8080, "/path").uri()).isEqualTo("https://localhost:8080/path"); |
||||
} |
||||
|
||||
@Test |
||||
void uriWithUri() { |
||||
assertThat(this.server.uri(null)).isEqualTo("https://localhost:8080"); |
||||
assertThat(this.server.uri("")).isEqualTo("https://localhost:8080"); |
||||
assertThat(this.server.uri("/")).isEqualTo("https://localhost:8080/"); |
||||
assertThat(this.server.uri("/foo")).isEqualTo("https://localhost:8080/foo"); |
||||
assertThat(this.server.uri("https://example.com/foo")).isEqualTo("https://example.com/foo"); |
||||
} |
||||
|
||||
@Test |
||||
void uriUsesSingletonConnection() { |
||||
AtomicInteger counter = new AtomicInteger(); |
||||
LocalTestWebServer server = LocalTestWebServer.of(Scheme.HTTPS, |
||||
() -> new Connection(8080, "/" + counter.incrementAndGet())); |
||||
assertThat(server.uri()).isEqualTo("https://localhost:8080/1"); |
||||
assertThat(server.uri()).isEqualTo("https://localhost:8080/1"); |
||||
} |
||||
|
||||
@Test |
||||
void withPathCreatedNewInstance() { |
||||
assertThat(LocalTestWebServer.of(Scheme.HTTPS, 8080, "/path").withPath("/other").uri()) |
||||
.isEqualTo("https://localhost:8080/path/other"); |
||||
} |
||||
|
||||
@Test |
||||
@SuppressWarnings("NullAway") // Test null check
|
||||
void ofWhenConnectionSupplierIsNull() { |
||||
assertThatIllegalArgumentException().isThrownBy(() -> LocalTestWebServer.of(Scheme.HTTPS, null)) |
||||
.withMessage("'connectionSupplier' must not be null"); |
||||
} |
||||
|
||||
@Test |
||||
@WithResource(name = "META-INF/spring.factories", content = """ |
||||
org.springframework.boot.test.http.server.LocalTestWebServer$Provider=\ |
||||
org.springframework.boot.test.http.server.LocalTestWebServerTests$Provider1,\ |
||||
org.springframework.boot.test.http.server.LocalTestWebServerTests$Provider2,\ |
||||
org.springframework.boot.test.http.server.LocalTestWebServerTests$Provider3 |
||||
""") |
||||
void getReturnsFirstProvided() { |
||||
ApplicationContext applicationContext = new GenericApplicationContext(); |
||||
LocalTestWebServer provided = LocalTestWebServer.get(applicationContext); |
||||
assertThat(provided).isNotNull(); |
||||
assertThat(provided.uri()).isEqualTo("https://localhost:7070/p2"); |
||||
} |
||||
|
||||
@Test |
||||
@WithResource(name = "META-INF/spring.factories", content = """ |
||||
org.springframework.boot.test.http.server.LocalTestWebServer$Provider=\ |
||||
org.springframework.boot.test.http.server.LocalTestWebServerTests$Provider1 |
||||
""") |
||||
void getWhenNoneReturnsNull() { |
||||
ApplicationContext applicationContext = new GenericApplicationContext(); |
||||
LocalTestWebServer provided = LocalTestWebServer.get(applicationContext); |
||||
assertThat(provided).isNull(); |
||||
} |
||||
|
||||
@Test |
||||
@WithResource(name = "META-INF/spring.factories", content = """ |
||||
org.springframework.boot.test.http.server.LocalTestWebServer$Provider=\ |
||||
org.springframework.boot.test.http.server.LocalTestWebServerTests$Provider1 |
||||
""") |
||||
void getRequiredWhenNoneProvidedThrowsException() { |
||||
ApplicationContext applicationContext = new GenericApplicationContext(); |
||||
assertThatIllegalStateException().isThrownBy(() -> LocalTestWebServer.getRequired(applicationContext)) |
||||
.withMessage("No local test web server available"); |
||||
} |
||||
|
||||
static class Provider1 implements LocalTestWebServer.Provider { |
||||
|
||||
@Override |
||||
public @Nullable LocalTestWebServer getLocalTestWebServer() { |
||||
return null; |
||||
} |
||||
|
||||
} |
||||
|
||||
static class Provider2 implements LocalTestWebServer.Provider { |
||||
|
||||
@Override |
||||
public @Nullable LocalTestWebServer getLocalTestWebServer() { |
||||
return LocalTestWebServer.of(Scheme.HTTPS, 7070, "/p2"); |
||||
} |
||||
|
||||
} |
||||
|
||||
static class Provider3 implements LocalTestWebServer.Provider { |
||||
|
||||
@Override |
||||
public @Nullable LocalTestWebServer getLocalTestWebServer() { |
||||
throw new IllegalStateException(); |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
||||
21
module/spring-boot-web-server/src/main/java/org/springframework/boot/web/server/reactive/context/ReactiveWebServerApplicationContextBaseUrlProvider.java → module/spring-boot-web-server/src/main/java/org/springframework/boot/web/server/reactive/context/ReactiveWebServerApplicationContextLocalTestWebServerProvider.java
21
module/spring-boot-web-server/src/main/java/org/springframework/boot/web/server/reactive/context/ReactiveWebServerApplicationContextBaseUrlProvider.java → module/spring-boot-web-server/src/main/java/org/springframework/boot/web/server/reactive/context/ReactiveWebServerApplicationContextLocalTestWebServerProvider.java
21
module/spring-boot-web-server/src/main/java/org/springframework/boot/web/server/servlet/context/ServletWebServerApplicationContextBaseUrlProvider.java → module/spring-boot-web-server/src/main/java/org/springframework/boot/web/server/servlet/context/ServletWebServerApplicationContextLocalTestWebServerProvider.java
21
module/spring-boot-web-server/src/main/java/org/springframework/boot/web/server/servlet/context/ServletWebServerApplicationContextBaseUrlProvider.java → module/spring-boot-web-server/src/main/java/org/springframework/boot/web/server/servlet/context/ServletWebServerApplicationContextLocalTestWebServerProvider.java
Loading…
Reference in new issue