Default snippet encoding to UTF-8 and make it configurable

Previously, snippets were written to disk using the JVM’s default
encoding. While this could be controlled by the file.encoding system
property it was not ideal as, if the proper was not always set, it could
lead to platform-dependent output being generated. Furthermore,
Asciidoctor uses UTF-8 encoding by default so there was a risk of the
snippets being generated in a different encoding to the encoding
expected by Asciidoctor.

This commit updates the configuration so that, by default, snippets are
encoded as UTF-8. The RestDocumentationConfigurer API has been enhanced
to allow this default to be configured as part of building the
MockMvc instance. This enhancement as brought with it a more fluent
configuration API that separates configuration that is related to URIs
from configuration that is related to snippets.

Closes gh-67
This commit is contained in:
Andy Wilkinson
2015-05-12 18:11:48 +01:00
parent 68198b3145
commit 0c0d3b89bd
15 changed files with 501 additions and 123 deletions

View File

@@ -243,24 +243,48 @@ documenting. A custom `ResultHandler` is used to produce individual documentatio
snippets for its request and its response as well as a snippet that contains both its
request and its response.
You can configure the scheme, host, and port of any URIs that appear in the
documentation snippets:
The first step is to create a `MockMvc` instance using `MockMvcBuilders`, configuring
it by applying a `RestDocumentationConfigurer` that can be obtained from the static
`RestDocumentation.documentationConfiguration` method:
```java
@Before
public void setUp() {
this.mockMvc = MockMvcBuilders
.webAppContextSetup(this.context)
.apply(new RestDocumentationConfigurer()
.withScheme("https")
.withHost("localhost")
.withPort(8443))
.apply(documentationConfiguration())
.build();
}
```
The default values are `http`, `localhost`, and `8080`. You can omit the above
configuration if these defaults meet your needs.
This will apply the default REST documentation configuration:
| Setting | Default value
| ---------------- | -------------
| Scheme | http
| Host | localhost
| Port | 8080
| Context path | Empty string
| Snippet encoding | UTF-8
One or more of these settings can be overridden:
```java
@Before
public void setUp() {
this.mockMvc = MockMvcBuilders
.webAppContextSetup(this.context)
.apply(documentationConfiguration()
.uris()
.withScheme("https")
.withHost("api.example.com")
.withPort(443)
.withContextPath("/v3")
.and().snippets()
.withEncoding("ISO-8859-1"))
.build();
}
```
To document a MockMvc call, you use MockMvc's `andDo` method, passing it a
`RestDocumentationResultHandler` that can be easily obtained from