@Test public void curlSnippetWithCookies() throws Exception { this.webTestClient.get().uri("/").cookie("cookieName", "cookieVal") .accept(MediaType.APPLICATION_JSON).exchange().expectStatus().isOk() .expectBody().consumeWith(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 'https://api.example.com/' -i -X GET \\%n" + " -H 'Accept: application/json' \\%n" + " --cookie 'cookieName=cookieVal'")))); }
@Test public void httpieSnippetWithCookies() throws Exception { this.webTestClient.get().uri("/").cookie("cookieName", "cookieVal") .accept(MediaType.APPLICATION_JSON).exchange().expectStatus().isOk() .expectBody().consumeWith(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 'https://api.example.com/' \\%n" + " 'Accept:application/json' \\%n" + " 'Cookie:cookieName=cookieVal'")))); }
@Test public void responseWithSetCookie() throws Exception { this.webTestClient.get().uri("/set-cookie").exchange().expectStatus().isOk() .expectBody().consumeWith(document("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"))); }
@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'")))); }
@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'")))); }
@Test public void multipart() throws Exception { MultiValueMap<String, Object> multipartData = new LinkedMultiValueMap<>(); multipartData.add("a", "alpha"); multipartData.add("b", "bravo"); Consumer<EntityExchangeResult<byte[]>> documentation = document("multipart", requestParts(partWithName("a").description("Part a"), partWithName("b").description("Part b"))); this.webTestClient.post().uri("/upload") .body(BodyInserters.fromMultipartData(multipartData)).exchange() .expectStatus().isOk().expectBody().consumeWith(documentation); assertThat(new File("build/generated-snippets/multipart/request-parts.adoc")) .has(content(tableWithHeader(TemplateFormats.asciidoctor(), "Part", "Description").row("`a`", "Part a").row("`b`", "Part b"))); }
@Test public void customContextPath() throws Exception { MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context) .apply(documentationConfiguration(this.restDocumentation)).build(); 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'")))); }
@Test 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'")))); }
@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()) .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'")))); }
@Test public void responseWithCookie() { given().port(tomcat.getPort()) .filter(documentationConfiguration(this.restDocumentation)) .filter(document("set-cookie", preprocessResponse(removeHeaders(HttpHeaders.DATE, HttpHeaders.CONTENT_TYPE)))) .get("/set-cookie").then().statusCode(200); assertExpectedSnippetFilesExist(new File("build/generated-snippets/set-cookie"), "http-request.adoc", "http-response.adoc", "curl-request.adoc"); 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"))); }
@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'")))); }
@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'")))); }
@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")); 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'")))); }
@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'")))); }
@Test public void curlSnippetWithContent() throws Exception { String contentType = "text/plain; charset=UTF-8"; given().port(tomcat.getPort()) .filter(documentationConfiguration(this.restDocumentation)) .filter(document("curl-snippet-with-content")).accept("application/json") .body("content").contentType(contentType).post("/").then() .statusCode(200); 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:" + tomcat.getPort() + "/' -i -X POST \\%n" + " -H 'Accept: application/json' \\%n" + " -H 'Content-Type: " + contentType + "' \\%n" + " -d 'content'")))); }
@Test public void curlSnippetWithCookies() throws Exception { String contentType = "text/plain; charset=UTF-8"; given().port(tomcat.getPort()) .filter(documentationConfiguration(this.restDocumentation)) .filter(document("curl-snippet-with-cookies")).accept("application/json") .contentType(contentType).cookie("cookieName", "cookieVal").get("/") .then().statusCode(200); 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:" + tomcat.getPort() + "/' -i -X GET \\%n" + " -H 'Accept: application/json' \\%n" + " -H 'Content-Type: " + contentType + "' \\%n" + " --cookie 'cookieName=cookieVal'")))); }
@Test public void curlSnippetWithQueryStringOnPost() throws Exception { given().port(tomcat.getPort()) .filter(documentationConfiguration(this.restDocumentation)) .filter(document("curl-snippet-with-query-string")) .accept("application/json").param("foo", "bar").param("a", "alpha") .post("/?foo=bar").then().statusCode(200); String contentType = "application/x-www-form-urlencoded; charset=ISO-8859-1"; 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:" + tomcat.getPort() + "/?foo=bar' -i -X POST \\%n" + " -H 'Accept: application/json' \\%n" + " -H 'Content-Type: " + contentType + "' \\%n" + " -d 'a=alpha'")))); }
@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")))); 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"))); }
@Test public void requestParametersSnippet() { this.webTestClient.get().uri("/?a=alpha&b=bravo").exchange().expectStatus().isOk() .expectBody() .consumeWith(document("request-parameters", requestParameters( parameterWithName("a").description("Alpha description"), parameterWithName("b").description("Bravo description")))); assertThat(new File( "build/generated-snippets/request-parameters/request-parameters.adoc")) .has(content(tableWithHeader(TemplateFormats.asciidoctor(), "Parameter", "Description") .row("`a`", "Alpha description") .row("`b`", "Bravo description"))); }
@Test public void pathParametersSnippet() { this.webTestClient.get().uri("/{foo}/{bar}", "1", "2").exchange().expectStatus() .isOk().expectBody() .consumeWith(document("path-parameters", pathParameters( parameterWithName("foo").description("Foo description"), parameterWithName("bar").description("Bar description")))); assertThat( new File("build/generated-snippets/path-parameters/path-parameters.adoc")) .has(content( tableWithTitleAndHeader(TemplateFormats.asciidoctor(), "+/{foo}/{bar}+", "Parameter", "Description") .row("`foo`", "Foo description") .row("`bar`", "Bar description"))); }