Merge branch '1.2.x'

This commit is contained in:
Andy Wilkinson
2018-08-08 14:48:16 +01:00
155 changed files with 465 additions and 1030 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2017 the original author or authors.
* Copyright 2014-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -45,4 +45,5 @@ public final class RestAssuredOperationPreprocessorsConfigurer extends
FilterableResponseSpecification responseSpec, FilterContext context) {
return and().filter(requestSpec, responseSpec, context);
}
}

View File

@@ -164,9 +164,9 @@ class RestAssuredRequestConverter
List<OperationRequestPart> parts = new ArrayList<>();
for (MultiPartSpecification multiPartSpec : requestSpec.getMultiPartParams()) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(
multiPartSpec.getMimeType() == null ? MediaType.TEXT_PLAIN
: MediaType.parseMediaType(multiPartSpec.getMimeType()));
headers.setContentType((multiPartSpec.getMimeType() != null)
? MediaType.parseMediaType(multiPartSpec.getMimeType())
: MediaType.TEXT_PLAIN);
parts.add(new OperationRequestPartFactory().create(
multiPartSpec.getControlName(), multiPartSpec.getFileName(),
convertContent(multiPartSpec.getContent()), headers));

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2017 the original author or authors.
* Copyright 2014-2018 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.
@@ -41,7 +41,6 @@ public abstract class RestAssuredRestDocumentation {
/**
* Documents the API call with the given {@code identifier} using the given
* {@code snippets}.
*
* @param identifier an identifier for the API call that is being documented
* @param snippets the snippets that will document the API call
* @return a {@link RestDocumentationFilter} that will produce the documentation
@@ -56,7 +55,6 @@ public abstract class RestAssuredRestDocumentation {
* Documents the API call with the given {@code identifier} using the given
* {@code snippets} in addition to any default snippets. The given
* {@code requestPreprocessor} is applied to the request before it is documented.
*
* @param identifier an identifier for the API call that is being documented
* @param requestPreprocessor the request preprocessor
* @param snippets the snippets
@@ -72,7 +70,6 @@ public abstract class RestAssuredRestDocumentation {
* Documents the API call with the given {@code identifier} using the given
* {@code snippets} in addition to any default snippets. The given
* {@code responsePreprocessor} is applied to the request before it is documented.
*
* @param identifier an identifier for the API call that is being documented
* @param responsePreprocessor the response preprocessor
* @param snippets the snippets
@@ -89,7 +86,6 @@ public abstract class RestAssuredRestDocumentation {
* {@code snippets} in addition to any default snippets. The given
* {@code requestPreprocessor} and {@code responsePreprocessor} are applied to the
* request and response respectively before they are documented.
*
* @param identifier an identifier for the API call that is being documented
* @param requestPreprocessor the request preprocessor
* @param responsePreprocessor the response preprocessor
@@ -107,7 +103,6 @@ public abstract class RestAssuredRestDocumentation {
/**
* Provides access to a {@link RestAssuredRestDocumentationConfigurer} that can be
* used to configure Spring REST Docs using the given {@code contextProvider}.
*
* @param contextProvider the context provider
* @return the configurer
*/

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2017 the original author or authors.
* Copyright 2014-2018 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.
@@ -63,7 +63,6 @@ public class RestDocumentationFilter implements Filter {
/**
* Returns the configuration that should be used when calling the delgate. The
* configuration is derived from the given {@code requestSpec} and {@code context}.
*
* @param requestSpec the request specification
* @param context the filter context
* @return the configuration
@@ -83,7 +82,6 @@ public class RestDocumentationFilter implements Filter {
/**
* Creates a new {@link RestDocumentationFilter} that will produce documentation using
* the given {@code snippets}.
*
* @param snippets the snippets
* @return the new result handler
*/

View File

@@ -44,7 +44,6 @@ public abstract class RestAssuredPreprocessors {
/**
* Returns a {@code UriModifyingOperationPreprocessor} that will modify URIs in the
* request or response by changing one or more of their host, scheme, and port.
*
* @return the preprocessor
* @deprecated since 2.0.1 in favor of {@link Preprocessors#modifyUris()}
*/

View File

@@ -87,11 +87,11 @@ public class RestAssuredRestDocumentationConfigurerTests {
assertThat(configuration
.get(RestDocumentationGenerator.ATTRIBUTE_NAME_DEFAULT_SNIPPETS))
.isInstanceOf(List.class);
assertThat(configuration
.get(RestDocumentationGenerator.ATTRIBUTE_NAME_DEFAULT_OPERATION_REQUEST_PREPROCESSOR))
assertThat(configuration.get(
RestDocumentationGenerator.ATTRIBUTE_NAME_DEFAULT_OPERATION_REQUEST_PREPROCESSOR))
.isInstanceOf(OperationRequestPreprocessor.class);
assertThat(configuration
.get(RestDocumentationGenerator.ATTRIBUTE_NAME_DEFAULT_OPERATION_RESPONSE_PREPROCESSOR))
assertThat(configuration.get(
RestDocumentationGenerator.ATTRIBUTE_NAME_DEFAULT_OPERATION_RESPONSE_PREPROCESSOR))
.isInstanceOf(OperationResponsePreprocessor.class);
}

View File

@@ -36,6 +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.restdocs.templates.TemplateFormat;
import org.springframework.restdocs.templates.TemplateFormats;
import org.springframework.restdocs.test.SnippetConditions;
import org.springframework.restdocs.test.SnippetConditions.CodeBlockCondition;
import org.springframework.restdocs.test.SnippetConditions.HttpRequestCondition;
import org.springframework.restdocs.test.SnippetConditions.HttpResponseCondition;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMethod;
@@ -64,10 +70,6 @@ import static org.springframework.restdocs.request.RequestDocumentation.requestP
import static org.springframework.restdocs.request.RequestDocumentation.requestParts;
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.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.
@@ -104,7 +106,7 @@ public class RestAssuredRestDocumentationIntegrationTests {
assertThat(new File(
"build/generated-snippets/curl-snippet-with-content/curl-request.adoc"))
.has(content(codeBlock(asciidoctor(), "bash")
.has(content(codeBlock(TemplateFormats.asciidoctor(), "bash")
.withContent(String.format("$ curl 'http://localhost:"
+ tomcat.getPort() + "/' -i -X POST \\%n"
+ " -H 'Accept: application/json' \\%n"
@@ -122,7 +124,7 @@ public class RestAssuredRestDocumentationIntegrationTests {
.then().statusCode(200);
assertThat(new File(
"build/generated-snippets/curl-snippet-with-cookies/curl-request.adoc"))
.has(content(codeBlock(asciidoctor(), "bash")
.has(content(codeBlock(TemplateFormats.asciidoctor(), "bash")
.withContent(String.format("$ curl 'http://localhost:"
+ tomcat.getPort() + "/' -i -X GET \\%n"
+ " -H 'Accept: application/json' \\%n"
@@ -141,9 +143,10 @@ public class RestAssuredRestDocumentationIntegrationTests {
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(asciidoctor(), "bash").withContent(
String.format("$ curl " + "'http://localhost:"
+ tomcat.getPort() + "/?foo=bar' -i -X POST \\%n"
.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'"))));
@@ -285,9 +288,9 @@ public class RestAssuredRestDocumentationIntegrationTests {
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(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
@@ -306,21 +309,28 @@ public class RestAssuredRestDocumentationIntegrationTests {
.get("/").then().statusCode(200);
assertThat(
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\"}")));
.has(content(httpRequest(TemplateFormats.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\"}")));
String prettyPrinted = String.format("{%n \"a\" : \"<<beta>>\"%n}");
assertThat(new File(
"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)));
.has(content(httpRequest(TemplateFormats.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)));
}
@Test
@@ -339,11 +349,15 @@ public class RestAssuredRestDocumentationIntegrationTests {
String prettyPrinted = String.format("{%n \"a\" : \"<<beta>>\"%n}");
assertThat(new File(
"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)));
.has(content(httpRequest(TemplateFormats.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)));
}
@Test
@@ -361,8 +375,8 @@ 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"))
.has(content(httpResponse(asciidoctor(), HttpStatus.OK)
"build/generated-snippets/preprocessed-response/http-response.adoc")).has(
content(httpResponse(TemplateFormats.asciidoctor(), HttpStatus.OK)
.header("Foo", "https://api.example.com/foo/bar")
.header("Content-Type", "application/json;charset=UTF-8")
.header(HttpHeaders.CONTENT_LENGTH,
@@ -387,12 +401,14 @@ public class RestAssuredRestDocumentationIntegrationTests {
+ "[ {%n \"rel\" : \"rel\",%n \"href\" : \"...\"%n } ]%n}");
assertThat(new File(
"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)));
.has(content(
httpResponse(TemplateFormats.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)));
}
@Test
@@ -441,4 +457,17 @@ public class RestAssuredRestDocumentationIntegrationTests {
};
}
private CodeBlockCondition<?> codeBlock(TemplateFormat format, String language) {
return SnippetConditions.codeBlock(format, language);
}
private HttpRequestCondition httpRequest(TemplateFormat format,
RequestMethod requestMethod, String uri) {
return SnippetConditions.httpRequest(format, requestMethod, uri);
}
private HttpResponseCondition httpResponse(TemplateFormat format, HttpStatus status) {
return SnippetConditions.httpResponse(format, status);
}
}