Allow preprocessing to be applied to every test
Following the reworking of the API, it was no longer possible to apply preprocessing to every test and to still customize the snippets used by that test. This commit adds a new snippets() method to RestDocumentationResultHandler that allows additional snippets to be configured once the result handler has been created. The documentation has been updated to describe how to apply preprocessing to every test and Spring HATEOAS sample has been updated to illustrate the approach. Closes gh-88
This commit is contained in:
@@ -79,7 +79,7 @@ public class RestDocumentationResultHandler implements ResultHandler {
|
||||
this.identifier = identifier;
|
||||
this.requestPreprocessor = requestPreprocessor;
|
||||
this.responsePreprocessor = responsePreprocessor;
|
||||
this.snippets = Arrays.asList(snippets);
|
||||
this.snippets = new ArrayList<>(Arrays.asList(snippets));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -102,11 +102,23 @@ public class RestDocumentationResultHandler implements ResultHandler {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the given {@code snippets} such that that are documented when this result
|
||||
* handler is called.
|
||||
*
|
||||
* @param snippets the snippets to add
|
||||
* @return this {@code ResultDocumentationResultHandler}
|
||||
*/
|
||||
public RestDocumentationResultHandler snippets(Snippet... snippets) {
|
||||
this.snippets.addAll(Arrays.asList(snippets));
|
||||
return this;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private List<Snippet> getSnippets(MvcResult result) {
|
||||
List<Snippet> combinedSnippets = new ArrayList<>((List<Snippet>) result
|
||||
.getRequest()
|
||||
.getAttribute("org.springframework.restdocs.defaultSnippets"));
|
||||
.getRequest().getAttribute(
|
||||
"org.springframework.restdocs.mockmvc.defaultSnippets"));
|
||||
combinedSnippets.addAll(this.snippets);
|
||||
return combinedSnippets;
|
||||
}
|
||||
|
||||
@@ -64,7 +64,7 @@ public class SnippetConfigurer extends
|
||||
void apply(MockHttpServletRequest request) {
|
||||
((WriterResolver) request.getAttribute(WriterResolver.class.getName()))
|
||||
.setEncoding(this.snippetEncoding);
|
||||
request.setAttribute("org.springframework.restdocs.defaultSnippets",
|
||||
request.setAttribute("org.springframework.restdocs.mockmvc.defaultSnippets",
|
||||
this.defaultSnippets);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user