Add support for documenting body of a request, response or request part
Closes gh-318 Closes gh-319
This commit is contained in:
@@ -26,6 +26,7 @@ import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.springframework.restdocs.payload.PayloadDocumentation.beneathPath;
|
||||
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
|
||||
import static org.springframework.restdocs.payload.PayloadDocumentation.requestFields;
|
||||
import static org.springframework.restdocs.payload.PayloadDocumentation.responseBody;
|
||||
import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields;
|
||||
import static org.springframework.restdocs.payload.PayloadDocumentation.subsectionWithPath;
|
||||
import static org.springframework.restdocs.restassured.RestAssuredRestDocumentation.document;
|
||||
@@ -46,7 +47,7 @@ public class Payload {
|
||||
.then().assertThat().statusCode(is(200));
|
||||
// end::response[]
|
||||
}
|
||||
|
||||
|
||||
public void subsection() throws Exception {
|
||||
// tag::subsection[]
|
||||
RestAssured.given(this.spec).accept("application/json")
|
||||
@@ -107,15 +108,24 @@ public class Payload {
|
||||
// end::book-array[]
|
||||
}
|
||||
|
||||
public void subsectionBeneathPath() throws Exception {
|
||||
// tag::beneath-path[]
|
||||
public void fieldsSubsection() throws Exception {
|
||||
// tag::fields-subsection[]
|
||||
RestAssured.given(this.spec).accept("application/json")
|
||||
.filter(document("location", responseFields(beneathPath("weather.temperature"), // <1>
|
||||
fieldWithPath("high").description("The forecast high in degrees celcius"), // <2>
|
||||
fieldWithPath("low").description("The forecast low in degrees celcius"))))
|
||||
.when().get("/locations/1")
|
||||
.then().assertThat().statusCode(is(200));
|
||||
// end::beneath-path[]
|
||||
// end::fields-subsection[]
|
||||
}
|
||||
|
||||
public void bodySubsection() throws Exception {
|
||||
// tag::body-subsection[]
|
||||
RestAssured.given(this.spec).accept("application/json")
|
||||
.filter(document("location", responseBody(beneathPath("weather.temperature")))) // <1>
|
||||
.when().get("/locations/1")
|
||||
.then().assertThat().statusCode(is(200));
|
||||
// end::body-subsection[]
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ import com.jayway.restassured.specification.RequestSpecification;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
|
||||
import static org.springframework.restdocs.payload.PayloadDocumentation.requestPartBody;
|
||||
import static org.springframework.restdocs.payload.PayloadDocumentation.requestPartFields;
|
||||
import static org.springframework.restdocs.restassured.RestAssuredRestDocumentation.document;
|
||||
|
||||
@@ -32,8 +33,8 @@ public class RequestPartPayload {
|
||||
|
||||
private RequestSpecification spec;
|
||||
|
||||
public void response() throws Exception {
|
||||
// tag::payload[]
|
||||
public void fields() throws Exception {
|
||||
// tag::fields[]
|
||||
Map<String, String> metadata = new HashMap<>();
|
||||
metadata.put("version", "1.0");
|
||||
RestAssured.given(this.spec).accept("application/json")
|
||||
@@ -42,7 +43,19 @@ public class RequestPartPayload {
|
||||
.when().multiPart("image", new File("image.png"), "image/png")
|
||||
.multiPart("metadata", metadata).post("images")
|
||||
.then().assertThat().statusCode(is(200));
|
||||
// end::payload[]
|
||||
// end::fields[]
|
||||
}
|
||||
|
||||
public void body() throws Exception {
|
||||
// tag::body[]
|
||||
Map<String, String> metadata = new HashMap<>();
|
||||
metadata.put("version", "1.0");
|
||||
RestAssured.given(this.spec).accept("application/json")
|
||||
.filter(document("image-upload", requestPartBody("metadata"))) // <1>
|
||||
.when().multiPart("image", new File("image.png"), "image/png")
|
||||
.multiPart("metadata", metadata).post("images")
|
||||
.then().assertThat().statusCode(is(200));
|
||||
// end::body[]
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user