Rework HTTP client modules
This commit is contained in:
committed by
Phillip Webb
parent
4763ef2463
commit
7a9be5bd4a
@@ -0,0 +1,144 @@
|
||||
/*
|
||||
* Copyright 2012-2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* 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.restclient.test;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.boot.restclient.RestClientCustomizer;
|
||||
import org.springframework.http.client.BufferingClientHttpRequestFactory;
|
||||
import org.springframework.test.web.client.MockRestServiceServer;
|
||||
import org.springframework.test.web.client.MockRestServiceServer.MockRestServiceServerBuilder;
|
||||
import org.springframework.test.web.client.RequestExpectationManager;
|
||||
import org.springframework.test.web.client.SimpleRequestExpectationManager;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.web.client.RestClient;
|
||||
import org.springframework.web.client.RestClient.Builder;
|
||||
|
||||
/**
|
||||
* {@link RestClientCustomizer} that can be applied to {@link Builder RestClient.Builder}
|
||||
* instances to add {@link MockRestServiceServer} support.
|
||||
* <p>
|
||||
* Typically applied to an existing builder before it is used, for example:
|
||||
* <pre class="code">
|
||||
* MockServerRestClientCustomizer customizer = new MockServerRestClientCustomizer();
|
||||
* RestClient.Builder builder = RestClient.builder();
|
||||
* customizer.customize(builder);
|
||||
* MyBean bean = new MyBean(client.build());
|
||||
* customizer.getServer().expect(requestTo("/hello")).andRespond(withSuccess());
|
||||
* bean.makeRestCall();
|
||||
* </pre>
|
||||
* <p>
|
||||
* If the customizer is only used once, the {@link #getServer()} method can be used to
|
||||
* obtain the mock server. If the customizer has been used more than once the
|
||||
* {@link #getServer(RestClient.Builder)} or {@link #getServers()} method must be used to
|
||||
* access the related server.
|
||||
* <p>
|
||||
* If a mock server is used in more than one test case in a test class, it might be
|
||||
* necessary to reset the expectations on the server between tests using
|
||||
* {@code getServer().reset()} or {@code getServer(restClientBuilder).reset()}.
|
||||
*
|
||||
* @author Scott Frederick
|
||||
* @since 3.2.0
|
||||
* @see #getServer()
|
||||
* @see #getServer(RestClient.Builder)
|
||||
*/
|
||||
public class MockServerRestClientCustomizer implements RestClientCustomizer {
|
||||
|
||||
private final Map<RestClient.Builder, RequestExpectationManager> expectationManagers = new ConcurrentHashMap<>();
|
||||
|
||||
private final Map<RestClient.Builder, MockRestServiceServer> servers = new ConcurrentHashMap<>();
|
||||
|
||||
private final Supplier<? extends RequestExpectationManager> expectationManagerSupplier;
|
||||
|
||||
private boolean bufferContent;
|
||||
|
||||
public MockServerRestClientCustomizer() {
|
||||
this(SimpleRequestExpectationManager::new);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link MockServerRestClientCustomizer} instance.
|
||||
* @param expectationManager the expectation manager class to use
|
||||
*/
|
||||
public MockServerRestClientCustomizer(Class<? extends RequestExpectationManager> expectationManager) {
|
||||
this(() -> BeanUtils.instantiateClass(expectationManager));
|
||||
Assert.notNull(expectationManager, "'expectationManager' must not be null");
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link MockServerRestClientCustomizer} instance.
|
||||
* @param expectationManagerSupplier a supplier that provides the
|
||||
* {@link RequestExpectationManager} to use
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public MockServerRestClientCustomizer(Supplier<? extends RequestExpectationManager> expectationManagerSupplier) {
|
||||
Assert.notNull(expectationManagerSupplier, "'expectationManagerSupplier' must not be null");
|
||||
this.expectationManagerSupplier = expectationManagerSupplier;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set if the {@link BufferingClientHttpRequestFactory} wrapper should be used to
|
||||
* buffer the input and output streams, and for example, allow multiple reads of the
|
||||
* response body.
|
||||
* @param bufferContent if request and response content should be buffered
|
||||
* @since 3.1.0
|
||||
*/
|
||||
public void setBufferContent(boolean bufferContent) {
|
||||
this.bufferContent = bufferContent;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void customize(RestClient.Builder restClientBuilder) {
|
||||
RequestExpectationManager expectationManager = createExpectationManager();
|
||||
MockRestServiceServerBuilder serverBuilder = MockRestServiceServer.bindTo(restClientBuilder);
|
||||
if (this.bufferContent) {
|
||||
serverBuilder.bufferContent();
|
||||
}
|
||||
MockRestServiceServer server = serverBuilder.build(expectationManager);
|
||||
this.expectationManagers.put(restClientBuilder, expectationManager);
|
||||
this.servers.put(restClientBuilder, server);
|
||||
}
|
||||
|
||||
protected RequestExpectationManager createExpectationManager() {
|
||||
return this.expectationManagerSupplier.get();
|
||||
}
|
||||
|
||||
public MockRestServiceServer getServer() {
|
||||
Assert.state(!this.servers.isEmpty(), "Unable to return a single MockRestServiceServer since "
|
||||
+ "MockServerRestClientCustomizer has not been bound to a RestClient");
|
||||
Assert.state(this.servers.size() == 1, "Unable to return a single MockRestServiceServer since "
|
||||
+ "MockServerRestClientCustomizer has been bound to more than one RestClient");
|
||||
return this.servers.values().iterator().next();
|
||||
}
|
||||
|
||||
public Map<RestClient.Builder, RequestExpectationManager> getExpectationManagers() {
|
||||
return this.expectationManagers;
|
||||
}
|
||||
|
||||
public MockRestServiceServer getServer(RestClient.Builder restClientBuilder) {
|
||||
return this.servers.get(restClientBuilder);
|
||||
}
|
||||
|
||||
public Map<RestClient.Builder, MockRestServiceServer> getServers() {
|
||||
return Collections.unmodifiableMap(this.servers);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
/*
|
||||
* Copyright 2012-2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* 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.restclient.test;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.boot.restclient.RestTemplateBuilder;
|
||||
import org.springframework.boot.restclient.RestTemplateCustomizer;
|
||||
import org.springframework.http.client.BufferingClientHttpRequestFactory;
|
||||
import org.springframework.test.web.client.MockRestServiceServer;
|
||||
import org.springframework.test.web.client.MockRestServiceServer.MockRestServiceServerBuilder;
|
||||
import org.springframework.test.web.client.RequestExpectationManager;
|
||||
import org.springframework.test.web.client.SimpleRequestExpectationManager;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
/**
|
||||
* {@link RestTemplateCustomizer} that can be applied to a {@link RestTemplateBuilder}
|
||||
* instances to add {@link MockRestServiceServer} support.
|
||||
* <p>
|
||||
* Typically applied to an existing builder before it is used, for example:
|
||||
* <pre class="code">
|
||||
* MockServerRestTemplateCustomizer customizer = new MockServerRestTemplateCustomizer();
|
||||
* MyBean bean = new MyBean(new RestTemplateBuilder(customizer));
|
||||
* customizer.getServer().expect(requestTo("/hello")).andRespond(withSuccess());
|
||||
* bean.makeRestCall();
|
||||
* </pre>
|
||||
* <p>
|
||||
* If the customizer is only used once, the {@link #getServer()} method can be used to
|
||||
* obtain the mock server. If the customizer has been used more than once the
|
||||
* {@link #getServer(RestTemplate)} or {@link #getServers()} method must be used to access
|
||||
* the related server.
|
||||
* <p>
|
||||
* If a mock server is used in more than one test case in a test class, it might be
|
||||
* necessary to reset the expectations on the server between tests using
|
||||
* {@code getServer().reset()} or {@code getServer(restTemplate).reset()}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Moritz Halbritter
|
||||
* @author Chinmoy Chakraborty
|
||||
* @since 1.4.0
|
||||
* @see #getServer()
|
||||
* @see #getServer(RestTemplate)
|
||||
*/
|
||||
public class MockServerRestTemplateCustomizer implements RestTemplateCustomizer {
|
||||
|
||||
private final Map<RestTemplate, RequestExpectationManager> expectationManagers = new ConcurrentHashMap<>();
|
||||
|
||||
private final Map<RestTemplate, MockRestServiceServer> servers = new ConcurrentHashMap<>();
|
||||
|
||||
private final Supplier<? extends RequestExpectationManager> expectationManagerSupplier;
|
||||
|
||||
private boolean detectRootUri = true;
|
||||
|
||||
private boolean bufferContent;
|
||||
|
||||
public MockServerRestTemplateCustomizer() {
|
||||
this(SimpleRequestExpectationManager::new);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link MockServerRestTemplateCustomizer} instance.
|
||||
* @param expectationManager the expectation manager class to use
|
||||
*/
|
||||
public MockServerRestTemplateCustomizer(Class<? extends RequestExpectationManager> expectationManager) {
|
||||
this(() -> BeanUtils.instantiateClass(expectationManager));
|
||||
Assert.notNull(expectationManager, "'expectationManager' must not be null");
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link MockServerRestTemplateCustomizer} instance.
|
||||
* @param expectationManagerSupplier a supplier that provides the
|
||||
* {@link RequestExpectationManager} to use
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public MockServerRestTemplateCustomizer(Supplier<? extends RequestExpectationManager> expectationManagerSupplier) {
|
||||
Assert.notNull(expectationManagerSupplier, "'expectationManagerSupplier' must not be null");
|
||||
this.expectationManagerSupplier = expectationManagerSupplier;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set if root URIs from {@link RootUriRequestExpectationManager} should be detected
|
||||
* and applied to the {@link MockRestServiceServer}.
|
||||
* @param detectRootUri if root URIs should be detected
|
||||
*/
|
||||
public void setDetectRootUri(boolean detectRootUri) {
|
||||
this.detectRootUri = detectRootUri;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set if the {@link BufferingClientHttpRequestFactory} wrapper should be used to
|
||||
* buffer the input and output streams, and for example, allow multiple reads of the
|
||||
* response body.
|
||||
* @param bufferContent if request and response content should be buffered
|
||||
* @since 3.1.0
|
||||
*/
|
||||
public void setBufferContent(boolean bufferContent) {
|
||||
this.bufferContent = bufferContent;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void customize(RestTemplate restTemplate) {
|
||||
RequestExpectationManager expectationManager = createExpectationManager();
|
||||
if (this.detectRootUri) {
|
||||
expectationManager = RootUriRequestExpectationManager.forRestTemplate(restTemplate, expectationManager);
|
||||
}
|
||||
MockRestServiceServerBuilder serverBuilder = MockRestServiceServer.bindTo(restTemplate);
|
||||
if (this.bufferContent) {
|
||||
serverBuilder.bufferContent();
|
||||
}
|
||||
MockRestServiceServer server = serverBuilder.build(expectationManager);
|
||||
this.expectationManagers.put(restTemplate, expectationManager);
|
||||
this.servers.put(restTemplate, server);
|
||||
}
|
||||
|
||||
protected RequestExpectationManager createExpectationManager() {
|
||||
return this.expectationManagerSupplier.get();
|
||||
}
|
||||
|
||||
public MockRestServiceServer getServer() {
|
||||
Assert.state(!this.servers.isEmpty(), "Unable to return a single MockRestServiceServer since "
|
||||
+ "MockServerRestTemplateCustomizer has not been bound to a RestTemplate");
|
||||
Assert.state(this.servers.size() == 1, "Unable to return a single MockRestServiceServer since "
|
||||
+ "MockServerRestTemplateCustomizer has been bound to more than one RestTemplate");
|
||||
return this.servers.values().iterator().next();
|
||||
}
|
||||
|
||||
public Map<RestTemplate, RequestExpectationManager> getExpectationManagers() {
|
||||
return this.expectationManagers;
|
||||
}
|
||||
|
||||
public MockRestServiceServer getServer(RestTemplate restTemplate) {
|
||||
return this.servers.get(restTemplate);
|
||||
}
|
||||
|
||||
public Map<RestTemplate, MockRestServiceServer> getServers() {
|
||||
return Collections.unmodifiableMap(this.servers);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,201 @@
|
||||
/*
|
||||
* Copyright 2012-2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* 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.restclient.test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.time.Duration;
|
||||
|
||||
import org.springframework.boot.restclient.RootUriTemplateHandler;
|
||||
import org.springframework.http.client.ClientHttpRequest;
|
||||
import org.springframework.http.client.ClientHttpResponse;
|
||||
import org.springframework.http.client.support.HttpRequestWrapper;
|
||||
import org.springframework.mock.http.client.MockClientHttpRequest;
|
||||
import org.springframework.test.web.client.ExpectedCount;
|
||||
import org.springframework.test.web.client.MockRestServiceServer;
|
||||
import org.springframework.test.web.client.MockRestServiceServer.MockRestServiceServerBuilder;
|
||||
import org.springframework.test.web.client.RequestExpectationManager;
|
||||
import org.springframework.test.web.client.RequestMatcher;
|
||||
import org.springframework.test.web.client.ResponseActions;
|
||||
import org.springframework.test.web.client.SimpleRequestExpectationManager;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.web.util.UriTemplateHandler;
|
||||
|
||||
/**
|
||||
* {@link RequestExpectationManager} that strips the specified root URI from the request
|
||||
* before verification. Can be used to simply test declarations when all REST calls start
|
||||
* the same way. For example: <pre class="code">
|
||||
* RestTemplate restTemplate = new RestTemplateBuilder().rootUri("https://example.com").build();
|
||||
* MockRestServiceServer server = RootUriRequestExpectationManager.bindTo(restTemplate);
|
||||
* server.expect(requestTo("/hello")).andRespond(withSuccess());
|
||||
* restTemplate.getForEntity("/hello", String.class);
|
||||
* </pre>
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @since 1.4.0
|
||||
* @see RootUriTemplateHandler
|
||||
* @see #bindTo(RestTemplate)
|
||||
* @see #forRestTemplate(RestTemplate, RequestExpectationManager)
|
||||
*/
|
||||
public class RootUriRequestExpectationManager implements RequestExpectationManager {
|
||||
|
||||
private final String rootUri;
|
||||
|
||||
private final RequestExpectationManager expectationManager;
|
||||
|
||||
public RootUriRequestExpectationManager(String rootUri, RequestExpectationManager expectationManager) {
|
||||
Assert.notNull(rootUri, "'rootUri' must not be null");
|
||||
Assert.notNull(expectationManager, "'expectationManager' must not be null");
|
||||
this.rootUri = rootUri;
|
||||
this.expectationManager = expectationManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResponseActions expectRequest(ExpectedCount count, RequestMatcher requestMatcher) {
|
||||
return this.expectationManager.expectRequest(count, requestMatcher);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClientHttpResponse validateRequest(ClientHttpRequest request) throws IOException {
|
||||
String uri = request.getURI().toString();
|
||||
if (uri.startsWith(this.rootUri)) {
|
||||
request = replaceURI(request, uri.substring(this.rootUri.length()));
|
||||
}
|
||||
try {
|
||||
return this.expectationManager.validateRequest(request);
|
||||
}
|
||||
catch (AssertionError ex) {
|
||||
String message = ex.getMessage();
|
||||
String prefix = "Request URI expected:</";
|
||||
if (message != null && message.startsWith(prefix)) {
|
||||
throw new AssertionError(
|
||||
"Request URI expected:<" + this.rootUri + message.substring(prefix.length() - 1));
|
||||
}
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
private ClientHttpRequest replaceURI(ClientHttpRequest request, String replacementUri) {
|
||||
URI uri;
|
||||
try {
|
||||
uri = new URI(replacementUri);
|
||||
if (request instanceof MockClientHttpRequest mockClientHttpRequest) {
|
||||
mockClientHttpRequest.setURI(uri);
|
||||
return mockClientHttpRequest;
|
||||
}
|
||||
return new ReplaceUriClientHttpRequest(uri, request);
|
||||
}
|
||||
catch (URISyntaxException ex) {
|
||||
throw new IllegalStateException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void verify() {
|
||||
this.expectationManager.verify();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void verify(Duration timeout) {
|
||||
this.expectationManager.verify(timeout);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reset() {
|
||||
this.expectationManager.reset();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a bound {@link MockRestServiceServer} for the given {@link RestTemplate},
|
||||
* configured with {@link RootUriRequestExpectationManager} when possible.
|
||||
* @param restTemplate the source REST template
|
||||
* @return a configured {@link MockRestServiceServer}
|
||||
*/
|
||||
public static MockRestServiceServer bindTo(RestTemplate restTemplate) {
|
||||
return bindTo(restTemplate, new SimpleRequestExpectationManager());
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a bound {@link MockRestServiceServer} for the given {@link RestTemplate},
|
||||
* configured with {@link RootUriRequestExpectationManager} when possible.
|
||||
* @param restTemplate the source REST template
|
||||
* @param expectationManager the source {@link RequestExpectationManager}
|
||||
* @return a configured {@link MockRestServiceServer}
|
||||
*/
|
||||
public static MockRestServiceServer bindTo(RestTemplate restTemplate,
|
||||
RequestExpectationManager expectationManager) {
|
||||
MockRestServiceServerBuilder builder = MockRestServiceServer.bindTo(restTemplate);
|
||||
return builder.build(forRestTemplate(restTemplate, expectationManager));
|
||||
}
|
||||
|
||||
/**
|
||||
* Return {@link RequestExpectationManager} to be used for binding with the specified
|
||||
* {@link RestTemplate}. If the {@link RestTemplate} is using a
|
||||
* {@link RootUriTemplateHandler} then a {@link RootUriRequestExpectationManager} is
|
||||
* returned, otherwise the source manager is returned unchanged.
|
||||
* @param restTemplate the source REST template
|
||||
* @param expectationManager the source {@link RequestExpectationManager}
|
||||
* @return a {@link RequestExpectationManager} to be bound to the template
|
||||
*/
|
||||
public static RequestExpectationManager forRestTemplate(RestTemplate restTemplate,
|
||||
RequestExpectationManager expectationManager) {
|
||||
Assert.notNull(restTemplate, "'restTemplate' must not be null");
|
||||
UriTemplateHandler templateHandler = restTemplate.getUriTemplateHandler();
|
||||
if (templateHandler instanceof RootUriTemplateHandler rootHandler) {
|
||||
return new RootUriRequestExpectationManager(rootHandler.getRootUri(), expectationManager);
|
||||
}
|
||||
return expectationManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link ClientHttpRequest} wrapper to replace the request URI.
|
||||
*/
|
||||
private static class ReplaceUriClientHttpRequest extends HttpRequestWrapper implements ClientHttpRequest {
|
||||
|
||||
private final URI uri;
|
||||
|
||||
ReplaceUriClientHttpRequest(URI uri, ClientHttpRequest request) {
|
||||
super(request);
|
||||
this.uri = uri;
|
||||
}
|
||||
|
||||
@Override
|
||||
public URI getURI() {
|
||||
return this.uri;
|
||||
}
|
||||
|
||||
@Override
|
||||
public OutputStream getBody() throws IOException {
|
||||
return getRequest().getBody();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClientHttpResponse execute() throws IOException {
|
||||
return getRequest().execute();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClientHttpRequest getRequest() {
|
||||
return (ClientHttpRequest) super.getRequest();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* Copyright 2012-2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* RestClient and RestTemplate test utilities.
|
||||
*/
|
||||
package org.springframework.boot.restclient.test;
|
||||
@@ -0,0 +1,163 @@
|
||||
/*
|
||||
* Copyright 2012-2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* 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.restclient.test;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.test.web.client.RequestExpectationManager;
|
||||
import org.springframework.test.web.client.SimpleRequestExpectationManager;
|
||||
import org.springframework.test.web.client.UnorderedRequestExpectationManager;
|
||||
import org.springframework.web.client.RestClient;
|
||||
import org.springframework.web.client.RestClient.Builder;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
||||
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
|
||||
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
|
||||
|
||||
/**
|
||||
* Tests for {@link MockServerRestClientCustomizer}.
|
||||
*
|
||||
* @author Scott Frederick
|
||||
*/
|
||||
class MockServerRestClientCustomizerTests {
|
||||
|
||||
private MockServerRestClientCustomizer customizer;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
this.customizer = new MockServerRestClientCustomizer();
|
||||
}
|
||||
|
||||
@Test
|
||||
void createShouldUseSimpleRequestExpectationManager() {
|
||||
MockServerRestClientCustomizer customizer = new MockServerRestClientCustomizer();
|
||||
customizer.customize(RestClient.builder());
|
||||
assertThat(customizer.getServer()).extracting("expectationManager")
|
||||
.isInstanceOf(SimpleRequestExpectationManager.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void createWhenExpectationManagerClassIsNullShouldThrowException() {
|
||||
Class<? extends RequestExpectationManager> expectationManager = null;
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> new MockServerRestClientCustomizer(expectationManager))
|
||||
.withMessageContaining("'expectationManager' must not be null");
|
||||
}
|
||||
|
||||
@Test
|
||||
void createWhenExpectationManagerSupplierIsNullShouldThrowException() {
|
||||
Supplier<? extends RequestExpectationManager> expectationManagerSupplier = null;
|
||||
assertThatIllegalArgumentException()
|
||||
.isThrownBy(() -> new MockServerRestClientCustomizer(expectationManagerSupplier))
|
||||
.withMessageContaining("'expectationManagerSupplier' must not be null");
|
||||
}
|
||||
|
||||
@Test
|
||||
void createShouldUseExpectationManagerClass() {
|
||||
MockServerRestClientCustomizer customizer = new MockServerRestClientCustomizer(
|
||||
UnorderedRequestExpectationManager.class);
|
||||
customizer.customize(RestClient.builder());
|
||||
assertThat(customizer.getServer()).extracting("expectationManager")
|
||||
.isInstanceOf(UnorderedRequestExpectationManager.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void createShouldUseSupplier() {
|
||||
MockServerRestClientCustomizer customizer = new MockServerRestClientCustomizer(
|
||||
UnorderedRequestExpectationManager::new);
|
||||
customizer.customize(RestClient.builder());
|
||||
assertThat(customizer.getServer()).extracting("expectationManager")
|
||||
.isInstanceOf(UnorderedRequestExpectationManager.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void customizeShouldBindServer() {
|
||||
Builder builder = RestClient.builder();
|
||||
this.customizer.customize(builder);
|
||||
this.customizer.getServer().expect(requestTo("/test")).andRespond(withSuccess());
|
||||
builder.build().get().uri("/test").retrieve().toEntity(String.class);
|
||||
this.customizer.getServer().verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
void getServerWhenNoServersAreBoundShouldThrowException() {
|
||||
assertThatIllegalStateException().isThrownBy(this.customizer::getServer)
|
||||
.withMessageContaining("Unable to return a single MockRestServiceServer since "
|
||||
+ "MockServerRestClientCustomizer has not been bound to a RestClient");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getServerWhenMultipleServersAreBoundShouldThrowException() {
|
||||
this.customizer.customize(RestClient.builder());
|
||||
this.customizer.customize(RestClient.builder());
|
||||
assertThatIllegalStateException().isThrownBy(this.customizer::getServer)
|
||||
.withMessageContaining("Unable to return a single MockRestServiceServer since "
|
||||
+ "MockServerRestClientCustomizer has been bound to more than one RestClient");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getServerWhenSingleServerIsBoundShouldReturnServer() {
|
||||
Builder builder = RestClient.builder();
|
||||
this.customizer.customize(builder);
|
||||
assertThat(this.customizer.getServer()).isEqualTo(this.customizer.getServer(builder));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getServerWhenRestClientBuilderIsFoundShouldReturnServer() {
|
||||
Builder builder1 = RestClient.builder();
|
||||
Builder builder2 = RestClient.builder();
|
||||
this.customizer.customize(builder1);
|
||||
this.customizer.customize(builder2);
|
||||
assertThat(this.customizer.getServer(builder1)).isNotNull();
|
||||
assertThat(this.customizer.getServer(builder2)).isNotNull().isNotSameAs(this.customizer.getServer(builder1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getServerWhenRestClientBuilderIsNotFoundShouldReturnNull() {
|
||||
Builder builder1 = RestClient.builder();
|
||||
Builder builder2 = RestClient.builder();
|
||||
this.customizer.customize(builder1);
|
||||
assertThat(this.customizer.getServer(builder1)).isNotNull();
|
||||
assertThat(this.customizer.getServer(builder2)).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void getServersShouldReturnServers() {
|
||||
Builder builder1 = RestClient.builder();
|
||||
Builder builder2 = RestClient.builder();
|
||||
this.customizer.customize(builder1);
|
||||
this.customizer.customize(builder2);
|
||||
assertThat(this.customizer.getServers()).containsOnlyKeys(builder1, builder2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getExpectationManagersShouldReturnExpectationManagers() {
|
||||
Builder builder1 = RestClient.builder();
|
||||
Builder builder2 = RestClient.builder();
|
||||
this.customizer.customize(builder1);
|
||||
this.customizer.customize(builder2);
|
||||
RequestExpectationManager manager1 = this.customizer.getExpectationManagers().get(builder1);
|
||||
RequestExpectationManager manager2 = this.customizer.getExpectationManagers().get(builder2);
|
||||
assertThat(this.customizer.getServer(builder1)).extracting("expectationManager").isEqualTo(manager1);
|
||||
assertThat(this.customizer.getServer(builder2)).extracting("expectationManager").isEqualTo(manager2);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,199 @@
|
||||
/*
|
||||
* Copyright 2012-2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* 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.restclient.test;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.restclient.RestTemplateBuilder;
|
||||
import org.springframework.http.client.BufferingClientHttpRequestFactory;
|
||||
import org.springframework.http.client.ClientHttpRequestFactory;
|
||||
import org.springframework.test.web.client.RequestExpectationManager;
|
||||
import org.springframework.test.web.client.SimpleRequestExpectationManager;
|
||||
import org.springframework.test.web.client.UnorderedRequestExpectationManager;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
||||
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
|
||||
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
|
||||
|
||||
/**
|
||||
* Tests for {@link MockServerRestTemplateCustomizer}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Moritz Halbritter
|
||||
*/
|
||||
class MockServerRestTemplateCustomizerTests {
|
||||
|
||||
private MockServerRestTemplateCustomizer customizer;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
this.customizer = new MockServerRestTemplateCustomizer();
|
||||
}
|
||||
|
||||
@Test
|
||||
void createShouldUseSimpleRequestExpectationManager() {
|
||||
MockServerRestTemplateCustomizer customizer = new MockServerRestTemplateCustomizer();
|
||||
customizer.customize(new RestTemplate());
|
||||
assertThat(customizer.getServer()).extracting("expectationManager")
|
||||
.isInstanceOf(SimpleRequestExpectationManager.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void createWhenExpectationManagerClassIsNullShouldThrowException() {
|
||||
Class<? extends RequestExpectationManager> expectationManager = null;
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> new MockServerRestTemplateCustomizer(expectationManager))
|
||||
.withMessageContaining("'expectationManager' must not be null");
|
||||
}
|
||||
|
||||
@Test
|
||||
void createWhenExpectationManagerSupplierIsNullShouldThrowException() {
|
||||
Supplier<? extends RequestExpectationManager> expectationManagerSupplier = null;
|
||||
assertThatIllegalArgumentException()
|
||||
.isThrownBy(() -> new MockServerRestTemplateCustomizer(expectationManagerSupplier))
|
||||
.withMessageContaining("'expectationManagerSupplier' must not be null");
|
||||
}
|
||||
|
||||
@Test
|
||||
void createShouldUseExpectationManagerClass() {
|
||||
MockServerRestTemplateCustomizer customizer = new MockServerRestTemplateCustomizer(
|
||||
UnorderedRequestExpectationManager.class);
|
||||
customizer.customize(new RestTemplate());
|
||||
assertThat(customizer.getServer()).extracting("expectationManager")
|
||||
.isInstanceOf(UnorderedRequestExpectationManager.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void createShouldUseSupplier() {
|
||||
MockServerRestTemplateCustomizer customizer = new MockServerRestTemplateCustomizer(
|
||||
UnorderedRequestExpectationManager::new);
|
||||
customizer.customize(new RestTemplate());
|
||||
assertThat(customizer.getServer()).extracting("expectationManager")
|
||||
.isInstanceOf(UnorderedRequestExpectationManager.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void detectRootUriShouldDefaultToTrue() {
|
||||
MockServerRestTemplateCustomizer customizer = new MockServerRestTemplateCustomizer(
|
||||
UnorderedRequestExpectationManager.class);
|
||||
customizer.customize(new RestTemplateBuilder().rootUri("https://example.com").build());
|
||||
assertThat(customizer.getServer()).extracting("expectationManager")
|
||||
.isInstanceOf(RootUriRequestExpectationManager.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void setDetectRootUriShouldDisableRootUriDetection() {
|
||||
this.customizer.setDetectRootUri(false);
|
||||
this.customizer.customize(new RestTemplateBuilder().rootUri("https://example.com").build());
|
||||
assertThat(this.customizer.getServer()).extracting("expectationManager")
|
||||
.isInstanceOf(SimpleRequestExpectationManager.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void bufferContentShouldDefaultToFalse() {
|
||||
MockServerRestTemplateCustomizer customizer = new MockServerRestTemplateCustomizer();
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
customizer.customize(restTemplate);
|
||||
assertThat(restTemplate.getRequestFactory()).isInstanceOf(ClientHttpRequestFactory.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void setBufferContentShouldEnableContentBuffering() {
|
||||
MockServerRestTemplateCustomizer customizer = new MockServerRestTemplateCustomizer();
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
customizer.setBufferContent(true);
|
||||
customizer.customize(restTemplate);
|
||||
assertThat(restTemplate.getRequestFactory()).isInstanceOf(BufferingClientHttpRequestFactory.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void customizeShouldBindServer() {
|
||||
RestTemplate template = new RestTemplateBuilder(this.customizer).build();
|
||||
this.customizer.getServer().expect(requestTo("/test")).andRespond(withSuccess());
|
||||
template.getForEntity("/test", String.class);
|
||||
this.customizer.getServer().verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
void getServerWhenNoServersAreBoundShouldThrowException() {
|
||||
assertThatIllegalStateException().isThrownBy(this.customizer::getServer)
|
||||
.withMessageContaining("Unable to return a single MockRestServiceServer since "
|
||||
+ "MockServerRestTemplateCustomizer has not been bound to a RestTemplate");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getServerWhenMultipleServersAreBoundShouldThrowException() {
|
||||
this.customizer.customize(new RestTemplate());
|
||||
this.customizer.customize(new RestTemplate());
|
||||
assertThatIllegalStateException().isThrownBy(this.customizer::getServer)
|
||||
.withMessageContaining("Unable to return a single MockRestServiceServer since "
|
||||
+ "MockServerRestTemplateCustomizer has been bound to more than one RestTemplate");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getServerWhenSingleServerIsBoundShouldReturnServer() {
|
||||
RestTemplate template = new RestTemplate();
|
||||
this.customizer.customize(template);
|
||||
assertThat(this.customizer.getServer()).isEqualTo(this.customizer.getServer(template));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getServerWhenRestTemplateIsFoundShouldReturnServer() {
|
||||
RestTemplate template1 = new RestTemplate();
|
||||
RestTemplate template2 = new RestTemplate();
|
||||
this.customizer.customize(template1);
|
||||
this.customizer.customize(template2);
|
||||
assertThat(this.customizer.getServer(template1)).isNotNull();
|
||||
assertThat(this.customizer.getServer(template2)).isNotNull().isNotSameAs(this.customizer.getServer(template1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getServerWhenRestTemplateIsNotFoundShouldReturnNull() {
|
||||
RestTemplate template1 = new RestTemplate();
|
||||
RestTemplate template2 = new RestTemplate();
|
||||
this.customizer.customize(template1);
|
||||
assertThat(this.customizer.getServer(template1)).isNotNull();
|
||||
assertThat(this.customizer.getServer(template2)).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void getServersShouldReturnServers() {
|
||||
RestTemplate template1 = new RestTemplate();
|
||||
RestTemplate template2 = new RestTemplate();
|
||||
this.customizer.customize(template1);
|
||||
this.customizer.customize(template2);
|
||||
assertThat(this.customizer.getServers()).containsOnlyKeys(template1, template2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getExpectationManagersShouldReturnExpectationManagers() {
|
||||
RestTemplate template1 = new RestTemplate();
|
||||
RestTemplate template2 = new RestTemplate();
|
||||
this.customizer.customize(template1);
|
||||
this.customizer.customize(template2);
|
||||
RequestExpectationManager manager1 = this.customizer.getExpectationManagers().get(template1);
|
||||
RequestExpectationManager manager2 = this.customizer.getExpectationManagers().get(template2);
|
||||
assertThat(this.customizer.getServer(template1)).extracting("expectationManager").isEqualTo(manager1);
|
||||
assertThat(this.customizer.getServer(template2)).extracting("expectationManager").isEqualTo(manager2);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,175 @@
|
||||
/*
|
||||
* Copyright 2012-2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* 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.restclient.test;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import org.springframework.boot.restclient.RestTemplateBuilder;
|
||||
import org.springframework.http.client.ClientHttpRequest;
|
||||
import org.springframework.http.client.support.HttpRequestWrapper;
|
||||
import org.springframework.test.web.client.ExpectedCount;
|
||||
import org.springframework.test.web.client.MockRestServiceServer;
|
||||
import org.springframework.test.web.client.RequestExpectationManager;
|
||||
import org.springframework.test.web.client.RequestMatcher;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.assertArg;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.BDDMockito.then;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
|
||||
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
|
||||
|
||||
/**
|
||||
* Tests for {@link RootUriRequestExpectationManager}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class RootUriRequestExpectationManagerTests {
|
||||
|
||||
private final String uri = "https://example.com";
|
||||
|
||||
@Mock
|
||||
private RequestExpectationManager delegate;
|
||||
|
||||
private RootUriRequestExpectationManager manager;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
this.manager = new RootUriRequestExpectationManager(this.uri, this.delegate);
|
||||
}
|
||||
|
||||
@Test
|
||||
void createWhenRootUriIsNullShouldThrowException() {
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> new RootUriRequestExpectationManager(null, this.delegate))
|
||||
.withMessageContaining("'rootUri' must not be null");
|
||||
}
|
||||
|
||||
@Test
|
||||
void createWhenExpectationManagerIsNullShouldThrowException() {
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> new RootUriRequestExpectationManager(this.uri, null))
|
||||
.withMessageContaining("'expectationManager' must not be null");
|
||||
}
|
||||
|
||||
@Test
|
||||
void expectRequestShouldDelegateToExpectationManager() {
|
||||
ExpectedCount count = ExpectedCount.once();
|
||||
RequestMatcher requestMatcher = mock(RequestMatcher.class);
|
||||
this.manager.expectRequest(count, requestMatcher);
|
||||
then(this.delegate).should().expectRequest(count, requestMatcher);
|
||||
}
|
||||
|
||||
@Test
|
||||
void validateRequestWhenUriDoesNotStartWithRootUriShouldDelegateToExpectationManager() throws Exception {
|
||||
ClientHttpRequest request = mock(ClientHttpRequest.class);
|
||||
given(request.getURI()).willReturn(new URI("https://spring.io/test"));
|
||||
this.manager.validateRequest(request);
|
||||
then(this.delegate).should().validateRequest(request);
|
||||
}
|
||||
|
||||
@Test
|
||||
void validateRequestWhenUriStartsWithRootUriShouldReplaceUri() throws Exception {
|
||||
ClientHttpRequest request = mock(ClientHttpRequest.class);
|
||||
given(request.getURI()).willReturn(new URI(this.uri + "/hello"));
|
||||
this.manager.validateRequest(request);
|
||||
URI expectedURI = new URI("/hello");
|
||||
then(this.delegate).should()
|
||||
.validateRequest(assertArg((actual) -> assertThat(actual).isInstanceOfSatisfying(HttpRequestWrapper.class,
|
||||
(requestWrapper) -> {
|
||||
assertThat(requestWrapper.getRequest()).isSameAs(request);
|
||||
assertThat(requestWrapper.getURI()).isEqualTo(expectedURI);
|
||||
})));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void validateRequestWhenRequestUriAssertionIsThrownShouldReplaceUriInMessage() throws Exception {
|
||||
ClientHttpRequest request = mock(ClientHttpRequest.class);
|
||||
given(request.getURI()).willReturn(new URI(this.uri + "/hello"));
|
||||
given(this.delegate.validateRequest(any(ClientHttpRequest.class)))
|
||||
.willThrow(new AssertionError("Request URI expected:</hello> was:<https://example.com/bad>"));
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> this.manager.validateRequest(request))
|
||||
.withMessageContaining("Request URI expected:<https://example.com/hello>");
|
||||
}
|
||||
|
||||
@Test
|
||||
void resetRequestShouldDelegateToExpectationManager() {
|
||||
this.manager.reset();
|
||||
then(this.delegate).should().reset();
|
||||
}
|
||||
|
||||
@Test
|
||||
void bindToShouldReturnMockRestServiceServer() {
|
||||
RestTemplate restTemplate = new RestTemplateBuilder().build();
|
||||
MockRestServiceServer bound = RootUriRequestExpectationManager.bindTo(restTemplate);
|
||||
assertThat(bound).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void bindToWithExpectationManagerShouldReturnMockRestServiceServer() {
|
||||
RestTemplate restTemplate = new RestTemplateBuilder().build();
|
||||
MockRestServiceServer bound = RootUriRequestExpectationManager.bindTo(restTemplate, this.delegate);
|
||||
assertThat(bound).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void forRestTemplateWhenUsingRootUriTemplateHandlerShouldReturnRootUriRequestExpectationManager() {
|
||||
RestTemplate restTemplate = new RestTemplateBuilder().rootUri(this.uri).build();
|
||||
RequestExpectationManager actual = RootUriRequestExpectationManager.forRestTemplate(restTemplate,
|
||||
this.delegate);
|
||||
assertThat(actual).isInstanceOf(RootUriRequestExpectationManager.class);
|
||||
assertThat(actual).extracting("rootUri").isEqualTo(this.uri);
|
||||
}
|
||||
|
||||
@Test
|
||||
void forRestTemplateWhenNotUsingRootUriTemplateHandlerShouldReturnOriginalRequestExpectationManager() {
|
||||
RestTemplate restTemplate = new RestTemplateBuilder().build();
|
||||
RequestExpectationManager actual = RootUriRequestExpectationManager.forRestTemplate(restTemplate,
|
||||
this.delegate);
|
||||
assertThat(actual).isSameAs(this.delegate);
|
||||
}
|
||||
|
||||
@Test
|
||||
void boundRestTemplateShouldPrefixRootUri() {
|
||||
RestTemplate restTemplate = new RestTemplateBuilder().rootUri("https://example.com").build();
|
||||
MockRestServiceServer server = RootUriRequestExpectationManager.bindTo(restTemplate);
|
||||
server.expect(requestTo("/hello")).andRespond(withSuccess());
|
||||
restTemplate.getForEntity("/hello", String.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void boundRestTemplateWhenUrlIncludesDomainShouldNotPrefixRootUri() {
|
||||
RestTemplate restTemplate = new RestTemplateBuilder().rootUri("https://example.com").build();
|
||||
MockRestServiceServer server = RootUriRequestExpectationManager.bindTo(restTemplate);
|
||||
server.expect(requestTo("/hello")).andRespond(withSuccess());
|
||||
assertThatExceptionOfType(AssertionError.class)
|
||||
.isThrownBy(() -> restTemplate.getForEntity("https://spring.io/hello", String.class))
|
||||
.withMessageContaining("expected:<https://example.com/hello> but was:<https://spring.io/hello>");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright 2012-2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* 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.restclient.test.scan;
|
||||
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* A simple factory bean with no generics. Used to test early initialization doesn't
|
||||
* occur.
|
||||
*
|
||||
* @author Madhura Bhave
|
||||
*/
|
||||
@Component
|
||||
@SuppressWarnings("rawtypes")
|
||||
public class SimpleFactoryBean implements FactoryBean {
|
||||
|
||||
private static boolean isInitializedEarly = false;
|
||||
|
||||
public SimpleFactoryBean() {
|
||||
isInitializedEarly = true;
|
||||
throw new RuntimeException();
|
||||
}
|
||||
|
||||
@Autowired
|
||||
public SimpleFactoryBean(ApplicationContext context) {
|
||||
if (isInitializedEarly) {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getObject() {
|
||||
return new Object();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> getObjectType() {
|
||||
return Object.class;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user