Make it easier to document common portions of req and resp payloads

This commit adds a new andWithPrefix(String, FieldDescriptor[]) method
to both RequestFieldsSnippet and ResponseFieldsSnippet. It can be
used to add descriptors to an existing snippet, applying the given
prefix to the additional descriptors as it does so. This allows the
descriptors for a portion of a payload to be created once and then
reused, irrespective of where in the payload the portion appears.

Closes gh-221
This commit is contained in:
Andy Wilkinson
2016-04-22 10:30:52 +01:00
parent 2897b8d1b4
commit b62c5f0374
9 changed files with 249 additions and 2 deletions

View File

@@ -16,6 +16,7 @@
package com.example.restassured;
import org.springframework.restdocs.payload.FieldDescriptor;
import org.springframework.restdocs.payload.JsonFieldType;
import com.jayway.restassured.RestAssured;
@@ -72,4 +73,27 @@ public class Payload {
.then().assertThat().statusCode(is(200));
}
public void descriptorReuse() throws Exception {
FieldDescriptor[] book = new FieldDescriptor[] {
fieldWithPath("title").description("Title of the book"),
fieldWithPath("author").description("Author of the book") };
// tag::single-book[]
RestAssured.given(this.spec).accept("application/json")
.filter(document("book", responseFields(book))) // <1>
.when().get("/books/1")
.then().assertThat().statusCode(is(200));
// end::single-book[]
// tag::book-array[]
RestAssured.given(this.spec).accept("application/json")
.filter(document("books", responseFields(
fieldWithPath("[]").description("An array of books")) // <1>
.andWithPrefix("[].", book))) // <2>
.when().get("/books/1")
.then().assertThat().statusCode(is(200));
// end::book-array[]
}
}