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

@@ -23,6 +23,7 @@ import java.io.FileNotFoundException;
import java.net.URI;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import io.restassured.RestAssured;
@@ -33,15 +34,14 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.http.HttpHeaders;
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 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;
/**
* Tests for {@link RestAssuredRequestConverter}.
@@ -64,8 +64,8 @@ public class RestAssuredRequestConverterTests {
requestSpec.get("/foo/bar");
OperationRequest request = this.factory
.convert((FilterableRequestSpecification) requestSpec);
assertThat(request.getUri(), is(equalTo(
URI.create("http://localhost:" + tomcat.getPort() + "/foo/bar"))));
assertThat(request.getUri()).isEqualTo(
URI.create("http://localhost:" + tomcat.getPort() + "/foo/bar"));
}
@Test
@@ -74,7 +74,7 @@ public class RestAssuredRequestConverterTests {
requestSpec.head("/foo/bar");
OperationRequest request = this.factory
.convert((FilterableRequestSpecification) requestSpec);
assertThat(request.getMethod(), is(equalTo(HttpMethod.HEAD)));
assertThat(request.getMethod()).isEqualTo(HttpMethod.HEAD);
}
@Test
@@ -84,8 +84,9 @@ public class RestAssuredRequestConverterTests {
requestSpec.get("/");
OperationRequest request = this.factory
.convert((FilterableRequestSpecification) requestSpec);
assertThat(request.getParameters().size(), is(1));
assertThat(request.getParameters().get("foo"), is(equalTo(Arrays.asList("bar"))));
assertThat(request.getParameters()).hasSize(1);
assertThat(request.getParameters()).containsEntry("foo",
Collections.singletonList("bar"));
}
@Test
@@ -94,9 +95,9 @@ public class RestAssuredRequestConverterTests {
requestSpec.get("/?foo=bar&foo=qix");
OperationRequest request = this.factory
.convert((FilterableRequestSpecification) requestSpec);
assertThat(request.getParameters().size(), is(1));
assertThat(request.getParameters().get("foo"),
is(equalTo(Arrays.asList("bar", "qix"))));
assertThat(request.getParameters()).hasSize(1);
assertThat(request.getParameters()).containsEntry("foo",
Arrays.asList("bar", "qix"));
}
@Test
@@ -106,8 +107,9 @@ public class RestAssuredRequestConverterTests {
requestSpec.get("/");
OperationRequest request = this.factory
.convert((FilterableRequestSpecification) requestSpec);
assertThat(request.getParameters().size(), is(1));
assertThat(request.getParameters().get("foo"), is(equalTo(Arrays.asList("bar"))));
assertThat(request.getParameters()).hasSize(1);
assertThat(request.getParameters()).containsEntry("foo",
Collections.singletonList("bar"));
}
@Test
@@ -117,8 +119,9 @@ public class RestAssuredRequestConverterTests {
requestSpec.get("/");
OperationRequest request = this.factory
.convert((FilterableRequestSpecification) requestSpec);
assertThat(request.getParameters().size(), is(1));
assertThat(request.getParameters().get("foo"), is(equalTo(Arrays.asList("bar"))));
assertThat(request.getParameters()).hasSize(1);
assertThat(request.getParameters()).containsEntry("foo",
Collections.singletonList("bar"));
}
@Test
@@ -128,10 +131,11 @@ public class RestAssuredRequestConverterTests {
requestSpec.get("/");
OperationRequest request = this.factory
.convert((FilterableRequestSpecification) requestSpec);
assertThat(request.getHeaders().toString(), request.getHeaders().size(), is(2));
assertThat(request.getHeaders().get("Foo"), is(equalTo(Arrays.asList("bar"))));
assertThat(request.getHeaders().get("Host"),
is(equalTo(Arrays.asList("localhost:" + tomcat.getPort()))));
assertThat(request.getHeaders()).hasSize(2);
assertThat(request.getHeaders()).containsEntry("Foo",
Collections.singletonList("bar"));
assertThat(request.getHeaders()).containsEntry("Host",
Collections.singletonList("localhost:" + tomcat.getPort()));
}
@Test
@@ -141,12 +145,13 @@ public class RestAssuredRequestConverterTests {
requestSpec.get("/");
OperationRequest request = this.factory
.convert((FilterableRequestSpecification) requestSpec);
assertThat(request.getHeaders().toString(), request.getHeaders().size(), is(3));
assertThat(request.getHeaders().get("Foo"), is(equalTo(Arrays.asList("bar"))));
assertThat(request.getHeaders().get("Accept"),
is(equalTo(Arrays.asList("application/json"))));
assertThat(request.getHeaders().get("Host"),
is(equalTo(Arrays.asList("localhost:" + tomcat.getPort()))));
assertThat(request.getHeaders()).hasSize(3);
assertThat(request.getHeaders()).containsEntry("Foo",
Collections.singletonList("bar"));
assertThat(request.getHeaders()).containsEntry("Accept",
Collections.singletonList("application/json"));
assertThat(request.getHeaders()).containsEntry("Host",
Collections.singletonList("localhost:" + tomcat.getPort()));
}
@Test
@@ -156,17 +161,17 @@ public class RestAssuredRequestConverterTests {
requestSpec.get("/");
OperationRequest request = this.factory
.convert((FilterableRequestSpecification) requestSpec);
assertThat(request.getCookies().size(), is(equalTo(2)));
assertThat(request.getCookies().size()).isEqualTo(2);
Iterator<RequestCookie> cookieIterator = request.getCookies().iterator();
RequestCookie cookie1 = cookieIterator.next();
assertThat(cookie1.getName(), is(equalTo("cookie1")));
assertThat(cookie1.getValue(), is(equalTo("cookieVal1")));
assertThat(cookie1.getName()).isEqualTo("cookie1");
assertThat(cookie1.getValue()).isEqualTo("cookieVal1");
RequestCookie cookie2 = cookieIterator.next();
assertThat(cookie2.getName(), is(equalTo("cookie2")));
assertThat(cookie2.getValue(), is(equalTo("cookieVal2")));
assertThat(cookie2.getName()).isEqualTo("cookie2");
assertThat(cookie2.getValue()).isEqualTo("cookieVal2");
}
@Test
@@ -178,19 +183,15 @@ public class RestAssuredRequestConverterTests {
OperationRequest request = this.factory
.convert((FilterableRequestSpecification) requestSpec);
Collection<OperationRequestPart> parts = request.getParts();
assertThat(parts.size(), is(2));
Iterator<OperationRequestPart> iterator = parts.iterator();
OperationRequestPart part = iterator.next();
assertThat(part.getName(), is(equalTo("a")));
assertThat(part.getSubmittedFileName(), is(equalTo("a.txt")));
assertThat(part.getContentAsString(), is(equalTo("alpha")));
assertThat(part.getHeaders().getContentType(), is(equalTo(MediaType.TEXT_PLAIN)));
part = iterator.next();
assertThat(part.getName(), is(equalTo("b")));
assertThat(part.getSubmittedFileName(), is(equalTo("file")));
assertThat(part.getContentAsString(), is(equalTo("{\"foo\":\"bar\"}")));
assertThat(part.getHeaders().getContentType(),
is(equalTo(MediaType.APPLICATION_JSON)));
assertThat(parts).hasSize(2);
assertThat(parts).extracting("name").containsExactly("a", "b");
assertThat(parts).extracting("submittedFileName").containsExactly("a.txt",
"file");
assertThat(parts).extracting("contentAsString").containsExactly("alpha",
"{\"foo\":\"bar\"}");
assertThat(parts).extracting("headers").extracting(HttpHeaders.CONTENT_TYPE)
.containsExactly(Collections.singletonList(MediaType.TEXT_PLAIN_VALUE),
Collections.singletonList(MediaType.APPLICATION_JSON_VALUE));
}
@Test
@@ -208,7 +209,7 @@ public class RestAssuredRequestConverterTests {
requestSpec.post();
OperationRequest request = this.factory
.convert((FilterableRequestSpecification) requestSpec);
assertThat(request.getContentAsString(), is(equalTo("body")));
assertThat(request.getContentAsString()).isEqualTo("body");
}
@Test
@@ -218,7 +219,7 @@ public class RestAssuredRequestConverterTests {
requestSpec.post();
OperationRequest request = this.factory
.convert((FilterableRequestSpecification) requestSpec);
assertThat(request.getContentAsString(), is(equalTo("{\"foo\":\"bar\"}")));
assertThat(request.getContentAsString()).isEqualTo("{\"foo\":\"bar\"}");
}
@Test
@@ -229,7 +230,7 @@ public class RestAssuredRequestConverterTests {
requestSpec.post();
OperationRequest request = this.factory
.convert((FilterableRequestSpecification) requestSpec);
assertThat(request.getContent(), is(equalTo(new byte[] { 1, 2, 3, 4 })));
assertThat(request.getContent()).isEqualTo(new byte[] { 1, 2, 3, 4 });
}
@Test
@@ -239,7 +240,7 @@ public class RestAssuredRequestConverterTests {
requestSpec.post();
OperationRequest request = this.factory
.convert((FilterableRequestSpecification) requestSpec);
assertThat(request.getContentAsString(), is(equalTo("file")));
assertThat(request.getContentAsString()).isEqualTo("file");
}
@Test
@@ -261,8 +262,8 @@ public class RestAssuredRequestConverterTests {
requestSpec.post();
OperationRequest request = this.factory
.convert((FilterableRequestSpecification) requestSpec);
assertThat(request.getParts().iterator().next().getContentAsString(),
is(equalTo("foo")));
assertThat(request.getParts().iterator().next().getContentAsString())
.isEqualTo("foo");
}
@Test
@@ -272,8 +273,8 @@ public class RestAssuredRequestConverterTests {
requestSpec.post();
OperationRequest request = this.factory
.convert((FilterableRequestSpecification) requestSpec);
assertThat(request.getParts().iterator().next().getContentAsString(),
is(equalTo("foo")));
assertThat(request.getParts().iterator().next().getContentAsString())
.isEqualTo("foo");
}
@Test
@@ -283,8 +284,8 @@ public class RestAssuredRequestConverterTests {
requestSpec.post();
OperationRequest request = this.factory
.convert((FilterableRequestSpecification) requestSpec);
assertThat(request.getParts().iterator().next().getContentAsString(),
is(equalTo("foo")));
assertThat(request.getParts().iterator().next().getContentAsString())
.isEqualTo("foo");
}
@Test
@@ -294,8 +295,8 @@ public class RestAssuredRequestConverterTests {
requestSpec.post();
OperationRequest request = this.factory
.convert((FilterableRequestSpecification) requestSpec);
assertThat(request.getParts().iterator().next().getContentAsString(),
is(equalTo("file")));
assertThat(request.getParts().iterator().next().getContentAsString())
.isEqualTo("file");
}
@Test
@@ -317,8 +318,8 @@ public class RestAssuredRequestConverterTests {
requestSpec.post();
OperationRequest request = this.factory
.convert((FilterableRequestSpecification) requestSpec);
assertThat(request.getParts().iterator().next().getContentAsString(),
is(equalTo("{\"foo\":\"bar\"}")));
assertThat(request.getParts().iterator().next().getContentAsString())
.isEqualTo("{\"foo\":\"bar\"}");
}
/**

View File

@@ -34,10 +34,7 @@ import org.springframework.restdocs.operation.preprocess.Preprocessors;
import org.springframework.restdocs.snippet.WriterResolver;
import org.springframework.restdocs.templates.TemplateEngine;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.Matchers.hasEntry;
import static org.junit.Assert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
@@ -83,20 +80,19 @@ public class RestAssuredRestDocumentationConfigurerTests {
configurationCaptor.capture());
@SuppressWarnings("unchecked")
Map<String, Object> configuration = configurationCaptor.getValue();
assertThat(configuration, hasEntry(equalTo(TemplateEngine.class.getName()),
instanceOf(TemplateEngine.class)));
assertThat(configuration, hasEntry(equalTo(WriterResolver.class.getName()),
instanceOf(WriterResolver.class)));
assertThat(configuration,
hasEntry(equalTo(
RestDocumentationGenerator.ATTRIBUTE_NAME_DEFAULT_SNIPPETS),
instanceOf(List.class)));
assertThat(configuration, hasEntry(equalTo(
RestDocumentationGenerator.ATTRIBUTE_NAME_DEFAULT_OPERATION_REQUEST_PREPROCESSOR),
instanceOf(OperationRequestPreprocessor.class)));
assertThat(configuration, hasEntry(equalTo(
RestDocumentationGenerator.ATTRIBUTE_NAME_DEFAULT_OPERATION_RESPONSE_PREPROCESSOR),
instanceOf(OperationResponsePreprocessor.class)));
assertThat(configuration.get(TemplateEngine.class.getName()))
.isInstanceOf(TemplateEngine.class);
assertThat(configuration.get(WriterResolver.class.getName()))
.isInstanceOf(WriterResolver.class);
assertThat(configuration
.get(RestDocumentationGenerator.ATTRIBUTE_NAME_DEFAULT_SNIPPETS))
.isInstanceOf(List.class);
assertThat(configuration
.get(RestDocumentationGenerator.ATTRIBUTE_NAME_DEFAULT_OPERATION_REQUEST_PREPROCESSOR))
.isInstanceOf(OperationRequestPreprocessor.class);
assertThat(configuration
.get(RestDocumentationGenerator.ATTRIBUTE_NAME_DEFAULT_OPERATION_RESPONSE_PREPROCESSOR))
.isInstanceOf(OperationResponsePreprocessor.class);
}
}

View File

@@ -17,12 +17,17 @@
package org.springframework.restdocs.restassured3;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.charset.StandardCharsets;
import java.util.regex.Pattern;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.specification.RequestSpecification;
import org.assertj.core.api.Condition;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
@@ -31,13 +36,12 @@ import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.restdocs.JUnitRestDocumentation;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMethod;
import static io.restassured.RestAssured.given;
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.assertj.core.api.Assertions.fail;
import static org.springframework.restdocs.headers.HeaderDocumentation.headerWithName;
import static org.springframework.restdocs.headers.HeaderDocumentation.responseHeaders;
import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.linkWithRel;
@@ -61,10 +65,9 @@ import static org.springframework.restdocs.request.RequestDocumentation.requestP
import static org.springframework.restdocs.restassured3.RestAssuredRestDocumentation.document;
import static org.springframework.restdocs.restassured3.RestAssuredRestDocumentation.documentationConfiguration;
import static org.springframework.restdocs.templates.TemplateFormats.asciidoctor;
import static org.springframework.restdocs.test.SnippetMatchers.codeBlock;
import static org.springframework.restdocs.test.SnippetMatchers.httpRequest;
import static org.springframework.restdocs.test.SnippetMatchers.httpResponse;
import static org.springframework.restdocs.test.SnippetMatchers.snippet;
import static org.springframework.restdocs.test.SnippetConditions.codeBlock;
import static org.springframework.restdocs.test.SnippetConditions.httpRequest;
import static org.springframework.restdocs.test.SnippetConditions.httpResponse;
/**
* Integration tests for using Spring REST Docs with REST Assured.
@@ -100,13 +103,13 @@ public class RestAssuredRestDocumentationIntegrationTests {
.statusCode(200);
assertThat(new File(
"build/generated-snippets/curl-snippet-with-content/curl-request.adoc"),
is(snippet(asciidoctor()).withContents(codeBlock(asciidoctor(), "bash")
.content(String.format("$ curl 'http://localhost:"
+ tomcat.getPort() + "/' -i -X POST \\%n"
+ " -H 'Accept: application/json' \\%n"
+ " -H 'Content-Type: " + contentType + "' \\%n"
+ " -d 'content'")))));
"build/generated-snippets/curl-snippet-with-content/curl-request.adoc"))
.has(content(codeBlock(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
@@ -118,13 +121,14 @@ public class RestAssuredRestDocumentationIntegrationTests {
.contentType(contentType).cookie("cookieName", "cookieVal").get("/")
.then().statusCode(200);
assertThat(new File(
"build/generated-snippets/curl-snippet-with-cookies/curl-request.adoc"),
is(snippet(asciidoctor()).withContents(codeBlock(asciidoctor(), "bash")
.content(String.format("$ curl 'http://localhost:"
+ tomcat.getPort() + "/' -i -X GET \\%n"
+ " -H 'Accept: application/json' \\%n"
+ " -H 'Content-Type: " + contentType + "' \\%n"
+ " --cookie 'cookieName=cookieVal'")))));
"build/generated-snippets/curl-snippet-with-cookies/curl-request.adoc"))
.has(content(codeBlock(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
@@ -136,13 +140,13 @@ public class RestAssuredRestDocumentationIntegrationTests {
.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"),
is(snippet(asciidoctor()).withContents(codeBlock(asciidoctor(), "bash")
.content(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'")))));
"build/generated-snippets/curl-snippet-with-query-string/curl-request.adoc"))
.has(content(codeBlock(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
@@ -219,7 +223,6 @@ public class RestAssuredRestDocumentationIntegrationTests {
subsectionWithPath("links")
.description("Links to other resources"))))
.accept("application/json").get("/").then().statusCode(200);
assertExpectedSnippetFilesExist(
new File("build/generated-snippets/response-fields"), "http-request.adoc",
"http-response.adoc", "curl-request.adoc", "response-fields.adoc");
@@ -281,12 +284,10 @@ public class RestAssuredRestDocumentationIntegrationTests {
.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"),
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
@@ -304,24 +305,22 @@ public class RestAssuredRestDocumentationIntegrationTests {
removeHeaders("a", HttpHeaders.CONTENT_LENGTH))))
.get("/").then().statusCode(200);
assertThat(
new File("build/generated-snippets/original-request/http-request.adoc"),
is(snippet(asciidoctor())
.withContents(httpRequest(asciidoctor(), RequestMethod.GET, "/")
new File("build/generated-snippets/original-request/http-request.adoc"))
.has(content(httpRequest(asciidoctor(), RequestMethod.GET, "/")
.header("a", "alpha").header("b", "bravo")
.header("Accept", MediaType.APPLICATION_JSON_VALUE)
.header("Content-Type", "application/json; charset=UTF-8")
.header("Host", "localhost:" + tomcat.getPort())
.header("Content-Length", "13")
.content("{\"a\":\"alpha\"}"))));
.content("{\"a\":\"alpha\"}")));
String prettyPrinted = String.format("{%n \"a\" : \"<<beta>>\"%n}");
assertThat(new File(
"build/generated-snippets/preprocessed-request/http-request.adoc"),
is(snippet(asciidoctor())
.withContents(httpRequest(asciidoctor(), RequestMethod.GET, "/")
"build/generated-snippets/preprocessed-request/http-request.adoc"))
.has(content(httpRequest(asciidoctor(), RequestMethod.GET, "/")
.header("b", "bravo")
.header("Accept", MediaType.APPLICATION_JSON_VALUE)
.header("Content-Type", "application/json; charset=UTF-8")
.header("Host", "localhost").content(prettyPrinted))));
.header("Host", "localhost").content(prettyPrinted)));
}
@Test
@@ -339,13 +338,12 @@ public class RestAssuredRestDocumentationIntegrationTests {
.statusCode(200);
String prettyPrinted = String.format("{%n \"a\" : \"<<beta>>\"%n}");
assertThat(new File(
"build/generated-snippets/default-preprocessed-request/http-request.adoc"),
is(snippet(asciidoctor())
.withContents(httpRequest(asciidoctor(), RequestMethod.GET, "/")
"build/generated-snippets/default-preprocessed-request/http-request.adoc"))
.has(content(httpRequest(asciidoctor(), RequestMethod.GET, "/")
.header("b", "bravo")
.header("Accept", MediaType.APPLICATION_JSON_VALUE)
.header("Content-Type", "application/json; charset=UTF-8")
.header("Host", "localhost").content(prettyPrinted))));
.header("Host", "localhost").content(prettyPrinted)));
}
@Test
@@ -363,14 +361,13 @@ public class RestAssuredRestDocumentationIntegrationTests {
String prettyPrinted = String.format("{%n \"a\" : \"<<beta>>\",%n \"links\" : "
+ "[ {%n \"rel\" : \"rel\",%n \"href\" : \"...\"%n } ]%n}");
assertThat(new File(
"build/generated-snippets/preprocessed-response/http-response.adoc"),
is(snippet(asciidoctor())
.withContents(httpResponse(asciidoctor(), HttpStatus.OK)
"build/generated-snippets/preprocessed-response/http-response.adoc"))
.has(content(httpResponse(asciidoctor(), HttpStatus.OK)
.header("Foo", "https://api.example.com/foo/bar")
.header("Content-Type", "application/json;charset=UTF-8")
.header(HttpHeaders.CONTENT_LENGTH,
prettyPrinted.getBytes().length)
.content(prettyPrinted))));
.content(prettyPrinted)));
}
@Test
@@ -389,14 +386,13 @@ public class RestAssuredRestDocumentationIntegrationTests {
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"),
is(snippet(asciidoctor())
.withContents(httpResponse(asciidoctor(), HttpStatus.OK)
"build/generated-snippets/default-preprocessed-response/http-response.adoc"))
.has(content(httpResponse(asciidoctor(), HttpStatus.OK)
.header("Foo", "https://api.example.com/foo/bar")
.header("Content-Type", "application/json;charset=UTF-8")
.header(HttpHeaders.CONTENT_LENGTH,
prettyPrinted.getBytes().length)
.content(prettyPrinted))));
.content(prettyPrinted)));
}
@Test
@@ -416,15 +412,33 @@ public class RestAssuredRestDocumentationIntegrationTests {
Thread.currentThread().setContextClassLoader(previous);
}
assertThat(new File(
"build/generated-snippets/custom-snippet-template/curl-request.adoc"),
is(snippet(asciidoctor()).withContents(equalTo("Custom curl request"))));
"build/generated-snippets/custom-snippet-template/curl-request.adoc"))
.hasContent("Custom curl request");
}
private void assertExpectedSnippetFilesExist(File directory, String... snippets) {
for (String snippet : snippets) {
File snippetFile = new File(directory, snippet);
assertTrue("Snippet " + snippetFile + " not found", snippetFile.isFile());
assertThat(new File(directory, snippet)).isFile();
}
}
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;
}
}
};
}
}

View File

@@ -35,9 +35,7 @@ import org.springframework.restdocs.operation.OperationResponseFactory;
import org.springframework.restdocs.operation.Parameters;
import org.springframework.restdocs.operation.RequestCookie;
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;
/**
* Tests for {@link UriModifyingOperationPreprocessor}.
@@ -58,8 +56,7 @@ public class UriModifyingOperationPreprocessorTests {
this.preprocessor.scheme("https");
OperationRequest processed = this.preprocessor
.preprocess(createRequestWithUri("http://localhost:12345"));
assertThat(processed.getUri(),
is(equalTo(URI.create("https://localhost:12345"))));
assertThat(processed.getUri()).isEqualTo(URI.create("https://localhost:12345"));
}
@Test
@@ -67,10 +64,10 @@ public class UriModifyingOperationPreprocessorTests {
this.preprocessor.host("api.example.com");
OperationRequest processed = this.preprocessor
.preprocess(createRequestWithUri("http://api.foo.com:12345"));
assertThat(processed.getUri(),
is(equalTo(URI.create("http://api.example.com:12345"))));
assertThat(processed.getHeaders().getFirst(HttpHeaders.HOST),
is(equalTo("api.example.com:12345")));
assertThat(processed.getUri())
.isEqualTo(URI.create("http://api.example.com:12345"));
assertThat(processed.getHeaders().getFirst(HttpHeaders.HOST))
.isEqualTo("api.example.com:12345");
}
@Test
@@ -78,10 +75,10 @@ public class UriModifyingOperationPreprocessorTests {
this.preprocessor.port(23456);
OperationRequest processed = this.preprocessor
.preprocess(createRequestWithUri("http://api.example.com:12345"));
assertThat(processed.getUri(),
is(equalTo(URI.create("http://api.example.com:23456"))));
assertThat(processed.getHeaders().getFirst(HttpHeaders.HOST),
is(equalTo("api.example.com:23456")));
assertThat(processed.getUri())
.isEqualTo(URI.create("http://api.example.com:23456"));
assertThat(processed.getHeaders().getFirst(HttpHeaders.HOST))
.isEqualTo("api.example.com:23456");
}
@Test
@@ -89,9 +86,9 @@ public class UriModifyingOperationPreprocessorTests {
this.preprocessor.removePort();
OperationRequest processed = this.preprocessor
.preprocess(createRequestWithUri("http://api.example.com:12345"));
assertThat(processed.getUri(), is(equalTo(URI.create("http://api.example.com"))));
assertThat(processed.getHeaders().getFirst(HttpHeaders.HOST),
is(equalTo("api.example.com")));
assertThat(processed.getUri()).isEqualTo(URI.create("http://api.example.com"));
assertThat(processed.getHeaders().getFirst(HttpHeaders.HOST))
.isEqualTo("api.example.com");
}
@Test
@@ -99,8 +96,8 @@ public class UriModifyingOperationPreprocessorTests {
this.preprocessor.removePort();
OperationRequest processed = this.preprocessor
.preprocess(createRequestWithUri("http://api.example.com:12345/foo/bar"));
assertThat(processed.getUri(),
is(equalTo(URI.create("http://api.example.com/foo/bar"))));
assertThat(processed.getUri())
.isEqualTo(URI.create("http://api.example.com/foo/bar"));
}
@Test
@@ -108,8 +105,8 @@ public class UriModifyingOperationPreprocessorTests {
this.preprocessor.removePort();
OperationRequest processed = this.preprocessor
.preprocess(createRequestWithUri("http://api.example.com:12345?foo=bar"));
assertThat(processed.getUri(),
is(equalTo(URI.create("http://api.example.com?foo=bar"))));
assertThat(processed.getUri())
.isEqualTo(URI.create("http://api.example.com?foo=bar"));
}
@Test
@@ -117,8 +114,8 @@ public class UriModifyingOperationPreprocessorTests {
this.preprocessor.removePort();
OperationRequest processed = this.preprocessor
.preprocess(createRequestWithUri("http://api.example.com:12345#foo"));
assertThat(processed.getUri(),
is(equalTo(URI.create("http://api.example.com#foo"))));
assertThat(processed.getUri())
.isEqualTo(URI.create("http://api.example.com#foo"));
}
@Test
@@ -127,8 +124,8 @@ public class UriModifyingOperationPreprocessorTests {
OperationRequest processed = this.preprocessor
.preprocess(createRequestWithContent(
"The uri 'http://localhost:12345' should be used"));
assertThat(new String(processed.getContent()),
is(equalTo("The uri 'https://localhost:12345' should be used")));
assertThat(new String(processed.getContent()))
.isEqualTo("The uri 'https://localhost:12345' should be used");
}
@Test
@@ -137,8 +134,8 @@ public class UriModifyingOperationPreprocessorTests {
OperationRequest processed = this.preprocessor
.preprocess(createRequestWithContent(
"The uri 'http://localhost:12345' should be used"));
assertThat(new String(processed.getContent()),
is(equalTo("The uri 'http://api.example.com:12345' should be used")));
assertThat(new String(processed.getContent()))
.isEqualTo("The uri 'http://api.example.com:12345' should be used");
}
@Test
@@ -147,8 +144,8 @@ public class UriModifyingOperationPreprocessorTests {
OperationRequest processed = this.preprocessor
.preprocess(createRequestWithContent(
"The uri 'http://localhost:12345' should be used"));
assertThat(new String(processed.getContent()),
is(equalTo("The uri 'http://localhost:23456' should be used")));
assertThat(new String(processed.getContent()))
.isEqualTo("The uri 'http://localhost:23456' should be used");
}
@Test
@@ -157,8 +154,8 @@ public class UriModifyingOperationPreprocessorTests {
OperationRequest processed = this.preprocessor
.preprocess(createRequestWithContent(
"The uri 'http://localhost:12345' should be used"));
assertThat(new String(processed.getContent()),
is(equalTo("The uri 'http://localhost' should be used")));
assertThat(new String(processed.getContent()))
.isEqualTo("The uri 'http://localhost' should be used");
}
@Test
@@ -167,8 +164,8 @@ public class UriModifyingOperationPreprocessorTests {
OperationRequest processed = this.preprocessor
.preprocess(createRequestWithContent(
"Use 'http://localhost:12345' or 'https://localhost:23456' to access the service"));
assertThat(new String(processed.getContent()), is(equalTo(
"Use 'http://localhost' or 'https://localhost' to access the service")));
assertThat(new String(processed.getContent())).isEqualTo(
"Use 'http://localhost' or 'https://localhost' to access the service");
}
@Test
@@ -177,8 +174,8 @@ public class UriModifyingOperationPreprocessorTests {
OperationRequest processed = this.preprocessor
.preprocess(createRequestWithContent(
"The uri 'http://localhost:12345/foo/bar' should be used"));
assertThat(new String(processed.getContent()),
is(equalTo("The uri 'http://localhost/foo/bar' should be used")));
assertThat(new String(processed.getContent()))
.isEqualTo("The uri 'http://localhost/foo/bar' should be used");
}
@Test
@@ -187,8 +184,8 @@ public class UriModifyingOperationPreprocessorTests {
OperationRequest processed = this.preprocessor
.preprocess(createRequestWithContent(
"The uri 'http://localhost:12345?foo=bar' should be used"));
assertThat(new String(processed.getContent()),
is(equalTo("The uri 'http://localhost?foo=bar' should be used")));
assertThat(new String(processed.getContent()))
.isEqualTo("The uri 'http://localhost?foo=bar' should be used");
}
@Test
@@ -197,8 +194,8 @@ public class UriModifyingOperationPreprocessorTests {
OperationRequest processed = this.preprocessor
.preprocess(createRequestWithContent(
"The uri 'http://localhost:12345#foo' should be used"));
assertThat(new String(processed.getContent()),
is(equalTo("The uri 'http://localhost#foo' should be used")));
assertThat(new String(processed.getContent()))
.isEqualTo("The uri 'http://localhost#foo' should be used");
}
@Test
@@ -207,8 +204,8 @@ public class UriModifyingOperationPreprocessorTests {
OperationResponse processed = this.preprocessor
.preprocess(createResponseWithContent(
"The uri 'http://localhost:12345' should be used"));
assertThat(new String(processed.getContent()),
is(equalTo("The uri 'https://localhost:12345' should be used")));
assertThat(new String(processed.getContent()))
.isEqualTo("The uri 'https://localhost:12345' should be used");
}
@Test
@@ -217,8 +214,8 @@ public class UriModifyingOperationPreprocessorTests {
OperationResponse processed = this.preprocessor
.preprocess(createResponseWithContent(
"The uri 'http://localhost:12345' should be used"));
assertThat(new String(processed.getContent()),
is(equalTo("The uri 'http://api.example.com:12345' should be used")));
assertThat(new String(processed.getContent()))
.isEqualTo("The uri 'http://api.example.com:12345' should be used");
}
@Test
@@ -227,8 +224,8 @@ public class UriModifyingOperationPreprocessorTests {
OperationResponse processed = this.preprocessor
.preprocess(createResponseWithContent(
"The uri 'http://localhost:12345' should be used"));
assertThat(new String(processed.getContent()),
is(equalTo("The uri 'http://localhost:23456' should be used")));
assertThat(new String(processed.getContent()))
.isEqualTo("The uri 'http://localhost:23456' should be used");
}
@Test
@@ -237,8 +234,8 @@ public class UriModifyingOperationPreprocessorTests {
OperationResponse processed = this.preprocessor
.preprocess(createResponseWithContent(
"The uri 'http://localhost:12345' should be used"));
assertThat(new String(processed.getContent()),
is(equalTo("The uri 'http://localhost' should be used")));
assertThat(new String(processed.getContent()))
.isEqualTo("The uri 'http://localhost' should be used");
}
@Test
@@ -247,8 +244,8 @@ public class UriModifyingOperationPreprocessorTests {
OperationResponse processed = this.preprocessor
.preprocess(createResponseWithContent(
"Use 'http://localhost:12345' or 'https://localhost:23456' to access the service"));
assertThat(new String(processed.getContent()), is(equalTo(
"Use 'http://localhost' or 'https://localhost' to access the service")));
assertThat(new String(processed.getContent())).isEqualTo(
"Use 'http://localhost' or 'https://localhost' to access the service");
}
@Test
@@ -257,8 +254,8 @@ public class UriModifyingOperationPreprocessorTests {
OperationResponse processed = this.preprocessor
.preprocess(createResponseWithContent(
"The uri 'http://localhost:12345/foo/bar' should be used"));
assertThat(new String(processed.getContent()),
is(equalTo("The uri 'http://localhost/foo/bar' should be used")));
assertThat(new String(processed.getContent()))
.isEqualTo("The uri 'http://localhost/foo/bar' should be used");
}
@Test
@@ -267,8 +264,8 @@ public class UriModifyingOperationPreprocessorTests {
OperationResponse processed = this.preprocessor
.preprocess(createResponseWithContent(
"The uri 'http://localhost:12345?foo=bar' should be used"));
assertThat(new String(processed.getContent()),
is(equalTo("The uri 'http://localhost?foo=bar' should be used")));
assertThat(new String(processed.getContent()))
.isEqualTo("The uri 'http://localhost?foo=bar' should be used");
}
@Test
@@ -277,34 +274,33 @@ public class UriModifyingOperationPreprocessorTests {
OperationResponse processed = this.preprocessor
.preprocess(createResponseWithContent(
"The uri 'http://localhost:12345#foo' should be used"));
assertThat(new String(processed.getContent()),
is(equalTo("The uri 'http://localhost#foo' should be used")));
assertThat(new String(processed.getContent()))
.isEqualTo("The uri 'http://localhost#foo' should be used");
}
@Test
public void urisInRequestHeadersCanBeModified() {
OperationRequest processed = this.preprocessor.host("api.example.com")
.preprocess(createRequestWithHeader("Foo", "http://locahost:12345"));
assertThat(processed.getHeaders().getFirst("Foo"),
is(equalTo("http://api.example.com:12345")));
assertThat(processed.getHeaders().getFirst("Host"),
is(equalTo("api.example.com")));
assertThat(processed.getHeaders().getFirst("Foo"))
.isEqualTo("http://api.example.com:12345");
assertThat(processed.getHeaders().getFirst("Host")).isEqualTo("api.example.com");
}
@Test
public void urisInResponseHeadersCanBeModified() {
OperationResponse processed = this.preprocessor.host("api.example.com")
.preprocess(createResponseWithHeader("Foo", "http://locahost:12345"));
assertThat(processed.getHeaders().getFirst("Foo"),
is(equalTo("http://api.example.com:12345")));
assertThat(processed.getHeaders().getFirst("Foo"))
.isEqualTo("http://api.example.com:12345");
}
@Test
public void urisInRequestPartHeadersCanBeModified() {
OperationRequest processed = this.preprocessor.host("api.example.com").preprocess(
createRequestWithPartWithHeader("Foo", "http://locahost:12345"));
assertThat(processed.getParts().iterator().next().getHeaders().getFirst("Foo"),
is(equalTo("http://api.example.com:12345")));
assertThat(processed.getParts().iterator().next().getHeaders().getFirst("Foo"))
.isEqualTo("http://api.example.com:12345");
}
@Test
@@ -312,8 +308,8 @@ public class UriModifyingOperationPreprocessorTests {
OperationRequest processed = this.preprocessor.host("api.example.com")
.preprocess(createRequestWithPartWithContent(
"The uri 'http://localhost:12345' should be used"));
assertThat(new String(processed.getParts().iterator().next().getContent()),
is(equalTo("The uri 'http://api.example.com:12345' should be used")));
assertThat(new String(processed.getParts().iterator().next().getContent()))
.isEqualTo("The uri 'http://api.example.com:12345' should be used");
}
@Test
@@ -321,8 +317,8 @@ public class UriModifyingOperationPreprocessorTests {
this.preprocessor.scheme("https");
OperationRequest processed = this.preprocessor
.preprocess(createRequestWithUri("http://localhost:12345?foo=%7B%7D"));
assertThat(processed.getUri(),
is(equalTo(URI.create("https://localhost:12345?foo=%7B%7D"))));
assertThat(processed.getUri())
.isEqualTo(URI.create("https://localhost:12345?foo=%7B%7D"));
}
@@ -334,7 +330,7 @@ public class UriModifyingOperationPreprocessorTests {
new HttpHeaders(), new Parameters(),
Collections.<OperationRequestPart>emptyList(), cookies);
OperationRequest processed = this.preprocessor.preprocess(request);
assertThat(processed.getCookies().size(), is(equalTo(1)));
assertThat(processed.getCookies().size()).isEqualTo(1);
}
private OperationRequest createRequestWithUri(String uri) {