|
|
|
|
@@ -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);
|
|
|
|
|
|