Add support for modifying a request prior to it being documented

Prior to this commit it was not possible to modify a request prior to
it being documented, only a response. This commit builds on the new
Operation abstraction to simplify the existing response modification
support and to add support for request modification.

Closes gh-84
This commit is contained in:
Andy Wilkinson
2015-09-02 12:40:59 +01:00
parent 3579b34a77
commit 9d8bbf0558
34 changed files with 992 additions and 885 deletions

View File

@@ -0,0 +1,56 @@
[[customizing-requests-and-responses]]
== Customizing requests and responses
There may be situations where you do not want to document a request exactly as it was sent
or a response exactly as it was received. Spring REST Docs provides a number of
preprocessors that can be used to modify a request or response before it's documented.
Preprocessing is configured by calling `document` with an `OperationRequestPreprocessor`,
and/or an `OperationResponsePreprocessor`. Instances can be obtained using the
static `preprocessRequest` and `preprocessResponse` methods on `Preprocessors`:
[source,java,indent=0]
----
include::{examples-dir}/com/example/Preprocessing.java[tags=general]
----
<1> Apply a request preprocessor that will remove the header named `Foo`.
<2> Apply a response preprocessor that will pretty print its content.
Various built in preprocessors, including those illustrated above, are available via the
static methods on `Preprocessors`. See below for further details.
[[customizing-requests-and-responses-pretty-printing]]
=== Pretty printing
`prettyPrint` on `Preprocessors` formats the content of the request or response
to make it easier to read.
[[customizing-requests-and-responses-masking-links]]
=== Masking links
If you're documenting a Hypermedia-based API, you may want to encourage clients to
navigate the API using links rather than through the use of hard coded URIs. One way to do
this is to limit the use of URIs in the documentation. `maskLinks` on
`Preprocessors` replaces the `href` of any links in the response with `...`. A
different replacement can also be specified if you wish.
[[customizing-requests-and-responses-removing-headers]]
=== Removing headers
`removeHeaders` on `Preprocessors` removes any occurrences of the named headers
from the request or response.
[[customizing-requests-and-responses-replacing-patterns]]
=== Replacing patterns
`replacePattern` on `Preprocessors` provides a general purpose mechanism for
replacing content in a request or response. Any occurrences of a regular expression are
replaced.

View File

@@ -1,45 +0,0 @@
[[customizing-responses]]
== Customizing responses
There may be situations where you do not want to document a response exactly as received.
Spring REST Docs provides a number of response post processors that can be used to modify
a response after it is received but before it's documented.
Response modification is configured using a `ResponseModifier`. An instance can be
obtained using the static `modifyResponseTo` method on `RestDocumentation`. Once the
response modifications have been provided, documentation can be configured as usual
via the `andDocument` method:
[source,java,indent=0]
----
include::{examples-dir}/com/example/ResponsePostProcessing.java[tags=general]
----
<1> Call `modifyResponseTo` to configure response modifications, passing in one or more
`ResponsePostProcessor` implementations.
<2> Proceed with documenting the call.
[[customizing-responses-pretty-printing]]
=== Pretty printing
`prettyPrintContent` on `ResponsePostProcessors` formats the body of the response to
make it easier to read.
[[customizing-responses-masking-links]]
=== Masking links
If you're documenting a Hypermedia-based API, you may want to encourage clients to
navigate the API using links rather than through the use of hard coded URIs. One way to do
this is to limit the use of URIs in the documentation. `maskLinks` on
`ResponsePostProcessors` replaces the `href` of any links in the response with `...`. A
different replacement can also be specified if you wish.
=== Removing headers
`removeHeaders` on `ResponsePostProcessors` removes any occurrences of the named headers
from the response.
=== Replacing patterns
`replacePattern` on `ResponsePostProcessors` provides a general purpose mechanism for
replacing content in a response. Any occurrences of a regular expression are replaced.

View File

@@ -23,7 +23,7 @@ snippets produced with Spring MVC Test.
include::introduction.adoc[]
include::getting-started.adoc[]
include::documenting-your-api.adoc[]
include::customizing-responses.adoc[]
include::customizing-requests-and-responses.adoc[]
include::configuration.adoc[]
include::working-with-asciidoctor.adoc[]
include::contributing.adoc[]

View File

@@ -16,13 +16,17 @@
package com.example;
import static org.springframework.restdocs.RestDocumentation.modifyResponseTo;
import static org.springframework.restdocs.RestDocumentationRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessRequest;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessResponse;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.removeHeaders;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.prettyPrint;
import static org.springframework.restdocs.RestDocumentation.document;
import org.springframework.test.web.servlet.MockMvc;
public class ResponsePostProcessing {
public class Preprocessing {
private MockMvc mockMvc;
@@ -30,8 +34,9 @@ public class ResponsePostProcessing {
// tag::general[]
this.mockMvc.perform(get("/"))
.andExpect(status().isOk())
.andDo(modifyResponseTo(/* ... */) // <1>
.andDocument("index")); // <2>
.andDo(document("index",
preprocessRequest(removeHeaders("Foo")), // <1>
preprocessResponse(prettyPrint()))); // <2>
// end::general[]
}