Merge branch '1.1.x'
This commit is contained in:
@@ -21,6 +21,7 @@ import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@@ -41,10 +42,12 @@ import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.restdocs.JUnitRestDocumentation;
|
||||
import org.springframework.restdocs.mockmvc.MockMvcRestDocumentationIntegrationTests.TestConfiguration;
|
||||
import org.springframework.restdocs.test.SnippetMatchers.HttpRequestMatcher;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.MvcResult;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.util.FileSystemUtils;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
@@ -62,6 +65,7 @@ import static org.springframework.restdocs.headers.HeaderDocumentation.headerWit
|
||||
import static org.springframework.restdocs.headers.HeaderDocumentation.responseHeaders;
|
||||
import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.linkWithRel;
|
||||
import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.links;
|
||||
import static org.springframework.restdocs.mockmvc.IterableEnumeration.iterable;
|
||||
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
|
||||
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration;
|
||||
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
|
||||
@@ -423,42 +427,44 @@ public class MockMvcRestDocumentationIntegrationTests {
|
||||
|
||||
Pattern pattern = Pattern.compile("(\"alpha\")");
|
||||
|
||||
mockMvc.perform(get("/").header("a", "alpha").header("b", "bravo")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.accept(MediaType.APPLICATION_JSON).content("{\"a\":\"alpha\"}"))
|
||||
MvcResult result = mockMvc
|
||||
.perform(get("/").header("a", "alpha").header("b", "bravo")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.accept(MediaType.APPLICATION_JSON).content("{\"a\":\"alpha\"}"))
|
||||
.andExpect(status().isOk()).andDo(document("original-request"))
|
||||
.andDo(document("preprocessed-request",
|
||||
preprocessRequest(prettyPrint(),
|
||||
removeHeaders("a", HttpHeaders.HOST,
|
||||
HttpHeaders.CONTENT_LENGTH),
|
||||
replacePattern(pattern, "\"<<beta>>\""))));
|
||||
replacePattern(pattern, "\"<<beta>>\""))))
|
||||
.andReturn();
|
||||
|
||||
HttpRequestMatcher originalRequest = httpRequest(asciidoctor(), RequestMethod.GET,
|
||||
"/");
|
||||
for (String headerName : iterable(result.getRequest().getHeaderNames())) {
|
||||
originalRequest.header(headerName, result.getRequest().getHeader(headerName));
|
||||
}
|
||||
assertThat(
|
||||
new File("build/generated-snippets/original-request/http-request.adoc"),
|
||||
is(snippet(
|
||||
asciidoctor())
|
||||
.withContents(
|
||||
httpRequest(asciidoctor(), RequestMethod.GET, "/")
|
||||
.header("Content-Type",
|
||||
"application/json")
|
||||
.header("a", "alpha").header("b", "bravo")
|
||||
.header("Accept",
|
||||
MediaType.APPLICATION_JSON_VALUE)
|
||||
.header("Host", "localhost:8080")
|
||||
.header("Content-Length", "13")
|
||||
.content("{\"a\":\"alpha\"}"))));
|
||||
is(snippet(asciidoctor()).withContents(originalRequest
|
||||
.header("Host", "localhost:8080").header("Content-Length", "13")
|
||||
.content("{\"a\":\"alpha\"}"))));
|
||||
HttpRequestMatcher preprocessedRequest = httpRequest(asciidoctor(),
|
||||
RequestMethod.GET, "/");
|
||||
List<String> removedHeaders = Arrays.asList("a", HttpHeaders.HOST,
|
||||
HttpHeaders.CONTENT_LENGTH);
|
||||
for (String headerName : iterable(result.getRequest().getHeaderNames())) {
|
||||
if (!removedHeaders.contains(headerName)) {
|
||||
preprocessedRequest.header(headerName,
|
||||
result.getRequest().getHeader(headerName));
|
||||
}
|
||||
}
|
||||
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("Content-Type",
|
||||
"application/json")
|
||||
.header("b", "bravo")
|
||||
.header("Accept",
|
||||
MediaType.APPLICATION_JSON_VALUE)
|
||||
.content(prettyPrinted))));
|
||||
assertThat(
|
||||
new File(
|
||||
"build/generated-snippets/preprocessed-request/http-request.adoc"),
|
||||
is(snippet(asciidoctor())
|
||||
.withContents(preprocessedRequest.content(prettyPrinted))));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -4,13 +4,10 @@ dependencies {
|
||||
compile project(':spring-restdocs-core')
|
||||
compile 'com.jayway.restassured:rest-assured'
|
||||
|
||||
testCompile 'org.apache.tomcat.embed:tomcat-embed-core:8.5.11'
|
||||
testCompile 'org.mockito:mockito-core'
|
||||
testCompile 'org.hamcrest:hamcrest-library'
|
||||
testCompile 'org.springframework.hateoas:spring-hateoas'
|
||||
testCompile 'org.springframework.boot:spring-boot-starter-web:1.4.1.RELEASE'
|
||||
testCompile 'org.springframework.boot:spring-boot-test:1.4.1.RELEASE'
|
||||
testCompile project(path: ':spring-restdocs-core', configuration: 'testArtifacts')
|
||||
testRuntime 'commons-logging:commons-logging:1.2'
|
||||
}
|
||||
|
||||
test {
|
||||
|
||||
@@ -28,24 +28,16 @@ import java.util.Iterator;
|
||||
import com.jayway.restassured.RestAssured;
|
||||
import com.jayway.restassured.specification.FilterableRequestSpecification;
|
||||
import com.jayway.restassured.specification.RequestSpecification;
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.context.embedded.LocalServerPort;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
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.context.junit4.SpringRunner;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
@@ -56,31 +48,29 @@ import static org.junit.Assert.assertThat;
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
|
||||
public class RestAssuredRequestConverterTests {
|
||||
|
||||
@ClassRule
|
||||
public static TomcatServer tomcat = new TomcatServer();
|
||||
|
||||
@Rule
|
||||
public final ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
private final RestAssuredRequestConverter factory = new RestAssuredRequestConverter();
|
||||
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
|
||||
@Test
|
||||
public void requestUri() {
|
||||
RequestSpecification requestSpec = RestAssured.given().port(this.port);
|
||||
RequestSpecification requestSpec = RestAssured.given().port(tomcat.getPort());
|
||||
requestSpec.get("/foo/bar");
|
||||
OperationRequest request = this.factory
|
||||
.convert((FilterableRequestSpecification) requestSpec);
|
||||
assertThat(request.getUri(),
|
||||
is(equalTo(URI.create("http://localhost:" + this.port + "/foo/bar"))));
|
||||
assertThat(request.getUri(), is(equalTo(
|
||||
URI.create("http://localhost:" + tomcat.getPort() + "/foo/bar"))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void requestMethod() {
|
||||
RequestSpecification requestSpec = RestAssured.given().port(this.port);
|
||||
RequestSpecification requestSpec = RestAssured.given().port(tomcat.getPort());
|
||||
requestSpec.head("/foo/bar");
|
||||
OperationRequest request = this.factory
|
||||
.convert((FilterableRequestSpecification) requestSpec);
|
||||
@@ -89,7 +79,7 @@ public class RestAssuredRequestConverterTests {
|
||||
|
||||
@Test
|
||||
public void queryStringParameters() {
|
||||
RequestSpecification requestSpec = RestAssured.given().port(this.port)
|
||||
RequestSpecification requestSpec = RestAssured.given().port(tomcat.getPort())
|
||||
.queryParam("foo", "bar");
|
||||
requestSpec.get("/");
|
||||
OperationRequest request = this.factory
|
||||
@@ -100,7 +90,7 @@ public class RestAssuredRequestConverterTests {
|
||||
|
||||
@Test
|
||||
public void queryStringFromUrlParameters() {
|
||||
RequestSpecification requestSpec = RestAssured.given().port(this.port);
|
||||
RequestSpecification requestSpec = RestAssured.given().port(tomcat.getPort());
|
||||
requestSpec.get("/?foo=bar");
|
||||
OperationRequest request = this.factory
|
||||
.convert((FilterableRequestSpecification) requestSpec);
|
||||
@@ -110,7 +100,7 @@ public class RestAssuredRequestConverterTests {
|
||||
|
||||
@Test
|
||||
public void formParameters() {
|
||||
RequestSpecification requestSpec = RestAssured.given().port(this.port)
|
||||
RequestSpecification requestSpec = RestAssured.given().port(tomcat.getPort())
|
||||
.formParameter("foo", "bar");
|
||||
requestSpec.get("/");
|
||||
OperationRequest request = this.factory
|
||||
@@ -121,7 +111,7 @@ public class RestAssuredRequestConverterTests {
|
||||
|
||||
@Test
|
||||
public void requestParameters() {
|
||||
RequestSpecification requestSpec = RestAssured.given().port(this.port)
|
||||
RequestSpecification requestSpec = RestAssured.given().port(tomcat.getPort())
|
||||
.parameter("foo", "bar");
|
||||
requestSpec.get("/");
|
||||
OperationRequest request = this.factory
|
||||
@@ -132,7 +122,7 @@ public class RestAssuredRequestConverterTests {
|
||||
|
||||
@Test
|
||||
public void headers() {
|
||||
RequestSpecification requestSpec = RestAssured.given().port(this.port)
|
||||
RequestSpecification requestSpec = RestAssured.given().port(tomcat.getPort())
|
||||
.header("Foo", "bar");
|
||||
requestSpec.get("/");
|
||||
OperationRequest request = this.factory
|
||||
@@ -141,12 +131,12 @@ public class RestAssuredRequestConverterTests {
|
||||
assertThat(request.getHeaders().get("Foo"), is(equalTo(Arrays.asList("bar"))));
|
||||
assertThat(request.getHeaders().get("Accept"), is(equalTo(Arrays.asList("*/*"))));
|
||||
assertThat(request.getHeaders().get("Host"),
|
||||
is(equalTo(Arrays.asList("localhost:" + this.port))));
|
||||
is(equalTo(Arrays.asList("localhost:" + tomcat.getPort()))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cookies() {
|
||||
RequestSpecification requestSpec = RestAssured.given().port(this.port)
|
||||
RequestSpecification requestSpec = RestAssured.given().port(tomcat.getPort())
|
||||
.cookie("cookie1", "cookieVal1").cookie("cookie2", "cookieVal2");
|
||||
requestSpec.get("/");
|
||||
OperationRequest request = this.factory
|
||||
@@ -166,10 +156,10 @@ public class RestAssuredRequestConverterTests {
|
||||
|
||||
@Test
|
||||
public void multipart() {
|
||||
RequestSpecification requestSpec = RestAssured.given().port(this.port)
|
||||
RequestSpecification requestSpec = RestAssured.given().port(tomcat.getPort())
|
||||
.multiPart("a", "a.txt", "alpha", null)
|
||||
.multiPart("b", new ObjectBody("bar"), "application/json");
|
||||
requestSpec.post().then().statusCode(200);
|
||||
requestSpec.post();
|
||||
OperationRequest request = this.factory
|
||||
.convert((FilterableRequestSpecification) requestSpec);
|
||||
Collection<OperationRequestPart> parts = request.getParts();
|
||||
@@ -191,7 +181,7 @@ public class RestAssuredRequestConverterTests {
|
||||
@Test
|
||||
public void byteArrayBody() {
|
||||
RequestSpecification requestSpec = RestAssured.given().body("body".getBytes())
|
||||
.port(this.port);
|
||||
.port(tomcat.getPort());
|
||||
requestSpec.post();
|
||||
this.factory.convert((FilterableRequestSpecification) requestSpec);
|
||||
}
|
||||
@@ -199,7 +189,7 @@ public class RestAssuredRequestConverterTests {
|
||||
@Test
|
||||
public void stringBody() {
|
||||
RequestSpecification requestSpec = RestAssured.given().body("body")
|
||||
.port(this.port);
|
||||
.port(tomcat.getPort());
|
||||
requestSpec.post();
|
||||
OperationRequest request = this.factory
|
||||
.convert((FilterableRequestSpecification) requestSpec);
|
||||
@@ -209,7 +199,7 @@ public class RestAssuredRequestConverterTests {
|
||||
@Test
|
||||
public void objectBody() {
|
||||
RequestSpecification requestSpec = RestAssured.given().body(new ObjectBody("bar"))
|
||||
.port(this.port);
|
||||
.port(tomcat.getPort());
|
||||
requestSpec.post();
|
||||
OperationRequest request = this.factory
|
||||
.convert((FilterableRequestSpecification) requestSpec);
|
||||
@@ -220,7 +210,7 @@ public class RestAssuredRequestConverterTests {
|
||||
public void byteArrayInputStreamBody() {
|
||||
RequestSpecification requestSpec = RestAssured.given()
|
||||
.body(new ByteArrayInputStream(new byte[] { 1, 2, 3, 4 }))
|
||||
.port(this.port);
|
||||
.port(tomcat.getPort());
|
||||
requestSpec.post();
|
||||
OperationRequest request = this.factory
|
||||
.convert((FilterableRequestSpecification) requestSpec);
|
||||
@@ -230,7 +220,7 @@ public class RestAssuredRequestConverterTests {
|
||||
@Test
|
||||
public void fileBody() {
|
||||
RequestSpecification requestSpec = RestAssured.given()
|
||||
.body(new File("src/test/resources/body.txt")).port(this.port);
|
||||
.body(new File("src/test/resources/body.txt")).port(tomcat.getPort());
|
||||
requestSpec.post();
|
||||
OperationRequest request = this.factory
|
||||
.convert((FilterableRequestSpecification) requestSpec);
|
||||
@@ -241,7 +231,7 @@ public class RestAssuredRequestConverterTests {
|
||||
public void fileInputStreamBody() throws FileNotFoundException {
|
||||
FileInputStream inputStream = new FileInputStream("src/test/resources/body.txt");
|
||||
RequestSpecification requestSpec = RestAssured.given().body(inputStream)
|
||||
.port(this.port);
|
||||
.port(tomcat.getPort());
|
||||
requestSpec.post();
|
||||
this.thrown.expect(IllegalStateException.class);
|
||||
this.thrown.expectMessage("Cannot read content from input stream " + inputStream
|
||||
@@ -251,7 +241,7 @@ public class RestAssuredRequestConverterTests {
|
||||
|
||||
@Test
|
||||
public void multipartWithByteArrayInputStreamBody() {
|
||||
RequestSpecification requestSpec = RestAssured.given().port(this.port)
|
||||
RequestSpecification requestSpec = RestAssured.given().port(tomcat.getPort())
|
||||
.multiPart("foo", "foo.txt", new ByteArrayInputStream("foo".getBytes()));
|
||||
requestSpec.post();
|
||||
OperationRequest request = this.factory
|
||||
@@ -262,7 +252,7 @@ public class RestAssuredRequestConverterTests {
|
||||
|
||||
@Test
|
||||
public void multipartWithStringBody() {
|
||||
RequestSpecification requestSpec = RestAssured.given().port(this.port)
|
||||
RequestSpecification requestSpec = RestAssured.given().port(tomcat.getPort())
|
||||
.multiPart("control", "foo");
|
||||
requestSpec.post();
|
||||
OperationRequest request = this.factory
|
||||
@@ -273,7 +263,7 @@ public class RestAssuredRequestConverterTests {
|
||||
|
||||
@Test
|
||||
public void multipartWithByteArrayBody() {
|
||||
RequestSpecification requestSpec = RestAssured.given().port(this.port)
|
||||
RequestSpecification requestSpec = RestAssured.given().port(tomcat.getPort())
|
||||
.multiPart("control", "file", "foo".getBytes());
|
||||
requestSpec.post();
|
||||
OperationRequest request = this.factory
|
||||
@@ -284,7 +274,7 @@ public class RestAssuredRequestConverterTests {
|
||||
|
||||
@Test
|
||||
public void multipartWithFileBody() {
|
||||
RequestSpecification requestSpec = RestAssured.given().port(this.port)
|
||||
RequestSpecification requestSpec = RestAssured.given().port(tomcat.getPort())
|
||||
.multiPart(new File("src/test/resources/body.txt"));
|
||||
requestSpec.post();
|
||||
OperationRequest request = this.factory
|
||||
@@ -296,7 +286,7 @@ public class RestAssuredRequestConverterTests {
|
||||
@Test
|
||||
public void multipartWithFileInputStreamBody() throws FileNotFoundException {
|
||||
FileInputStream inputStream = new FileInputStream("src/test/resources/body.txt");
|
||||
RequestSpecification requestSpec = RestAssured.given().port(this.port)
|
||||
RequestSpecification requestSpec = RestAssured.given().port(tomcat.getPort())
|
||||
.multiPart("foo", "foo.txt", inputStream);
|
||||
requestSpec.post();
|
||||
this.thrown.expect(IllegalStateException.class);
|
||||
@@ -307,7 +297,7 @@ public class RestAssuredRequestConverterTests {
|
||||
|
||||
@Test
|
||||
public void multipartWithObjectBody() {
|
||||
RequestSpecification requestSpec = RestAssured.given().port(this.port)
|
||||
RequestSpecification requestSpec = RestAssured.given().port(tomcat.getPort())
|
||||
.multiPart("control", new ObjectBody("bar"));
|
||||
requestSpec.post();
|
||||
OperationRequest request = this.factory
|
||||
@@ -316,20 +306,6 @@ public class RestAssuredRequestConverterTests {
|
||||
is(equalTo("{\"foo\":\"bar\"}")));
|
||||
}
|
||||
|
||||
/**
|
||||
* Minimal test web application.
|
||||
*/
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@RestController
|
||||
static class TestApplication {
|
||||
|
||||
@RequestMapping("/")
|
||||
void handle() {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Sample object body to verify JSON serialization.
|
||||
*/
|
||||
|
||||
@@ -19,31 +19,19 @@ package org.springframework.restdocs.restassured;
|
||||
import java.io.File;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.util.Arrays;
|
||||
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.ClassRule;
|
||||
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.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.restdocs.JUnitRestDocumentation;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import static com.jayway.restassured.RestAssured.given;
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
@@ -83,19 +71,18 @@ import static org.springframework.restdocs.test.SnippetMatchers.snippet;
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
|
||||
public class RestAssuredRestDocumentationIntegrationTests {
|
||||
|
||||
@Rule
|
||||
public JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation();
|
||||
|
||||
@Value("${local.server.port}")
|
||||
private int port;
|
||||
@ClassRule
|
||||
public static TomcatServer tomcat = new TomcatServer();
|
||||
|
||||
@Test
|
||||
public void defaultSnippetGeneration() {
|
||||
given().port(this.port).filter(documentationConfiguration(this.restDocumentation))
|
||||
given().port(tomcat.getPort())
|
||||
.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");
|
||||
@@ -104,7 +91,8 @@ 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(tomcat.getPort())
|
||||
.filter(documentationConfiguration(this.restDocumentation))
|
||||
.filter(document("curl-snippet-with-content")).accept("application/json")
|
||||
.content("content").contentType(contentType).post("/").then()
|
||||
.statusCode(200);
|
||||
@@ -113,7 +101,7 @@ public class RestAssuredRestDocumentationIntegrationTests {
|
||||
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 "
|
||||
.content("$ curl 'http://localhost:" + tomcat.getPort() + "/' -i "
|
||||
+ "-X POST -H 'Accept: application/json' "
|
||||
+ "-H 'Content-Type: " + contentType + "' "
|
||||
+ "-d 'content'"))));
|
||||
@@ -122,7 +110,8 @@ public class RestAssuredRestDocumentationIntegrationTests {
|
||||
@Test
|
||||
public void curlSnippetWithCookies() throws Exception {
|
||||
String contentType = "text/plain; charset=UTF-8";
|
||||
given().port(this.port).filter(documentationConfiguration(this.restDocumentation))
|
||||
given().port(tomcat.getPort())
|
||||
.filter(documentationConfiguration(this.restDocumentation))
|
||||
.filter(document("curl-snippet-with-cookies")).accept("application/json")
|
||||
.contentType(contentType).cookie("cookieName", "cookieVal").get("/")
|
||||
.then().statusCode(200);
|
||||
@@ -130,7 +119,7 @@ public class RestAssuredRestDocumentationIntegrationTests {
|
||||
new File(
|
||||
"build/generated-snippets/curl-snippet-with-cookies/curl-request.adoc"),
|
||||
is(snippet(asciidoctor()).withContents(codeBlock(asciidoctor(), "bash")
|
||||
.content("$ curl 'http://localhost:" + this.port + "/' -i "
|
||||
.content("$ curl 'http://localhost:" + tomcat.getPort() + "/' -i "
|
||||
+ "-H 'Accept: application/json' " + "-H 'Content-Type: "
|
||||
+ contentType + "' "
|
||||
+ "--cookie 'cookieName=cookieVal'"))));
|
||||
@@ -138,7 +127,8 @@ public class RestAssuredRestDocumentationIntegrationTests {
|
||||
|
||||
@Test
|
||||
public void curlSnippetWithQueryStringOnPost() throws Exception {
|
||||
given().port(this.port).filter(documentationConfiguration(this.restDocumentation))
|
||||
given().port(tomcat.getPort())
|
||||
.filter(documentationConfiguration(this.restDocumentation))
|
||||
.filter(document("curl-snippet-with-query-string"))
|
||||
.accept("application/json").param("foo", "bar").param("a", "alpha")
|
||||
.post("/?foo=bar").then().statusCode(200);
|
||||
@@ -147,7 +137,7 @@ public class RestAssuredRestDocumentationIntegrationTests {
|
||||
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
|
||||
.content("$ curl " + "'http://localhost:" + tomcat.getPort()
|
||||
+ "/?foo=bar' -i -X POST "
|
||||
+ "-H 'Accept: application/json' " + "-H 'Content-Type: "
|
||||
+ contentType + "' " + "-d 'a=alpha'"))));
|
||||
@@ -155,7 +145,8 @@ public class RestAssuredRestDocumentationIntegrationTests {
|
||||
|
||||
@Test
|
||||
public void linksSnippet() throws Exception {
|
||||
given().port(this.port).filter(documentationConfiguration(this.restDocumentation))
|
||||
given().port(tomcat.getPort())
|
||||
.filter(documentationConfiguration(this.restDocumentation))
|
||||
.filter(document("links",
|
||||
links(linkWithRel("rel").description("The description"))))
|
||||
.accept("application/json").get("/").then().statusCode(200);
|
||||
@@ -166,7 +157,8 @@ public class RestAssuredRestDocumentationIntegrationTests {
|
||||
|
||||
@Test
|
||||
public void pathParametersSnippet() throws Exception {
|
||||
given().port(this.port).filter(documentationConfiguration(this.restDocumentation))
|
||||
given().port(tomcat.getPort())
|
||||
.filter(documentationConfiguration(this.restDocumentation))
|
||||
.filter(document("path-parameters",
|
||||
pathParameters(
|
||||
parameterWithName("foo").description("The description"))))
|
||||
@@ -178,7 +170,8 @@ public class RestAssuredRestDocumentationIntegrationTests {
|
||||
|
||||
@Test
|
||||
public void requestParametersSnippet() throws Exception {
|
||||
given().port(this.port).filter(documentationConfiguration(this.restDocumentation))
|
||||
given().port(tomcat.getPort())
|
||||
.filter(documentationConfiguration(this.restDocumentation))
|
||||
.filter(document("request-parameters",
|
||||
requestParameters(
|
||||
parameterWithName("foo").description("The description"))))
|
||||
@@ -192,7 +185,8 @@ public class RestAssuredRestDocumentationIntegrationTests {
|
||||
|
||||
@Test
|
||||
public void requestFieldsSnippet() throws Exception {
|
||||
given().port(this.port).filter(documentationConfiguration(this.restDocumentation))
|
||||
given().port(tomcat.getPort())
|
||||
.filter(documentationConfiguration(this.restDocumentation))
|
||||
.filter(document("request-fields",
|
||||
requestFields(fieldWithPath("a").description("The description"))))
|
||||
.accept("application/json").content("{\"a\":\"alpha\"}").post("/").then()
|
||||
@@ -204,7 +198,8 @@ public class RestAssuredRestDocumentationIntegrationTests {
|
||||
|
||||
@Test
|
||||
public void requestPartsSnippet() throws Exception {
|
||||
given().port(this.port).filter(documentationConfiguration(this.restDocumentation))
|
||||
given().port(tomcat.getPort())
|
||||
.filter(documentationConfiguration(this.restDocumentation))
|
||||
.filter(document("request-parts",
|
||||
requestParts(partWithName("a").description("The description"))))
|
||||
.multiPart("a", "foo").post("/upload").then().statusCode(200);
|
||||
@@ -215,7 +210,8 @@ public class RestAssuredRestDocumentationIntegrationTests {
|
||||
|
||||
@Test
|
||||
public void responseFieldsSnippet() throws Exception {
|
||||
given().port(this.port).filter(documentationConfiguration(this.restDocumentation))
|
||||
given().port(tomcat.getPort())
|
||||
.filter(documentationConfiguration(this.restDocumentation))
|
||||
.filter(document("response-fields",
|
||||
responseFields(fieldWithPath("a").description("The description"),
|
||||
subsectionWithPath("links")
|
||||
@@ -229,7 +225,8 @@ public class RestAssuredRestDocumentationIntegrationTests {
|
||||
|
||||
@Test
|
||||
public void parameterizedOutputDirectory() throws Exception {
|
||||
given().port(this.port).filter(documentationConfiguration(this.restDocumentation))
|
||||
given().port(tomcat.getPort())
|
||||
.filter(documentationConfiguration(this.restDocumentation))
|
||||
.filter(document("{method-name}")).get("/").then().statusCode(200);
|
||||
assertExpectedSnippetFilesExist(
|
||||
new File("build/generated-snippets/parameterized-output-directory"),
|
||||
@@ -238,7 +235,7 @@ public class RestAssuredRestDocumentationIntegrationTests {
|
||||
|
||||
@Test
|
||||
public void multiStep() throws Exception {
|
||||
RequestSpecification spec = new RequestSpecBuilder().setPort(this.port)
|
||||
RequestSpecification spec = new RequestSpecBuilder().setPort(tomcat.getPort())
|
||||
.addFilter(documentationConfiguration(this.restDocumentation))
|
||||
.addFilter(document("{method-name}-{step}")).build();
|
||||
given(spec).get("/").then().statusCode(200);
|
||||
@@ -258,7 +255,7 @@ public class RestAssuredRestDocumentationIntegrationTests {
|
||||
@Test
|
||||
public void additionalSnippets() throws Exception {
|
||||
RestDocumentationFilter documentation = document("{method-name}-{step}");
|
||||
RequestSpecification spec = new RequestSpecBuilder().setPort(this.port)
|
||||
RequestSpecification spec = new RequestSpecBuilder().setPort(tomcat.getPort())
|
||||
.addFilter(documentationConfiguration(this.restDocumentation))
|
||||
.addFilter(documentation).build();
|
||||
given(spec)
|
||||
@@ -275,7 +272,8 @@ public class RestAssuredRestDocumentationIntegrationTests {
|
||||
@Test
|
||||
public void preprocessedRequest() throws Exception {
|
||||
Pattern pattern = Pattern.compile("(\"alpha\")");
|
||||
given().port(this.port).filter(documentationConfiguration(this.restDocumentation))
|
||||
given().port(tomcat.getPort())
|
||||
.filter(documentationConfiguration(this.restDocumentation))
|
||||
.header("a", "alpha").header("b", "bravo").contentType("application/json")
|
||||
.accept("application/json").content("{\"a\":\"alpha\"}")
|
||||
.filter(document("original-request"))
|
||||
@@ -292,7 +290,7 @@ public class RestAssuredRestDocumentationIntegrationTests {
|
||||
.header("a", "alpha").header("b", "bravo")
|
||||
.header("Accept", MediaType.APPLICATION_JSON_VALUE)
|
||||
.header("Content-Type", "application/json; charset=UTF-8")
|
||||
.header("Host", "localhost:" + this.port)
|
||||
.header("Host", "localhost:" + tomcat.getPort())
|
||||
.header("Content-Length", "13")
|
||||
.content("{\"a\":\"alpha\"}"))));
|
||||
String prettyPrinted = String.format("{%n \"a\" : \"<<beta>>\"%n}");
|
||||
@@ -310,7 +308,8 @@ public class RestAssuredRestDocumentationIntegrationTests {
|
||||
@Test
|
||||
public void preprocessedResponse() throws Exception {
|
||||
Pattern pattern = Pattern.compile("(\"alpha\")");
|
||||
given().port(this.port).filter(documentationConfiguration(this.restDocumentation))
|
||||
given().port(tomcat.getPort())
|
||||
.filter(documentationConfiguration(this.restDocumentation))
|
||||
.filter(document("original-response"))
|
||||
.filter(document("preprocessed-response", preprocessResponse(
|
||||
prettyPrint(), maskLinks(),
|
||||
@@ -340,7 +339,7 @@ public class RestAssuredRestDocumentationIntegrationTests {
|
||||
ClassLoader previous = Thread.currentThread().getContextClassLoader();
|
||||
Thread.currentThread().setContextClassLoader(classLoader);
|
||||
try {
|
||||
given().port(this.port).accept("application/json")
|
||||
given().port(tomcat.getPort()).accept("application/json")
|
||||
.filter(documentationConfiguration(this.restDocumentation))
|
||||
.filter(document("custom-snippet-template")).get("/").then()
|
||||
.statusCode(200);
|
||||
@@ -361,38 +360,4 @@ public class RestAssuredRestDocumentationIntegrationTests {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Minimal test application called by the tests.
|
||||
*/
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@RestController
|
||||
static class TestApplication {
|
||||
|
||||
@RequestMapping(value = "/", produces = MediaType.APPLICATION_JSON_VALUE)
|
||||
public ResponseEntity<Map<String, Object>> foo() {
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
response.put("a", "alpha");
|
||||
Map<String, String> link = new HashMap<>();
|
||||
link.put("rel", "rel");
|
||||
link.put("href", "href");
|
||||
response.put("links", Arrays.asList(link));
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.add("a", "alpha");
|
||||
headers.add("Foo", "http://localhost:12345/foo/bar");
|
||||
return new ResponseEntity<>(response, headers, HttpStatus.OK);
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/company/5", produces = MediaType.APPLICATION_JSON_VALUE)
|
||||
public String bar() {
|
||||
return "{\"companyName\": \"FooBar\",\"employee\": [{\"name\": \"Lorem\",\"age\": \"42\"},{\"name\": \"Ipsum\",\"age\": \"24\"}]}";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
||||
public void upload() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
* Copyright 2014-2017 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.restdocs.restassured;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.apache.catalina.Context;
|
||||
import org.apache.catalina.LifecycleException;
|
||||
import org.apache.catalina.startup.Tomcat;
|
||||
import org.junit.rules.ExternalResource;
|
||||
|
||||
/**
|
||||
* {@link ExternalResource} that starts and stops a Tomcat server.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
class TomcatServer extends ExternalResource {
|
||||
|
||||
private Tomcat tomcat;
|
||||
|
||||
private int port;
|
||||
|
||||
@Override
|
||||
protected void before() throws LifecycleException {
|
||||
this.tomcat = new Tomcat();
|
||||
this.tomcat.getConnector().setPort(0);
|
||||
Context context = this.tomcat.addContext("/", null);
|
||||
this.tomcat.addServlet("/", "test", new TestServlet());
|
||||
context.addServletMappingDecoded("/", "test");
|
||||
this.tomcat.start();
|
||||
this.port = this.tomcat.getConnector().getLocalPort();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void after() {
|
||||
try {
|
||||
this.tomcat.stop();
|
||||
}
|
||||
catch (LifecycleException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
int getPort() {
|
||||
return this.port;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link HttpServlet} used to handle requests in the tests.
|
||||
*/
|
||||
private static final class TestServlet extends HttpServlet {
|
||||
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response)
|
||||
throws ServletException, IOException {
|
||||
respondWithJson(response);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doPost(HttpServletRequest request, HttpServletResponse response)
|
||||
throws ServletException, IOException {
|
||||
respondWithJson(response);
|
||||
}
|
||||
|
||||
private void respondWithJson(HttpServletResponse response)
|
||||
throws IOException, JsonProcessingException {
|
||||
response.setCharacterEncoding("UTF-8");
|
||||
response.setContentType("application/json");
|
||||
Map<String, Object> content = new HashMap<>();
|
||||
content.put("a", "alpha");
|
||||
Map<String, String> link = new HashMap<>();
|
||||
link.put("rel", "rel");
|
||||
link.put("href", "href");
|
||||
content.put("links", Arrays.asList(link));
|
||||
response.getWriter().println(new ObjectMapper().writeValueAsString(content));
|
||||
response.setHeader("a", "alpha");
|
||||
response.setHeader("Foo", "http://localhost:12345/foo/bar");
|
||||
response.flushBuffer();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user