Merge branch '1.0.x'

This commit is contained in:
Andy Wilkinson
2016-02-15 12:25:49 +00:00
121 changed files with 1672 additions and 1529 deletions

View File

@@ -22,6 +22,10 @@ import java.util.Collection;
import java.util.List;
import java.util.Map.Entry;
import com.jayway.restassured.response.Header;
import com.jayway.restassured.specification.FilterableRequestSpecification;
import com.jayway.restassured.specification.MultiPartSpecification;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
@@ -32,18 +36,14 @@ import org.springframework.restdocs.operation.OperationRequestPartFactory;
import org.springframework.restdocs.operation.Parameters;
import org.springframework.restdocs.operation.RequestConverter;
import com.jayway.restassured.response.Header;
import com.jayway.restassured.specification.FilterableRequestSpecification;
import com.jayway.restassured.specification.MultiPartSpecification;
/**
* A converter for creating an {@link OperationRequest} from a REST Assured
* {@link FilterableRequestSpecification}.
*
* @author Andy Wilkinson
*/
class RestAssuredRequestConverter implements
RequestConverter<FilterableRequestSpecification> {
class RestAssuredRequestConverter
implements RequestConverter<FilterableRequestSpecification> {
@Override
public OperationRequest convert(FilterableRequestSpecification requestSpec) {
@@ -68,8 +68,8 @@ class RestAssuredRequestConverter implements
return new byte[0];
}
else {
throw new IllegalStateException("Unsupported request content: "
+ content.getClass().getName());
throw new IllegalStateException(
"Unsupported request content: " + content.getClass().getName());
}
}
@@ -100,8 +100,9 @@ class RestAssuredRequestConverter implements
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.TEXT_PLAIN
: MediaType.parseMediaType(multiPartSpec.getMimeType()));
parts.add(new OperationRequestPartFactory().create(
multiPartSpec.getControlName(), multiPartSpec.getFileName(),
convertContent(multiPartSpec.getContent()), headers));

View File

