Merge branch '1.0.x'

This commit is contained in:
Andy Wilkinson
2016-02-15 12:25:49 +00:00
121 changed files with 1672 additions and 1529 deletions

View File

@@ -23,6 +23,7 @@ import java.util.Arrays;
import javax.servlet.http.Part;
import org.junit.Test;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletRequest;
@@ -53,8 +54,8 @@ public class MockMvcRequestConverterTests {
@Test
public void httpRequest() throws Exception {
OperationRequest request = createOperationRequest(MockMvcRequestBuilders
.get("/foo"));
OperationRequest request = createOperationRequest(
MockMvcRequestBuilders.get("/foo"));
assertThat(request.getUri(), is(URI.create("http://localhost/foo")));
assertThat(request.getMethod(), is(HttpMethod.GET));
}
@@ -71,8 +72,8 @@ public class MockMvcRequestConverterTests {
@Test
public void requestWithContextPath() throws Exception {
OperationRequest request = createOperationRequest(MockMvcRequestBuilders.get(
"/foo/bar").contextPath("/foo"));
OperationRequest request = createOperationRequest(
MockMvcRequestBuilders.get("/foo/bar").contextPath("/foo"));
assertThat(request.getUri(), is(URI.create("http://localhost/foo/bar")));
assertThat(request.getMethod(), is(HttpMethod.GET));
}
@@ -124,8 +125,8 @@ public class MockMvcRequestConverterTests {
@Test
public void getRequestWithQueryStringPopulatesParameters() throws Exception {
OperationRequest request = createOperationRequest(MockMvcRequestBuilders
.get("/foo?a=alpha&b=bravo"));
OperationRequest request = createOperationRequest(
MockMvcRequestBuilders.get("/foo?a=alpha&b=bravo"));
assertThat(request.getUri(),
is(URI.create("http://localhost/foo?a=alpha&b=bravo")));
assertThat(request.getParameters().size(), is(2));
@@ -148,9 +149,9 @@ public class MockMvcRequestConverterTests {
@Test
public void mockMultipartFileUpload() throws Exception {
OperationRequest request = createOperationRequest(MockMvcRequestBuilders
.fileUpload("/foo").file(
new MockMultipartFile("file", new byte[] { 1, 2, 3, 4 })));
OperationRequest request = createOperationRequest(
MockMvcRequestBuilders.fileUpload("/foo")
.file(new MockMultipartFile("file", new byte[] { 1, 2, 3, 4 })));
assertThat(request.getUri(), is(URI.create("http://localhost/foo")));
assertThat(request.getMethod(), is(HttpMethod.POST));
assertThat(request.getParts().size(), is(1));
@@ -164,10 +165,9 @@ public class MockMvcRequestConverterTests {
@Test
public void mockMultipartFileUploadWithContentType() throws Exception {
OperationRequest request = createOperationRequest(MockMvcRequestBuilders
.fileUpload("/foo").file(
new MockMultipartFile("file", "original", "image/png",
new byte[] { 1, 2, 3, 4 })));
OperationRequest request = createOperationRequest(
MockMvcRequestBuilders.fileUpload("/foo").file(new MockMultipartFile(
"file", "original", "image/png", new byte[] { 1, 2, 3, 4 })));
assertThat(request.getUri(), is(URI.create("http://localhost/foo")));
assertThat(request.getMethod(), is(HttpMethod.POST));
assertThat(request.getParts().size(), is(1));
@@ -186,8 +186,8 @@ public class MockMvcRequestConverterTests {
given(mockPart.getHeaderNames()).willReturn(Arrays.asList("a", "b"));
given(mockPart.getHeaders("a")).willReturn(Arrays.asList("alpha"));
given(mockPart.getHeaders("b")).willReturn(Arrays.asList("bravo", "banana"));
given(mockPart.getInputStream()).willReturn(
new ByteArrayInputStream(new byte[] { 1, 2, 3, 4 }));
given(mockPart.getInputStream())
.willReturn(new ByteArrayInputStream(new byte[] { 1, 2, 3, 4 }));
given(mockPart.getName()).willReturn("part-name");
given(mockPart.getSubmittedFileName()).willReturn("submitted.txt");
mockRequest.addPart(mockPart);
@@ -210,8 +210,8 @@ public class MockMvcRequestConverterTests {
given(mockPart.getHeaderNames()).willReturn(Arrays.asList("a", "b"));
given(mockPart.getHeaders("a")).willReturn(Arrays.asList("alpha"));
given(mockPart.getHeaders("b")).willReturn(Arrays.asList("bravo", "banana"));
given(mockPart.getInputStream()).willReturn(
new ByteArrayInputStream(new byte[] { 1, 2, 3, 4 }));
given(mockPart.getInputStream())
.willReturn(new ByteArrayInputStream(new byte[] { 1, 2, 3, 4 }));
given(mockPart.getName()).willReturn("part-name");
given(mockPart.getSubmittedFileName()).willReturn("submitted.png");
given(mockPart.getContentType()).willReturn("image/png");

View File

@@ -20,6 +20,7 @@ import java.net.URI;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.hateoas.mvc.BasicLinkBuilder;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.restdocs.JUnitRestDocumentation;
@@ -58,7 +59,7 @@ public class MockMvcRestDocumentationConfigurerTests {
public void customScheme() {
RequestPostProcessor postProcessor = new MockMvcRestDocumentationConfigurer(
this.restDocumentation).uris().withScheme("https")
.beforeMockMvcCreated(null, null);
.beforeMockMvcCreated(null, null);
postProcessor.postProcessRequest(this.request);
assertUriConfiguration("https", "localhost", 8080);
@@ -68,7 +69,7 @@ public class MockMvcRestDocumentationConfigurerTests {
public void customHost() {
RequestPostProcessor postProcessor = new MockMvcRestDocumentationConfigurer(
this.restDocumentation).uris().withHost("api.example.com")
.beforeMockMvcCreated(null, null);
.beforeMockMvcCreated(null, null);
postProcessor.postProcessRequest(this.request);
assertUriConfiguration("http", "api.example.com", 8080);
@@ -77,8 +78,8 @@ public class MockMvcRestDocumentationConfigurerTests {
@Test
public void customPort() {
RequestPostProcessor postProcessor = new MockMvcRestDocumentationConfigurer(
this.restDocumentation).uris().withPort(8081)
.beforeMockMvcCreated(null, null);
this.restDocumentation).uris().withPort(8081).beforeMockMvcCreated(null,
null);
postProcessor.postProcessRequest(this.request);
assertUriConfiguration("http", "localhost", 8081);
@@ -87,8 +88,8 @@ public class MockMvcRestDocumentationConfigurerTests {
@Test
public void noContentLengthHeaderWhenRequestHasNotContent() {
RequestPostProcessor postProcessor = new MockMvcRestDocumentationConfigurer(
this.restDocumentation).uris().withPort(8081)
.beforeMockMvcCreated(null, null);
this.restDocumentation).uris().withPort(8081).beforeMockMvcCreated(null,
null);
postProcessor.postProcessRequest(this.request);
assertThat(this.request.getHeader("Content-Length"), is(nullValue()));
}
@@ -97,8 +98,8 @@ public class MockMvcRestDocumentationConfigurerTests {
assertEquals(scheme, this.request.getScheme());
assertEquals(host, this.request.getServerName());
assertEquals(port, this.request.getServerPort());
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(
this.request));
RequestContextHolder
.setRequestAttributes(new ServletRequestAttributes(this.request));
try {
URI uri = BasicLinkBuilder.linkToCurrentMapping().toUri();
assertEquals(scheme, uri.getScheme());

View File

@@ -29,6 +29,7 @@ import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -114,10 +115,10 @@ public class MockMvcRestDocumentationIntegrationTests {
@Test
public void basicSnippetGeneration() throws Exception {
MockMvc mockMvc = MockMvcBuilders
.webAppContextSetup(this.context)
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
.apply(new MockMvcRestDocumentationConfigurer(this.restDocumentation)
.snippets().withEncoding("UTF-8")).build();
.snippets().withEncoding("UTF-8"))
.build();
mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk()).andDo(document("basic"));
@@ -127,15 +128,15 @@ public class MockMvcRestDocumentationIntegrationTests {
@Test
public void markdownSnippetGeneration() throws Exception {
MockMvc mockMvc = MockMvcBuilders
.webAppContextSetup(this.context)
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
.apply(new MockMvcRestDocumentationConfigurer(this.restDocumentation)
.snippets().withEncoding("UTF-8").withTemplateFormat(markdown())).build();
.snippets().withEncoding("UTF-8").withTemplateFormat(markdown()))
.build();
mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk()).andDo(document("basic-markdown"));
assertExpectedSnippetFilesExist(new File(
"build/generated-snippets/basic-markdown"), "http-request.md",
assertExpectedSnippetFilesExist(
new File("build/generated-snippets/basic-markdown"), "http-request.md",
"http-response.md", "curl-request.md");
}
@@ -149,10 +150,9 @@ public class MockMvcRestDocumentationIntegrationTests {
assertThat(
new File(
"build/generated-snippets/curl-snippet-with-content/curl-request.adoc"),
is(snippet(asciidoctor()).withContents(
codeBlock(asciidoctor(), "bash").content(
"$ curl " + "'http://localhost:8080/' -i -X POST "
+ "-H 'Accept: application/json' -d 'content'"))));
is(snippet(asciidoctor()).withContents(codeBlock(asciidoctor(), "bash")
.content("$ curl " + "'http://localhost:8080/' -i -X POST "
+ "-H 'Accept: application/json' -d 'content'"))));
}
@Test
@@ -160,18 +160,16 @@ public class MockMvcRestDocumentationIntegrationTests {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
.apply(documentationConfiguration(this.restDocumentation)).build();
mockMvc.perform(
post("/?foo=bar").param("foo", "bar").param("a", "alpha")
.accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
mockMvc.perform(post("/?foo=bar").param("foo", "bar").param("a", "alpha")
.accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
.andDo(document("curl-snippet-with-query-string"));
assertThat(
new File(
"build/generated-snippets/curl-snippet-with-query-string/curl-request.adoc"),
is(snippet(asciidoctor()).withContents(
codeBlock(asciidoctor(), "bash").content(
"$ curl "
+ "'http://localhost:8080/?foo=bar' -i -X POST "
+ "-H 'Accept: application/json' -d 'a=alpha'"))));
is(snippet(asciidoctor())
.withContents(codeBlock(asciidoctor(), "bash").content("$ curl "
+ "'http://localhost:8080/?foo=bar' -i -X POST "
+ "-H 'Accept: application/json' -d 'a=alpha'"))));
}
@Test
@@ -180,8 +178,7 @@ public class MockMvcRestDocumentationIntegrationTests {
.apply(documentationConfiguration(this.restDocumentation)).build();
mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(document("links",
.andExpect(status().isOk()).andDo(document("links",
links(linkWithRel("rel").description("The description"))));
assertExpectedSnippetFilesExist(new File("build/generated-snippets/links"),
@@ -195,9 +192,8 @@ public class MockMvcRestDocumentationIntegrationTests {
.apply(documentationConfiguration(this.restDocumentation)).build();
mockMvc.perform(get("{foo}", "/").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(document("links", pathParameters(parameterWithName("foo")
.description("The description"))));
.andExpect(status().isOk()).andDo(document("links", pathParameters(
parameterWithName("foo").description("The description"))));
assertExpectedSnippetFilesExist(new File("build/generated-snippets/links"),
"http-request.adoc", "http-response.adoc", "curl-request.adoc",
@@ -210,9 +206,8 @@ public class MockMvcRestDocumentationIntegrationTests {
.apply(documentationConfiguration(this.restDocumentation)).build();
mockMvc.perform(get("/").param("foo", "bar").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(document("links", requestParameters(parameterWithName("foo")
.description("The description"))));
.andExpect(status().isOk()).andDo(document("links", requestParameters(
parameterWithName("foo").description("The description"))));
assertExpectedSnippetFilesExist(new File("build/generated-snippets/links"),
"http-request.adoc", "http-response.adoc", "curl-request.adoc",
@@ -224,12 +219,10 @@ public class MockMvcRestDocumentationIntegrationTests {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
.apply(documentationConfiguration(this.restDocumentation)).build();
mockMvc.perform(
get("/").param("foo", "bar").content("{\"a\":\"alpha\"}")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(document("links",
requestFields(fieldWithPath("a").description("The description"))));
mockMvc.perform(get("/").param("foo", "bar").content("{\"a\":\"alpha\"}")
.accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
.andDo(document("links", requestFields(
fieldWithPath("a").description("The description"))));
assertExpectedSnippetFilesExist(new File("build/generated-snippets/links"),
"http-request.adoc", "http-response.adoc", "curl-request.adoc",
@@ -243,12 +236,10 @@ public class MockMvcRestDocumentationIntegrationTests {
mockMvc.perform(get("/").param("foo", "bar").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(document(
"links",
responseFields(
fieldWithPath("a").description("The description"),
fieldWithPath("links").description(
"Links to other resources"))));
.andDo(document("links",
responseFields(fieldWithPath("a")
.description("The description"),
fieldWithPath("links").description("Links to other resources"))));
assertExpectedSnippetFilesExist(new File("build/generated-snippets/links"),
"http-request.adoc", "http-response.adoc", "curl-request.adoc",
@@ -262,8 +253,8 @@ public class MockMvcRestDocumentationIntegrationTests {
mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk()).andDo(document("{method-name}"));
assertExpectedSnippetFilesExist(new File(
"build/generated-snippets/parameterized-output-directory"),
assertExpectedSnippetFilesExist(
new File("build/generated-snippets/parameterized-output-directory"),
"http-request.adoc", "http-response.adoc", "curl-request.adoc");
}
@@ -273,20 +264,20 @@ public class MockMvcRestDocumentationIntegrationTests {
.apply(documentationConfiguration(this.restDocumentation))
.alwaysDo(document("{method-name}-{step}")).build();
mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON)).andExpect(
status().isOk());
mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
assertExpectedSnippetFilesExist(
new File("build/generated-snippets/multi-step-1/"), "http-request.adoc",
"http-response.adoc", "curl-request.adoc");
mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON)).andExpect(
status().isOk());
mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
assertExpectedSnippetFilesExist(
new File("build/generated-snippets/multi-step-2/"), "http-request.adoc",
"http-response.adoc", "curl-request.adoc");
mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON)).andExpect(
status().isOk());
mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
assertExpectedSnippetFilesExist(
new File("build/generated-snippets/multi-step-3/"), "http-request.adoc",
"http-response.adoc", "curl-request.adoc");
@@ -299,36 +290,34 @@ public class MockMvcRestDocumentationIntegrationTests {
Pattern pattern = Pattern.compile("(\"alpha\")");
mockMvc.perform(
get("/").header("a", "alpha").header("b", "bravo")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON).content("{\"a\":\"alpha\"}"))
.andExpect(status().isOk())
.andDo(document("original-request"))
.andDo(document(
"preprocessed-request",
preprocessRequest(
prettyPrint(),
mockMvc.perform(get("/").header("a", "alpha").header("b", "bravo")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON).content("{\"a\":\"alpha\"}"))
.andExpect(status().isOk()).andDo(document("original-request"))
.andDo(document("preprocessed-request",
preprocessRequest(prettyPrint(),
removeHeaders("a", HttpHeaders.HOST,
HttpHeaders.CONTENT_LENGTH),
replacePattern(pattern, "\"<<beta>>\""))));
assertThat(
new File("build/generated-snippets/original-request/http-request.adoc"),
is(snippet(asciidoctor()).withContents(
httpRequest(asciidoctor(), RequestMethod.GET, "/")
.header("a", "alpha").header("b", "bravo")
.header("Content-Type", "application/json")
.header("Accept", MediaType.APPLICATION_JSON_VALUE)
.header("Host", "localhost")
.header("Content-Length", "13")
.content("{\"a\":\"alpha\"}"))));
is(snippet(asciidoctor())
.withContents(
httpRequest(asciidoctor(), RequestMethod.GET, "/")
.header("a", "alpha").header("b", "bravo")
.header("Content-Type", "application/json")
.header("Accept",
MediaType.APPLICATION_JSON_VALUE)
.header("Host", "localhost")
.header("Content-Length", "13")
.content("{\"a\":\"alpha\"}"))));
String prettyPrinted = String.format("{%n \"a\" : \"<<beta>>\"%n}");
assertThat(
new File(
"build/generated-snippets/preprocessed-request/http-request.adoc"),
is(snippet(asciidoctor()).withContents(
httpRequest(asciidoctor(), RequestMethod.GET, "/")
is(snippet(asciidoctor())
.withContents(httpRequest(asciidoctor(), RequestMethod.GET, "/")
.header("b", "bravo")
.header("Content-Type", "application/json")
.header("Accept", MediaType.APPLICATION_JSON_VALUE)
@@ -343,31 +332,30 @@ public class MockMvcRestDocumentationIntegrationTests {
Pattern pattern = Pattern.compile("(\"alpha\")");
mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(document("original-response"))
.andDo(document(
"preprocessed-response",
preprocessResponse(prettyPrint(), maskLinks(),
removeHeaders("a"),
.andExpect(status().isOk()).andDo(document("original-response"))
.andDo(document("preprocessed-response",
preprocessResponse(prettyPrint(), maskLinks(), removeHeaders("a"),
replacePattern(pattern, "\"<<beta>>\""))));
String original = "{\"a\":\"alpha\",\"links\":[{\"rel\":\"rel\","
+ "\"href\":\"href\"}]}";
assertThat(
new File("build/generated-snippets/original-response/http-response.adoc"),
is(snippet(asciidoctor()).withContents(
httpResponse(asciidoctor(), HttpStatus.OK)
.header("a", "alpha")
.header("Content-Type", "application/json")
.header(HttpHeaders.CONTENT_LENGTH,
original.getBytes().length).content(original))));
is(snippet(asciidoctor())
.withContents(
httpResponse(asciidoctor(), HttpStatus.OK)
.header("a", "alpha")
.header("Content-Type", "application/json")
.header(HttpHeaders.CONTENT_LENGTH,
original.getBytes().length)
.content(original))));
String prettyPrinted = String.format("{%n \"a\" : \"<<beta>>\",%n \"links\" : "
+ "[ {%n \"rel\" : \"rel\",%n \"href\" : \"...\"%n } ]%n}");
assertThat(
new File(
"build/generated-snippets/preprocessed-response/http-response.adoc"),
is(snippet(asciidoctor()).withContents(
httpResponse(asciidoctor(), HttpStatus.OK)
is(snippet(asciidoctor())
.withContents(httpResponse(asciidoctor(), HttpStatus.OK)
.header("Content-Type", "application/json")
.header(HttpHeaders.CONTENT_LENGTH,
prettyPrinted.getBytes().length)
@@ -379,8 +367,8 @@ public class MockMvcRestDocumentationIntegrationTests {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
.apply(documentationConfiguration(this.restDocumentation)).build();
ClassLoader classLoader = new URLClassLoader(new URL[] { new File(
"src/test/resources/custom-snippet-templates").toURI().toURL() },
ClassLoader classLoader = new URLClassLoader(new URL[] {
new File("src/test/resources/custom-snippet-templates").toURI().toURL() },
getClass().getClassLoader());
ClassLoader previous = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(classLoader);
@@ -392,15 +380,13 @@ public class MockMvcRestDocumentationIntegrationTests {
finally {
Thread.currentThread().setContextClassLoader(previous);
}
assertThat(new File(
"build/generated-snippets/custom-snippet-template/curl-request.adoc"),
assertThat(
new File(
"build/generated-snippets/custom-snippet-template/curl-request.adoc"),
is(snippet(asciidoctor()).withContents(equalTo("Custom curl request"))));
mockMvc.perform(get("/")).andDo(
document(
"index",
curlRequest(attributes(key("title").value(
"Access the index using curl")))));
mockMvc.perform(get("/")).andDo(document("index", curlRequest(
attributes(key("title").value("Access the index using curl")))));
}
@Test
@@ -412,12 +398,11 @@ public class MockMvcRestDocumentationIntegrationTests {
get("/custom/").contextPath("/custom").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk()).andDo(document("custom-context-path"));
assertThat(
new File("build/generated-snippets/custom-context-path/curl-request.adoc"),
new File(
"build/generated-snippets/custom-context-path/curl-request.adoc"),
is(snippet(asciidoctor())
.withContents(
codeBlock(asciidoctor(), "bash")
.content(
"$ curl 'http://localhost:8080/custom/' -i -H 'Accept: application/json'"))));
.withContents(codeBlock(asciidoctor(), "bash").content(
"$ curl 'http://localhost:8080/custom/' -i -H 'Accept: application/json'"))));
}
private void assertExpectedSnippetFilesExist(File directory, String... snippets) {

View File

@@ -21,6 +21,7 @@ import java.net.URI;
import javax.servlet.ServletContext;
import org.junit.Test;
import org.springframework.http.HttpMethod;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockServletContext;
@@ -144,8 +145,8 @@ public class RestDocumentationRequestBuildersTests {
HttpMethod httpMethod) {
MockHttpServletRequest request = builder.buildRequest(this.servletContext);
assertThat(
(String) request
.getAttribute(RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE),
(String) request.getAttribute(
RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE),
is(equalTo("{template}")));
assertThat(request.getRequestURI(), is(equalTo("t")));
assertThat(request.getMethod(), is(equalTo(httpMethod.name())));