Merge branch '1.2.x'

This commit is contained in:
Andy Wilkinson
2018-08-08 12:27:43 +01:00
84 changed files with 2454 additions and 2937 deletions

View File

@@ -18,7 +18,6 @@ package org.springframework.restdocs.webtestclient;
import java.net.URI;
import java.util.Arrays;
import java.util.Iterator;
import org.junit.Test;
@@ -29,7 +28,6 @@ import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.restdocs.operation.OperationRequest;
import org.springframework.restdocs.operation.OperationRequestPart;
import org.springframework.restdocs.operation.RequestCookie;
import org.springframework.test.web.reactive.server.ExchangeResult;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.util.LinkedMultiValueMap;
@@ -39,12 +37,7 @@ import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.Matchers.hasEntry;
import static org.junit.Assert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
import static org.springframework.web.reactive.function.server.RequestPredicates.POST;
@@ -64,8 +57,8 @@ public class WebTestClientRequestConverterTests {
.configureClient().baseUrl("http://localhost").build().get().uri("/foo")
.exchange().expectBody().returnResult();
OperationRequest request = this.converter.convert(result);
assertThat(request.getUri(), is(URI.create("http://localhost/foo")));
assertThat(request.getMethod(), is(HttpMethod.GET));
assertThat(request.getUri()).isEqualTo(URI.create("http://localhost/foo"));
assertThat(request.getMethod()).isEqualTo(HttpMethod.GET);
}
@Test
@@ -75,8 +68,8 @@ public class WebTestClientRequestConverterTests {
.configureClient().baseUrl("http://localhost:8080").build().get()
.uri("/foo").exchange().expectBody().returnResult();
OperationRequest request = this.converter.convert(result);
assertThat(request.getUri(), is(URI.create("http://localhost:8080/foo")));
assertThat(request.getMethod(), is(HttpMethod.GET));
assertThat(request.getUri()).isEqualTo(URI.create("http://localhost:8080/foo"));
assertThat(request.getMethod()).isEqualTo(HttpMethod.GET);
}
@Test
@@ -87,10 +80,11 @@ public class WebTestClientRequestConverterTests {
.header("a", "alpha", "apple").header("b", "bravo").exchange()
.expectBody().returnResult();
OperationRequest request = this.converter.convert(result);
assertThat(request.getUri(), is(URI.create("http://localhost/foo")));
assertThat(request.getMethod(), is(HttpMethod.GET));
assertThat(request.getHeaders(), hasEntry("a", Arrays.asList("alpha", "apple")));
assertThat(request.getHeaders(), hasEntry("b", Arrays.asList("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("b", Arrays.asList("bravo"));
}
@Test
@@ -100,8 +94,8 @@ public class WebTestClientRequestConverterTests {
.configureClient().baseUrl("https://localhost").build().get().uri("/foo")
.exchange().expectBody().returnResult();
OperationRequest request = this.converter.convert(result);
assertThat(request.getUri(), is(URI.create("https://localhost/foo")));
assertThat(request.getMethod(), is(HttpMethod.GET));
assertThat(request.getUri()).isEqualTo(URI.create("https://localhost/foo"));
assertThat(request.getMethod()).isEqualTo(HttpMethod.GET);
}
@Test
@@ -111,8 +105,8 @@ public class WebTestClientRequestConverterTests {
.configureClient().baseUrl("https://localhost:8443").build().get()
.uri("/foo").exchange().expectBody().returnResult();
OperationRequest request = this.converter.convert(result);
assertThat(request.getUri(), is(URI.create("https://localhost:8443/foo")));
assertThat(request.getMethod(), is(HttpMethod.GET));
assertThat(request.getUri()).isEqualTo(URI.create("https://localhost:8443/foo"));
assertThat(request.getMethod()).isEqualTo(HttpMethod.GET);
}
@Test
@@ -122,12 +116,12 @@ public class WebTestClientRequestConverterTests {
.configureClient().baseUrl("http://localhost").build().get()
.uri("/foo?a=alpha&b=bravo").exchange().expectBody().returnResult();
OperationRequest request = this.converter.convert(result);
assertThat(request.getUri(),
is(URI.create("http://localhost/foo?a=alpha&b=bravo")));
assertThat(request.getParameters().size(), is(2));
assertThat(request.getParameters(), hasEntry("a", Arrays.asList("alpha")));
assertThat(request.getParameters(), hasEntry("b", Arrays.asList("bravo")));
assertThat(request.getMethod(), is(HttpMethod.GET));
assertThat(request.getUri())
.isEqualTo(URI.create("http://localhost/foo?a=alpha&b=bravo"));
assertThat(request.getParameters()).hasSize(2);
assertThat(request.getParameters()).containsEntry("a", Arrays.asList("alpha"));
assertThat(request.getParameters()).containsEntry("b", Arrays.asList("bravo"));
assertThat(request.getMethod()).isEqualTo(HttpMethod.GET);
}
@Test
@@ -143,12 +137,12 @@ public class WebTestClientRequestConverterTests {
.uri("/foo").body(BodyInserters.fromFormData(parameters)).exchange()
.expectBody().returnResult();
OperationRequest request = this.converter.convert(result);
assertThat(request.getUri(), is(URI.create("http://localhost/foo")));
assertThat(request.getMethod(), is(HttpMethod.POST));
assertThat(request.getParameters().size(), is(2));
assertThat(request.getParameters(),
hasEntry("a", Arrays.asList("alpha", "apple")));
assertThat(request.getParameters(), hasEntry("b", Arrays.asList("br&vo")));
assertThat(request.getUri()).isEqualTo(URI.create("http://localhost/foo"));
assertThat(request.getMethod()).isEqualTo(HttpMethod.POST);
assertThat(request.getParameters()).hasSize(2);
assertThat(request.getParameters()).containsEntry("a",
Arrays.asList("alpha", "apple"));
assertThat(request.getParameters()).containsEntry("b", Arrays.asList("br&vo"));
}
@Test
@@ -161,13 +155,13 @@ public class WebTestClientRequestConverterTests {
.uri(URI.create("http://localhost/foo?a=alpha&a=apple&b=br%26vo"))
.exchange().expectBody().returnResult();
OperationRequest request = this.converter.convert(result);
assertThat(request.getUri(),
is(URI.create("http://localhost/foo?a=alpha&a=apple&b=br%26vo")));
assertThat(request.getMethod(), is(HttpMethod.POST));
assertThat(request.getParameters().size(), is(2));
assertThat(request.getParameters(),
hasEntry("a", Arrays.asList("alpha", "apple")));
assertThat(request.getParameters(), hasEntry("b", Arrays.asList("br&vo")));
assertThat(request.getUri())
.isEqualTo(URI.create("http://localhost/foo?a=alpha&a=apple&b=br%26vo"));
assertThat(request.getMethod()).isEqualTo(HttpMethod.POST);
assertThat(request.getParameters()).hasSize(2);
assertThat(request.getParameters()).containsEntry("a",
Arrays.asList("alpha", "apple"));
assertThat(request.getParameters()).containsEntry("b", Arrays.asList("br&vo"));
}
@Test
@@ -183,13 +177,13 @@ public class WebTestClientRequestConverterTests {
.body(BodyInserters.fromFormData(parameters)).exchange().expectBody()
.returnResult();
OperationRequest request = this.converter.convert(result);
assertThat(request.getUri(),
is(URI.create("http://localhost/foo?a=alpha&b=br%26vo")));
assertThat(request.getMethod(), is(HttpMethod.POST));
assertThat(request.getParameters().size(), is(2));
assertThat(request.getParameters(),
hasEntry("a", Arrays.asList("alpha", "apple")));
assertThat(request.getParameters(), hasEntry("b", Arrays.asList("br&vo")));
assertThat(request.getUri())
.isEqualTo(URI.create("http://localhost/foo?a=alpha&b=br%26vo"));
assertThat(request.getMethod()).isEqualTo(HttpMethod.POST);
assertThat(request.getParameters()).hasSize(2);
assertThat(request.getParameters()).containsEntry("a",
Arrays.asList("alpha", "apple"));
assertThat(request.getParameters()).containsEntry("b", Arrays.asList("br&vo"));
}
@Test
@@ -200,8 +194,8 @@ public class WebTestClientRequestConverterTests {
.configureClient().baseUrl("http://localhost").build().post().uri("/foo")
.exchange().expectBody().returnResult();
OperationRequest request = this.converter.convert(result);
assertThat(request.getUri(), is(URI.create("http://localhost/foo")));
assertThat(request.getMethod(), is(HttpMethod.POST));
assertThat(request.getUri()).isEqualTo(URI.create("http://localhost/foo"));
assertThat(request.getMethod()).isEqualTo(HttpMethod.POST);
}
@Test
@@ -216,17 +210,16 @@ public class WebTestClientRequestConverterTests {
.uri("/foo").body(BodyInserters.fromMultipartData(multipartData))
.exchange().expectBody().returnResult();
OperationRequest request = this.converter.convert(result);
assertThat(request.getUri(), is(URI.create("http://localhost/foo")));
assertThat(request.getMethod(), is(HttpMethod.POST));
assertThat(request.getParts().size(), is(1));
assertThat(request.getUri()).isEqualTo(URI.create("http://localhost/foo"));
assertThat(request.getMethod()).isEqualTo(HttpMethod.POST);
assertThat(request.getParts()).hasSize(1);
OperationRequestPart part = request.getParts().iterator().next();
assertThat(part.getName(), is(equalTo("file")));
assertThat(part.getSubmittedFileName(), is(nullValue()));
assertThat(part.getHeaders().size(), is(2));
assertThat(part.getHeaders().getContentLength(), is(4L));
assertThat(part.getHeaders().getContentDisposition().getName(),
is(equalTo("file")));
assertThat(part.getContent(), is(equalTo(new byte[] { 1, 2, 3, 4 })));
assertThat(part.getName()).isEqualTo("file");
assertThat(part.getSubmittedFileName()).isNull();
assertThat(part.getHeaders()).hasSize(2);
assertThat(part.getHeaders().getContentLength()).isEqualTo(4L);
assertThat(part.getHeaders().getContentDisposition().getName()).isEqualTo("file");
assertThat(part.getContent()).containsExactly(1, 2, 3, 4);
}
@Test
@@ -248,19 +241,19 @@ public class WebTestClientRequestConverterTests {
.uri("/foo").body(BodyInserters.fromMultipartData(multipartData))
.exchange().expectBody().returnResult();
OperationRequest request = this.converter.convert(result);
assertThat(request.getUri(), is(URI.create("http://localhost/foo")));
assertThat(request.getMethod(), is(HttpMethod.POST));
assertThat(request.getParts().size(), is(1));
assertThat(request.getUri()).isEqualTo(URI.create("http://localhost/foo"));
assertThat(request.getMethod()).isEqualTo(HttpMethod.POST);
assertThat(request.getParts()).hasSize(1);
OperationRequestPart part = request.getParts().iterator().next();
assertThat(part.getName(), is(equalTo("file")));
assertThat(part.getSubmittedFileName(), is(equalTo("image.png")));
assertThat(part.getHeaders().size(), is(3));
assertThat(part.getHeaders().getContentLength(), is(4L));
assertThat(part.getName()).isEqualTo("file");
assertThat(part.getSubmittedFileName()).isEqualTo("image.png");
assertThat(part.getHeaders()).hasSize(3);
assertThat(part.getHeaders().getContentLength()).isEqualTo(4);
ContentDisposition contentDisposition = part.getHeaders().getContentDisposition();
assertThat(contentDisposition.getName(), is(equalTo("file")));
assertThat(contentDisposition.getFilename(), is(equalTo("image.png")));
assertThat(part.getHeaders().getContentType(), is(equalTo(MediaType.IMAGE_PNG)));
assertThat(part.getContent(), is(equalTo(new byte[] { 1, 2, 3, 4 })));
assertThat(contentDisposition.getName()).isEqualTo("file");
assertThat(contentDisposition.getFilename()).isEqualTo("image.png");
assertThat(part.getHeaders().getContentType()).isEqualTo(MediaType.IMAGE_PNG);
assertThat(part.getContent()).containsExactly(1, 2, 3, 4);
}
@Test
@@ -270,20 +263,16 @@ public class WebTestClientRequestConverterTests {
.configureClient().baseUrl("http://localhost").build().get().uri("/foo")
.cookie("cookieName1", "cookieVal1").cookie("cookieName2", "cookieVal2")
.exchange().expectBody().returnResult();
assertThat(result.getRequestHeaders().get(HttpHeaders.COOKIE),
is(notNullValue()));
assertThat(result.getRequestHeaders().get(HttpHeaders.COOKIE)).isNotNull();
OperationRequest request = this.converter.convert(result);
assertThat(request.getUri(), is(URI.create("http://localhost/foo")));
assertThat(request.getMethod(), is(HttpMethod.GET));
assertThat(request.getCookies().size(), is(equalTo(2)));
assertThat(request.getHeaders().get(HttpHeaders.COOKIE), is(nullValue()));
Iterator<RequestCookie> cookieIterator = request.getCookies().iterator();
RequestCookie cookie1 = cookieIterator.next();
assertThat(cookie1.getName(), is(equalTo("cookieName1")));
assertThat(cookie1.getValue(), is(equalTo("cookieVal1")));
RequestCookie cookie2 = cookieIterator.next();
assertThat(cookie2.getName(), is(equalTo("cookieName2")));
assertThat(cookie2.getValue(), is(equalTo("cookieVal2")));
assertThat(request.getUri()).isEqualTo(URI.create("http://localhost/foo"));
assertThat(request.getMethod()).isEqualTo(HttpMethod.GET);
assertThat(request.getCookies()).hasSize(2);
assertThat(request.getHeaders().get(HttpHeaders.COOKIE)).isNull();
assertThat(request.getCookies()).extracting("name").containsExactly("cookieName1",
"cookieName2");
assertThat(request.getCookies()).extracting("value").containsExactly("cookieVal1",
"cookieVal2");
}
}

View File

@@ -30,10 +30,7 @@ import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
/**
@@ -53,11 +50,11 @@ public class WebTestClientResponseConverterTests {
.configureClient().baseUrl("http://localhost").build().get().uri("/foo")
.exchange().expectBody().returnResult();
OperationResponse response = this.converter.convert(result);
assertThat(response.getStatus(), is(HttpStatus.OK));
assertThat(response.getContentAsString(), is(equalTo("Hello, World!")));
assertThat(response.getHeaders().getContentType(),
is(MediaType.parseMediaType("text/plain;charset=UTF-8")));
assertThat(response.getHeaders().getContentLength(), is(13L));
assertThat(response.getStatus()).isEqualTo(HttpStatus.OK);
assertThat(response.getContentAsString()).isEqualTo("Hello, World!");
assertThat(response.getHeaders().getContentType())
.isEqualTo(MediaType.parseMediaType("text/plain;charset=UTF-8"));
assertThat(response.getHeaders().getContentLength()).isEqualTo(13);
}
@Test
@@ -71,10 +68,9 @@ public class WebTestClientResponseConverterTests {
.configureClient().baseUrl("http://localhost").build().get().uri("/foo")
.exchange().expectBody().returnResult();
OperationResponse response = this.converter.convert(result);
assertThat(response.getHeaders().size(), is(1));
assertTrue(response.getHeaders().containsKey(HttpHeaders.SET_COOKIE));
assertThat(response.getHeaders().get(HttpHeaders.SET_COOKIE), equalTo(
Collections.singletonList("name=value; Domain=localhost; HttpOnly")));
assertThat(response.getHeaders()).hasSize(1);
assertThat(response.getHeaders()).containsEntry(HttpHeaders.SET_COOKIE,
Collections.singletonList("name=value; Domain=localhost; HttpOnly"));
}
}

View File

@@ -17,7 +17,6 @@
package org.springframework.restdocs.webtestclient;
import java.net.URI;
import java.util.Map;
import org.junit.Rule;
import org.junit.Test;
@@ -29,11 +28,7 @@ import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.web.reactive.function.client.ClientRequest;
import org.springframework.web.reactive.function.client.ExchangeFunction;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
@@ -55,11 +50,10 @@ public class WebTestClientRestDocumentationConfigurerTests {
ClientRequest request = ClientRequest.create(HttpMethod.GET, URI.create("/test"))
.header(WebTestClient.WEBTESTCLIENT_REQUEST_ID, "1").build();
this.configurer.filter(request, mock(ExchangeFunction.class));
Map<String, Object> configuration = WebTestClientRestDocumentationConfigurer
.retrieveConfiguration(request.headers());
assertThat(configuration, notNullValue());
assertThat(WebTestClientRestDocumentationConfigurer
.retrieveConfiguration(request.headers()), nullValue());
.retrieveConfiguration(request.headers())).isNotNull();
assertThat(WebTestClientRestDocumentationConfigurer
.retrieveConfiguration(request.headers())).isNull();
}
@Test
@@ -72,8 +66,8 @@ public class WebTestClientRestDocumentationConfigurerTests {
ArgumentCaptor<ClientRequest> requestCaptor = ArgumentCaptor
.forClass(ClientRequest.class);
verify(exchangeFunction).exchange(requestCaptor.capture());
assertThat(requestCaptor.getValue().url(),
is(equalTo(URI.create("http://localhost:8080/test?foo=bar#baz"))));
assertThat(requestCaptor.getValue().url())
.isEqualTo(URI.create("http://localhost:8080/test?foo=bar#baz"));
}
@Test
@@ -87,8 +81,8 @@ public class WebTestClientRestDocumentationConfigurerTests {
ArgumentCaptor<ClientRequest> requestCaptor = ArgumentCaptor
.forClass(ClientRequest.class);
verify(exchangeFunction).exchange(requestCaptor.capture());
assertThat(requestCaptor.getValue().url(),
is(equalTo(URI.create("https://api.example.com:4567/test?foo=bar#baz"))));
assertThat(requestCaptor.getValue().url())
.isEqualTo(URI.create("https://api.example.com:4567/test?foo=bar#baz"));
}
}

View File

@@ -17,6 +17,10 @@
package org.springframework.restdocs.webtestclient;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
@@ -24,6 +28,7 @@ import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.assertj.core.api.Condition;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@@ -36,6 +41,7 @@ import org.springframework.restdocs.JUnitRestDocumentation;
import org.springframework.restdocs.templates.TemplateFormats;
import org.springframework.test.web.reactive.server.EntityExchangeResult;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.FileSystemUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
@@ -46,20 +52,18 @@ import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName;
import static org.springframework.restdocs.request.RequestDocumentation.partWithName;
import static org.springframework.restdocs.request.RequestDocumentation.pathParameters;
import static org.springframework.restdocs.request.RequestDocumentation.requestParameters;
import static org.springframework.restdocs.request.RequestDocumentation.requestParts;
import static org.springframework.restdocs.templates.TemplateFormats.asciidoctor;
import static org.springframework.restdocs.test.SnippetMatchers.codeBlock;
import static org.springframework.restdocs.test.SnippetMatchers.httpResponse;
import static org.springframework.restdocs.test.SnippetMatchers.snippet;
import static org.springframework.restdocs.test.SnippetMatchers.tableWithHeader;
import static org.springframework.restdocs.test.SnippetMatchers.tableWithTitleAndHeader;
import static org.springframework.restdocs.test.SnippetConditions.codeBlock;
import static org.springframework.restdocs.test.SnippetConditions.httpResponse;
import static org.springframework.restdocs.test.SnippetConditions.tableWithHeader;
import static org.springframework.restdocs.test.SnippetConditions.tableWithTitleAndHeader;
import static org.springframework.restdocs.webtestclient.WebTestClientRestDocumentation.document;
import static org.springframework.restdocs.webtestclient.WebTestClientRestDocumentation.documentationConfiguration;
import static org.springframework.web.reactive.function.BodyInserters.fromObject;
@@ -118,12 +122,12 @@ public class WebTestClientRestDocumentationIntegrationTests {
parameterWithName("foo").description("Foo description"),
parameterWithName("bar").description("Bar description"))));
assertThat(
new File("build/generated-snippets/path-parameters/path-parameters.adoc"),
is(snippet(asciidoctor()).withContents(
tableWithTitleAndHeader(TemplateFormats.asciidoctor(),
"+/{foo}/{bar}+", "Parameter", "Description")
.row("`foo`", "Foo description")
.row("`bar`", "Bar description"))));
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")));
}
@Test
@@ -134,11 +138,11 @@ public class WebTestClientRestDocumentationIntegrationTests {
parameterWithName("a").description("Alpha description"),
parameterWithName("b").description("Bravo description"))));
assertThat(new File(
"build/generated-snippets/request-parameters/request-parameters.adoc"),
is(snippet(asciidoctor()).withContents(
tableWithHeader(TemplateFormats.asciidoctor(), "Parameter",
"Description").row("`a`", "Alpha description").row("`b`",
"Bravo description"))));
"build/generated-snippets/request-parameters/request-parameters.adoc"))
.has(content(tableWithHeader(TemplateFormats.asciidoctor(),
"Parameter", "Description")
.row("`a`", "Alpha description")
.row("`b`", "Bravo description")));
}
@Test
@@ -152,22 +156,19 @@ public class WebTestClientRestDocumentationIntegrationTests {
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"),
is(snippet(asciidoctor())
.withContents(tableWithHeader(TemplateFormats.asciidoctor(),
"Part", "Description").row("`a`", "Part a").row("`b`",
"Part b"))));
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 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"),
is(snippet(asciidoctor())
.withContents(httpResponse(asciidoctor(), HttpStatus.OK).header(
HttpHeaders.SET_COOKIE,
"name=value; Domain=localhost; HttpOnly"))));
assertThat(new File("build/generated-snippets/set-cookie/http-response.adoc"))
.has(content(httpResponse(asciidoctor(), HttpStatus.OK).header(
HttpHeaders.SET_COOKIE,
"name=value; Domain=localhost; HttpOnly")));
}
@Test
@@ -176,12 +177,11 @@ public class WebTestClientRestDocumentationIntegrationTests {
.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"),
is(snippet(asciidoctor())
.withContents(codeBlock(asciidoctor(), "bash").content(String
"build/generated-snippets/curl-snippet-with-cookies/curl-request.adoc"))
.has(content(codeBlock(asciidoctor(), "bash").withContent(String
.format("$ curl 'https://api.example.com/' -i -X GET \\%n"
+ " -H 'Accept: application/json' \\%n"
+ " --cookie 'cookieName=cookieVal'")))));
+ " --cookie 'cookieName=cookieVal'"))));
}
@Test
@@ -190,12 +190,11 @@ public class WebTestClientRestDocumentationIntegrationTests {
.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"),
is(snippet(asciidoctor())
.withContents(codeBlock(asciidoctor(), "bash").content(
"build/generated-snippets/httpie-snippet-with-cookies/httpie-request.adoc"))
.has(content(codeBlock(asciidoctor(), "bash").withContent(
String.format("$ http GET 'https://api.example.com/' \\%n"
+ " 'Accept:application/json' \\%n"
+ " 'Cookie:cookieName=cookieVal'")))));
+ " 'Cookie:cookieName=cookieVal'"))));
}
private void assertExpectedSnippetFilesExist(File directory, String... snippets) {
@@ -203,7 +202,26 @@ public class WebTestClientRestDocumentationIntegrationTests {
Set<File> expected = Stream.of(snippets)
.map((snippet) -> new File(directory, snippet))
.collect(Collectors.toSet());
assertThat(actual, equalTo(expected));
assertThat(actual).isEqualTo(expected);
}
private Condition<File> content(final Condition<String> delegate) {
return new Condition<File>() {
@Override
public boolean matches(File value) {
try {
return delegate
.matches(FileCopyUtils.copyToString(new InputStreamReader(
new FileInputStream(value), StandardCharsets.UTF_8)));
}
catch (IOException ex) {
fail("Failed to read '" + value + "'", ex);
return false;
}
}
};
}
/**