Improve diagnostics for null configuration
See gh-583
This commit is contained in:
committed by
Andy Wilkinson
parent
c4d664c3a2
commit
b5f2454c00
@@ -30,6 +30,7 @@ import org.springframework.test.web.reactive.server.WebTestClient;
|
||||
import org.springframework.test.web.reactive.server.WebTestClient.BodyContentSpec;
|
||||
import org.springframework.test.web.reactive.server.WebTestClient.BodySpec;
|
||||
import org.springframework.test.web.reactive.server.WebTestClient.Builder;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
|
||||
|
||||
/**
|
||||
@@ -75,7 +76,7 @@ public abstract class WebTestClientRestDocumentation {
|
||||
*/
|
||||
public static <T extends ExchangeResult> Consumer<T> document(String identifier, Snippet... snippets) {
|
||||
return (result) -> new RestDocumentationGenerator<>(identifier, REQUEST_CONVERTER, RESPONSE_CONVERTER, snippets)
|
||||
.handle(result, result, retrieveConfiguration(result));
|
||||
.handle(result, result, getRequiredConfiguration(result));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -92,7 +93,7 @@ public abstract class WebTestClientRestDocumentation {
|
||||
public static <T extends ExchangeResult> Consumer<T> document(String identifier,
|
||||
OperationRequestPreprocessor requestPreprocessor, Snippet... snippets) {
|
||||
return (result) -> new RestDocumentationGenerator<>(identifier, REQUEST_CONVERTER, RESPONSE_CONVERTER,
|
||||
requestPreprocessor, snippets).handle(result, result, retrieveConfiguration(result));
|
||||
requestPreprocessor, snippets).handle(result, result, getRequiredConfiguration(result));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -109,7 +110,7 @@ public abstract class WebTestClientRestDocumentation {
|
||||
public static <T extends ExchangeResult> Consumer<T> document(String identifier,
|
||||
OperationResponsePreprocessor responsePreprocessor, Snippet... snippets) {
|
||||
return (result) -> new RestDocumentationGenerator<>(identifier, REQUEST_CONVERTER, RESPONSE_CONVERTER,
|
||||
responsePreprocessor, snippets).handle(result, result, retrieveConfiguration(result));
|
||||
responsePreprocessor, snippets).handle(result, result, getRequiredConfiguration(result));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -130,12 +131,17 @@ public abstract class WebTestClientRestDocumentation {
|
||||
Snippet... snippets) {
|
||||
return (result) -> new RestDocumentationGenerator<>(identifier, REQUEST_CONVERTER, RESPONSE_CONVERTER,
|
||||
requestPreprocessor, responsePreprocessor, snippets).handle(result, result,
|
||||
retrieveConfiguration(result));
|
||||
getRequiredConfiguration(result));
|
||||
}
|
||||
|
||||
private static Map<String, Object> retrieveConfiguration(ExchangeResult result) {
|
||||
Map<String, Object> configuration = new HashMap<>(
|
||||
WebTestClientRestDocumentationConfigurer.retrieveConfiguration(result.getRequestHeaders()));
|
||||
private static Map<String, Object> getRequiredConfiguration(ExchangeResult result) {
|
||||
Map<String, Object> config = WebTestClientRestDocumentationConfigurer
|
||||
.retrieveConfiguration(result.getRequestHeaders());
|
||||
Assert.state(config != null,
|
||||
() -> String.format("There is no REST Docs configuration. Looks like '%s' "
|
||||
+ "was not invoked or configuration has already been removed. Please check your configuration.",
|
||||
WebTestClientRestDocumentationConfigurer.class.getName()));
|
||||
Map<String, Object> configuration = new HashMap<>(config);
|
||||
configuration.put(RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE, result.getUriTemplate());
|
||||
return configuration;
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ import java.io.InputStreamReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -58,6 +59,7 @@ import org.springframework.web.reactive.function.server.RouterFunctions;
|
||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName;
|
||||
import static org.springframework.restdocs.request.RequestDocumentation.partWithName;
|
||||
@@ -173,6 +175,20 @@ public class WebTestClientRestDocumentationIntegrationTests {
|
||||
+ " 'Accept:application/json' \\%n" + " 'Cookie:cookieName=cookieVal'"))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void illegalStateExceptionShouldBeThrownWhenCallDocumentWebClientNotConfigured() {
|
||||
assertThatThrownBy(() -> this.webTestClient
|
||||
.mutateWith((builder, httpHandlerBuilder, connector) -> builder.filters(List::clear).build()).get()
|
||||
.uri("/").exchange().expectBody().consumeWith(document("default-snippets")))
|
||||
.isInstanceOf(IllegalStateException.class).hasMessageContaining(missingConfiguration());
|
||||
}
|
||||
|
||||
private String missingConfiguration() {
|
||||
return "There is no REST Docs configuration. Looks like "
|
||||
+ "'org.springframework.restdocs.webtestclient.WebTestClientRestDocumentationConfigurer' "
|
||||
+ "was not invoked or configuration has already been removed. Please check your configuration.";
|
||||
}
|
||||
|
||||
private void assertExpectedSnippetFilesExist(File directory, String... snippets) {
|
||||
Set<File> actual = new HashSet<>(Arrays.asList(directory.listFiles()));
|
||||
Set<File> expected = Stream.of(snippets).map((snippet) -> new File(directory, snippet))
|
||||
|
||||
Reference in New Issue
Block a user