Skip to content

Commit 130b411

Browse files
committed
Add support for using REST Assured to generate documentation snippets
This commit adds a new module, spring-restdocs-restassured, that can be used to generate documentation snippets when testing a service with REST Assured. Please refer to the updated reference documentation for details. Thanks to Johan Haleby for making a change to REST Assured so that path parameters could be documented. Closes gh-102
1 parent f73108a commit 130b411

File tree

66 files changed

+3360
-432
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+3360
-432
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ subprojects {
5858
}
5959
dependencies {
6060
dependency 'com.fasterxml.jackson.core:jackson-databind:2.4.6.1'
61+
dependency 'com.jayway.restassured:rest-assured:2.8.0'
6162
dependency 'com.samskivert:jmustache:1.11'
6263
dependency 'commons-codec:commons-codec:1.10'
6364
dependency 'javax.servlet:javax.servlet-api:3.1.0'

config/checkstyle/checkstyle.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
<module name="AvoidStarImport" />
7474
<module name="AvoidStaticImport">
7575
<property name="excludes"
76-
value="org.junit.Assert.*, org.junit.Assume.*, org.hamcrest.CoreMatchers.*, org.hamcrest.Matchers.*, org.mockito.Mockito.*, org.mockito.BDDMockito.*, org.mockito.Matchers.*, org.springframework.restdocs.curl.CurlDocumentation.*, org.springframework.restdocs.headers.HeaderDocumentation.*, org.springframework.restdocs.hypermedia.HypermediaDocumentation.*, org.springframework.restdocs.mockmvc.IterableEnumeration.*, org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.*, org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.*, org.springframework.restdocs.payload.PayloadDocumentation.*, org.springframework.restdocs.operation.preprocess.Preprocessors.*, org.springframework.restdocs.request.RequestDocumentation.*, org.springframework.restdocs.snippet.Attributes.*, org.springframework.restdocs.test.SnippetMatchers.*, org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*, org.springframework.test.web.servlet.result.MockMvcResultMatchers.*" />
76+
value="com.jayway.restassured.RestAssured.*, org.junit.Assert.*, org.junit.Assume.*, org.hamcrest.CoreMatchers.*, org.hamcrest.Matchers.*, org.mockito.Mockito.*, org.mockito.BDDMockito.*, org.mockito.Matchers.*, org.springframework.restdocs.curl.CurlDocumentation.*, org.springframework.restdocs.headers.HeaderDocumentation.*, org.springframework.restdocs.hypermedia.HypermediaDocumentation.*, org.springframework.restdocs.mockmvc.IterableEnumeration.*, org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.*, org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.*, org.springframework.restdocs.payload.PayloadDocumentation.*, org.springframework.restdocs.operation.preprocess.Preprocessors.*, org.springframework.restdocs.request.RequestDocumentation.*, org.springframework.restdocs.restassured.RestAssuredRestDocumentation.*, org.springframework.restdocs.restassured.operation.preprocess.RestAssuredPreprocessors.*, org.springframework.restdocs.snippet.Attributes.*, org.springframework.restdocs.test.SnippetMatchers.*, org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*, org.springframework.test.web.servlet.result.MockMvcResultMatchers.*" />
7777
</module>
7878
<module name="IllegalImport" />
7979
<module name="RedundantImport" />

docs/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ plugins {
44

55
dependencies {
66
testCompile project(':spring-restdocs-mockmvc')
7+
testCompile project(':spring-restdocs-restassured')
78
testCompile 'javax.validation:validation-api'
89
}
910

docs/src/docs/asciidoc/configuration.adoc

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66
[[configuration-uris]]
77
=== Documented URIs
88

9-
The default configuration for URIs documented by Spring REST Docs is:
9+
NOTE: As REST Assured tests a service by making actual HTTP requests, the documented
10+
URIs cannot be customized in this way. You should use the
11+
<<customizing-requests-and-responses-preprocessors-modify-uris, REST-Assured specific
12+
preprocessor>> instead.
13+
14+
When using MockMvc, the default configuration for URIs documented by Spring REST Docs is:
1015

1116
|===
1217
|Setting |Default
@@ -21,12 +26,12 @@ The default configuration for URIs documented by Spring REST Docs is:
2126
|`8080`
2227
|===
2328

24-
This configuration is applied by `RestDocumentationMockMvcConfigurer`. You can use its API
29+
This configuration is applied by `MockMvcRestDocumentationConfigurer`. You can use its API
2530
to change one or more of the defaults to suit your needs:
2631

2732
[source,java,indent=0]
2833
----
29-
include::{examples-dir}/com/example/CustomUriConfiguration.java[tags=custom-uri-configuration]
34+
include::{examples-dir}/com/example/mockmvc/CustomUriConfiguration.java[tags=custom-uri-configuration]
3035
----
3136

3237
TIP: To configure a request's context path, use the `contextPath` method on
@@ -38,12 +43,19 @@ TIP: To configure a request's context path, use the `contextPath` method on
3843
=== Snippet encoding
3944

4045
The default encoding used by Asciidoctor is `UTF-8`. Spring REST Docs adopts the same
41-
default for the snippets that it generates. If you require an encoding other than `UTF-8`,
42-
use `RestDocumentationMockMvcConfigurer` to configure it:
46+
default for the snippets that it generates. You can change the default snippet encoding
47+
using the `RestDocumentationConfigurer` API. For example, to use `ISO-8859-1`:
4348

44-
[source,java,indent=0]
49+
[source,java,indent=0,role="primary"]
50+
.MockMvc
51+
----
52+
include::{examples-dir}/com/example/mockmvc/CustomEncoding.java[tags=custom-encoding]
53+
----
54+
55+
[source,java,indent=0,role="secondary"]
56+
.REST Assured
4557
----
46-
include::{examples-dir}/com/example/CustomEncoding.java[tags=custom-encoding]
58+
include::{examples-dir}/com/example/restassured/CustomEncoding.java[tags=custom-encoding]
4759
----
4860

4961

@@ -57,11 +69,18 @@ Three snippets are produced by default:
5769
- `http-request`
5870
- `http-response`
5971

60-
This default configuration is applied by `RestDocumentationMockMvcConfigurer`. You can use
61-
its API to change the configuration. For example, to only produce the `curl-request`
72+
You can change the default snippet configuration during setup using the
73+
`RestDocumentationConfigurer` API. For example, to only produce the `curl-request`
6274
snippet by default:
6375

64-
[source,java,indent=0]
76+
[source,java,indent=0,role="primary"]
77+
.MockMvc
78+
----
79+
include::{examples-dir}/com/example/mockmvc/CustomDefaultSnippets.java[tags=custom-default-snippets]
80+
----
81+
82+
[source,java,indent=0,role="secondary"]
83+
.REST Assured
6584
----
66-
include::{examples-dir}/com/example/CustomDefaultSnippetsConfiguration.java[tags=custom-default-snippets]
85+
include::{examples-dir}/com/example/restassured/CustomDefaultSnippets.java[tags=custom-default-snippets]
6786
----

docs/src/docs/asciidoc/customizing-requests-and-responses.adoc

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,18 @@ and/or an `OperationResponsePreprocessor`. Instances can be obtained using the
1010
static `preprocessRequest` and `preprocessResponse` methods on `Preprocessors`. For
1111
example:
1212

13-
[source,java,indent=0]
13+
[source,java,indent=0,role="primary"]
14+
.MockMvc
1415
----
15-
include::{examples-dir}/com/example/PerTestPreprocessing.java[tags=preprocessing]
16+
include::{examples-dir}/com/example/mockmvc/PerTestPreprocessing.java[tags=preprocessing]
17+
----
18+
<1> Apply a request preprocessor that will remove the header named `Foo`.
19+
<2> Apply a response preprocessor that will pretty print its content.
20+
21+
[source,java,indent=0,role="secondary"]
22+
.REST Assured
23+
----
24+
include::{examples-dir}/com/example/restassured/PerTestPreprocessing.java[tags=preprocessing]
1625
----
1726
<1> Apply a request preprocessor that will remove the header named `Foo`.
1827
<2> Apply a response preprocessor that will pretty print its content.
@@ -22,25 +31,44 @@ so by configuring the preprocessors in your `@Before` method and using the
2231
<<documentating-your-api-parameterized-output-directories, support for parameterized
2332
output directories>>:
2433

25-
[source,java,indent=0]
34+
[source,java,indent=0,role="primary"]
35+
.MockMvc
2636
----
27-
include::{examples-dir}/com/example/EveryTestPreprocessing.java[tags=setup]
37+
include::{examples-dir}/com/example/mockmvc/EveryTestPreprocessing.java[tags=setup]
2838
----
29-
<1> Create the `RestDocumentationResultHandler`, configured to preprocess the request
39+
<1> Create a `RestDocumentationResultHandler`, configured to preprocess the request
3040
and response.
31-
<2> Create the `MockMvc` instance, configured to always call the documentation result
41+
<2> Create a `MockMvc` instance, configured to always call the documentation result
3242
handler.
3343

34-
Then, in each test, the `RestDocumentationResultHandler` can be configured with anything
35-
test-specific. For example:
44+
[source,java,indent=0,role="secondary"]
45+
.REST Assured
46+
----
47+
include::{examples-dir}/com/example/restassured/EveryTestPreprocessing.java[tags=setup]
48+
----
49+
<1> Create a `RestDocumentationFilter`, configured to preprocess the request
50+
and response.
51+
<2> Create a `RequestSpecification` instance, configured to always call the documentation
52+
filter.
53+
54+
Then, in each test, any configuration specific to that test can be performed. For example:
55+
56+
[source,java,indent=0,role="primary"]
57+
.MockMvc
58+
----
59+
include::{examples-dir}/com/example/mockmvc/EveryTestPreprocessing.java[tags=use]
60+
----
61+
<1> Document the links specific to the resource that is being tested
62+
<2> The request and response will be preprocessed due to the use of `alwaysDo` above.
3663

37-
[source,java,indent=0]
64+
[source,java,indent=0,role="secondary"]
65+
.REST Assured
3866
----
39-
include::{examples-dir}/com/example/EveryTestPreprocessing.java[tags=use]
67+
include::{examples-dir}/com/example/restassured/EveryTestPreprocessing.java[tags=use]
4068
----
4169
<1> Document the links specific to the resource that is being tested
42-
<2> The `perform` call will automatically produce the documentation snippets due to the
43-
use of `alwaysDo` above.
70+
<2> The request and response will be preprocessed due to the configuration of the
71+
`RequestSpecification` in the `setUp` method.
4472

4573
Various built in preprocessors, including those illustrated above, are available via the
4674
static methods on `Preprocessors`. See <<Preprocessors, below>> for further details.
@@ -86,6 +114,20 @@ from the request or response.
86114
replacing content in a request or response. Any occurrences of a regular expression are
87115
replaced.
88116

117+
118+
119+
[[customizing-requests-and-responses-preprocessors-modify-uris]]
120+
==== Modifying URIs
121+
122+
TIP: If you are using MockMvc, URIs should be customized by <<configuration-uris, changing
123+
the configuration>>.
124+
125+
`modifyUris` on `RestAssuredPreprocessors` can be used to modify any URIs in a request
126+
or a response. When using REST Assured, this allows you to customize the URIs that appear
127+
in the documentation while testing a local instance of the service.
128+
129+
130+
89131
[[customizing-requests-and-responses-preprocessors-writing-your-own]]
90132
==== Writing your own preprocessor
91133

0 commit comments

Comments
 (0)