@@ -16,15 +16,15 @@
package org.springframework.restdocs.restassured;
import com.jayway.restassured.response.Header;
import com.jayway.restassured.response.Response;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.restdocs.operation.OperationResponse;
import org.springframework.restdocs.operation.OperationResponseFactory;
import org.springframework.restdocs.operation.ResponseConverter;
import com.jayway.restassured.response.Header;
import com.jayway.restassured.response.Response;
/**
* A converter for creating an {@link OperationResponse} from a REST Assured
* {@link Response}.

View File

@@ -45,7 +45,8 @@ public abstract class RestAssuredRestDocumentation {
* @param snippets the snippets that will document the API call
* @return a {@link RestDocumentationFilter} that will produce the documentation
*/
public static RestDocumentationFilter document(String identifier, Snippet... snippets) {
public static RestDocumentationFilter document(String identifier,
Snippet... snippets) {
return new RestDocumentationFilter(new RestDocumentationGenerator<>(identifier,
REQUEST_CONVERTER, RESPONSE_CONVERTER, snippets));
}

View File

@@ -19,23 +19,22 @@ package org.springframework.restdocs.restassured;
import java.util.HashMap;
import java.util.Map;
import org.springframework.restdocs.RestDocumentationContext;
import org.springframework.restdocs.RestDocumentationContextProvider;
import org.springframework.restdocs.config.RestDocumentationConfigurer;
import com.jayway.restassured.filter.Filter;
import com.jayway.restassured.filter.FilterContext;
import com.jayway.restassured.response.Response;
import com.jayway.restassured.specification.FilterableRequestSpecification;
import com.jayway.restassured.specification.FilterableResponseSpecification;
import org.springframework.restdocs.RestDocumentationContext;
import org.springframework.restdocs.RestDocumentationContextProvider;
import org.springframework.restdocs.config.RestDocumentationConfigurer;
/**
* A REST Assured-specific {@link RestDocumentationConfigurer}.
*
* @author Andy Wilkinson
*/
public final class RestAssuredRestDocumentationConfigurer
extends
public final class RestAssuredRestDocumentationConfigurer extends
RestDocumentationConfigurer<RestAssuredSnippetConfigurer, RestAssuredRestDocumentationConfigurer>
implements Filter {

View File

@@ -16,22 +16,21 @@
package org.springframework.restdocs.restassured;
import org.springframework.restdocs.config.SnippetConfigurer;
import com.jayway.restassured.filter.Filter;
import com.jayway.restassured.filter.FilterContext;
import com.jayway.restassured.response.Response;
import com.jayway.restassured.specification.FilterableRequestSpecification;
import com.jayway.restassured.specification.FilterableResponseSpecification;
import org.springframework.restdocs.config.SnippetConfigurer;
/**
* A configurer that can be used to configure the generated documentation snippets when
* using REST Assured.
*
* @author Andy Wilkinson
*/
public final class RestAssuredSnippetConfigurer
extends
public final class RestAssuredSnippetConfigurer extends
SnippetConfigurer<RestAssuredRestDocumentationConfigurer, RestAssuredSnippetConfigurer>
implements Filter {

View File

@@ -19,17 +19,17 @@ package org.springframework.restdocs.restassured;
import java.util.HashMap;
import java.util.Map;
import org.springframework.restdocs.RestDocumentationContext;
import org.springframework.restdocs.generate.RestDocumentationGenerator;
import org.springframework.restdocs.snippet.Snippet;
import org.springframework.util.Assert;
import com.jayway.restassured.filter.Filter;
import com.jayway.restassured.filter.FilterContext;
import com.jayway.restassured.response.Response;
import com.jayway.restassured.specification.FilterableRequestSpecification;
import com.jayway.restassured.specification.FilterableResponseSpecification;
import org.springframework.restdocs.RestDocumentationContext;
import org.springframework.restdocs.generate.RestDocumentationGenerator;
import org.springframework.restdocs.snippet.Snippet;
import org.springframework.util.Assert;
/**
* A REST Assured {@link Filter} for documenting RESTful APIs.
*
@@ -54,9 +54,9 @@ public final class RestDocumentationFilter implements Filter {
Map<String, Object> configuration = new HashMap<>(
context.<Map<String, Object>>getValue(CONTEXT_KEY_CONFIGURATION));
configuration.put(RestDocumentationContext.class.getName(), context
.<RestDocumentationContext>getValue(RestDocumentationContext.class
.getName()));
configuration.put(RestDocumentationContext.class.getName(),
context.<RestDocumentationContext>getValue(
RestDocumentationContext.class.getName()));
configuration.put(RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE,
requestSpec.getUserDefinedPath());

View File

@@ -139,17 +139,17 @@ public final class UriModifyingOperationPreprocessor implements OperationPreproc
if (this.host != null) {
modifiedHeaders.set(HttpHeaders.HOST, this.host);
}
return this.contentModifyingDelegate.preprocess(new OperationRequestFactory()
.create(uriBuilder.build().toUri(), request.getMethod(),
request.getContent(), modifiedHeaders, request.getParameters(),
modify(request.getParts())));
return this.contentModifyingDelegate.preprocess(
new OperationRequestFactory().create(uriBuilder.build().toUri(),
request.getMethod(), request.getContent(), modifiedHeaders,
request.getParameters(), modify(request.getParts())));
}
@Override
public OperationResponse preprocess(OperationResponse response) {
return this.contentModifyingDelegate.preprocess(new OperationResponseFactory()
.create(response.getStatus(), modify(response.getHeaders()),
response.getContent()));
return this.contentModifyingDelegate
.preprocess(new OperationResponseFactory().create(response.getStatus(),
modify(response.getHeaders()), response.getContent()));
}
private HttpHeaders modify(HttpHeaders headers) {
@@ -162,13 +162,15 @@ public final class UriModifyingOperationPreprocessor implements OperationPreproc
return modified;
}
private Collection<OperationRequestPart> modify(Collection<OperationRequestPart> parts) {
private Collection<OperationRequestPart> modify(
Collection<OperationRequestPart> parts) {
List<OperationRequestPart> modifiedParts = new ArrayList<>();
OperationRequestPartFactory factory = new OperationRequestPartFactory();
for (OperationRequestPart part : parts) {
modifiedParts.add(factory.create(part.getName(), part.getSubmittedFileName(),
this.contentModifier.modifyContent(part.getContent(), part
.getHeaders().getContentType()), modify(part.getHeaders())));
this.contentModifier.modifyContent(part.getContent(),
part.getHeaders().getContentType()),
modify(part.getHeaders())));
}
return modifiedParts;
}
@@ -225,8 +227,8 @@ public final class UriModifyingOperationPreprocessor implements OperationPreproc
if (matcher.start(i) >= 0) {
previous = matcher.end(i);
}
builder.append(getReplacement(matcher.group(i),
replacements.get(i - 1)));
builder.append(
getReplacement(matcher.group(i), replacements.get(i - 1)));
}
}

View File

@@ -19,4 +19,3 @@
* documented.
*/
package org.springframework.restdocs.restassured.operation.preprocess;

View File

@@ -18,4 +18,3 @@
* Core classes for using Spring REST Docs with REST Assured.
*/
package org.springframework.restdocs.restassured;

View File

@@ -23,10 +23,14 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import com.jayway.restassured.RestAssured;
import com.jayway.restassured.specification.FilterableRequestSpecification;
import com.jayway.restassured.specification.RequestSpecification;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.IntegrationTest;
@@ -42,10 +46,6 @@ import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jayway.restassured.RestAssured;
import com.jayway.restassured.specification.FilterableRequestSpecification;
import com.jayway.restassured.specification.RequestSpecification;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@@ -189,8 +189,8 @@ public class RestAssuredRequestConverterTests {
@Test
public void objectBody() {
RequestSpecification requestSpec = RestAssured.given()
.body(new ObjectBody("bar")).port(this.port);
RequestSpecification requestSpec = RestAssured.given().body(new ObjectBody("bar"))
.port(this.port);
requestSpec.post();
OperationRequest request = this.factory
.convert((FilterableRequestSpecification) requestSpec);
@@ -203,8 +203,8 @@ public class RestAssuredRequestConverterTests {
.body(new ByteArrayInputStream(new byte[] { 1, 2, 3, 4 }))
.port(this.port);
requestSpec.post();
this.thrown
.expectMessage(equalTo("Unsupported request content: java.io.ByteArrayInputStream"));
this.thrown.expectMessage(
equalTo("Unsupported request content: java.io.ByteArrayInputStream"));
this.factory.convert((FilterableRequestSpecification) requestSpec);
}

View File

@@ -19,18 +19,18 @@ package org.springframework.restdocs.restassured;
import java.util.List;
import java.util.Map;
import com.jayway.restassured.filter.FilterContext;
import com.jayway.restassured.specification.FilterableRequestSpecification;
import com.jayway.restassured.specification.FilterableResponseSpecification;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.springframework.restdocs.JUnitRestDocumentation;
import org.springframework.restdocs.generate.RestDocumentationGenerator;
import org.springframework.restdocs.snippet.WriterResolver;
import org.springframework.restdocs.templates.TemplateEngine;
import com.jayway.restassured.filter.FilterContext;
import com.jayway.restassured.specification.FilterableRequestSpecification;
import com.jayway.restassured.specification.FilterableResponseSpecification;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.Matchers.hasEntry;
@@ -48,11 +48,14 @@ import static org.mockito.Mockito.verify;
public class RestAssuredRestDocumentationConfigurerTests {
@Rule
public final JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation("build");
public final JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation(
"build");
private final FilterableRequestSpecification requestSpec = mock(FilterableRequestSpecification.class);
private final FilterableRequestSpecification requestSpec = mock(
FilterableRequestSpecification.class);
private final FilterableResponseSpecification responseSpec = mock(FilterableResponseSpecification.class);
private final FilterableResponseSpecification responseSpec = mock(
FilterableResponseSpecification.class);
private final FilterContext filterContext = mock(FilterContext.class);
@@ -76,16 +79,11 @@ 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,
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)));

View File

@@ -24,9 +24,12 @@ import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;
import com.jayway.restassured.builder.RequestSpecBuilder;
import com.jayway.restassured.specification.RequestSpecification;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.IntegrationTest;
@@ -45,9 +48,6 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.jayway.restassured.builder.RequestSpecBuilder;
import com.jayway.restassured.specification.RequestSpecification;
import static com.jayway.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
@@ -96,8 +96,7 @@ public class RestAssuredRestDocumentationIntegrationTests {
@Test
public void defaultSnippetGeneration() {
given().port(this.port)
.filter(documentationConfiguration(this.restDocumentation))
given().port(this.port).filter(documentationConfiguration(this.restDocumentation))
.filter(document("default")).get("/").then().statusCode(200);
assertExpectedSnippetFilesExist(new File("build/generated-snippets/default"),
"http-request.adoc", "http-response.adoc", "curl-request.adoc");
@@ -106,8 +105,7 @@ public class RestAssuredRestDocumentationIntegrationTests {
@Test
public void curlSnippetWithContent() throws Exception {
String contentType = "text/plain; charset=UTF-8";
given().port(this.port)
.filter(documentationConfiguration(this.restDocumentation))
given().port(this.port).filter(documentationConfiguration(this.restDocumentation))
.filter(document("curl-snippet-with-content")).accept("application/json")
.content("content").contentType(contentType).post("/").then()
.statusCode(200);
@@ -115,18 +113,16 @@ public class RestAssuredRestDocumentationIntegrationTests {
assertThat(
new File(
"build/generated-snippets/curl-snippet-with-content/curl-request.adoc"),
is(snippet(asciidoctor()).withContents(
codeBlock(asciidoctor(), "bash").content(
"$ curl 'http://localhost:" + this.port + "/' -i "
+ "-X POST -H 'Accept: application/json' "
+ "-H 'Content-Type: " + contentType + "' "
+ "-d 'content'"))));
is(snippet(asciidoctor()).withContents(codeBlock(asciidoctor(), "bash")
.content("$ curl 'http://localhost:" + this.port + "/' -i "
+ "-X POST -H 'Accept: application/json' "
+ "-H 'Content-Type: " + contentType + "' "
+ "-d 'content'"))));
}
@Test
public void curlSnippetWithQueryStringOnPost() throws Exception {
given().port(this.port)
.filter(documentationConfiguration(this.restDocumentation))
given().port(this.port).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);
@@ -134,19 +130,16 @@ public class RestAssuredRestDocumentationIntegrationTests {
assertThat(
new File(
"build/generated-snippets/curl-snippet-with-query-string/curl-request.adoc"),
is(snippet(asciidoctor()).withContents(
codeBlock(asciidoctor(), "bash").content(
"$ curl " + "'http://localhost:" + this.port
+ "/?foo=bar' -i -X POST "
+ "-H 'Accept: application/json' "
+ "-H 'Content-Type: " + contentType + "' "
+ "-d 'a=alpha'"))));
is(snippet(asciidoctor()).withContents(codeBlock(asciidoctor(), "bash")
.content("$ curl " + "'http://localhost:" + this.port
+ "/?foo=bar' -i -X POST "
+ "-H 'Accept: application/json' " + "-H 'Content-Type: "
+ contentType + "' " + "-d 'a=alpha'"))));
}
@Test
public void linksSnippet() throws Exception {
given().port(this.port)
.filter(documentationConfiguration(this.restDocumentation))
given().port(this.port).filter(documentationConfiguration(this.restDocumentation))
.filter(document("links",
links(linkWithRel("rel").description("The description"))))
.accept("application/json").get("/").then().statusCode(200);
@@ -157,68 +150,62 @@ public class RestAssuredRestDocumentationIntegrationTests {
@Test
public void pathParametersSnippet() throws Exception {
given().port(this.port)
.filter(documentationConfiguration(this.restDocumentation))
.filter(document(
"path-parameters",
pathParameters(parameterWithName("foo").description(
"The description")))).accept("application/json")
.get("/{foo}", "").then().statusCode(200);
assertExpectedSnippetFilesExist(new File(
"build/generated-snippets/path-parameters"), "http-request.adoc",
given().port(this.port).filter(documentationConfiguration(this.restDocumentation))
.filter(document("path-parameters",
pathParameters(
parameterWithName("foo").description("The description"))))
.accept("application/json").get("/{foo}", "").then().statusCode(200);
assertExpectedSnippetFilesExist(
new File("build/generated-snippets/path-parameters"), "http-request.adoc",
"http-response.adoc", "curl-request.adoc", "path-parameters.adoc");
}
@Test
public void requestParametersSnippet() throws Exception {
given().port(this.port)
.filter(documentationConfiguration(this.restDocumentation))
.filter(document(
"request-parameters",
requestParameters(parameterWithName("foo").description(
"The description")))).accept("application/json")
.param("foo", "bar").get("/").then().statusCode(200);
assertExpectedSnippetFilesExist(new File(
"build/generated-snippets/request-parameters"), "http-request.adoc",
"http-response.adoc", "curl-request.adoc", "request-parameters.adoc");
given().port(this.port).filter(documentationConfiguration(this.restDocumentation))
.filter(document("request-parameters",
requestParameters(
parameterWithName("foo").description("The description"))))
.accept("application/json").param("foo", "bar").get("/").then()
.statusCode(200);
assertExpectedSnippetFilesExist(
new File("build/generated-snippets/request-parameters"),
"http-request.adoc", "http-response.adoc", "curl-request.adoc",
"request-parameters.adoc");
}
@Test
public void requestFieldsSnippet() throws Exception {
given().port(this.port)
.filter(documentationConfiguration(this.restDocumentation))
.filter(document("request-fields", requestFields(fieldWithPath("a")
.description("The description")))).accept("application/json")
.content("{\"a\":\"alpha\"}").post("/").then().statusCode(200);
assertExpectedSnippetFilesExist(new File(
"build/generated-snippets/request-fields"), "http-request.adoc",
given().port(this.port).filter(documentationConfiguration(this.restDocumentation))
.filter(document("request-fields",
requestFields(fieldWithPath("a").description("The description"))))
.accept("application/json").content("{\"a\":\"alpha\"}").post("/").then()
.statusCode(200);
assertExpectedSnippetFilesExist(
new File("build/generated-snippets/request-fields"), "http-request.adoc",
"http-response.adoc", "curl-request.adoc", "request-fields.adoc");
}
@Test
public void responseFieldsSnippet() throws Exception {
given().port(this.port)
.filter(documentationConfiguration(this.restDocumentation))
.filter(document(
"response-fields",
responseFields(
fieldWithPath("a").description("The description"),
fieldWithPath("links").description(
"Links to other resources"))))
given().port(this.port).filter(documentationConfiguration(this.restDocumentation))
.filter(document("response-fields",
responseFields(fieldWithPath("a").description("The description"),
fieldWithPath("links")
.description("Links to other resources"))))
.accept("application/json").get("/").then().statusCode(200);
assertExpectedSnippetFilesExist(new File(
"build/generated-snippets/response-fields"), "http-request.adoc",
assertExpectedSnippetFilesExist(
new File("build/generated-snippets/response-fields"), "http-request.adoc",
"http-response.adoc", "curl-request.adoc", "response-fields.adoc");
}
@Test
public void parameterizedOutputDirectory() throws Exception {
given().port(this.port)
.filter(documentationConfiguration(this.restDocumentation))
given().port(this.port).filter(documentationConfiguration(this.restDocumentation))
.filter(document("{method-name}")).get("/").then().statusCode(200);
assertExpectedSnippetFilesExist(new File(
"build/generated-snippets/parameterized-output-directory"),
assertExpectedSnippetFilesExist(
new File("build/generated-snippets/parameterized-output-directory"),
"http-request.adoc", "http-response.adoc", "curl-request.adoc");
}
@@ -244,73 +231,56 @@ public class RestAssuredRestDocumentationIntegrationTests {
@Test
public void preprocessedRequest() throws Exception {
Pattern pattern = Pattern.compile("(\"alpha\")");
given().port(this.port)
.filter(documentationConfiguration(this.restDocumentation))
.header("a", "alpha")
.header("b", "bravo")
.contentType("application/json")
.accept("application/json")
.content("{\"a\":\"alpha\"}")
given().port(this.port).filter(documentationConfiguration(this.restDocumentation))
.header("a", "alpha").header("b", "bravo").contentType("application/json")
.accept("application/json").content("{\"a\":\"alpha\"}")
.filter(document("original-request"))
.filter(document(
"preprocessed-request",
preprocessRequest(
prettyPrint(),
.filter(document("preprocessed-request",
preprocessRequest(prettyPrint(),
removeHeaders("a", HttpHeaders.HOST,
HttpHeaders.CONTENT_LENGTH),
replacePattern(pattern, "\"<<beta>>\"")))).get("/")
.then().statusCode(200);
replacePattern(pattern, "\"<<beta>>\""))))
.get("/").then().statusCode(200);
assertThat(
new File("build/generated-snippets/original-request/http-request.adoc"),
is(snippet(asciidoctor())
.withContents(
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")
.header("Content-Length", "13")
.content("{\"a\":\"alpha\"}"))));
.withContents(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")
.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"),
is(snippet(asciidoctor())
.withContents(
httpRequest(asciidoctor(), RequestMethod.GET, "/")
.header("b", "bravo")
.header("Accept",
MediaType.APPLICATION_JSON_VALUE)
.header("Content-Type",
"application/json; charset=UTF-8")
.content(prettyPrinted))));
.withContents(httpRequest(asciidoctor(), RequestMethod.GET, "/")
.header("b", "bravo")
.header("Accept", MediaType.APPLICATION_JSON_VALUE)
.header("Content-Type", "application/json; charset=UTF-8")
.content(prettyPrinted))));
}
@Test
public void preprocessedResponse() throws Exception {
Pattern pattern = Pattern.compile("(\"alpha\")");
given().port(this.port)
.filter(documentationConfiguration(this.restDocumentation))
given().port(this.port).filter(documentationConfiguration(this.restDocumentation))
.filter(document("original-response"))
.filter(document(
"preprocessed-response",
preprocessResponse(
prettyPrint(),
maskLinks(),
removeHeaders("a", "Transfer-Encoding", "Date", "Server"),
replacePattern(pattern, "\"<<beta>>\""), modifyUris()
.scheme("https").host("api.example.com")
.removePort()))).get("/").then().statusCode(200);
.filter(document("preprocessed-response", preprocessResponse(
prettyPrint(), maskLinks(),
removeHeaders("a", "Transfer-Encoding", "Date", "Server"),
replacePattern(pattern, "\"<<beta>>\""), modifyUris()
.scheme("https").host("api.example.com").removePort())))
.get("/").then().statusCode(200);
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)
is(snippet(asciidoctor())
.withContents(httpResponse(asciidoctor(), HttpStatus.OK)
.header("Foo", "https://api.example.com/foo/bar")
.header("Content-Type", "application/json;charset=UTF-8")
.header(HttpHeaders.CONTENT_LENGTH,
@@ -320,8 +290,8 @@ public class RestAssuredRestDocumentationIntegrationTests {
@Test
public void customSnippetTemplate() throws Exception {
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);
@@ -334,8 +304,9 @@ public class RestAssuredRestDocumentationIntegrationTests {
finally {
Thread.currentThread().setContextClassLoader(previous);
}
assertThat(new File(
"build/generated-snippets/custom-snippet-template/curl-request.adoc"),
assertThat(
new File(
"build/generated-snippets/custom-snippet-template/curl-request.adoc"),
is(snippet(asciidoctor()).withContents(equalTo("Custom curl request"))));
}

View File

@@ -21,6 +21,7 @@ import java.util.Arrays;
import java.util.Collections;
import org.junit.Test;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
@@ -54,7 +55,8 @@ 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(),
is(equalTo(URI.create("https://localhost:12345"))));
}
@Test
@@ -114,7 +116,8 @@ public class UriModifyingOperationPreprocessorTests {
public void requestContentUriSchemeCanBeModified() {
this.preprocessor.scheme("https");
OperationRequest processed = this.preprocessor
.preprocess(createRequestWithContent("The uri 'http://localhost:12345' should be used"));
.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")));
}
@@ -123,7 +126,8 @@ public class UriModifyingOperationPreprocessorTests {
public void requestContentUriHostCanBeModified() {
this.preprocessor.host("api.example.com");
OperationRequest processed = this.preprocessor
.preprocess(createRequestWithContent("The uri 'http://localhost:12345' should be used"));
.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")));
}
@@ -132,7 +136,8 @@ public class UriModifyingOperationPreprocessorTests {
public void requestContentUriPortCanBeModified() {
this.preprocessor.port(23456);
OperationRequest processed = this.preprocessor
.preprocess(createRequestWithContent("The uri 'http://localhost:12345' should be used"));
.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")));
}
@@ -141,7 +146,8 @@ public class UriModifyingOperationPreprocessorTests {
public void requestContentUriPortCanBeRemoved() {
this.preprocessor.removePort();
OperationRequest processed = this.preprocessor
.preprocess(createRequestWithContent("The uri 'http://localhost:12345' should be used"));
.preprocess(createRequestWithContent(
"The uri 'http://localhost:12345' should be used"));
assertThat(new String(processed.getContent()),
is(equalTo("The uri 'http://localhost' should be used")));
}
@@ -150,17 +156,18 @@ public class UriModifyingOperationPreprocessorTests {
public void multipleRequestContentUrisCanBeModified() {
this.preprocessor.removePort();
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")));
.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")));
}
@Test
public void requestContentUriPathIsPreserved() {
this.preprocessor.removePort();
OperationRequest processed = this.preprocessor
.preprocess(createRequestWithContent("The uri 'http://localhost:12345/foo/bar' should be used"));
.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")));
}
@@ -169,7 +176,8 @@ public class UriModifyingOperationPreprocessorTests {
public void requestContentUriQueryIsPreserved() {
this.preprocessor.removePort();
OperationRequest processed = this.preprocessor
.preprocess(createRequestWithContent("The uri 'http://localhost:12345?foo=bar' should be used"));
.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")));
}
@@ -178,7 +186,8 @@ public class UriModifyingOperationPreprocessorTests {
public void requestContentUriAnchorIsPreserved() {
this.preprocessor.removePort();
OperationRequest processed = this.preprocessor
.preprocess(createRequestWithContent("The uri 'http://localhost:12345#foo' should be used"));
.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")));
}
@@ -187,7 +196,8 @@ public class UriModifyingOperationPreprocessorTests {
public void responseContentUriSchemeCanBeModified() {
this.preprocessor.scheme("https");
OperationResponse processed = this.preprocessor
.preprocess(createResponseWithContent("The uri 'http://localhost:12345' should be used"));
.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")));
}
@@ -196,7 +206,8 @@ public class UriModifyingOperationPreprocessorTests {
public void responseContentUriHostCanBeModified() {
this.preprocessor.host("api.example.com");
OperationResponse processed = this.preprocessor
.preprocess(createResponseWithContent("The uri 'http://localhost:12345' should be used"));
.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")));
}
@@ -205,7 +216,8 @@ public class UriModifyingOperationPreprocessorTests {
public void responseContentUriPortCanBeModified() {
this.preprocessor.port(23456);
OperationResponse processed = this.preprocessor
.preprocess(createResponseWithContent("The uri 'http://localhost:12345' should be used"));
.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")));
}
@@ -214,7 +226,8 @@ public class UriModifyingOperationPreprocessorTests {
public void responseContentUriPortCanBeRemoved() {
this.preprocessor.removePort();
OperationResponse processed = this.preprocessor
.preprocess(createResponseWithContent("The uri 'http://localhost:12345' should be used"));
.preprocess(createResponseWithContent(
"The uri 'http://localhost:12345' should be used"));
assertThat(new String(processed.getContent()),
is(equalTo("The uri 'http://localhost' should be used")));
}
@@ -223,17 +236,18 @@ public class UriModifyingOperationPreprocessorTests {
public void multipleResponseContentUrisCanBeModified() {
this.preprocessor.removePort();
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")));
.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")));
}
@Test
public void responseContentUriPathIsPreserved() {
this.preprocessor.removePort();
OperationResponse processed = this.preprocessor
.preprocess(createResponseWithContent("The uri 'http://localhost:12345/foo/bar' should be used"));
.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")));
}
@@ -242,7 +256,8 @@ public class UriModifyingOperationPreprocessorTests {
public void responseContentUriQueryIsPreserved() {
this.preprocessor.removePort();
OperationResponse processed = this.preprocessor
.preprocess(createResponseWithContent("The uri 'http://localhost:12345?foo=bar' should be used"));
.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")));
}
@@ -251,7 +266,8 @@ public class UriModifyingOperationPreprocessorTests {
public void responseContentUriAnchorIsPreserved() {
this.preprocessor.removePort();
OperationResponse processed = this.preprocessor
.preprocess(createResponseWithContent("The uri 'http://localhost:12345#foo' should be used"));
.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")));
}
@@ -276,19 +292,17 @@ public class UriModifyingOperationPreprocessorTests {
@Test
public void urisInRequestPartHeadersCanBeModified() {
OperationRequest processed = this.preprocessor.host("api.example.com")
.preprocess(
createRequestWithPartWithHeader("Foo", "http://locahost:12345"));
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")));
}
@Test
public void urisInRequestPartContentCanBeModified() {
OperationRequest processed = this.preprocessor
.host("api.example.com")
.preprocess(
createRequestWithPartWithContent("The uri 'http://localhost:12345' should be used"));
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")));
}
@@ -317,16 +331,16 @@ public class UriModifyingOperationPreprocessorTests {
HttpHeaders headers = new HttpHeaders();
headers.add(name, value);
return this.requestFactory.create(URI.create("http://localhost"), HttpMethod.GET,
new byte[0], new HttpHeaders(), new Parameters(), Arrays
.asList(new OperationRequestPartFactory().create("part",
"fileName", new byte[0], headers)));
new byte[0], new HttpHeaders(), new Parameters(),
Arrays.asList(new OperationRequestPartFactory().create("part", "fileName",
new byte[0], headers)));
}
private OperationRequest createRequestWithPartWithContent(String content) {
return this.requestFactory.create(URI.create("http://localhost"), HttpMethod.GET,
new byte[0], new HttpHeaders(), new Parameters(), Arrays
.asList(new OperationRequestPartFactory().create("part",
"fileName", content.getBytes(), new HttpHeaders())));
new byte[0], new HttpHeaders(), new Parameters(),
Arrays.asList(new OperationRequestPartFactory().create("part", "fileName",
content.getBytes(), new HttpHeaders())));
}
private OperationResponse createResponseWithContent(String content) {