Merge branch '1.2.x'

This commit is contained in:
Andy Wilkinson
2019-08-30 11:20:34 +01:00
192 changed files with 3407 additions and 5493 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2018 the original author or authors.
* Copyright 2014-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -51,16 +51,14 @@ public class MockMvcRequestConverterTests {
@Test
public void httpRequest() throws Exception {
OperationRequest request = createOperationRequest(
MockMvcRequestBuilders.get("/foo"));
OperationRequest request = createOperationRequest(MockMvcRequestBuilders.get("/foo"));
assertThat(request.getUri()).isEqualTo(URI.create("http://localhost/foo"));
assertThat(request.getMethod()).isEqualTo(HttpMethod.GET);
}
@Test
public void httpRequestWithCustomPort() throws Exception {
MockHttpServletRequest mockRequest = MockMvcRequestBuilders.get("/foo")
.buildRequest(new MockServletContext());
MockHttpServletRequest mockRequest = MockMvcRequestBuilders.get("/foo").buildRequest(new MockServletContext());
mockRequest.setServerPort(8080);
OperationRequest request = this.factory.convert(mockRequest);
assertThat(request.getUri()).isEqualTo(URI.create("http://localhost:8080/foo"));
@@ -69,28 +67,25 @@ 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()).isEqualTo(URI.create("http://localhost/foo/bar"));
assertThat(request.getMethod()).isEqualTo(HttpMethod.GET);
}
@Test
public void requestWithHeaders() throws Exception {
OperationRequest request = createOperationRequest(MockMvcRequestBuilders
.get("/foo").header("a", "alpha", "apple").header("b", "bravo"));
OperationRequest request = createOperationRequest(
MockMvcRequestBuilders.get("/foo").header("a", "alpha", "apple").header("b", "bravo"));
assertThat(request.getUri()).isEqualTo(URI.create("http://localhost/foo"));
assertThat(request.getMethod()).isEqualTo(HttpMethod.GET);
assertThat(request.getHeaders()).containsEntry("a",
Arrays.asList("alpha", "apple"));
assertThat(request.getHeaders()).containsEntry("a", Arrays.asList("alpha", "apple"));
assertThat(request.getHeaders()).containsEntry("b", Arrays.asList("bravo"));
}
@Test
public void requestWithCookies() throws Exception {
OperationRequest request = createOperationRequest(
MockMvcRequestBuilders.get("/foo").cookie(
new javax.servlet.http.Cookie("cookieName1", "cookieVal1"),
MockMvcRequestBuilders.get("/foo").cookie(new javax.servlet.http.Cookie("cookieName1", "cookieVal1"),
new javax.servlet.http.Cookie("cookieName2", "cookieVal2")));
assertThat(request.getUri()).isEqualTo(URI.create("http://localhost/foo"));
assertThat(request.getMethod()).isEqualTo(HttpMethod.GET);
@@ -109,8 +104,7 @@ public class MockMvcRequestConverterTests {
@Test
public void httpsRequest() throws Exception {
MockHttpServletRequest mockRequest = MockMvcRequestBuilders.get("/foo")
.buildRequest(new MockServletContext());
MockHttpServletRequest mockRequest = MockMvcRequestBuilders.get("/foo").buildRequest(new MockServletContext());
mockRequest.setScheme("https");
mockRequest.setServerPort(443);
OperationRequest request = this.factory.convert(mockRequest);
@@ -120,8 +114,7 @@ public class MockMvcRequestConverterTests {
@Test
public void httpsRequestWithCustomPort() throws Exception {
MockHttpServletRequest mockRequest = MockMvcRequestBuilders.get("/foo")
.buildRequest(new MockServletContext());
MockHttpServletRequest mockRequest = MockMvcRequestBuilders.get("/foo").buildRequest(new MockServletContext());
mockRequest.setScheme("https");
mockRequest.setServerPort(8443);
OperationRequest request = this.factory.convert(mockRequest);
@@ -131,23 +124,19 @@ public class MockMvcRequestConverterTests {
@Test
public void getRequestWithParametersProducesUriWithQueryString() throws Exception {
OperationRequest request = createOperationRequest(MockMvcRequestBuilders
.get("/foo").param("a", "alpha", "apple").param("b", "br&vo"));
assertThat(request.getUri())
.isEqualTo(URI.create("http://localhost/foo?a=alpha&a=apple&b=br%26vo"));
OperationRequest request = createOperationRequest(
MockMvcRequestBuilders.get("/foo").param("a", "alpha", "apple").param("b", "br&vo"));
assertThat(request.getUri()).isEqualTo(URI.create("http://localhost/foo?a=alpha&a=apple&b=br%26vo"));
assertThat(request.getParameters().size()).isEqualTo(2);
assertThat(request.getParameters()).containsEntry("a",
Arrays.asList("alpha", "apple"));
assertThat(request.getParameters()).containsEntry("a", Arrays.asList("alpha", "apple"));
assertThat(request.getParameters()).containsEntry("b", Arrays.asList("br&vo"));
assertThat(request.getMethod()).isEqualTo(HttpMethod.GET);
}
@Test
public void getRequestWithQueryStringPopulatesParameters() throws Exception {
OperationRequest request = createOperationRequest(
MockMvcRequestBuilders.get("/foo?a=alpha&b=bravo"));
assertThat(request.getUri())
.isEqualTo(URI.create("http://localhost/foo?a=alpha&b=bravo"));
OperationRequest request = createOperationRequest(MockMvcRequestBuilders.get("/foo?a=alpha&b=bravo"));
assertThat(request.getUri()).isEqualTo(URI.create("http://localhost/foo?a=alpha&b=bravo"));
assertThat(request.getParameters().size()).isEqualTo(2);
assertThat(request.getParameters()).containsEntry("a", Arrays.asList("alpha"));
assertThat(request.getParameters()).containsEntry("b", Arrays.asList("bravo"));
@@ -156,21 +145,19 @@ public class MockMvcRequestConverterTests {
@Test
public void postRequestWithParameters() throws Exception {
OperationRequest request = createOperationRequest(MockMvcRequestBuilders
.post("/foo").param("a", "alpha", "apple").param("b", "br&vo"));
OperationRequest request = createOperationRequest(
MockMvcRequestBuilders.post("/foo").param("a", "alpha", "apple").param("b", "br&vo"));
assertThat(request.getUri()).isEqualTo(URI.create("http://localhost/foo"));
assertThat(request.getMethod()).isEqualTo(HttpMethod.POST);
assertThat(request.getParameters().size()).isEqualTo(2);
assertThat(request.getParameters()).containsEntry("a",
Arrays.asList("alpha", "apple"));
assertThat(request.getParameters()).containsEntry("a", Arrays.asList("alpha", "apple"));
assertThat(request.getParameters()).containsEntry("b", Arrays.asList("br&vo"));
}
@Test
public void mockMultipartFileUpload() throws Exception {
OperationRequest request = createOperationRequest(
MockMvcRequestBuilders.multipart("/foo")
.file(new MockMultipartFile("file", new byte[] { 1, 2, 3, 4 })));
OperationRequest request = createOperationRequest(MockMvcRequestBuilders.multipart("/foo")
.file(new MockMultipartFile("file", new byte[] { 1, 2, 3, 4 })));
assertThat(request.getUri()).isEqualTo(URI.create("http://localhost/foo"));
assertThat(request.getMethod()).isEqualTo(HttpMethod.POST);
assertThat(request.getParts().size()).isEqualTo(1);
@@ -184,9 +171,8 @@ public class MockMvcRequestConverterTests {
@Test
public void mockMultipartFileUploadWithContentType() throws Exception {
OperationRequest request = createOperationRequest(
MockMvcRequestBuilders.multipart("/foo").file(new MockMultipartFile(
"file", "original", "image/png", new byte[] { 1, 2, 3, 4 })));
OperationRequest request = createOperationRequest(MockMvcRequestBuilders.multipart("/foo")
.file(new MockMultipartFile("file", "original", "image/png", new byte[] { 1, 2, 3, 4 })));
assertThat(request.getUri()).isEqualTo(URI.create("http://localhost/foo"));
assertThat(request.getMethod()).isEqualTo(HttpMethod.POST);
assertThat(request.getParts().size()).isEqualTo(1);
@@ -199,14 +185,12 @@ public class MockMvcRequestConverterTests {
@Test
public void requestWithPart() throws Exception {
MockHttpServletRequest mockRequest = MockMvcRequestBuilders.get("/foo")
.buildRequest(new MockServletContext());
MockHttpServletRequest mockRequest = MockMvcRequestBuilders.get("/foo").buildRequest(new MockServletContext());
Part mockPart = mock(Part.class);
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);
@@ -223,14 +207,12 @@ public class MockMvcRequestConverterTests {
@Test
public void requestWithPartWithContentType() throws Exception {
MockHttpServletRequest mockRequest = MockMvcRequestBuilders.get("/foo")
.buildRequest(new MockServletContext());
MockHttpServletRequest mockRequest = MockMvcRequestBuilders.get("/foo").buildRequest(new MockServletContext());
Part mockPart = mock(Part.class);
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");
@@ -246,8 +228,7 @@ public class MockMvcRequestConverterTests {
assertThat(part.getContent()).isEqualTo(new byte[] { 1, 2, 3, 4 });
}
private OperationRequest createOperationRequest(MockHttpServletRequestBuilder builder)
throws Exception {
private OperationRequest createOperationRequest(MockHttpServletRequestBuilder builder) throws Exception {
return this.factory.convert(builder.buildRequest(new MockServletContext()));
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2018 the original author or authors.
* Copyright 2014-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -45,44 +45,40 @@ public class MockMvcRestDocumentationConfigurerTests {
@Test
public void defaultConfiguration() {
RequestPostProcessor postProcessor = new MockMvcRestDocumentationConfigurer(
this.restDocumentation).beforeMockMvcCreated(null, null);
RequestPostProcessor postProcessor = new MockMvcRestDocumentationConfigurer(this.restDocumentation)
.beforeMockMvcCreated(null, null);
postProcessor.postProcessRequest(this.request);
assertUriConfiguration("http", "localhost", 8080);
}
@Test
public void customScheme() {
RequestPostProcessor postProcessor = new MockMvcRestDocumentationConfigurer(
this.restDocumentation).uris().withScheme("https")
.beforeMockMvcCreated(null, null);
RequestPostProcessor postProcessor = new MockMvcRestDocumentationConfigurer(this.restDocumentation).uris()
.withScheme("https").beforeMockMvcCreated(null, null);
postProcessor.postProcessRequest(this.request);
assertUriConfiguration("https", "localhost", 8080);
}
@Test
public void customHost() {
RequestPostProcessor postProcessor = new MockMvcRestDocumentationConfigurer(
this.restDocumentation).uris().withHost("api.example.com")
.beforeMockMvcCreated(null, null);
RequestPostProcessor postProcessor = new MockMvcRestDocumentationConfigurer(this.restDocumentation).uris()
.withHost("api.example.com").beforeMockMvcCreated(null, null);
postProcessor.postProcessRequest(this.request);
assertUriConfiguration("http", "api.example.com", 8080);
}
@Test
public void customPort() {
RequestPostProcessor postProcessor = new MockMvcRestDocumentationConfigurer(
this.restDocumentation).uris().withPort(8081).beforeMockMvcCreated(null,
null);
RequestPostProcessor postProcessor = new MockMvcRestDocumentationConfigurer(this.restDocumentation).uris()
.withPort(8081).beforeMockMvcCreated(null, null);
postProcessor.postProcessRequest(this.request);
assertUriConfiguration("http", "localhost", 8081);
}
@Test
public void noContentLengthHeaderWhenRequestHasNotContent() {
RequestPostProcessor postProcessor = new MockMvcRestDocumentationConfigurer(
this.restDocumentation).uris().withPort(8081).beforeMockMvcCreated(null,
null);
RequestPostProcessor postProcessor = new MockMvcRestDocumentationConfigurer(this.restDocumentation).uris()
.withPort(8081).beforeMockMvcCreated(null, null);
postProcessor.postProcessRequest(this.request);
assertThat(this.request.getHeader("Content-Length")).isNull();
}
@@ -91,8 +87,7 @@ public class MockMvcRestDocumentationConfigurerTests {
assertThat(scheme).isEqualTo(this.request.getScheme());
assertThat(host).isEqualTo(this.request.getServerName());
assertThat(port).isEqualTo(this.request.getServerPort());
RequestContextHolder
.setRequestAttributes(new ServletRequestAttributes(this.request));
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(this.request));
try {
URI uri = BasicLinkBuilder.linkToCurrentMapping().toUri();
assertThat(scheme).isEqualTo(uri.getScheme());

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2018 the original author or authors.
* Copyright 2014-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -128,26 +128,23 @@ public class MockMvcRestDocumentationIntegrationTests {
@Test
public void basicSnippetGeneration() throws Exception {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
.apply(new MockMvcRestDocumentationConfigurer(this.restDocumentation)
.snippets().withEncoding("UTF-8"))
.apply(new MockMvcRestDocumentationConfigurer(this.restDocumentation).snippets().withEncoding("UTF-8"))
.build();
mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk()).andDo(document("basic"));
assertExpectedSnippetFilesExist(new File("build/generated-snippets/basic"),
"http-request.adoc", "http-response.adoc", "curl-request.adoc");
mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
.andDo(document("basic"));
assertExpectedSnippetFilesExist(new File("build/generated-snippets/basic"), "http-request.adoc",
"http-response.adoc", "curl-request.adoc");
}
@Test
public void markdownSnippetGeneration() throws Exception {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
.apply(new MockMvcRestDocumentationConfigurer(this.restDocumentation)
.snippets().withEncoding("UTF-8")
.apply(new MockMvcRestDocumentationConfigurer(this.restDocumentation).snippets().withEncoding("UTF-8")
.withTemplateFormat(TemplateFormats.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",
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",
"http-response.md", "curl-request.md");
}
@@ -155,186 +152,147 @@ public class MockMvcRestDocumentationIntegrationTests {
public void curlSnippetWithContent() throws Exception {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
.apply(documentationConfiguration(this.restDocumentation)).build();
mockMvc.perform(post("/").accept(MediaType.APPLICATION_JSON).content("content"))
.andExpect(status().isOk()).andDo(document("curl-snippet-with-content"));
assertThat(new File(
"build/generated-snippets/curl-snippet-with-content/curl-request.adoc"))
.has(content(codeBlock(TemplateFormats.asciidoctor(), "bash")
.withContent(String.format(
"$ curl 'http://localhost:8080/' -i -X POST \\%n"
+ " -H 'Accept: application/json' \\%n"
+ " -d 'content'"))));
mockMvc.perform(post("/").accept(MediaType.APPLICATION_JSON).content("content")).andExpect(status().isOk())
.andDo(document("curl-snippet-with-content"));
assertThat(new File("build/generated-snippets/curl-snippet-with-content/curl-request.adoc"))
.has(content(codeBlock(TemplateFormats.asciidoctor(), "bash")
.withContent(String.format("$ curl 'http://localhost:8080/' -i -X POST \\%n"
+ " -H 'Accept: application/json' \\%n" + " -d 'content'"))));
}
@Test
public void curlSnippetWithCookies() throws Exception {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
.apply(documentationConfiguration(this.restDocumentation)).build();
mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON)
.cookie(new Cookie("cookieName", "cookieVal"))).andExpect(status().isOk())
.andDo(document("curl-snippet-with-cookies"));
assertThat(new File(
"build/generated-snippets/curl-snippet-with-cookies/curl-request.adoc"))
.has(content(codeBlock(TemplateFormats.asciidoctor(), "bash")
.withContent(String.format(
"$ curl 'http://localhost:8080/' -i -X GET \\%n"
+ " -H 'Accept: application/json' \\%n"
+ " --cookie 'cookieName=cookieVal'"))));
mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON).cookie(new Cookie("cookieName", "cookieVal")))
.andExpect(status().isOk()).andDo(document("curl-snippet-with-cookies"));
assertThat(new File("build/generated-snippets/curl-snippet-with-cookies/curl-request.adoc"))
.has(content(codeBlock(TemplateFormats.asciidoctor(), "bash")
.withContent(String.format("$ curl 'http://localhost:8080/' -i -X GET \\%n"
+ " -H 'Accept: application/json' \\%n" + " --cookie 'cookieName=cookieVal'"))));
}
@Test
public void curlSnippetWithQueryStringOnPost() throws Exception {
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())
.andDo(document("curl-snippet-with-query-string"));
assertThat(new File(
"build/generated-snippets/curl-snippet-with-query-string/curl-request.adoc"))
.has(content(codeBlock(TemplateFormats.asciidoctor(), "bash")
.withContent(String.format("$ curl "
+ "'http://localhost:8080/?foo=bar' -i -X POST \\%n"
+ " -H 'Accept: application/json' \\%n"
+ " -d 'a=alpha'"))));
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"))
.has(content(codeBlock(TemplateFormats.asciidoctor(), "bash")
.withContent(String.format("$ curl " + "'http://localhost:8080/?foo=bar' -i -X POST \\%n"
+ " -H 'Accept: application/json' \\%n" + " -d 'a=alpha'"))));
}
@Test
public void curlSnippetWithContentAndParametersOnPost() throws Exception {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
.apply(documentationConfiguration(this.restDocumentation)).build();
mockMvc.perform(post("/").param("a", "alpha").accept(MediaType.APPLICATION_JSON)
.content("some content")).andExpect(status().isOk())
.andDo(document("curl-snippet-with-content-and-parameters"));
assertThat(new File(
"build/generated-snippets/curl-snippet-with-content-and-parameters/curl-request.adoc"))
.has(content(codeBlock(TemplateFormats.asciidoctor(), "bash")
.withContent(String.format(
"$ curl 'http://localhost:8080/?a=alpha' -i -X POST \\%n"
+ " -H 'Accept: application/json' \\%n"
+ " -d 'some content'"))));
mockMvc.perform(post("/").param("a", "alpha").accept(MediaType.APPLICATION_JSON).content("some content"))
.andExpect(status().isOk()).andDo(document("curl-snippet-with-content-and-parameters"));
assertThat(new File("build/generated-snippets/curl-snippet-with-content-and-parameters/curl-request.adoc"))
.has(content(codeBlock(TemplateFormats.asciidoctor(), "bash")
.withContent(String.format("$ curl 'http://localhost:8080/?a=alpha' -i -X POST \\%n"
+ " -H 'Accept: application/json' \\%n" + " -d 'some content'"))));
}
@Test
public void httpieSnippetWithContent() throws Exception {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
.apply(documentationConfiguration(this.restDocumentation)).build();
mockMvc.perform(post("/").accept(MediaType.APPLICATION_JSON).content("content"))
.andExpect(status().isOk())
mockMvc.perform(post("/").accept(MediaType.APPLICATION_JSON).content("content")).andExpect(status().isOk())
.andDo(document("httpie-snippet-with-content"));
assertThat(new File(
"build/generated-snippets/httpie-snippet-with-content/httpie-request.adoc"))
.has(content(codeBlock(TemplateFormats.asciidoctor(), "bash")
.withContent(String.format("$ echo 'content' | "
+ "http POST 'http://localhost:8080/' \\%n"
+ " 'Accept:application/json'"))));
assertThat(new File("build/generated-snippets/httpie-snippet-with-content/httpie-request.adoc")).has(
content(codeBlock(TemplateFormats.asciidoctor(), "bash").withContent(String.format("$ echo 'content' | "
+ "http POST 'http://localhost:8080/' \\%n" + " 'Accept:application/json'"))));
}
@Test
public void httpieSnippetWithCookies() throws Exception {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
.apply(documentationConfiguration(this.restDocumentation)).build();
mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON)
.cookie(new Cookie("cookieName", "cookieVal"))).andExpect(status().isOk())
.andDo(document("httpie-snippet-with-cookies"));
assertThat(new File(
"build/generated-snippets/httpie-snippet-with-cookies/httpie-request.adoc"))
.has(content(codeBlock(TemplateFormats.asciidoctor(), "bash")
.withContent(String
.format("$ http GET 'http://localhost:8080/' \\%n"
+ " 'Accept:application/json' \\%n"
+ " 'Cookie:cookieName=cookieVal'"))));
mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON).cookie(new Cookie("cookieName", "cookieVal")))
.andExpect(status().isOk()).andDo(document("httpie-snippet-with-cookies"));
assertThat(new File("build/generated-snippets/httpie-snippet-with-cookies/httpie-request.adoc"))
.has(content(codeBlock(TemplateFormats.asciidoctor(), "bash")
.withContent(String.format("$ http GET 'http://localhost:8080/' \\%n"
+ " 'Accept:application/json' \\%n" + " 'Cookie:cookieName=cookieVal'"))));
}
@Test
public void httpieSnippetWithQueryStringOnPost() throws Exception {
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())
.andDo(document("httpie-snippet-with-query-string"));
assertThat(new File(
"build/generated-snippets/httpie-snippet-with-query-string/httpie-request.adoc"))
.has(content(codeBlock(TemplateFormats.asciidoctor(), "bash")
.withContent(String.format("$ http "
+ "--form POST 'http://localhost:8080/?foo=bar' \\%n"
+ " 'Accept:application/json' \\%n 'a=alpha'"))));
mockMvc.perform(post("/?foo=bar").param("foo", "bar").param("a", "alpha").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk()).andDo(document("httpie-snippet-with-query-string"));
assertThat(new File("build/generated-snippets/httpie-snippet-with-query-string/httpie-request.adoc"))
.has(content(codeBlock(TemplateFormats.asciidoctor(), "bash")
.withContent(String.format("$ http " + "--form POST 'http://localhost:8080/?foo=bar' \\%n"
+ " 'Accept:application/json' \\%n 'a=alpha'"))));
}
@Test
public void httpieSnippetWithContentAndParametersOnPost() throws Exception {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
.apply(documentationConfiguration(this.restDocumentation)).build();
mockMvc.perform(post("/").param("a", "alpha").content("some content")
.accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
.andDo(document("httpie-snippet-post-with-content-and-parameters"));
mockMvc.perform(post("/").param("a", "alpha").content("some content").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk()).andDo(document("httpie-snippet-post-with-content-and-parameters"));
assertThat(new File(
"build/generated-snippets/httpie-snippet-post-with-content-and-parameters/httpie-request.adoc"))
.has(content(codeBlock(TemplateFormats.asciidoctor(), "bash")
.withContent(String
.format("$ echo " + "'some content' | http POST "
+ "'http://localhost:8080/?a=alpha' \\%n"
+ " 'Accept:application/json'"))));
.withContent(String.format("$ echo " + "'some content' | http POST "
+ "'http://localhost:8080/?a=alpha' \\%n" + " 'Accept:application/json'"))));
}
@Test
public void linksSnippet() throws Exception {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
.apply(documentationConfiguration(this.restDocumentation)).build();
mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk()).andDo(document("links",
links(linkWithRel("rel").description("The description"))));
mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
.andDo(document("links", links(linkWithRel("rel").description("The description"))));
assertExpectedSnippetFilesExist(new File("build/generated-snippets/links"),
"http-request.adoc", "http-response.adoc", "curl-request.adoc",
"links.adoc");
assertExpectedSnippetFilesExist(new File("build/generated-snippets/links"), "http-request.adoc",
"http-response.adoc", "curl-request.adoc", "links.adoc");
}
@Test
public void pathParametersSnippet() throws Exception {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
.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"))));
assertExpectedSnippetFilesExist(new File("build/generated-snippets/links"),
"http-request.adoc", "http-response.adoc", "curl-request.adoc",
"path-parameters.adoc");
mockMvc.perform(get("{foo}", "/").accept(MediaType.APPLICATION_JSON)).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", "path-parameters.adoc");
}
@Test
public void requestParametersSnippet() throws Exception {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
.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"))));
assertExpectedSnippetFilesExist(new File("build/generated-snippets/links"),
"http-request.adoc", "http-response.adoc", "curl-request.adoc",
"request-parameters.adoc");
mockMvc.perform(get("/").param("foo", "bar").accept(MediaType.APPLICATION_JSON)).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", "request-parameters.adoc");
}
@Test
public void requestFieldsSnippet() throws Exception {
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"))));
assertExpectedSnippetFilesExist(new File("build/generated-snippets/links"),
"http-request.adoc", "http-response.adoc", "curl-request.adoc",
"request-fields.adoc");
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", "request-fields.adoc");
}
@Test
public void requestPartsSnippet() throws Exception {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
.apply(documentationConfiguration(this.restDocumentation)).build();
mockMvc.perform(multipart("/upload").file("foo", "bar".getBytes()))
.andExpect(status().isOk()).andDo(document("request-parts", requestParts(
partWithName("foo").description("The description"))));
assertExpectedSnippetFilesExist(
new File("build/generated-snippets/request-parts"), "http-request.adoc",
mockMvc.perform(multipart("/upload").file("foo", "bar".getBytes())).andExpect(status().isOk())
.andDo(document("request-parts", requestParts(partWithName("foo").description("The description"))));
assertExpectedSnippetFilesExist(new File("build/generated-snippets/request-parts"), "http-request.adoc",
"http-response.adoc", "curl-request.adoc", "request-parts.adoc");
}
@@ -342,62 +300,48 @@ public class MockMvcRestDocumentationIntegrationTests {
public void responseFieldsSnippet() throws Exception {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
.apply(documentationConfiguration(this.restDocumentation)).build();
mockMvc.perform(get("/").param("foo", "bar").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(document("links",
responseFields(fieldWithPath("a").description("The description"),
subsectionWithPath("links")
.description("Links to other resources"))));
assertExpectedSnippetFilesExist(new File("build/generated-snippets/links"),
"http-request.adoc", "http-response.adoc", "curl-request.adoc",
"response-fields.adoc");
mockMvc.perform(get("/").param("foo", "bar").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
.andDo(document("links", responseFields(fieldWithPath("a").description("The description"),
subsectionWithPath("links").description("Links to other resources"))));
assertExpectedSnippetFilesExist(new File("build/generated-snippets/links"), "http-request.adoc",
"http-response.adoc", "curl-request.adoc", "response-fields.adoc");
}
@Test
public void responseWithSetCookie() throws Exception {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
.apply(documentationConfiguration(this.restDocumentation)).build();
mockMvc.perform(get("/set-cookie")).andExpect(status().isOk())
.andDo(document("set-cookie",
responseHeaders(headerWithName(HttpHeaders.SET_COOKIE)
.description("set-cookie"))));
mockMvc.perform(get("/set-cookie")).andExpect(status().isOk()).andDo(document("set-cookie",
responseHeaders(headerWithName(HttpHeaders.SET_COOKIE).description("set-cookie"))));
assertThat(new File("build/generated-snippets/set-cookie/http-response.adoc"))
.has(content(httpResponse(TemplateFormats.asciidoctor(), HttpStatus.OK)
.header(HttpHeaders.SET_COOKIE,
"name=value; Domain=localhost; HttpOnly")));
.has(content(httpResponse(TemplateFormats.asciidoctor(), HttpStatus.OK).header(HttpHeaders.SET_COOKIE,
"name=value; Domain=localhost; HttpOnly")));
}
@Test
public void parameterizedOutputDirectory() throws Exception {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
.apply(documentationConfiguration(this.restDocumentation)).build();
mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk()).andDo(document("{method-name}"));
assertExpectedSnippetFilesExist(
new File("build/generated-snippets/parameterized-output-directory"),
mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
.andDo(document("{method-name}"));
assertExpectedSnippetFilesExist(new File("build/generated-snippets/parameterized-output-directory"),
"http-request.adoc", "http-response.adoc", "curl-request.adoc");
}
@Test
public void multiStep() throws Exception {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
.apply(documentationConfiguration(this.restDocumentation))
.alwaysDo(document("{method-name}-{step}")).build();
mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
assertExpectedSnippetFilesExist(
new File("build/generated-snippets/multi-step-1/"), "http-request.adoc",
.apply(documentationConfiguration(this.restDocumentation)).alwaysDo(document("{method-name}-{step}"))
.build();
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());
assertExpectedSnippetFilesExist(
new File("build/generated-snippets/multi-step-2/"), "http-request.adoc",
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());
assertExpectedSnippetFilesExist(
new File("build/generated-snippets/multi-step-3/"), "http-request.adoc",
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");
}
@@ -405,16 +349,11 @@ public class MockMvcRestDocumentationIntegrationTests {
public void alwaysDoWithAdditionalSnippets() throws Exception {
RestDocumentationResultHandler documentation = document("{method-name}-{step}");
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
.apply(documentationConfiguration(this.restDocumentation))
.alwaysDo(documentation).build();
mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk()).andDo(documentation.document(
responseHeaders(headerWithName("a").description("one"))));
assertExpectedSnippetFilesExist(
new File(
"build/generated-snippets/always-do-with-additional-snippets-1/"),
"http-request.adoc", "http-response.adoc", "curl-request.adoc",
"response-headers.adoc");
.apply(documentationConfiguration(this.restDocumentation)).alwaysDo(documentation).build();
mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
.andDo(documentation.document(responseHeaders(headerWithName("a").description("one"))));
assertExpectedSnippetFilesExist(new File("build/generated-snippets/always-do-with-additional-snippets-1/"),
"http-request.adoc", "http-response.adoc", "curl-request.adoc", "response-headers.adoc");
}
@Test
@@ -423,76 +362,56 @@ public class MockMvcRestDocumentationIntegrationTests {
.apply(documentationConfiguration(this.restDocumentation)).build();
Pattern pattern = Pattern.compile("(\"alpha\")");
MvcResult result = mockMvc
.perform(get("/").header("a", "alpha").header("b", "bravo")
.contentType(MediaType.APPLICATION_JSON)
.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),
removeHeaders("a", HttpHeaders.HOST, HttpHeaders.CONTENT_LENGTH),
replacePattern(pattern, "\"<<beta>>\""))))
.andReturn();
HttpRequestCondition originalRequest = httpRequest(TemplateFormats.asciidoctor(),
RequestMethod.GET, "/");
for (String headerName : IterableEnumeration
.of(result.getRequest().getHeaderNames())) {
HttpRequestCondition originalRequest = httpRequest(TemplateFormats.asciidoctor(), RequestMethod.GET, "/");
for (String headerName : IterableEnumeration.of(result.getRequest().getHeaderNames())) {
originalRequest.header(headerName, result.getRequest().getHeader(headerName));
}
assertThat(
new File("build/generated-snippets/original-request/http-request.adoc"))
.has(content(originalRequest.header("Host", "localhost:8080")
.header("Content-Length", "13")
.content("{\"a\":\"alpha\"}")));
HttpRequestCondition preprocessedRequest = httpRequest(
TemplateFormats.asciidoctor(), RequestMethod.GET, "/");
List<String> removedHeaders = Arrays.asList("a", HttpHeaders.HOST,
HttpHeaders.CONTENT_LENGTH);
for (String headerName : IterableEnumeration
.of(result.getRequest().getHeaderNames())) {
assertThat(new File("build/generated-snippets/original-request/http-request.adoc")).has(content(originalRequest
.header("Host", "localhost:8080").header("Content-Length", "13").content("{\"a\":\"alpha\"}")));
HttpRequestCondition preprocessedRequest = httpRequest(TemplateFormats.asciidoctor(), RequestMethod.GET, "/");
List<String> removedHeaders = Arrays.asList("a", HttpHeaders.HOST, HttpHeaders.CONTENT_LENGTH);
for (String headerName : IterableEnumeration.of(result.getRequest().getHeaderNames())) {
if (!removedHeaders.contains(headerName)) {
preprocessedRequest.header(headerName,
result.getRequest().getHeader(headerName));
preprocessedRequest.header(headerName, result.getRequest().getHeader(headerName));
}
}
String prettyPrinted = String.format("{%n \"a\" : \"<<beta>>\"%n}");
assertThat(new File(
"build/generated-snippets/preprocessed-request/http-request.adoc"))
.has(content(preprocessedRequest.content(prettyPrinted)));
assertThat(new File("build/generated-snippets/preprocessed-request/http-request.adoc"))
.has(content(preprocessedRequest.content(prettyPrinted)));
}
@Test
public void defaultPreprocessedRequest() throws Exception {
Pattern pattern = Pattern.compile("(\"alpha\")");
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
.apply(documentationConfiguration(this.restDocumentation)
.operationPreprocessors().withRequestDefaults(prettyPrint(),
removeHeaders("a", HttpHeaders.HOST,
HttpHeaders.CONTENT_LENGTH),
replacePattern(pattern, "\"<<beta>>\"")))
.apply(documentationConfiguration(this.restDocumentation).operationPreprocessors().withRequestDefaults(
prettyPrint(), removeHeaders("a", HttpHeaders.HOST, HttpHeaders.CONTENT_LENGTH),
replacePattern(pattern, "\"<<beta>>\"")))
.build();
MvcResult result = mockMvc
.perform(get("/").header("a", "alpha").header("b", "bravo")
.contentType(MediaType.APPLICATION_JSON)
.perform(get("/").header("a", "alpha").header("b", "bravo").contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON).content("{\"a\":\"alpha\"}"))
.andDo(document("default-preprocessed-request")).andReturn();
HttpRequestCondition preprocessedRequest = httpRequest(
TemplateFormats.asciidoctor(), RequestMethod.GET, "/");
List<String> removedHeaders = Arrays.asList("a", HttpHeaders.HOST,
HttpHeaders.CONTENT_LENGTH);
for (String headerName : IterableEnumeration
.of(result.getRequest().getHeaderNames())) {
HttpRequestCondition preprocessedRequest = httpRequest(TemplateFormats.asciidoctor(), RequestMethod.GET, "/");
List<String> removedHeaders = Arrays.asList("a", HttpHeaders.HOST, HttpHeaders.CONTENT_LENGTH);
for (String headerName : IterableEnumeration.of(result.getRequest().getHeaderNames())) {
if (!removedHeaders.contains(headerName)) {
preprocessedRequest.header(headerName,
result.getRequest().getHeader(headerName));
preprocessedRequest.header(headerName, result.getRequest().getHeader(headerName));
}
}
String prettyPrinted = String.format("{%n \"a\" : \"<<beta>>\"%n}");
assertThat(new File(
"build/generated-snippets/default-preprocessed-request/http-request.adoc"))
.has(content(preprocessedRequest.content(prettyPrinted)));
assertThat(new File("build/generated-snippets/default-preprocessed-request/http-request.adoc"))
.has(content(preprocessedRequest.content(prettyPrinted)));
}
@Test
@@ -502,80 +421,61 @@ 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"),
replacePattern(pattern, "\"<<beta>>\""))));
mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON)).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")).has(
content(httpResponse(TemplateFormats.asciidoctor(), HttpStatus.OK)
.header("a", "alpha")
.header("Content-Type", "application/json;charset=UTF-8")
.header(HttpHeaders.CONTENT_LENGTH,
original.getBytes().length)
.content(original)));
String original = "{\"a\":\"alpha\",\"links\":[{\"rel\":\"rel\"," + "\"href\":\"href\"}]}";
assertThat(new File("build/generated-snippets/original-response/http-response.adoc"))
.has(content(httpResponse(TemplateFormats.asciidoctor(), HttpStatus.OK).header("a", "alpha")
.header("Content-Type", "application/json;charset=UTF-8")
.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")).has(
content(httpResponse(TemplateFormats.asciidoctor(), HttpStatus.OK)
.header("Content-Type", "application/json;charset=UTF-8")
.header(HttpHeaders.CONTENT_LENGTH,
prettyPrinted.getBytes().length)
.content(prettyPrinted)));
assertThat(new File("build/generated-snippets/preprocessed-response/http-response.adoc"))
.has(content(httpResponse(TemplateFormats.asciidoctor(), HttpStatus.OK)
.header("Content-Type", "application/json;charset=UTF-8")
.header(HttpHeaders.CONTENT_LENGTH, prettyPrinted.getBytes().length).content(prettyPrinted)));
}
@Test
public void defaultPreprocessedResponse() throws Exception {
Pattern pattern = Pattern.compile("(\"alpha\")");
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
.apply(documentationConfiguration(this.restDocumentation)
.operationPreprocessors().withResponseDefaults(prettyPrint(),
maskLinks(), removeHeaders("a"),
replacePattern(pattern, "\"<<beta>>\"")))
.apply(documentationConfiguration(this.restDocumentation).operationPreprocessors().withResponseDefaults(
prettyPrint(), maskLinks(), removeHeaders("a"), replacePattern(pattern, "\"<<beta>>\"")))
.build();
mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
.andDo(document("default-preprocessed-response"));
String prettyPrinted = String.format("{%n \"a\" : \"<<beta>>\",%n \"links\" : "
+ "[ {%n \"rel\" : \"rel\",%n \"href\" : \"...\"%n } ]%n}");
assertThat(new File(
"build/generated-snippets/default-preprocessed-response/http-response.adoc"))
.has(content(
httpResponse(TemplateFormats.asciidoctor(), HttpStatus.OK)
.header("Content-Type",
"application/json;charset=UTF-8")
.header(HttpHeaders.CONTENT_LENGTH,
prettyPrinted.getBytes().length)
.content(prettyPrinted)));
assertThat(new File("build/generated-snippets/default-preprocessed-response/http-response.adoc"))
.has(content(httpResponse(TemplateFormats.asciidoctor(), HttpStatus.OK)
.header("Content-Type", "application/json;charset=UTF-8")
.header(HttpHeaders.CONTENT_LENGTH, prettyPrinted.getBytes().length).content(prettyPrinted)));
}
@Test
public void customSnippetTemplate() throws Exception {
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);
try {
mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
.andDo(document("custom-snippet-template"));
}
finally {
Thread.currentThread().setContextClassLoader(previous);
}
assertThat(new File(
"build/generated-snippets/custom-snippet-template/curl-request.adoc"))
.hasContent("Custom curl request");
assertThat(new File("build/generated-snippets/custom-snippet-template/curl-request.adoc"))
.hasContent("Custom curl request");
}
@Test
@@ -583,24 +483,20 @@ public class MockMvcRestDocumentationIntegrationTests {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
.apply(documentationConfiguration(this.restDocumentation)).build();
mockMvc.perform(
get("/custom/").contextPath("/custom").accept(MediaType.APPLICATION_JSON))
mockMvc.perform(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"))
.has(content(codeBlock(TemplateFormats.asciidoctor(), "bash")
.withContent(String.format(
"$ curl 'http://localhost:8080/custom/' -i -X GET \\%n"
+ " -H 'Accept: application/json'"))));
assertThat(new File("build/generated-snippets/custom-context-path/curl-request.adoc"))
.has(content(codeBlock(TemplateFormats.asciidoctor(), "bash")
.withContent(String.format("$ curl 'http://localhost:8080/custom/' -i -X GET \\%n"
+ " -H 'Accept: application/json'"))));
}
@Test
public void multiPart() throws Exception {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
.apply(documentationConfiguration(this.restDocumentation)).build();
mockMvc.perform(multipart("/upload").file("test", "content".getBytes()))
.andExpect(status().isOk()).andDo(document("upload",
requestParts(partWithName("test").description("Foo"))));
mockMvc.perform(multipart("/upload").file("test", "content".getBytes())).andExpect(status().isOk())
.andDo(document("upload", requestParts(partWithName("test").description("Foo"))));
}
private void assertExpectedSnippetFilesExist(File directory, String... snippets) {
@@ -615,9 +511,8 @@ public class MockMvcRestDocumentationIntegrationTests {
@Override
public boolean matches(File value) {
try {
return delegate
.matches(FileCopyUtils.copyToString(new InputStreamReader(
new FileInputStream(value), StandardCharsets.UTF_8)));
return delegate.matches(FileCopyUtils
.copyToString(new InputStreamReader(new FileInputStream(value), StandardCharsets.UTF_8)));
}
catch (IOException ex) {
fail("Failed to read '" + value + "'", ex);
@@ -632,8 +527,7 @@ public class MockMvcRestDocumentationIntegrationTests {
return SnippetConditions.codeBlock(format, language);
}
private HttpRequestCondition httpRequest(TemplateFormat format,
RequestMethod requestMethod, String uri) {
private HttpRequestCondition httpRequest(TemplateFormat format, RequestMethod requestMethod, String uri) {
return SnippetConditions.httpRequest(format, requestMethod, uri);
}
@@ -649,7 +543,7 @@ public class MockMvcRestDocumentationIntegrationTests {
static class TestConfiguration {
@Bean
public TestController testController() {
TestController testController() {
return new TestController();
}
@@ -659,7 +553,7 @@ public class MockMvcRestDocumentationIntegrationTests {
private static class TestController {
@RequestMapping(value = "/", produces = "application/json;charset=UTF-8")
public ResponseEntity<Map<String, Object>> foo() {
ResponseEntity<Map<String, Object>> foo() {
Map<String, Object> response = new HashMap<>();
response.put("a", "alpha");
Map<String, String> link = new HashMap<>();
@@ -672,17 +566,17 @@ public class MockMvcRestDocumentationIntegrationTests {
}
@RequestMapping(value = "/company/5", produces = MediaType.APPLICATION_JSON_VALUE)
public String bar() {
String bar() {
return "{\"companyName\": \"FooBar\",\"employee\": [{\"name\": \"Lorem\",\"age\": \"42\"},{\"name\": \"Ipsum\",\"age\": \"24\"}]}";
}
@RequestMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public void upload() {
void upload() {
}
@RequestMapping("/set-cookie")
public void setCookie(HttpServletResponse response) {
void setCookie(HttpServletResponse response) {
Cookie cookie = new Cookie("name", "value");
cookie.setDomain("localhost");
cookie.setHttpOnly(true);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2018 the original author or authors.
* Copyright 2014-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -139,12 +139,10 @@ public class RestDocumentationRequestBuildersTests {
assertUri(fileUpload(URI.create("/uri")), HttpMethod.POST);
}
private void assertTemplate(MockHttpServletRequestBuilder builder,
HttpMethod httpMethod) {
private void assertTemplate(MockHttpServletRequestBuilder builder, HttpMethod httpMethod) {
MockHttpServletRequest request = builder.buildRequest(this.servletContext);
assertThat((String) request
.getAttribute(RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE))
.isEqualTo("{template}");
assertThat((String) request.getAttribute(RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE))
.isEqualTo("{template}");
assertThat(request.getRequestURI()).isEqualTo("t");
assertThat(request.getMethod()).isEqualTo(httpMethod.name());
}