Merge branch '1.1.x'

This commit is contained in:
Andy Wilkinson
2016-08-24 10:05:58 +01:00
24 changed files with 917 additions and 1123 deletions

View File

@@ -52,6 +52,9 @@ public abstract class AbstractSnippetTests {
@Rule
public ExpectedSnippet snippet;
@Rule
public OperationBuilder operationBuilder;
@Parameters(name = "{0}")
public static List<Object[]> parameters() {
return Arrays.asList(new Object[] { "Asciidoctor", asciidoctor() },
@@ -61,6 +64,7 @@ public abstract class AbstractSnippetTests {
public AbstractSnippetTests(String name, TemplateFormat templateFormat) {
this.snippet = new ExpectedSnippet(templateFormat);
this.templateFormat = templateFormat;
this.operationBuilder = new OperationBuilder(this.templateFormat);
}
public CodeBlockMatcher<?> codeBlock(String language) {
@@ -84,11 +88,6 @@ public abstract class AbstractSnippetTests {
return SnippetMatchers.httpResponse(this.templateFormat, responseStatus);
}
public OperationBuilder operationBuilder(String name) {
return new OperationBuilder(name, this.snippet.getOutputDirectory(),
this.templateFormat);
}
protected FileSystemResource snippetResource(String name) {
return new FileSystemResource("src/test/resources/custom-snippet-templates/"
+ this.templateFormat.getId() + "/" + name + ".snippet");

View File

@@ -55,245 +55,197 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
@Test
public void getRequest() throws IOException {
this.snippet.expectCurlRequest("get-request").withContents(
this.snippet.expectCurlRequest().withContents(
codeBlock("bash").content("$ curl 'http://localhost/foo' -i"));
new CurlRequestSnippet().document(
operationBuilder("get-request").request("http://localhost/foo").build());
new CurlRequestSnippet()
.document(this.operationBuilder.request("http://localhost/foo").build());
}
@Test
public void getRequestWithParameter() throws IOException {
this.snippet.expectCurlRequest("get-request").withContents(
this.snippet.expectCurlRequest().withContents(
codeBlock("bash").content("$ curl 'http://localhost/foo?a=alpha' -i"));
new CurlRequestSnippet().document(operationBuilder("get-request")
new CurlRequestSnippet().document(this.operationBuilder
.request("http://localhost/foo").param("a", "alpha").build());
}
@Test
public void nonGetRequest() throws IOException {
this.snippet.expectCurlRequest("non-get-request").withContents(
this.snippet.expectCurlRequest().withContents(
codeBlock("bash").content("$ curl 'http://localhost/foo' -i -X POST"));
new CurlRequestSnippet().document(operationBuilder("non-get-request")
new CurlRequestSnippet().document(this.operationBuilder
.request("http://localhost/foo").method("POST").build());
}
@Test
public void requestWithContent() throws IOException {
this.snippet.expectCurlRequest("request-with-content")
.withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo' -i -d 'content'"));
new CurlRequestSnippet().document(operationBuilder("request-with-content")
this.snippet.expectCurlRequest().withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo' -i -d 'content'"));
new CurlRequestSnippet().document(this.operationBuilder
.request("http://localhost/foo").content("content").build());
}
@Test
public void getRequestWithQueryString() throws IOException {
this.snippet.expectCurlRequest("request-with-query-string")
.withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo?param=value' -i"));
new CurlRequestSnippet().document(operationBuilder("request-with-query-string")
this.snippet.expectCurlRequest().withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo?param=value' -i"));
new CurlRequestSnippet().document(this.operationBuilder
.request("http://localhost/foo?param=value").build());
}
@Test
public void getRequestWithTotallyOverlappingQueryStringAndParameters()
throws IOException {
this.snippet
.expectCurlRequest(
"request-with-totally-overlapping-query-string-and-parameters")
.withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo?param=value' -i"));
new CurlRequestSnippet().document(operationBuilder(
"request-with-totally-overlapping-query-string-and-parameters")
.request("http://localhost/foo?param=value")
this.snippet.expectCurlRequest().withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo?param=value' -i"));
new CurlRequestSnippet().document(
this.operationBuilder.request("http://localhost/foo?param=value")
.param("param", "value").build());
}
@Test
public void getRequestWithPartiallyOverlappingQueryStringAndParameters()
throws IOException {
this.snippet
.expectCurlRequest(
"request-with-partially-overlapping-query-string-and-parameters")
.withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo?a=alpha&b=bravo' -i"));
new CurlRequestSnippet().document(operationBuilder(
"request-with-partially-overlapping-query-string-and-parameters")
.request("http://localhost/foo?a=alpha").param("a", "alpha")
.param("b", "bravo").build());
this.snippet.expectCurlRequest().withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo?a=alpha&b=bravo' -i"));
new CurlRequestSnippet()
.document(this.operationBuilder.request("http://localhost/foo?a=alpha")
.param("a", "alpha").param("b", "bravo").build());
}
@Test
public void getRequestWithDisjointQueryStringAndParameters() throws IOException {
this.snippet
.expectCurlRequest(
"request-with-partially-overlapping-query-string-and-parameters")
.withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo?a=alpha&b=bravo' -i"));
new CurlRequestSnippet().document(operationBuilder(
"request-with-partially-overlapping-query-string-and-parameters")
.request("http://localhost/foo?a=alpha").param("b", "bravo")
.build());
this.snippet.expectCurlRequest().withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo?a=alpha&b=bravo' -i"));
new CurlRequestSnippet().document(this.operationBuilder
.request("http://localhost/foo?a=alpha").param("b", "bravo").build());
}
@Test
public void getRequestWithQueryStringWithNoValue() throws IOException {
this.snippet.expectCurlRequest("request-with-query-string-with-no-value")
.withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo?param' -i"));
new CurlRequestSnippet()
.document(operationBuilder("request-with-query-string-with-no-value")
.request("http://localhost/foo?param").build());
this.snippet.expectCurlRequest().withContents(
codeBlock("bash").content("$ curl 'http://localhost/foo?param' -i"));
new CurlRequestSnippet().document(
this.operationBuilder.request("http://localhost/foo?param").build());
}
@Test
public void postRequestWithQueryString() throws IOException {
this.snippet.expectCurlRequest("post-request-with-query-string")
.withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo?param=value' -i -X POST"));
new CurlRequestSnippet()
.document(operationBuilder("post-request-with-query-string")
.request("http://localhost/foo?param=value").method("POST")
.build());
this.snippet.expectCurlRequest().withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo?param=value' -i -X POST"));
new CurlRequestSnippet().document(this.operationBuilder
.request("http://localhost/foo?param=value").method("POST").build());
}
@Test
public void postRequestWithQueryStringWithNoValue() throws IOException {
this.snippet.expectCurlRequest("post-request-with-query-string-with-no-value")
.withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo?param' -i -X POST"));
new CurlRequestSnippet()
.document(operationBuilder("post-request-with-query-string-with-no-value")
.request("http://localhost/foo?param").method("POST").build());
this.snippet.expectCurlRequest().withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo?param' -i -X POST"));
new CurlRequestSnippet().document(this.operationBuilder
.request("http://localhost/foo?param").method("POST").build());
}
@Test
public void postRequestWithOneParameter() throws IOException {
this.snippet.expectCurlRequest("post-request-with-one-parameter")
.withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo' -i -X POST -d 'k1=v1'"));
this.snippet.expectCurlRequest().withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo' -i -X POST -d 'k1=v1'"));
new CurlRequestSnippet()
.document(operationBuilder("post-request-with-one-parameter")
.request("http://localhost/foo").method("POST").param("k1", "v1")
.build());
.document(this.operationBuilder.request("http://localhost/foo")
.method("POST").param("k1", "v1").build());
}
@Test
public void postRequestWithOneParameterWithNoValue() throws IOException {
this.snippet.expectCurlRequest("post-request-with-one-parameter-with-no-value")
.withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo' -i -X POST -d 'k1='"));
new CurlRequestSnippet().document(
operationBuilder("post-request-with-one-parameter-with-no-value")
.request("http://localhost/foo").method("POST").param("k1")
.build());
this.snippet.expectCurlRequest().withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo' -i -X POST -d 'k1='"));
new CurlRequestSnippet().document(this.operationBuilder
.request("http://localhost/foo").method("POST").param("k1").build());
}
@Test
public void postRequestWithMultipleParameters() throws IOException {
this.snippet.expectCurlRequest("post-request-with-multiple-parameters")
this.snippet.expectCurlRequest()
.withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo' -i -X POST"
+ " -d 'k1=v1&k1=v1-bis&k2=v2'"));
new CurlRequestSnippet()
.document(operationBuilder("post-request-with-multiple-parameters")
.request("http://localhost/foo").method("POST")
new CurlRequestSnippet().document(
this.operationBuilder.request("http://localhost/foo").method("POST")
.param("k1", "v1", "v1-bis").param("k2", "v2").build());
}
@Test
public void postRequestWithUrlEncodedParameter() throws IOException {
this.snippet.expectCurlRequest("post-request-with-url-encoded-parameter")
.withContents(codeBlock("bash").content(
"$ curl 'http://localhost/foo' -i -X POST -d 'k1=a%26b'"));
this.snippet.expectCurlRequest().withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo' -i -X POST -d 'k1=a%26b'"));
new CurlRequestSnippet()
.document(operationBuilder("post-request-with-url-encoded-parameter")
.request("http://localhost/foo").method("POST").param("k1", "a&b")
.build());
.document(this.operationBuilder.request("http://localhost/foo")
.method("POST").param("k1", "a&b").build());
}
@Test
public void postRequestWithDisjointQueryStringAndParameter() throws IOException {
this.snippet
.expectCurlRequest(
"post-request-with-disjoint-query-string-and-parameter")
.withContents(codeBlock("bash").content(
"$ curl 'http://localhost/foo?a=alpha' -i -X POST -d 'b=bravo'"));
new CurlRequestSnippet().document(
operationBuilder("post-request-with-disjoint-query-string-and-parameter")
.request("http://localhost/foo?a=alpha").method("POST")
.param("b", "bravo").build());
this.snippet.expectCurlRequest().withContents(codeBlock("bash").content(
"$ curl 'http://localhost/foo?a=alpha' -i -X POST -d 'b=bravo'"));
new CurlRequestSnippet()
.document(this.operationBuilder.request("http://localhost/foo?a=alpha")
.method("POST").param("b", "bravo").build());
}
@Test
public void postRequestWithTotallyOverlappingQueryStringAndParameters()
throws IOException {
this.snippet
.expectCurlRequest(
"post-request-with-totally-overlapping-query-string-and-parameters")
.withContents(codeBlock("bash").content(
"$ curl 'http://localhost/foo?a=alpha&b=bravo' -i -X POST"));
new CurlRequestSnippet().document(operationBuilder(
"post-request-with-totally-overlapping-query-string-and-parameters")
.request("http://localhost/foo?a=alpha&b=bravo").method("POST")
.param("a", "alpha").param("b", "bravo").build());
this.snippet.expectCurlRequest().withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo?a=alpha&b=bravo' -i -X POST"));
new CurlRequestSnippet().document(
this.operationBuilder.request("http://localhost/foo?a=alpha&b=bravo")
.method("POST").param("a", "alpha").param("b", "bravo").build());
}
@Test
public void postRequestWithPartiallyOverlappingQueryStringAndParameters()
throws IOException {
this.snippet
.expectCurlRequest(
"post-request-with-partially-overlapping-query-string-and-parameters")
.withContents(codeBlock("bash").content(
"$ curl 'http://localhost/foo?a=alpha' -i -X POST -d 'b=bravo'"));
new CurlRequestSnippet().document(operationBuilder(
"post-request-with-partially-overlapping-query-string-and-parameters")
.request("http://localhost/foo?a=alpha").method("POST")
.param("a", "alpha").param("b", "bravo").build());
this.snippet.expectCurlRequest().withContents(codeBlock("bash").content(
"$ curl 'http://localhost/foo?a=alpha' -i -X POST -d 'b=bravo'"));
new CurlRequestSnippet()
.document(this.operationBuilder.request("http://localhost/foo?a=alpha")
.method("POST").param("a", "alpha").param("b", "bravo").build());
}
@Test
public void putRequestWithOneParameter() throws IOException {
this.snippet.expectCurlRequest("put-request-with-one-parameter")
.withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo' -i -X PUT -d 'k1=v1'"));
new CurlRequestSnippet()
.document(operationBuilder("put-request-with-one-parameter")
.request("http://localhost/foo").method("PUT").param("k1", "v1")
.build());
this.snippet.expectCurlRequest().withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo' -i -X PUT -d 'k1=v1'"));
new CurlRequestSnippet().document(this.operationBuilder
.request("http://localhost/foo").method("PUT").param("k1", "v1").build());
}
@Test
public void putRequestWithMultipleParameters() throws IOException {
this.snippet.expectCurlRequest("put-request-with-multiple-parameters")
this.snippet.expectCurlRequest()
.withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo' -i -X PUT"
+ " -d 'k1=v1&k1=v1-bis&k2=v2'"));
new CurlRequestSnippet()
.document(operationBuilder("put-request-with-multiple-parameters")
.request("http://localhost/foo").method("PUT").param("k1", "v1")
.param("k1", "v1-bis").param("k2", "v2").build());
new CurlRequestSnippet().document(this.operationBuilder
.request("http://localhost/foo").method("PUT").param("k1", "v1")
.param("k1", "v1-bis").param("k2", "v2").build());
}
@Test
public void putRequestWithUrlEncodedParameter() throws IOException {
this.snippet.expectCurlRequest("put-request-with-url-encoded-parameter")
.withContents(codeBlock("bash").content(
"$ curl 'http://localhost/foo' -i -X PUT -d 'k1=a%26b'"));
this.snippet.expectCurlRequest().withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo' -i -X PUT -d 'k1=a%26b'"));
new CurlRequestSnippet()
.document(operationBuilder("put-request-with-url-encoded-parameter")
.request("http://localhost/foo").method("PUT").param("k1", "a&b")
.build());
.document(this.operationBuilder.request("http://localhost/foo")
.method("PUT").param("k1", "a&b").build());
}
@Test
public void requestWithHeaders() throws IOException {
this.snippet.expectCurlRequest("request-with-headers")
this.snippet.expectCurlRequest()
.withContents(codeBlock("bash").content("$ curl 'http://localhost/foo' -i"
+ " -H 'Content-Type: application/json' -H 'a: alpha'"));
new CurlRequestSnippet().document(
operationBuilder("request-with-headers").request("http://localhost/foo")
new CurlRequestSnippet()
.document(this.operationBuilder.request("http://localhost/foo")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_JSON_VALUE)
.header("a", "alpha").build());
@@ -304,15 +256,12 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
String expectedContent = "$ curl 'http://localhost/upload' -i -X POST -H "
+ "'Content-Type: multipart/form-data' -F "
+ "'metadata={\"description\": \"foo\"}'";
this.snippet.expectCurlRequest("multipart-post-no-original-filename")
this.snippet.expectCurlRequest()
.withContents(codeBlock("bash").content(expectedContent));
new CurlRequestSnippet()
.document(operationBuilder("multipart-post-no-original-filename")
.request("http://localhost/upload").method("POST")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.MULTIPART_FORM_DATA_VALUE)
.part("metadata", "{\"description\": \"foo\"}".getBytes())
.build());
new CurlRequestSnippet().document(this.operationBuilder
.request("http://localhost/upload").method("POST")
.header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE)
.part("metadata", "{\"description\": \"foo\"}".getBytes()).build());
}
@Test
@@ -320,11 +269,10 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
String expectedContent = "$ curl 'http://localhost/upload' -i -X POST -H "
+ "'Content-Type: multipart/form-data' -F "
+ "'image=@documents/images/example.png;type=image/png'";
this.snippet.expectCurlRequest("multipart-post-with-content-type")
this.snippet.expectCurlRequest()
.withContents(codeBlock("bash").content(expectedContent));
new CurlRequestSnippet()
.document(operationBuilder("multipart-post-with-content-type")
.request("http://localhost/upload").method("POST")
new CurlRequestSnippet().document(
this.operationBuilder.request("http://localhost/upload").method("POST")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.MULTIPART_FORM_DATA_VALUE)
.part("image", new byte[0])
@@ -337,11 +285,10 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
String expectedContent = "$ curl 'http://localhost/upload' -i -X POST -H "
+ "'Content-Type: multipart/form-data' -F "
+ "'image=@documents/images/example.png'";
this.snippet.expectCurlRequest("multipart-post")
this.snippet.expectCurlRequest()
.withContents(codeBlock("bash").content(expectedContent));
new CurlRequestSnippet()
.document(operationBuilder("multipart-post")
.request("http://localhost/upload").method("POST")
new CurlRequestSnippet().document(
this.operationBuilder.request("http://localhost/upload").method("POST")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.MULTIPART_FORM_DATA_VALUE)
.part("image", new byte[0])
@@ -354,11 +301,10 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
+ "'Content-Type: multipart/form-data' -F "
+ "'image=@documents/images/example.png' -F 'a=apple' -F 'a=avocado' "
+ "-F 'b=banana'";
this.snippet.expectCurlRequest("multipart-post-with-parameters")
this.snippet.expectCurlRequest()
.withContents(codeBlock("bash").content(expectedContent));
new CurlRequestSnippet()
.document(operationBuilder("multipart-post-with-parameters")
.request("http://localhost/upload").method("POST")
new CurlRequestSnippet().document(
this.operationBuilder.request("http://localhost/upload").method("POST")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.MULTIPART_FORM_DATA_VALUE)
.part("image", new byte[0])
@@ -368,10 +314,10 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
@Test
public void basicAuthCredentialsAreSuppliedUsingUserOption() throws IOException {
this.snippet.expectCurlRequest("basic-auth").withContents(codeBlock("bash")
this.snippet.expectCurlRequest().withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo' -i -u 'user:secret'"));
new CurlRequestSnippet()
.document(operationBuilder("basic-auth").request("http://localhost/foo")
.document(this.operationBuilder.request("http://localhost/foo")
.header(HttpHeaders.AUTHORIZATION,
"Basic " + Base64Utils
.encodeToString("user:secret".getBytes()))
@@ -380,7 +326,7 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
@Test
public void customAttributes() throws IOException {
this.snippet.expectCurlRequest("custom-attributes")
this.snippet.expectCurlRequest()
.withContents(containsString("curl request title"));
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
given(resolver.resolveTemplateResource("curl-request"))
@@ -389,7 +335,7 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
attributes(
key("title").value("curl request title")))
.document(
operationBuilder("custom-attributes")
this.operationBuilder
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(
resolver))
@@ -398,12 +344,12 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
@Test
public void customHostHeaderIsIncluded() throws IOException {
this.snippet.expectCurlRequest("custom-host-header")
this.snippet.expectCurlRequest()
.withContents(codeBlock("bash").content(
"$ curl 'http://localhost/foo' -i" + " -H 'Host: api.example.com'"
+ " -H 'Content-Type: application/json' -H 'a: alpha'"));
new CurlRequestSnippet().document(
operationBuilder("custom-host-header").request("http://localhost/foo")
new CurlRequestSnippet()
.document(this.operationBuilder.request("http://localhost/foo")
.header(HttpHeaders.HOST, "api.example.com")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_JSON_VALUE)
@@ -412,15 +358,13 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
@Test
public void postWithContentAndParameters() throws IOException {
this.snippet.expectCurlRequest("post-with-content-and-parameters")
this.snippet.expectCurlRequest()
.withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo?a=alpha&b=bravo' -i "
+ "-X POST -d 'Some content'"));
new CurlRequestSnippet()
.document(operationBuilder("post-with-content-and-parameters")
.request("http://localhost/foo").param("a", "alpha")
.method("POST").param("b", "bravo").content("Some content")
.build());
new CurlRequestSnippet().document(this.operationBuilder
.request("http://localhost/foo").param("a", "alpha").method("POST")
.param("b", "bravo").content("Some content").build());
}
}

View File

@@ -56,245 +56,197 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
@Test
public void getRequest() throws IOException {
this.snippet.expectHttpieRequest("get-request").withContents(
this.snippet.expectHttpieRequest().withContents(
codeBlock("bash").content("$ http GET 'http://localhost/foo'"));
new HttpieRequestSnippet().document(
operationBuilder("get-request").request("http://localhost/foo").build());
new HttpieRequestSnippet()
.document(this.operationBuilder.request("http://localhost/foo").build());
}
@Test
public void getRequestWithParameter() throws IOException {
this.snippet.expectHttpieRequest("get-request-with-parameter").withContents(
this.snippet.expectHttpieRequest().withContents(
codeBlock("bash").content("$ http GET 'http://localhost/foo?a=alpha'"));
new HttpieRequestSnippet().document(operationBuilder("get-request-with-parameter")
new HttpieRequestSnippet().document(this.operationBuilder
.request("http://localhost/foo").param("a", "alpha").build());
}
@Test
public void nonGetRequest() throws IOException {
this.snippet.expectHttpieRequest("non-get-request").withContents(
this.snippet.expectHttpieRequest().withContents(
codeBlock("bash").content("$ http POST 'http://localhost/foo'"));
new HttpieRequestSnippet().document(operationBuilder("non-get-request")
new HttpieRequestSnippet().document(this.operationBuilder
.request("http://localhost/foo").method("POST").build());
}
@Test
public void requestWithContent() throws IOException {
this.snippet.expectHttpieRequest("request-with-content")
.withContents(codeBlock("bash")
.content("$ echo 'content' | http GET 'http://localhost/foo'"));
new HttpieRequestSnippet().document(operationBuilder("request-with-content")
this.snippet.expectHttpieRequest().withContents(codeBlock("bash")
.content("$ echo 'content' | http GET 'http://localhost/foo'"));
new HttpieRequestSnippet().document(this.operationBuilder
.request("http://localhost/foo").content("content").build());
}
@Test
public void getRequestWithQueryString() throws IOException {
this.snippet.expectHttpieRequest("request-with-query-string")
.withContents(codeBlock("bash")
.content("$ http GET 'http://localhost/foo?param=value'"));
new HttpieRequestSnippet().document(operationBuilder("request-with-query-string")
this.snippet.expectHttpieRequest().withContents(codeBlock("bash")
.content("$ http GET 'http://localhost/foo?param=value'"));
new HttpieRequestSnippet().document(this.operationBuilder
.request("http://localhost/foo?param=value").build());
}
@Test
public void getRequestWithTotallyOverlappingQueryStringAndParameters()
throws IOException {
this.snippet
.expectHttpieRequest(
"request-with-totally-overlapping-query-string-and-parameters")
.withContents(codeBlock("bash")
.content("$ http GET 'http://localhost/foo?param=value'"));
new HttpieRequestSnippet().document(operationBuilder(
"request-with-totally-overlapping-query-string-and-parameters")
.request("http://localhost/foo?param=value")
this.snippet.expectHttpieRequest().withContents(codeBlock("bash")
.content("$ http GET 'http://localhost/foo?param=value'"));
new HttpieRequestSnippet().document(
this.operationBuilder.request("http://localhost/foo?param=value")
.param("param", "value").build());
}
@Test
public void getRequestWithPartiallyOverlappingQueryStringAndParameters()
throws IOException {
this.snippet
.expectHttpieRequest(
"request-with-partially-overlapping-query-string-and-parameters")
.withContents(codeBlock("bash")
.content("$ http GET 'http://localhost/foo?a=alpha&b=bravo'"));
new HttpieRequestSnippet().document(operationBuilder(
"request-with-partially-overlapping-query-string-and-parameters")
.request("http://localhost/foo?a=alpha").param("a", "alpha")
.param("b", "bravo").build());
this.snippet.expectHttpieRequest().withContents(codeBlock("bash")
.content("$ http GET 'http://localhost/foo?a=alpha&b=bravo'"));
new HttpieRequestSnippet()
.document(this.operationBuilder.request("http://localhost/foo?a=alpha")
.param("a", "alpha").param("b", "bravo").build());
}
@Test
public void getRequestWithDisjointQueryStringAndParameters() throws IOException {
this.snippet
.expectHttpieRequest(
"request-with-partially-overlapping-query-string-and-parameters")
.withContents(codeBlock("bash")
.content("$ http GET 'http://localhost/foo?a=alpha&b=bravo'"));
new HttpieRequestSnippet().document(operationBuilder(
"request-with-partially-overlapping-query-string-and-parameters")
.request("http://localhost/foo?a=alpha").param("b", "bravo")
.build());
this.snippet.expectHttpieRequest().withContents(codeBlock("bash")
.content("$ http GET 'http://localhost/foo?a=alpha&b=bravo'"));
new HttpieRequestSnippet().document(this.operationBuilder
.request("http://localhost/foo?a=alpha").param("b", "bravo").build());
}
@Test
public void getRequestWithQueryStringWithNoValue() throws IOException {
this.snippet.expectHttpieRequest("request-with-query-string-with-no-value")
.withContents(codeBlock("bash")
.content("$ http GET 'http://localhost/foo?param'"));
new HttpieRequestSnippet()
.document(operationBuilder("request-with-query-string-with-no-value")
.request("http://localhost/foo?param").build());
this.snippet.expectHttpieRequest().withContents(
codeBlock("bash").content("$ http GET 'http://localhost/foo?param'"));
new HttpieRequestSnippet().document(
this.operationBuilder.request("http://localhost/foo?param").build());
}
@Test
public void postRequestWithQueryString() throws IOException {
this.snippet.expectHttpieRequest("post-request-with-query-string")
.withContents(codeBlock("bash")
.content("$ http POST 'http://localhost/foo?param=value'"));
new HttpieRequestSnippet()
.document(operationBuilder("post-request-with-query-string")
.request("http://localhost/foo?param=value").method("POST")
.build());
this.snippet.expectHttpieRequest().withContents(codeBlock("bash")
.content("$ http POST 'http://localhost/foo?param=value'"));
new HttpieRequestSnippet().document(this.operationBuilder
.request("http://localhost/foo?param=value").method("POST").build());
}
@Test
public void postRequestWithQueryStringWithNoValue() throws IOException {
this.snippet.expectHttpieRequest("post-request-with-query-string-with-no-value")
.withContents(codeBlock("bash")
.content("$ http POST 'http://localhost/foo?param'"));
new HttpieRequestSnippet()
.document(operationBuilder("post-request-with-query-string-with-no-value")
.request("http://localhost/foo?param").method("POST").build());
this.snippet.expectHttpieRequest().withContents(
codeBlock("bash").content("$ http POST 'http://localhost/foo?param'"));
new HttpieRequestSnippet().document(this.operationBuilder
.request("http://localhost/foo?param").method("POST").build());
}
@Test
public void postRequestWithOneParameter() throws IOException {
this.snippet.expectHttpieRequest("post-request-with-one-parameter")
.withContents(codeBlock("bash")
.content("$ http --form POST 'http://localhost/foo' 'k1=v1'"));
this.snippet.expectHttpieRequest().withContents(codeBlock("bash")
.content("$ http --form POST 'http://localhost/foo' 'k1=v1'"));
new HttpieRequestSnippet()
.document(operationBuilder("post-request-with-one-parameter")
.request("http://localhost/foo").method("POST").param("k1", "v1")
.build());
.document(this.operationBuilder.request("http://localhost/foo")
.method("POST").param("k1", "v1").build());
}
@Test
public void postRequestWithOneParameterWithNoValue() throws IOException {
this.snippet.expectHttpieRequest("post-request-with-one-parameter-with-no-value")
.withContents(codeBlock("bash")
.content("$ http --form POST 'http://localhost/foo' 'k1='"));
new HttpieRequestSnippet().document(
operationBuilder("post-request-with-one-parameter-with-no-value")
.request("http://localhost/foo").method("POST").param("k1")
.build());
this.snippet.expectHttpieRequest().withContents(codeBlock("bash")
.content("$ http --form POST 'http://localhost/foo' 'k1='"));
new HttpieRequestSnippet().document(this.operationBuilder
.request("http://localhost/foo").method("POST").param("k1").build());
}
@Test
public void postRequestWithMultipleParameters() throws IOException {
this.snippet.expectHttpieRequest("post-request-with-multiple-parameters")
this.snippet.expectHttpieRequest()
.withContents(codeBlock("bash")
.content("$ http --form POST 'http://localhost/foo'"
+ " 'k1=v1' 'k1=v1-bis' 'k2=v2'"));
new HttpieRequestSnippet()
.document(operationBuilder("post-request-with-multiple-parameters")
.request("http://localhost/foo").method("POST")
new HttpieRequestSnippet().document(
this.operationBuilder.request("http://localhost/foo").method("POST")
.param("k1", "v1", "v1-bis").param("k2", "v2").build());
}
@Test
public void postRequestWithUrlEncodedParameter() throws IOException {
this.snippet.expectHttpieRequest("post-request-with-url-encoded-parameter")
.withContents(codeBlock("bash")
.content("$ http --form POST 'http://localhost/foo' 'k1=a&b'"));
this.snippet.expectHttpieRequest().withContents(codeBlock("bash")
.content("$ http --form POST 'http://localhost/foo' 'k1=a&b'"));
new HttpieRequestSnippet()
.document(operationBuilder("post-request-with-url-encoded-parameter")
.request("http://localhost/foo").method("POST").param("k1", "a&b")
.build());
.document(this.operationBuilder.request("http://localhost/foo")
.method("POST").param("k1", "a&b").build());
}
@Test
public void postRequestWithDisjointQueryStringAndParameter() throws IOException {
this.snippet
.expectHttpieRequest(
"post-request-with-disjoint-query-string-and-parameter")
.withContents(codeBlock("bash").content(
"$ http --form POST 'http://localhost/foo?a=alpha' 'b=bravo'"));
new HttpieRequestSnippet().document(
operationBuilder("post-request-with-disjoint-query-string-and-parameter")
.request("http://localhost/foo?a=alpha").method("POST")
.param("b", "bravo").build());
this.snippet.expectHttpieRequest().withContents(codeBlock("bash")
.content("$ http --form POST 'http://localhost/foo?a=alpha' 'b=bravo'"));
new HttpieRequestSnippet()
.document(this.operationBuilder.request("http://localhost/foo?a=alpha")
.method("POST").param("b", "bravo").build());
}
@Test
public void postRequestWithTotallyOverlappingQueryStringAndParameters()
throws IOException {
this.snippet
.expectHttpieRequest(
"post-request-with-totally-overlapping-query-string-and-parameters")
.withContents(codeBlock("bash")
.content("$ http POST 'http://localhost/foo?a=alpha&b=bravo'"));
new HttpieRequestSnippet().document(operationBuilder(
"post-request-with-totally-overlapping-query-string-and-parameters")
.request("http://localhost/foo?a=alpha&b=bravo").method("POST")
.param("a", "alpha").param("b", "bravo").build());
this.snippet.expectHttpieRequest().withContents(codeBlock("bash")
.content("$ http POST 'http://localhost/foo?a=alpha&b=bravo'"));
new HttpieRequestSnippet().document(
this.operationBuilder.request("http://localhost/foo?a=alpha&b=bravo")
.method("POST").param("a", "alpha").param("b", "bravo").build());
}
@Test
public void postRequestWithPartiallyOverlappingQueryStringAndParameters()
throws IOException {
this.snippet
.expectHttpieRequest(
"post-request-with-partially-overlapping-query-string-and-parameters")
.withContents(codeBlock("bash").content(
"$ http --form POST 'http://localhost/foo?a=alpha' 'b=bravo'"));
new HttpieRequestSnippet().document(operationBuilder(
"post-request-with-partially-overlapping-query-string-and-parameters")
.request("http://localhost/foo?a=alpha").method("POST")
.param("a", "alpha").param("b", "bravo").build());
this.snippet.expectHttpieRequest().withContents(codeBlock("bash")
.content("$ http --form POST 'http://localhost/foo?a=alpha' 'b=bravo'"));
new HttpieRequestSnippet()
.document(this.operationBuilder.request("http://localhost/foo?a=alpha")
.method("POST").param("a", "alpha").param("b", "bravo").build());
}
@Test
public void putRequestWithOneParameter() throws IOException {
this.snippet.expectHttpieRequest("put-request-with-one-parameter")
.withContents(codeBlock("bash")
.content("$ http --form PUT 'http://localhost/foo' 'k1=v1'"));
new HttpieRequestSnippet()
.document(operationBuilder("put-request-with-one-parameter")
.request("http://localhost/foo").method("PUT").param("k1", "v1")
.build());
this.snippet.expectHttpieRequest().withContents(codeBlock("bash")
.content("$ http --form PUT 'http://localhost/foo' 'k1=v1'"));
new HttpieRequestSnippet().document(this.operationBuilder
.request("http://localhost/foo").method("PUT").param("k1", "v1").build());
}
@Test
public void putRequestWithMultipleParameters() throws IOException {
this.snippet.expectHttpieRequest("put-request-with-multiple-parameters")
this.snippet.expectHttpieRequest()
.withContents(codeBlock("bash")
.content("$ http --form PUT 'http://localhost/foo'"
+ " 'k1=v1' 'k1=v1-bis' 'k2=v2'"));
new HttpieRequestSnippet()
.document(operationBuilder("put-request-with-multiple-parameters")
.request("http://localhost/foo").method("PUT").param("k1", "v1")
.param("k1", "v1-bis").param("k2", "v2").build());
new HttpieRequestSnippet().document(this.operationBuilder
.request("http://localhost/foo").method("PUT").param("k1", "v1")
.param("k1", "v1-bis").param("k2", "v2").build());
}
@Test
public void putRequestWithUrlEncodedParameter() throws IOException {
this.snippet.expectHttpieRequest("put-request-with-url-encoded-parameter")
.withContents(codeBlock("bash")
.content("$ http --form PUT 'http://localhost/foo' 'k1=a&b'"));
this.snippet.expectHttpieRequest().withContents(codeBlock("bash")
.content("$ http --form PUT 'http://localhost/foo' 'k1=a&b'"));
new HttpieRequestSnippet()
.document(operationBuilder("put-request-with-url-encoded-parameter")
.request("http://localhost/foo").method("PUT").param("k1", "a&b")
.build());
.document(this.operationBuilder.request("http://localhost/foo")
.method("PUT").param("k1", "a&b").build());
}
@Test
public void requestWithHeaders() throws IOException {
this.snippet.expectHttpieRequest("request-with-headers").withContents(
this.snippet.expectHttpieRequest().withContents(
codeBlock("bash").content("$ http GET 'http://localhost/foo'"
+ " 'Content-Type:application/json' 'a:alpha'"));
new HttpieRequestSnippet().document(
operationBuilder("request-with-headers").request("http://localhost/foo")
new HttpieRequestSnippet()
.document(this.operationBuilder.request("http://localhost/foo")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_JSON_VALUE)
.header("a", "alpha").build());
@@ -305,15 +257,12 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
String expectedContent = String
.format("$ http --form POST 'http://localhost/upload' \\%n"
+ " 'metadata'@<(echo '{\"description\": \"foo\"}')");
this.snippet.expectHttpieRequest("multipart-post-no-original-filename")
this.snippet.expectHttpieRequest()
.withContents(codeBlock("bash").content(expectedContent));
new HttpieRequestSnippet()
.document(operationBuilder("multipart-post-no-original-filename")
.request("http://localhost/upload").method("POST")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.MULTIPART_FORM_DATA_VALUE)
.part("metadata", "{\"description\": \"foo\"}".getBytes())
.build());
new HttpieRequestSnippet().document(this.operationBuilder
.request("http://localhost/upload").method("POST")
.header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE)
.part("metadata", "{\"description\": \"foo\"}".getBytes()).build());
}
@Test
@@ -322,11 +271,10 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
String expectedContent = String
.format("$ http --form POST 'http://localhost/upload' \\%n"
+ " 'image'@'documents/images/example.png'");
this.snippet.expectHttpieRequest("multipart-post-with-content-type")
this.snippet.expectHttpieRequest()
.withContents(codeBlock("bash").content(expectedContent));
new HttpieRequestSnippet()
.document(operationBuilder("multipart-post-with-content-type")
.request("http://localhost/upload").method("POST")
new HttpieRequestSnippet().document(
this.operationBuilder.request("http://localhost/upload").method("POST")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.MULTIPART_FORM_DATA_VALUE)
.part("image", new byte[0])
@@ -339,11 +287,10 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
String expectedContent = String
.format("$ http --form POST 'http://localhost/upload' \\%n"
+ " 'image'@'documents/images/example.png'");
this.snippet.expectHttpieRequest("multipart-post")
this.snippet.expectHttpieRequest()
.withContents(codeBlock("bash").content(expectedContent));
new HttpieRequestSnippet()
.document(operationBuilder("multipart-post")
.request("http://localhost/upload").method("POST")
new HttpieRequestSnippet().document(
this.operationBuilder.request("http://localhost/upload").method("POST")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.MULTIPART_FORM_DATA_VALUE)
.part("image", new byte[0])
@@ -356,11 +303,10 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
.format("$ http --form POST 'http://localhost/upload' \\%n"
+ " 'image'@'documents/images/example.png' 'a=apple' 'a=avocado'"
+ " 'b=banana'");
this.snippet.expectHttpieRequest("multipart-post-with-parameters")
this.snippet.expectHttpieRequest()
.withContents(codeBlock("bash").content(expectedContent));
new HttpieRequestSnippet()
.document(operationBuilder("multipart-post-with-parameters")
.request("http://localhost/upload").method("POST")
new HttpieRequestSnippet().document(
this.operationBuilder.request("http://localhost/upload").method("POST")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.MULTIPART_FORM_DATA_VALUE)
.part("image", new byte[0])
@@ -370,10 +316,10 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
@Test
public void basicAuthCredentialsAreSuppliedUsingAuthOption() throws IOException {
this.snippet.expectHttpieRequest("basic-auth").withContents(codeBlock("bash")
this.snippet.expectHttpieRequest().withContents(codeBlock("bash")
.content("$ http --auth 'user:secret' GET 'http://localhost/foo'"));
new HttpieRequestSnippet()
.document(operationBuilder("basic-auth").request("http://localhost/foo")
.document(this.operationBuilder.request("http://localhost/foo")
.header(HttpHeaders.AUTHORIZATION,
"Basic " + Base64Utils
.encodeToString("user:secret".getBytes()))
@@ -382,7 +328,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
@Test
public void customAttributes() throws IOException {
this.snippet.expectHttpieRequest("custom-attributes")
this.snippet.expectHttpieRequest()
.withContents(containsString("httpie request title"));
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
given(resolver.resolveTemplateResource("httpie-request"))
@@ -391,7 +337,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
attributes(
key("title").value("httpie request title")))
.document(
operationBuilder("custom-attributes")
this.operationBuilder
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(
resolver))
@@ -400,12 +346,12 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
@Test
public void customHostHeaderIsIncluded() throws IOException {
this.snippet.expectHttpieRequest("custom-host-header")
this.snippet.expectHttpieRequest()
.withContents(codeBlock("bash").content(
"$ http GET 'http://localhost/foo' 'Host:api.example.com'"
+ " 'Content-Type:application/json' 'a:alpha'"));
new HttpieRequestSnippet().document(
operationBuilder("custom-host-header").request("http://localhost/foo")
new HttpieRequestSnippet()
.document(this.operationBuilder.request("http://localhost/foo")
.header(HttpHeaders.HOST, "api.example.com")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_JSON_VALUE)
@@ -414,14 +360,12 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
@Test
public void postWithContentAndParameters() throws IOException {
this.snippet.expectHttpieRequest("post-with-content-and-parameters").withContents(
this.snippet.expectHttpieRequest().withContents(
codeBlock("bash").content("$ echo 'Some content' | http POST "
+ "'http://localhost/foo?a=alpha&b=bravo'"));
new HttpieRequestSnippet()
.document(operationBuilder("post-with-content-and-parameters")
.request("http://localhost/foo").method("POST")
.param("a", "alpha").param("b", "bravo").content("Some content")
.build());
new HttpieRequestSnippet().document(this.operationBuilder
.request("http://localhost/foo").method("POST").param("a", "alpha")
.param("b", "bravo").content("Some content").build());
}
}

View File

@@ -24,13 +24,13 @@ import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.restdocs.snippet.SnippetException;
import org.springframework.restdocs.templates.TemplateFormats;
import org.springframework.restdocs.test.ExpectedSnippet;
import org.springframework.restdocs.test.OperationBuilder;
import static org.hamcrest.CoreMatchers.endsWith;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.springframework.restdocs.headers.HeaderDocumentation.headerWithName;
import static org.springframework.restdocs.templates.TemplateFormats.asciidoctor;
/**
* Tests for failures when rendering {@link RequestHeadersSnippet} due to missing or
@@ -41,7 +41,10 @@ import static org.springframework.restdocs.headers.HeaderDocumentation.headerWit
public class RequestHeadersSnippetFailureTests {
@Rule
public ExpectedSnippet snippet = new ExpectedSnippet(TemplateFormats.asciidoctor());
public OperationBuilder operationBuilder = new OperationBuilder(asciidoctor());
@Rule
public ExpectedSnippet snippet = new ExpectedSnippet(asciidoctor());
@Rule
public ExpectedException thrown = ExpectedException.none();
@@ -53,10 +56,8 @@ public class RequestHeadersSnippetFailureTests {
.expectMessage(equalTo("Headers with the following names were not found"
+ " in the request: [Accept]"));
new RequestHeadersSnippet(
Arrays.asList(headerWithName("Accept").description("one")))
.document(new OperationBuilder("missing-request-headers",
this.snippet.getOutputDirectory())
.request("http://localhost").build());
Arrays.asList(headerWithName("Accept").description("one"))).document(
this.operationBuilder.request("http://localhost").build());
}
@Test
@@ -67,11 +68,8 @@ public class RequestHeadersSnippetFailureTests {
+ " in the request: [Accept]"));
new RequestHeadersSnippet(
Arrays.asList(headerWithName("Accept").description("one")))
.document(new OperationBuilder(
"undocumented-request-header-and-missing-request-header",
this.snippet.getOutputDirectory())
.request("http://localhost")
.header("X-Test", "test").build());
.document(this.operationBuilder.request("http://localhost")
.header("X-Test", "test").build());
}
}

View File

@@ -27,7 +27,6 @@ import org.springframework.restdocs.templates.TemplateFormat;
import org.springframework.restdocs.templates.TemplateFormats;
import org.springframework.restdocs.templates.TemplateResourceResolver;
import org.springframework.restdocs.templates.mustache.MustacheTemplateEngine;
import org.springframework.restdocs.test.OperationBuilder;
import static org.hamcrest.CoreMatchers.containsString;
import static org.mockito.BDDMockito.given;
@@ -50,7 +49,7 @@ public class RequestHeadersSnippetTests extends AbstractSnippetTests {
@Test
public void requestWithHeaders() throws IOException {
this.snippet.expectRequestHeaders("request-with-headers")
this.snippet.expectRequestHeaders()
.withContents(tableWithHeader("Name", "Description")
.row("`X-Test`", "one").row("`Accept`", "two")
.row("`Accept-Encoding`", "three")
@@ -62,64 +61,71 @@ public class RequestHeadersSnippetTests extends AbstractSnippetTests {
headerWithName("Accept-Encoding").description("three"),
headerWithName("Accept-Language").description("four"),
headerWithName("Cache-Control").description("five"),
headerWithName("Connection").description("six")))
.document(operationBuilder("request-with-headers")
.request("http://localhost")
.header("X-Test", "test").header("Accept", "*/*")
.header("Accept-Encoding", "gzip, deflate")
.header("Accept-Language", "en-US,en;q=0.5")
.header("Cache-Control", "max-age=0")
.header("Connection", "keep-alive").build());
headerWithName(
"Connection")
.description("six")))
.document(
this.operationBuilder
.request(
"http://localhost")
.header("X-Test", "test")
.header("Accept", "*/*")
.header("Accept-Encoding",
"gzip, deflate")
.header("Accept-Language",
"en-US,en;q=0.5")
.header("Cache-Control",
"max-age=0")
.header("Connection",
"keep-alive")
.build());
}
@Test
public void caseInsensitiveRequestHeaders() throws IOException {
this.snippet.expectRequestHeaders("case-insensitive-request-headers")
.withContents(
tableWithHeader("Name", "Description").row("`X-Test`", "one"));
this.snippet.expectRequestHeaders().withContents(
tableWithHeader("Name", "Description").row("`X-Test`", "one"));
new RequestHeadersSnippet(
Arrays.asList(headerWithName("X-Test").description("one")))
.document(operationBuilder("case-insensitive-request-headers")
.request("/").header("X-test", "test").build());
.document(this.operationBuilder.request("/")
.header("X-test", "test").build());
}
@Test
public void undocumentedRequestHeader() throws IOException {
new RequestHeadersSnippet(
Arrays.asList(headerWithName("X-Test").description("one")))
.document(new OperationBuilder("undocumented-request-header",
this.snippet.getOutputDirectory())
.request("http://localhost")
.header("X-Test", "test").header("Accept", "*/*")
.build());
.document(this.operationBuilder.request("http://localhost")
.header("X-Test", "test").header("Accept", "*/*")
.build());
}
@Test
public void requestHeadersWithCustomAttributes() throws IOException {
this.snippet.expectRequestHeaders("request-headers-with-custom-attributes")
.withContents(containsString("Custom title"));
this.snippet.expectRequestHeaders().withContents(containsString("Custom title"));
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
given(resolver.resolveTemplateResource("request-headers"))
.willReturn(snippetResource("request-headers-with-title"));
new RequestHeadersSnippet(
Arrays.asList(headerWithName("X-Test").description("one")),
attributes(key("title").value("Custom title"))).document(
operationBuilder("request-headers-with-custom-attributes")
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(resolver))
.request("http://localhost").header("X-Test", "test")
.build());
attributes(
key("title").value("Custom title")))
.document(
this.operationBuilder
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(
resolver))
.request("http://localhost")
.header("X-Test", "test").build());
}
@Test
public void requestHeadersWithCustomDescriptorAttributes() throws IOException {
this.snippet
.expectRequestHeaders("request-headers-with-custom-descriptor-attributes")
.withContents(//
tableWithHeader("Name", "Description", "Foo")
.row("X-Test", "one", "alpha")
.row("Accept-Encoding", "two", "bravo")
.row("Accept", "three", "charlie"));
this.snippet.expectRequestHeaders().withContents(//
tableWithHeader("Name", "Description", "Foo")
.row("X-Test", "one", "alpha")
.row("Accept-Encoding", "two", "bravo")
.row("Accept", "three", "charlie"));
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
given(resolver.resolveTemplateResource("request-headers"))
.willReturn(snippetResource("request-headers-with-extra-column"));
@@ -130,8 +136,8 @@ public class RequestHeadersSnippetTests extends AbstractSnippetTests {
.attributes(key("foo").value("bravo")),
headerWithName("Accept").description("three")
.attributes(key("foo").value("charlie"))))
.document(operationBuilder(
"request-headers-with-custom-descriptor-attributes")
.document(
this.operationBuilder
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(
resolver))
@@ -144,7 +150,7 @@ public class RequestHeadersSnippetTests extends AbstractSnippetTests {
@Test
public void additionalDescriptors() throws IOException {
this.snippet.expectRequestHeaders("additional-descriptors")
this.snippet.expectRequestHeaders()
.withContents(tableWithHeader("Name", "Description")
.row("`X-Test`", "one").row("`Accept`", "two")
.row("`Accept-Encoding`", "three")
@@ -156,10 +162,12 @@ public class RequestHeadersSnippetTests extends AbstractSnippetTests {
headerWithName("Accept-Encoding").description("three"),
headerWithName("Accept-Language").description("four"))
.and(headerWithName("Cache-Control").description("five"),
headerWithName("Connection").description("six"))
.document(operationBuilder("additional-descriptors")
.request("http://localhost").header("X-Test", "test")
.header("Accept", "*/*")
headerWithName(
"Connection")
.description(
"six"))
.document(this.operationBuilder.request("http://localhost")
.header("X-Test", "test").header("Accept", "*/*")
.header("Accept-Encoding", "gzip, deflate")
.header("Accept-Language", "en-US,en;q=0.5")
.header("Cache-Control", "max-age=0")
@@ -168,14 +176,13 @@ public class RequestHeadersSnippetTests extends AbstractSnippetTests {
@Test
public void tableCellContentIsEscapedWhenNecessary() throws IOException {
this.snippet.expectRequestHeaders("request-with-escaped-headers").withContents(
this.snippet.expectRequestHeaders().withContents(
tableWithHeader("Name", "Description").row(escapeIfNecessary("`Foo|Bar`"),
escapeIfNecessary("one|two")));
new RequestHeadersSnippet(
Arrays.asList(headerWithName("Foo|Bar").description("one|two")))
.document(operationBuilder("request-with-escaped-headers")
.request("http://localhost").header("Foo|Bar", "baz")
.build());
.document(this.operationBuilder.request("http://localhost")
.header("Foo|Bar", "baz").build());
}
private String escapeIfNecessary(String input) {

View File

@@ -24,13 +24,13 @@ import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.restdocs.snippet.SnippetException;
import org.springframework.restdocs.templates.TemplateFormats;
import org.springframework.restdocs.test.ExpectedSnippet;
import org.springframework.restdocs.test.OperationBuilder;
import static org.hamcrest.CoreMatchers.endsWith;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.springframework.restdocs.headers.HeaderDocumentation.headerWithName;
import static org.springframework.restdocs.templates.TemplateFormats.asciidoctor;
/**
* Tests for failures when rendering {@link ResponseHeadersSnippet} due to missing or
@@ -41,7 +41,10 @@ import static org.springframework.restdocs.headers.HeaderDocumentation.headerWit
public class ResponseHeadersSnippetFailureTests {
@Rule
public ExpectedSnippet snippet = new ExpectedSnippet(TemplateFormats.asciidoctor());
public OperationBuilder operationBuilder = new OperationBuilder(asciidoctor());
@Rule
public ExpectedSnippet snippet = new ExpectedSnippet(asciidoctor());
@Rule
public ExpectedException thrown = ExpectedException.none();
@@ -54,8 +57,7 @@ public class ResponseHeadersSnippetFailureTests {
+ " in the response: [Content-Type]"));
new ResponseHeadersSnippet(
Arrays.asList(headerWithName("Content-Type").description("one")))
.document(new OperationBuilder("missing-response-headers",
this.snippet.getOutputDirectory()).response().build());
.document(this.operationBuilder.response().build());
}
@Test
@@ -66,10 +68,8 @@ public class ResponseHeadersSnippetFailureTests {
+ " in the response: [Content-Type]"));
new ResponseHeadersSnippet(
Arrays.asList(headerWithName("Content-Type").description("one")))
.document(new OperationBuilder(
"undocumented-response-header-and-missing-response-header",
this.snippet.getOutputDirectory()).response()
.header("X-Test", "test").build());
.document(this.operationBuilder.response()
.header("X-Test", "test").build());
}
}

View File

@@ -27,7 +27,6 @@ import org.springframework.restdocs.templates.TemplateFormat;
import org.springframework.restdocs.templates.TemplateFormats;
import org.springframework.restdocs.templates.TemplateResourceResolver;
import org.springframework.restdocs.templates.mustache.MustacheTemplateEngine;
import org.springframework.restdocs.test.OperationBuilder;
import static org.hamcrest.CoreMatchers.containsString;
import static org.mockito.BDDMockito.given;
@@ -50,7 +49,7 @@ public class ResponseHeadersSnippetTests extends AbstractSnippetTests {
@Test
public void responseWithHeaders() throws IOException {
this.snippet.expectResponseHeaders("response-headers").withContents(
this.snippet.expectResponseHeaders().withContents(
tableWithHeader("Name", "Description").row("`X-Test`", "one")
.row("`Content-Type`", "two").row("`Etag`", "three")
.row("`Cache-Control`", "five").row("`Vary`", "six"));
@@ -61,7 +60,7 @@ public class ResponseHeadersSnippetTests extends AbstractSnippetTests {
headerWithName("Cache-Control").description("five"),
headerWithName("Vary").description("six")))
.document(
operationBuilder("response-headers").response()
this.operationBuilder.response()
.header("X-Test", "test")
.header("Content-Type",
"application/json")
@@ -72,44 +71,44 @@ public class ResponseHeadersSnippetTests extends AbstractSnippetTests {
@Test
public void caseInsensitiveResponseHeaders() throws IOException {
this.snippet.expectResponseHeaders("case-insensitive-response-headers")
.withContents(
tableWithHeader("Name", "Description").row("`X-Test`", "one"));
this.snippet.expectResponseHeaders().withContents(
tableWithHeader("Name", "Description").row("`X-Test`", "one"));
new ResponseHeadersSnippet(
Arrays.asList(headerWithName("X-Test").description("one")))
.document(operationBuilder("case-insensitive-response-headers")
.response().header("X-test", "test").build());
.document(this.operationBuilder.response()
.header("X-test", "test").build());
}
@Test
public void undocumentedResponseHeader() throws IOException {
new ResponseHeadersSnippet(
Arrays.asList(headerWithName("X-Test").description("one")))
.document(new OperationBuilder("undocumented-response-header",
this.snippet.getOutputDirectory()).response()
.header("X-Test", "test")
.header("Content-Type", "*/*").build());
Arrays.asList(headerWithName("X-Test").description("one"))).document(
this.operationBuilder.response().header("X-Test", "test")
.header("Content-Type", "*/*").build());
}
@Test
public void responseHeadersWithCustomAttributes() throws IOException {
this.snippet.expectResponseHeaders("response-headers-with-custom-attributes")
.withContents(containsString("Custom title"));
this.snippet.expectResponseHeaders().withContents(containsString("Custom title"));
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
given(resolver.resolveTemplateResource("response-headers"))
.willReturn(snippetResource("response-headers-with-title"));
new ResponseHeadersSnippet(
Arrays.asList(headerWithName("X-Test").description("one")),
attributes(key("title").value("Custom title"))).document(
operationBuilder("response-headers-with-custom-attributes")
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(resolver))
.response().header("X-Test", "test").build());
attributes(
key("title").value("Custom title")))
.document(
this.operationBuilder
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(
resolver))
.response().header("X-Test", "test")
.build());
}
@Test
public void responseHeadersWithCustomDescriptorAttributes() throws IOException {
this.snippet.expectResponseHeaders("response-headers-with-custom-attributes")
this.snippet.expectResponseHeaders()
.withContents(tableWithHeader("Name", "Description", "Foo")
.row("X-Test", "one", "alpha").row("Content-Type", "two", "bravo")
.row("Etag", "three", "charlie"));
@@ -123,8 +122,8 @@ public class ResponseHeadersSnippetTests extends AbstractSnippetTests {
.attributes(key("foo").value("bravo")),
headerWithName("Etag").description("three")
.attributes(key("foo").value("charlie"))))
.document(operationBuilder(
"response-headers-with-custom-attributes")
.document(
this.operationBuilder
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(
resolver))
@@ -137,7 +136,7 @@ public class ResponseHeadersSnippetTests extends AbstractSnippetTests {
@Test
public void additionalDescriptors() throws IOException {
this.snippet.expectResponseHeaders("additional-descriptors").withContents(
this.snippet.expectResponseHeaders().withContents(
tableWithHeader("Name", "Description").row("`X-Test`", "one")
.row("`Content-Type`", "two").row("`Etag`", "three")
.row("`Cache-Control`", "five").row("`Vary`", "six"));
@@ -146,10 +145,8 @@ public class ResponseHeadersSnippetTests extends AbstractSnippetTests {
headerWithName("Content-Type").description("two"),
headerWithName("Etag").description("three"))
.and(headerWithName("Cache-Control").description("five"),
headerWithName("Vary")
.description("six"))
.document(operationBuilder("additional-descriptors").response()
.header("X-Test", "test")
headerWithName("Vary").description("six"))
.document(this.operationBuilder.response().header("X-Test", "test")
.header("Content-Type", "application/json")
.header("Etag", "lskjadldj3ii32l2ij23")
.header("Cache-Control", "max-age=0").header("Vary", "User-Agent")
@@ -158,13 +155,13 @@ public class ResponseHeadersSnippetTests extends AbstractSnippetTests {
@Test
public void tableCellContentIsEscapedWhenNecessary() throws IOException {
this.snippet.expectResponseHeaders("response-with-escaped-headers").withContents(
this.snippet.expectResponseHeaders().withContents(
tableWithHeader("Name", "Description").row(escapeIfNecessary("`Foo|Bar`"),
escapeIfNecessary("one|two")));
new ResponseHeadersSnippet(
Arrays.asList(headerWithName("Foo|Bar").description("one|two")))
.document(operationBuilder("response-with-escaped-headers")
.response().header("Foo|Bar", "baz").build());
.document(this.operationBuilder.response()
.header("Foo|Bar", "baz").build());
}
private String escapeIfNecessary(String input) {

View File

@@ -51,245 +51,222 @@ public class HttpRequestSnippetTests extends AbstractSnippetTests {
@Test
public void getRequest() throws IOException {
this.snippet.expectHttpRequest("get-request")
this.snippet.expectHttpRequest()
.withContents(httpRequest(RequestMethod.GET, "/foo").header("Alpha", "a")
.header(HttpHeaders.HOST, "localhost"));
new HttpRequestSnippet().document(operationBuilder("get-request")
new HttpRequestSnippet().document(this.operationBuilder
.request("http://localhost/foo").header("Alpha", "a").build());
}
@Test
public void getRequestWithParameters() throws IOException {
this.snippet.expectHttpRequest("get-request-with-parameters")
this.snippet.expectHttpRequest()
.withContents(httpRequest(RequestMethod.GET, "/foo?b=bravo")
.header("Alpha", "a").header(HttpHeaders.HOST, "localhost"));
new HttpRequestSnippet().document(operationBuilder("get-request-with-parameters")
.request("http://localhost/foo").header("Alpha", "a").param("b", "bravo")
.build());
new HttpRequestSnippet()
.document(this.operationBuilder.request("http://localhost/foo")
.header("Alpha", "a").param("b", "bravo").build());
}
@Test
public void getRequestWithPort() throws IOException {
this.snippet.expectHttpRequest("get-request")
this.snippet.expectHttpRequest()
.withContents(httpRequest(RequestMethod.GET, "/foo").header("Alpha", "a")
.header(HttpHeaders.HOST, "localhost:8080"));
new HttpRequestSnippet().document(operationBuilder("get-request")
new HttpRequestSnippet().document(this.operationBuilder
.request("http://localhost:8080/foo").header("Alpha", "a").build());
}
@Test
public void getRequestWithQueryString() throws IOException {
this.snippet.expectHttpRequest("get-request-with-query-string")
this.snippet.expectHttpRequest()
.withContents(httpRequest(RequestMethod.GET, "/foo?bar=baz")
.header(HttpHeaders.HOST, "localhost"));
new HttpRequestSnippet()
.document(operationBuilder("get-request-with-query-string")
.request("http://localhost/foo?bar=baz").build());
new HttpRequestSnippet().document(
this.operationBuilder.request("http://localhost/foo?bar=baz").build());
}
@Test
public void getRequestWithQueryStringWithNoValue() throws IOException {
this.snippet.expectHttpRequest("get-request-with-query-string-with-no-value")
this.snippet.expectHttpRequest()
.withContents(httpRequest(RequestMethod.GET, "/foo?bar")
.header(HttpHeaders.HOST, "localhost"));
new HttpRequestSnippet()
.document(operationBuilder("get-request-with-query-string-with-no-value")
.request("http://localhost/foo?bar").build());
new HttpRequestSnippet().document(
this.operationBuilder.request("http://localhost/foo?bar").build());
}
@Test
public void getWithPartiallyOverlappingQueryStringAndParameters() throws IOException {
this.snippet
.expectHttpRequest(
"get-with-partially-overlapping-query-string-and-parameters")
this.snippet.expectHttpRequest()
.withContents(httpRequest(RequestMethod.GET, "/foo?a=alpha&b=bravo")
.header(HttpHeaders.HOST, "localhost"));
new HttpRequestSnippet().document(operationBuilder(
"get-with-partially-overlapping-query-string-and-parameters")
.request("http://localhost/foo?a=alpha").param("a", "alpha")
.param("b", "bravo").build());
new HttpRequestSnippet()
.document(this.operationBuilder.request("http://localhost/foo?a=alpha")
.param("a", "alpha").param("b", "bravo").build());
}
@Test
public void getWithTotallyOverlappingQueryStringAndParameters() throws IOException {
this.snippet
.expectHttpRequest(
"get-with-totally-overlapping-query-string-and-parameters")
this.snippet.expectHttpRequest()
.withContents(httpRequest(RequestMethod.GET, "/foo?a=alpha&b=bravo")
.header(HttpHeaders.HOST, "localhost"));
new HttpRequestSnippet().document(operationBuilder(
"get-with-totally-overlapping-query-string-and-parameters")
.request("http://localhost/foo?a=alpha&b=bravo")
new HttpRequestSnippet().document(
this.operationBuilder.request("http://localhost/foo?a=alpha&b=bravo")
.param("a", "alpha").param("b", "bravo").build());
}
@Test
public void postRequestWithContent() throws IOException {
String content = "Hello, world";
this.snippet.expectHttpRequest("post-request-with-content")
this.snippet.expectHttpRequest()
.withContents(httpRequest(RequestMethod.POST, "/foo")
.header(HttpHeaders.HOST, "localhost").content(content).header(
HttpHeaders.CONTENT_LENGTH, content.getBytes().length));
new HttpRequestSnippet().document(operationBuilder("post-request-with-content")
new HttpRequestSnippet().document(this.operationBuilder
.request("http://localhost/foo").method("POST").content(content).build());
}
@Test
public void postRequestWithContentAndParameters() throws IOException {
String content = "Hello, world";
this.snippet.expectHttpRequest("post-request-with-content-and-parameters")
this.snippet.expectHttpRequest()
.withContents(httpRequest(RequestMethod.POST, "/foo?a=alpha")
.header(HttpHeaders.HOST, "localhost").content(content).header(
HttpHeaders.CONTENT_LENGTH, content.getBytes().length));
new HttpRequestSnippet()
.document(operationBuilder("post-request-with-content-and-parameters")
.request("http://localhost/foo").method("POST")
.param("a", "alpha").content(content).build());
.document(this.operationBuilder.request("http://localhost/foo")
.method("POST").param("a", "alpha").content(content).build());
}
@Test
public void postRequestWithContentAndDisjointQueryStringAndParameters()
throws IOException {
String content = "Hello, world";
this.snippet
.expectHttpRequest(
"post-request-with-content-and-disjoint-query-string-and-parameters")
this.snippet.expectHttpRequest()
.withContents(httpRequest(RequestMethod.POST, "/foo?b=bravo&a=alpha")
.header(HttpHeaders.HOST, "localhost").content(content)
.header(HttpHeaders.CONTENT_LENGTH, content.getBytes().length));
.header(HttpHeaders.HOST, "localhost").content(content).header(
HttpHeaders.CONTENT_LENGTH, content.getBytes().length));
new HttpRequestSnippet().document(operationBuilder(
"post-request-with-content-and-disjoint-query-string-and-parameters")
.request("http://localhost/foo?b=bravo").method("POST")
.param("a", "alpha").content(content).build());
new HttpRequestSnippet()
.document(this.operationBuilder.request("http://localhost/foo?b=bravo")
.method("POST").param("a", "alpha").content(content).build());
}
@Test
public void postRequestWithContentAndPartiallyOverlappingQueryStringAndParameters()
throws IOException {
String content = "Hello, world";
this.snippet
.expectHttpRequest(
"post-request-with-content-and-partially-overlapping-query-string-and-parameters")
this.snippet.expectHttpRequest()
.withContents(httpRequest(RequestMethod.POST, "/foo?b=bravo&a=alpha")
.header(HttpHeaders.HOST, "localhost").content(content)
.header(HttpHeaders.CONTENT_LENGTH, content.getBytes().length));
.header(HttpHeaders.HOST, "localhost").content(content).header(
HttpHeaders.CONTENT_LENGTH, content.getBytes().length));
new HttpRequestSnippet().document(operationBuilder(
"post-request-with-content-and-partially-overlapping-query-string-and-parameters")
.request("http://localhost/foo?b=bravo").method("POST")
.param("a", "alpha").param("b", "bravo").content(content)
.build());
new HttpRequestSnippet().document(this.operationBuilder
.request("http://localhost/foo?b=bravo").method("POST")
.param("a", "alpha").param("b", "bravo").content(content).build());
}
@Test
public void postRequestWithContentAndTotallyOverlappingQueryStringAndParameters()
throws IOException {
String content = "Hello, world";
this.snippet
.expectHttpRequest(
"post-request-with-content-and-totally-overlapping-query-string-and-parameters")
this.snippet.expectHttpRequest()
.withContents(httpRequest(RequestMethod.POST, "/foo?b=bravo&a=alpha")
.header(HttpHeaders.HOST, "localhost").content(content)
.header(HttpHeaders.CONTENT_LENGTH, content.getBytes().length));
.header(HttpHeaders.HOST, "localhost").content(content).header(
HttpHeaders.CONTENT_LENGTH, content.getBytes().length));
new HttpRequestSnippet().document(operationBuilder(
"post-request-with-content-and-totally-overlapping-query-string-and-parameters")
.request("http://localhost/foo?b=bravo&a=alpha").method("POST")
.param("a", "alpha").param("b", "bravo").content(content)
.build());
new HttpRequestSnippet().document(this.operationBuilder
.request("http://localhost/foo?b=bravo&a=alpha").method("POST")
.param("a", "alpha").param("b", "bravo").content(content).build());
}
@Test
public void postRequestWithCharset() throws IOException {
String japaneseContent = "\u30b3\u30f3\u30c6\u30f3\u30c4";
byte[] contentBytes = japaneseContent.getBytes("UTF-8");
this.snippet.expectHttpRequest("post-request-with-charset")
this.snippet.expectHttpRequest()
.withContents(httpRequest(RequestMethod.POST, "/foo")
.header("Content-Type", "text/plain;charset=UTF-8")
.header(HttpHeaders.HOST, "localhost")
.header(HttpHeaders.CONTENT_LENGTH, contentBytes.length)
.content(japaneseContent));
new HttpRequestSnippet().document(operationBuilder("post-request-with-charset")
.request("http://localhost/foo").method("POST")
.header("Content-Type", "text/plain;charset=UTF-8").content(contentBytes)
.build());
new HttpRequestSnippet()
.document(this.operationBuilder.request("http://localhost/foo")
.method("POST").header("Content-Type", "text/plain;charset=UTF-8")
.content(contentBytes).build());
}
@Test
public void postRequestWithParameter() throws IOException {
this.snippet.expectHttpRequest("post-request-with-parameter")
this.snippet.expectHttpRequest()
.withContents(httpRequest(RequestMethod.POST, "/foo")
.header(HttpHeaders.HOST, "localhost")
.header("Content-Type", "application/x-www-form-urlencoded")
.content("b%26r=baz&a=alpha"));
new HttpRequestSnippet().document(operationBuilder("post-request-with-parameter")
.request("http://localhost/foo").method("POST").param("b&r", "baz")
.param("a", "alpha").build());
new HttpRequestSnippet()
.document(this.operationBuilder.request("http://localhost/foo")
.method("POST").param("b&r", "baz").param("a", "alpha").build());
}
@Test
public void postRequestWithParameterWithNoValue() throws IOException {
this.snippet.expectHttpRequest("post-request-with-parameter")
this.snippet.expectHttpRequest()
.withContents(httpRequest(RequestMethod.POST, "/foo")
.header(HttpHeaders.HOST, "localhost")
.header("Content-Type", "application/x-www-form-urlencoded")
.content("bar="));
new HttpRequestSnippet().document(operationBuilder("post-request-with-parameter")
new HttpRequestSnippet().document(this.operationBuilder
.request("http://localhost/foo").method("POST").param("bar").build());
}
@Test
public void putRequestWithContent() throws IOException {
String content = "Hello, world";
this.snippet.expectHttpRequest("put-request-with-content")
this.snippet.expectHttpRequest()
.withContents(httpRequest(RequestMethod.PUT, "/foo")
.header(HttpHeaders.HOST, "localhost").content(content).header(
HttpHeaders.CONTENT_LENGTH, content.getBytes().length));
new HttpRequestSnippet().document(operationBuilder("put-request-with-content")
new HttpRequestSnippet().document(this.operationBuilder
.request("http://localhost/foo").method("PUT").content(content).build());
}
@Test
public void putRequestWithParameter() throws IOException {
this.snippet.expectHttpRequest("put-request-with-parameter")
this.snippet.expectHttpRequest()
.withContents(httpRequest(RequestMethod.PUT, "/foo")
.header(HttpHeaders.HOST, "localhost")
.header("Content-Type", "application/x-www-form-urlencoded")
.content("b%26r=baz&a=alpha"));
new HttpRequestSnippet().document(operationBuilder("put-request-with-parameter")
.request("http://localhost/foo").method("PUT").param("b&r", "baz")
.param("a", "alpha").build());
new HttpRequestSnippet()
.document(this.operationBuilder.request("http://localhost/foo")
.method("PUT").param("b&r", "baz").param("a", "alpha").build());
}
@Test
public void multipartPost() throws IOException {
String expectedContent = createPart(String.format(
"Content-Disposition: " + "form-data; " + "name=image%n%n<< data >>"));
this.snippet
.expectHttpRequest(
"multipart-post")
this.snippet.expectHttpRequest()
.withContents(httpRequest(RequestMethod.POST, "/upload")
.header("Content-Type",
"multipart/form-data; boundary=" + BOUNDARY)
.header(HttpHeaders.HOST, "localhost").content(expectedContent));
new HttpRequestSnippet()
.document(operationBuilder("multipart-post")
.request("http://localhost/upload").method("POST")
new HttpRequestSnippet().document(
this.operationBuilder.request("http://localhost/upload").method("POST")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.MULTIPART_FORM_DATA_VALUE)
.part("image", "<< data >>".getBytes()).build());
@@ -309,16 +286,13 @@ public class HttpRequestSnippetTests extends AbstractSnippetTests {
String filePart = createPart(String
.format("Content-Disposition: form-data; " + "name=image%n%n<< data >>"));
String expectedContent = param1Part + param2Part + param3Part + filePart;
this.snippet
.expectHttpRequest(
"multipart-post-with-parameters")
this.snippet.expectHttpRequest()
.withContents(httpRequest(RequestMethod.POST, "/upload")
.header("Content-Type",
"multipart/form-data; boundary=" + BOUNDARY)
.header(HttpHeaders.HOST, "localhost").content(expectedContent));
new HttpRequestSnippet()
.document(operationBuilder("multipart-post-with-parameters")
.request("http://localhost/upload").method("POST")
new HttpRequestSnippet().document(
this.operationBuilder.request("http://localhost/upload").method("POST")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.MULTIPART_FORM_DATA_VALUE)
.param("a", "apple", "avocado").param("b", "banana")
@@ -332,16 +306,13 @@ public class HttpRequestSnippetTests extends AbstractSnippetTests {
String filePart = createPart(String
.format("Content-Disposition: form-data; " + "name=image%n%n<< data >>"));
String expectedContent = paramPart + filePart;
this.snippet
.expectHttpRequest(
"multipart-post-with-parameter-with-no-value")
this.snippet.expectHttpRequest()
.withContents(httpRequest(RequestMethod.POST, "/upload")
.header("Content-Type",
"multipart/form-data; boundary=" + BOUNDARY)
.header(HttpHeaders.HOST, "localhost").content(expectedContent));
new HttpRequestSnippet()
.document(operationBuilder("multipart-post-with-parameter-with-no-value")
.request("http://localhost/upload").method("POST")
new HttpRequestSnippet().document(
this.operationBuilder.request("http://localhost/upload").method("POST")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.MULTIPART_FORM_DATA_VALUE)
.param("a").part("image", "<< data >>".getBytes()).build());
@@ -352,16 +323,13 @@ public class HttpRequestSnippetTests extends AbstractSnippetTests {
String expectedContent = createPart(
String.format("Content-Disposition: form-data; name=image%nContent-Type: "
+ "image/png%n%n<< data >>"));
this.snippet
.expectHttpRequest(
"multipart-post-with-content-type")
this.snippet.expectHttpRequest()
.withContents(httpRequest(RequestMethod.POST, "/upload")
.header("Content-Type",
"multipart/form-data; boundary=" + BOUNDARY)
.header(HttpHeaders.HOST, "localhost").content(expectedContent));
new HttpRequestSnippet()
.document(operationBuilder("multipart-post-with-content-type")
.request("http://localhost/upload").method("POST")
new HttpRequestSnippet().document(
this.operationBuilder.request("http://localhost/upload").method("POST")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.MULTIPART_FORM_DATA_VALUE)
.part("image", "<< data >>".getBytes())
@@ -371,26 +339,30 @@ public class HttpRequestSnippetTests extends AbstractSnippetTests {
@Test
public void getRequestWithCustomHost() throws IOException {
this.snippet.expectHttpRequest("get-request-custom-host")
this.snippet.expectHttpRequest()
.withContents(httpRequest(RequestMethod.GET, "/foo")
.header(HttpHeaders.HOST, "api.example.com"));
new HttpRequestSnippet().document(operationBuilder("get-request-custom-host")
.request("http://localhost/foo")
.header(HttpHeaders.HOST, "api.example.com").build());
new HttpRequestSnippet()
.document(this.operationBuilder.request("http://localhost/foo")
.header(HttpHeaders.HOST, "api.example.com").build());
}
@Test
public void requestWithCustomSnippetAttributes() throws IOException {
this.snippet.expectHttpRequest("request-with-snippet-attributes")
this.snippet.expectHttpRequest()
.withContents(containsString("Title for the request"));
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
given(resolver.resolveTemplateResource("http-request"))
.willReturn(snippetResource("http-request-with-title"));
new HttpRequestSnippet(attributes(key("title").value("Title for the request")))
.document(operationBuilder("request-with-snippet-attributes")
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(resolver))
.request("http://localhost/foo").build());
new HttpRequestSnippet(
attributes(
key("title").value("Title for the request")))
.document(
this.operationBuilder
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(
resolver))
.request("http://localhost/foo").build());
}
private String createPart(String content) {

View File

@@ -49,26 +49,24 @@ public class HttpResponseSnippetTests extends AbstractSnippetTests {
@Test
public void basicResponse() throws IOException {
this.snippet.expectHttpResponse("basic-response")
.withContents(httpResponse(HttpStatus.OK));
new HttpResponseSnippet().document(operationBuilder("basic-response").build());
this.snippet.expectHttpResponse().withContents(httpResponse(HttpStatus.OK));
new HttpResponseSnippet().document(this.operationBuilder.build());
}
@Test
public void nonOkResponse() throws IOException {
this.snippet.expectHttpResponse("non-ok-response")
this.snippet.expectHttpResponse()
.withContents(httpResponse(HttpStatus.BAD_REQUEST));
new HttpResponseSnippet().document(operationBuilder("non-ok-response").response()
new HttpResponseSnippet().document(this.operationBuilder.response()
.status(HttpStatus.BAD_REQUEST.value()).build());
}
@Test
public void responseWithHeaders() throws IOException {
this.snippet.expectHttpResponse("response-with-headers")
.withContents(httpResponse(HttpStatus.OK)
.header("Content-Type", "application/json").header("a", "alpha"));
this.snippet.expectHttpResponse().withContents(httpResponse(HttpStatus.OK)
.header("Content-Type", "application/json").header("a", "alpha"));
new HttpResponseSnippet()
.document(operationBuilder("response-with-headers").response()
.document(this.operationBuilder.response()
.header(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_JSON_VALUE)
.header("a", "alpha").build());
@@ -77,41 +75,37 @@ public class HttpResponseSnippetTests extends AbstractSnippetTests {
@Test
public void responseWithContent() throws IOException {
String content = "content";
this.snippet.expectHttpResponse("response-with-content")
this.snippet.expectHttpResponse()
.withContents(httpResponse(HttpStatus.OK).content(content)
.header(HttpHeaders.CONTENT_LENGTH, content.getBytes().length));
new HttpResponseSnippet().document(operationBuilder("response-with-content")
.response().content(content).build());
new HttpResponseSnippet()
.document(this.operationBuilder.response().content(content).build());
}
@Test
public void responseWithCharset() throws IOException {
String japaneseContent = "\u30b3\u30f3\u30c6\u30f3\u30c4";
byte[] contentBytes = japaneseContent.getBytes("UTF-8");
this.snippet.expectHttpResponse("response-with-charset")
this.snippet.expectHttpResponse()
.withContents(httpResponse(HttpStatus.OK)
.header("Content-Type", "text/plain;charset=UTF-8")
.content(japaneseContent)
.header(HttpHeaders.CONTENT_LENGTH, contentBytes.length));
new HttpResponseSnippet().document(operationBuilder("response-with-charset")
.response().header("Content-Type", "text/plain;charset=UTF-8")
.content(contentBytes).build());
new HttpResponseSnippet().document(this.operationBuilder.response()
.header("Content-Type", "text/plain;charset=UTF-8").content(contentBytes)
.build());
}
@Test
public void responseWithCustomSnippetAttributes() throws IOException {
this.snippet.expectHttpResponse("response-with-snippet-attributes")
this.snippet.expectHttpResponse()
.withContents(containsString("Title for the response"));
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
given(resolver.resolveTemplateResource("http-response"))
.willReturn(snippetResource("http-response-with-title"));
new HttpResponseSnippet(
attributes(key("title").value("Title for the response")))
.document(
operationBuilder("response-with-snippet-attributes")
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(resolver))
.build());
new HttpResponseSnippet(attributes(key("title").value("Title for the response")))
.document(this.operationBuilder.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(resolver)).build());
}
}

View File

@@ -25,11 +25,11 @@ import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.restdocs.snippet.SnippetException;
import org.springframework.restdocs.templates.TemplateFormats;
import org.springframework.restdocs.test.ExpectedSnippet;
import org.springframework.restdocs.test.OperationBuilder;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.springframework.restdocs.templates.TemplateFormats.asciidoctor;
/**
* Tests for failures when rendering {@link LinksSnippet} due to missing or undocumented
@@ -40,7 +40,10 @@ import static org.hamcrest.CoreMatchers.equalTo;
public class LinksSnippetFailureTests {
@Rule
public ExpectedSnippet snippet = new ExpectedSnippet(TemplateFormats.asciidoctor());
public OperationBuilder operationBuilder = new OperationBuilder(asciidoctor());
@Rule
public ExpectedSnippet snippet = new ExpectedSnippet(asciidoctor());
@Rule
public ExpectedException thrown = ExpectedException.none();
@@ -52,8 +55,7 @@ public class LinksSnippetFailureTests {
"Links with the following relations were not" + " documented: [foo]"));
new LinksSnippet(new StubLinkExtractor().withLinks(new Link("foo", "bar")),
Collections.<LinkDescriptor>emptyList())
.document(new OperationBuilder("undocumented-link",
this.snippet.getOutputDirectory()).build());
.document(this.operationBuilder.build());
}
@Test
@@ -63,8 +65,7 @@ public class LinksSnippetFailureTests {
+ " found in the response: [foo]"));
new LinksSnippet(new StubLinkExtractor(),
Arrays.asList(new LinkDescriptor("foo").description("bar")))
.document(new OperationBuilder("missing-link",
this.snippet.getOutputDirectory()).build());
.document(this.operationBuilder.build());
}
@Test
@@ -74,9 +75,8 @@ public class LinksSnippetFailureTests {
+ " documented: [a]. Links with the following relations were not"
+ " found in the response: [foo]"));
new LinksSnippet(new StubLinkExtractor().withLinks(new Link("a", "alpha")),
Arrays.asList(new LinkDescriptor("foo").description("bar"))).document(
new OperationBuilder("undocumented-link-and-missing-link",
this.snippet.getOutputDirectory()).build());
Arrays.asList(new LinkDescriptor("foo").description("bar")))
.document(this.operationBuilder.build());
}
@Test
@@ -87,8 +87,7 @@ public class LinksSnippetFailureTests {
+ " title was available from the link in the payload"));
new LinksSnippet(new StubLinkExtractor().withLinks(new Link("foo", "bar")),
Arrays.asList(new LinkDescriptor("foo")))
.document(new OperationBuilder("link-with-no-description",
this.snippet.getOutputDirectory()).build());
.document(this.operationBuilder.build());
}
}

View File

@@ -47,70 +47,66 @@ public class LinksSnippetTests extends AbstractSnippetTests {
@Test
public void ignoredLink() throws IOException {
this.snippet.expectLinks("ignored-link").withContents(
this.snippet.expectLinks().withContents(
tableWithHeader("Relation", "Description").row("`b`", "Link b"));
new LinksSnippet(
new StubLinkExtractor().withLinks(new Link("a", "alpha"),
new Link("b", "bravo")),
Arrays.asList(new LinkDescriptor("a").ignored(),
new LinkDescriptor("b").description("Link b")))
.document(operationBuilder("ignored-link").build());
.document(this.operationBuilder.build());
}
@Test
public void allUndocumentedLinksCanBeIgnored() throws IOException {
this.snippet.expectLinks("ignore-all-undocumented").withContents(
this.snippet.expectLinks().withContents(
tableWithHeader("Relation", "Description").row("`b`", "Link b"));
new LinksSnippet(
new StubLinkExtractor().withLinks(new Link("a", "alpha"),
new Link("b", "bravo")),
Arrays.asList(new LinkDescriptor("b").description("Link b")), true)
.document(operationBuilder("ignore-all-undocumented").build());
.document(this.operationBuilder.build());
}
@Test
public void presentOptionalLink() throws IOException {
this.snippet.expectLinks("present-optional-link").withContents(
this.snippet.expectLinks().withContents(
tableWithHeader("Relation", "Description").row("`foo`", "bar"));
new LinksSnippet(new StubLinkExtractor().withLinks(new Link("foo", "blah")),
Arrays.asList(new LinkDescriptor("foo").description("bar").optional()))
.document(operationBuilder("present-optional-link").build());
.document(this.operationBuilder.build());
}
@Test
public void missingOptionalLink() throws IOException {
this.snippet.expectLinks("missing-optional-link").withContents(
this.snippet.expectLinks().withContents(
tableWithHeader("Relation", "Description").row("`foo`", "bar"));
new LinksSnippet(new StubLinkExtractor(),
Arrays.asList(new LinkDescriptor("foo").description("bar").optional()))
.document(operationBuilder("missing-optional-link").build());
.document(this.operationBuilder.build());
}
@Test
public void documentedLinks() throws IOException {
this.snippet.expectLinks("documented-links")
.withContents(tableWithHeader("Relation", "Description").row("`a`", "one")
.row("`b`", "two"));
this.snippet.expectLinks().withContents(tableWithHeader("Relation", "Description")
.row("`a`", "one").row("`b`", "two"));
new LinksSnippet(
new StubLinkExtractor().withLinks(new Link("a", "alpha"),
new Link("b", "bravo")),
Arrays.asList(new LinkDescriptor("a").description("one"),
new LinkDescriptor("b").description("two")))
.document(operationBuilder("documented-links").build());
.document(this.operationBuilder.build());
}
@Test
public void linkDescriptionFromTitleInPayload() throws IOException {
this.snippet.expectLinks("link-description-from-title-in-payload")
.withContents(tableWithHeader("Relation", "Description").row("`a`", "one")
.row("`b`", "Link b"));
this.snippet.expectLinks().withContents(tableWithHeader("Relation", "Description")
.row("`a`", "one").row("`b`", "Link b"));
new LinksSnippet(
new StubLinkExtractor().withLinks(new Link("a", "alpha", "Link a"),
new Link("b", "bravo", "Link b")),
Arrays.asList(new LinkDescriptor("a").description("one"),
new LinkDescriptor("b"))).document(
operationBuilder("link-description-from-title-in-payload")
.build());
new LinkDescriptor("b"))).document(this.operationBuilder.build());
}
@Test
@@ -118,8 +114,7 @@ public class LinksSnippetTests extends AbstractSnippetTests {
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
given(resolver.resolveTemplateResource("links"))
.willReturn(snippetResource("links-with-title"));
this.snippet.expectLinks("links-with-custom-attributes")
.withContents(containsString("Title for the links"));
this.snippet.expectLinks().withContents(containsString("Title for the links"));
new LinksSnippet(
new StubLinkExtractor().withLinks(new Link("a", "alpha"),
@@ -128,7 +123,7 @@ public class LinksSnippetTests extends AbstractSnippetTests {
new LinkDescriptor("b").description("two")),
attributes(key("title").value("Title for the links")))
.document(
operationBuilder("links-with-custom-attributes")
this.operationBuilder
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(resolver))
.build());
@@ -139,7 +134,7 @@ public class LinksSnippetTests extends AbstractSnippetTests {
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
given(resolver.resolveTemplateResource("links"))
.willReturn(snippetResource("links-with-extra-column"));
this.snippet.expectLinks("links-with-custom-descriptor-attributes")
this.snippet.expectLinks()
.withContents(tableWithHeader("Relation", "Description", "Foo")
.row("a", "one", "alpha").row("b", "two", "bravo"));
@@ -149,38 +144,33 @@ public class LinksSnippetTests extends AbstractSnippetTests {
Arrays.asList(
new LinkDescriptor("a").description("one")
.attributes(key("foo").value("alpha")),
new LinkDescriptor("b").description("two").attributes(
key("foo").value("bravo"))))
.document(operationBuilder(
"links-with-custom-descriptor-attributes")
.attribute(TemplateEngine.class
.getName(),
new MustacheTemplateEngine(
resolver))
.build());
new LinkDescriptor("b").description("two")
.attributes(key("foo").value("bravo"))))
.document(this.operationBuilder.attribute(
TemplateEngine.class.getName(),
new MustacheTemplateEngine(resolver))
.build());
}
@Test
public void additionalDescriptors() throws IOException {
this.snippet.expectLinks("additional-descriptors")
.withContents(tableWithHeader("Relation", "Description").row("`a`", "one")
.row("`b`", "two"));
this.snippet.expectLinks().withContents(tableWithHeader("Relation", "Description")
.row("`a`", "one").row("`b`", "two"));
HypermediaDocumentation
.links(new StubLinkExtractor().withLinks(new Link("a", "alpha"),
new Link("b", "bravo")),
new LinkDescriptor("a").description("one"))
.and(new LinkDescriptor("b").description("two"))
.document(operationBuilder("additional-descriptors").build());
.document(this.operationBuilder.build());
}
@Test
public void tableCellContentIsEscapedWhenNecessary() throws IOException {
this.snippet.expectLinks("links-with-escaped-content")
.withContents(tableWithHeader("Relation", "Description").row(
escapeIfNecessary("`Foo|Bar`"), escapeIfNecessary("one|two")));
this.snippet.expectLinks().withContents(tableWithHeader("Relation", "Description")
.row(escapeIfNecessary("`Foo|Bar`"), escapeIfNecessary("one|two")));
new LinksSnippet(new StubLinkExtractor().withLinks(new Link("Foo|Bar", "foo")),
Arrays.asList(new LinkDescriptor("Foo|Bar").description("one|two")))
.document(operationBuilder("links-with-escaped-content").build());
.document(this.operationBuilder.build());
}
private String escapeIfNecessary(String input) {

View File

@@ -42,6 +42,9 @@ import static org.springframework.restdocs.test.SnippetMatchers.tableWithHeader;
*/
public class AsciidoctorRequestFieldsSnippetTests {
@Rule
public OperationBuilder operationBuilder = new OperationBuilder(asciidoctor());
@Rule
public ExpectedSnippet snippet = new ExpectedSnippet(asciidoctor());
@@ -50,21 +53,22 @@ public class AsciidoctorRequestFieldsSnippetTests {
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
given(resolver.resolveTemplateResource("request-fields"))
.willReturn(snippetResource("request-fields-with-list-description"));
this.snippet.expectRequestFields("request-fields-with-list-description")
.withContents(
tableWithHeader(asciidoctor(), "Path", "Type", "Description")
//
.row("a", "String", String.format(" - one%n - two"))
.configuration("[cols=\"1,1,1a\"]"));
this.snippet.expectRequestFields().withContents(
tableWithHeader(asciidoctor(), "Path", "Type", "Description")
//
.row("a", "String", String.format(" - one%n - two"))
.configuration("[cols=\"1,1,1a\"]"));
new RequestFieldsSnippet(Arrays.asList(
fieldWithPath("a").description(Arrays.asList("one", "two")))).document(
new OperationBuilder("request-fields-with-list-description",
this.snippet.getOutputDirectory())
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(resolver))
.request("http://localhost")
.content("{\"a\": \"foo\"}").build());
new RequestFieldsSnippet(
Arrays.asList(
fieldWithPath("a").description(Arrays.asList("one", "two"))))
.document(
this.operationBuilder
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(
resolver))
.request("http://localhost")
.content("{\"a\": \"foo\"}").build());
}
private FileSystemResource snippetResource(String name) {

View File

@@ -47,6 +47,10 @@ public class RequestFieldsSnippetFailureTests {
@Rule
public ExpectedSnippet snippet = new ExpectedSnippet(TemplateFormats.asciidoctor());
@Rule
public OperationBuilder operationBuilder = new OperationBuilder(
TemplateFormats.asciidoctor());
@Rule
public ExpectedException thrown = ExpectedException.none();
@@ -56,9 +60,8 @@ public class RequestFieldsSnippetFailureTests {
this.thrown.expectMessage(startsWith(
"The following parts of the payload were not" + " documented:"));
new RequestFieldsSnippet(Collections.<FieldDescriptor>emptyList())
.document(new OperationBuilder("undocumented-request-field",
this.snippet.getOutputDirectory()).request("http://localhost")
.content("{\"a\": 5}").build());
.document(this.operationBuilder.request("http://localhost")
.content("{\"a\": 5}").build());
}
@Test
@@ -67,9 +70,8 @@ public class RequestFieldsSnippetFailureTests {
this.thrown.expectMessage(equalTo("Fields with the following paths were not found"
+ " in the payload: [a.b]"));
new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a.b").description("one")))
.document(new OperationBuilder("missing-request-fields",
this.snippet.getOutputDirectory()).request("http://localhost")
.content("{}").build());
.document(this.operationBuilder.request("http://localhost").content("{}")
.build());
}
@Test
@@ -77,11 +79,8 @@ public class RequestFieldsSnippetFailureTests {
this.thrown.expect(FieldTypeRequiredException.class);
new RequestFieldsSnippet(
Arrays.asList(fieldWithPath("a.b").description("one").optional()))
.document(new OperationBuilder(
"missing-optional-request-field-with-no-type",
this.snippet.getOutputDirectory())
.request("http://localhost").content("{ }")
.build());
.document(this.operationBuilder.request("http://localhost")
.content("{ }").build());
}
@Test
@@ -93,10 +92,8 @@ public class RequestFieldsSnippetFailureTests {
.expectMessage(endsWith("Fields with the following paths were not found"
+ " in the payload: [a.b]"));
new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a.b").description("one")))
.document(new OperationBuilder(
"undocumented-request-field-and-missing-request-field",
this.snippet.getOutputDirectory()).request("http://localhost")
.content("{ \"a\": { \"c\": 5 }}").build());
.document(this.operationBuilder.request("http://localhost")
.content("{ \"a\": { \"c\": 5 }}").build());
}
@Test
@@ -105,9 +102,7 @@ public class RequestFieldsSnippetFailureTests {
this.thrown.expectMessage(
equalTo("Cannot document request fields as the request body is empty"));
new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a").description("one")))
.document(new OperationBuilder("no-request-body",
this.snippet.getOutputDirectory()).request("http://localhost")
.build());
.document(this.operationBuilder.request("http://localhost").build());
}
@Test
@@ -117,10 +112,8 @@ public class RequestFieldsSnippetFailureTests {
+ " Object but the actual type is Number"));
new RequestFieldsSnippet(Arrays
.asList(fieldWithPath("a").description("one").type(JsonFieldType.OBJECT)))
.document(new OperationBuilder("mismatched-field-types",
this.snippet.getOutputDirectory())
.request("http://localhost")
.content("{ \"a\": 5 }").build());
.document(this.operationBuilder.request("http://localhost")
.content("{ \"a\": 5 }").build());
}
@Test
@@ -130,11 +123,8 @@ public class RequestFieldsSnippetFailureTests {
+ " Object but the actual type is Varies"));
new RequestFieldsSnippet(Arrays.asList(
fieldWithPath("[].a").description("one").type(JsonFieldType.OBJECT)))
.document(new OperationBuilder("mismatched-field-types",
this.snippet.getOutputDirectory())
.request("http://localhost")
.content("[{ \"a\": 5 },{ \"a\": \"b\" }]")
.build());
.document(this.operationBuilder.request("http://localhost")
.content("[{ \"a\": 5 },{ \"a\": \"b\" }]").build());
}
@Test
@@ -143,23 +133,20 @@ public class RequestFieldsSnippetFailureTests {
this.thrown.expectMessage(startsWith(
"The following parts of the payload were not" + " documented:"));
new RequestFieldsSnippet(Collections.<FieldDescriptor>emptyList())
.document(new OperationBuilder("undocumented-xml-request-field",
this.snippet.getOutputDirectory()).request("http://localhost")
.content("<a><b>5</b></a>")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_XML_VALUE)
.build());
.document(this.operationBuilder.request("http://localhost")
.content("<a><b>5</b></a>").header(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_XML_VALUE)
.build());
}
@Test
public void xmlRequestFieldWithNoType() throws IOException {
this.thrown.expect(FieldTypeRequiredException.class);
new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a").description("one")))
.document(new OperationBuilder("missing-xml-request",
this.snippet.getOutputDirectory()).request("http://localhost")
.content("<a>5</a>").header(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_XML_VALUE)
.build());
.document(this.operationBuilder.request("http://localhost")
.content("<a>5</a>").header(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_XML_VALUE)
.build());
}
@Test
@@ -168,15 +155,10 @@ public class RequestFieldsSnippetFailureTests {
this.thrown.expectMessage(equalTo("Fields with the following paths were not found"
+ " in the payload: [a/b]"));
new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a/b").description("one"),
fieldWithPath("a").description("one")))
.document(
new OperationBuilder("missing-xml-request-fields",
this.snippet.getOutputDirectory())
.request("http://localhost")
.content("<a></a>")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_XML_VALUE)
.build());
fieldWithPath("a").description("one"))).document(this.operationBuilder
.request("http://localhost").content("<a></a>")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE)
.build());
}
@Test
@@ -189,13 +171,10 @@ public class RequestFieldsSnippetFailureTests {
.expectMessage(endsWith("Fields with the following paths were not found"
+ " in the payload: [a/b]"));
new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a/b").description("one")))
.document(new OperationBuilder(
"undocumented-xml-request-field-and-missing-xml-request-field",
this.snippet.getOutputDirectory()).request("http://localhost")
.content("<a><c>5</c></a>")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_XML_VALUE)
.build());
.document(this.operationBuilder.request("http://localhost")
.content("<a><c>5</c></a>").header(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_XML_VALUE)
.build());
}
}

View File

@@ -50,7 +50,7 @@ public class RequestFieldsSnippetTests extends AbstractSnippetTests {
@Test
public void mapRequestWithFields() throws IOException {
this.snippet.expectRequestFields("map-request-with-fields")
this.snippet.expectRequestFields()
.withContents(tableWithHeader("Path", "Type", "Description")
.row("`a.b`", "`Number`", "one").row("`a.c`", "`String`", "two")
.row("`a`", "`Object`", "three"));
@@ -58,15 +58,14 @@ public class RequestFieldsSnippetTests extends AbstractSnippetTests {
new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a.b").description("one"),
fieldWithPath("a.c").description("two"),
fieldWithPath("a").description("three")))
.document(operationBuilder("map-request-with-fields")
.request("http://localhost")
.document(this.operationBuilder.request("http://localhost")
.content("{\"a\": {\"b\": 5, \"c\": \"charlie\"}}")
.build());
}
@Test
public void arrayRequestWithFields() throws IOException {
this.snippet.expectRequestFields("array-request-with-fields")
this.snippet.expectRequestFields()
.withContents(tableWithHeader("Path", "Type", "Description")
.row("`[]a.b`", "`Number`", "one")
.row("`[]a.c`", "`String`", "two")
@@ -75,8 +74,7 @@ public class RequestFieldsSnippetTests extends AbstractSnippetTests {
new RequestFieldsSnippet(Arrays.asList(fieldWithPath("[]a.b").description("one"),
fieldWithPath("[]a.c").description("two"),
fieldWithPath("[]a").description("three")))
.document(operationBuilder("array-request-with-fields")
.request("http://localhost")
.document(this.operationBuilder.request("http://localhost")
.content(
"[{\"a\": {\"b\": 5}},{\"a\": {\"c\": \"charlie\"}}]")
.build());
@@ -84,64 +82,57 @@ public class RequestFieldsSnippetTests extends AbstractSnippetTests {
@Test
public void ignoredRequestField() throws IOException {
this.snippet.expectRequestFields("ignored-request-field")
this.snippet.expectRequestFields()
.withContents(tableWithHeader("Path", "Type", "Description").row("`b`",
"`Number`", "Field b"));
new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a").ignored(),
fieldWithPath("b").description("Field b")))
.document(operationBuilder("ignored-request-field")
.request("http://localhost")
.document(this.operationBuilder.request("http://localhost")
.content("{\"a\": 5, \"b\": 4}").build());
}
@Test
public void allUndocumentedRequestFieldsCanBeIgnored() throws IOException {
this.snippet.expectRequestFields("ignore-all-undocumented")
this.snippet.expectRequestFields()
.withContents(tableWithHeader("Path", "Type", "Description").row("`b`",
"`Number`", "Field b"));
new RequestFieldsSnippet(Arrays.asList(fieldWithPath("b").description("Field b")),
true).document(
operationBuilder("ignore-all-undocumented")
.request("http://localhost")
this.operationBuilder.request("http://localhost")
.content("{\"a\": 5, \"b\": 4}").build());
}
@Test
public void missingOptionalRequestField() throws IOException {
this.snippet.expectRequestFields("missing-optional-request-field")
this.snippet.expectRequestFields()
.withContents(tableWithHeader("Path", "Type", "Description").row("`a.b`",
"`String`", "one"));
new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a.b").description("one")
.type(JsonFieldType.STRING).optional()))
.document(operationBuilder("missing-optional-request-field")
.request("http://localhost").content("{}").build());
.document(this.operationBuilder.request("http://localhost")
.content("{}").build());
}
@Test
public void missingIgnoredOptionalRequestFieldDoesNotRequireAType()
throws IOException {
this.snippet
.expectRequestFields(
"missing-ignored-optional-request-field-does-not-require-a-type")
this.snippet.expectRequestFields()
.withContents(tableWithHeader("Path", "Type", "Description"));
new RequestFieldsSnippet(Arrays
.asList(fieldWithPath("a.b").description("one").ignored().optional()))
.document(operationBuilder(
"missing-ignored-optional-request-field-does-not-require-a-type")
.request("http://localhost").content("{}")
.build());
.document(this.operationBuilder.request("http://localhost")
.content("{}").build());
}
@Test
public void presentOptionalRequestField() throws IOException {
this.snippet.expectRequestFields("present-optional-request-field")
this.snippet.expectRequestFields()
.withContents(tableWithHeader("Path", "Type", "Description").row("`a.b`",
"`String`", "one"));
new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a.b").description("one")
.type(JsonFieldType.STRING).optional()))
.document(operationBuilder("present-optional-request-field")
.request("http://localhost")
.document(this.operationBuilder.request("http://localhost")
.content("{\"a\": { \"b\": \"bravo\"}}").build());
}
@@ -150,16 +141,18 @@ public class RequestFieldsSnippetTests extends AbstractSnippetTests {
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
given(resolver.resolveTemplateResource("request-fields"))
.willReturn(snippetResource("request-fields-with-title"));
this.snippet.expectRequestFields("request-fields-with-custom-attributes")
.withContents(containsString("Custom title"));
this.snippet.expectRequestFields().withContents(containsString("Custom title"));
new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a").description("one")),
attributes(key("title").value("Custom title"))).document(
operationBuilder("request-fields-with-custom-attributes")
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(resolver))
.request("http://localhost").content("{\"a\": \"foo\"}")
.build());
attributes(
key("title").value("Custom title")))
.document(
this.operationBuilder
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(
resolver))
.request("http://localhost")
.content("{\"a\": \"foo\"}").build());
}
@Test
@@ -167,8 +160,7 @@ public class RequestFieldsSnippetTests extends AbstractSnippetTests {
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
given(resolver.resolveTemplateResource("request-fields"))
.willReturn(snippetResource("request-fields-with-extra-column"));
this.snippet
.expectRequestFields("request-fields-with-custom-descriptor-attributes")
this.snippet.expectRequestFields()
.withContents(tableWithHeader("Path", "Type", "Description", "Foo")
.row("a.b", "Number", "one", "alpha")
.row("a.c", "String", "two", "bravo")
@@ -181,8 +173,8 @@ public class RequestFieldsSnippetTests extends AbstractSnippetTests {
.attributes(key("foo").value("bravo")),
fieldWithPath("a").description("three")
.attributes(key("foo").value("charlie"))))
.document(operationBuilder(
"request-fields-with-custom-descriptor-attributes")
.document(
this.operationBuilder
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(
resolver))
@@ -194,35 +186,31 @@ public class RequestFieldsSnippetTests extends AbstractSnippetTests {
@Test
public void fieldWithExplictExactlyMatchingType() throws IOException {
this.snippet
.expectRequestFields("request-field-with-explicit-exactly-matching-type")
this.snippet.expectRequestFields()
.withContents(tableWithHeader("Path", "Type", "Description").row("`a`",
"`Number`", "one"));
new RequestFieldsSnippet(Arrays
.asList(fieldWithPath("a").description("one").type(JsonFieldType.NUMBER)))
.document(operationBuilder(
"request-field-with-explicit-exactly-matching-type")
.request("http://localhost")
.content("{\"a\": 5 }").build());
.document(this.operationBuilder.request("http://localhost")
.content("{\"a\": 5 }").build());
}
@Test
public void fieldWithExplictVariesType() throws IOException {
this.snippet.expectRequestFields("request-field-with-explicit-varies-type")
this.snippet.expectRequestFields()
.withContents(tableWithHeader("Path", "Type", "Description").row("`a`",
"`Varies`", "one"));
new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a").description("one")
.type(JsonFieldType.VARIES))).document(
operationBuilder("request-field-with-explicit-varies-type")
.request("http://localhost").content("{\"a\": 5 }")
.build());
new RequestFieldsSnippet(Arrays
.asList(fieldWithPath("a").description("one").type(JsonFieldType.VARIES)))
.document(this.operationBuilder.request("http://localhost")
.content("{\"a\": 5 }").build());
}
@Test
public void xmlRequestFields() throws IOException {
this.snippet.expectRequestFields("xml-request")
this.snippet.expectRequestFields()
.withContents(tableWithHeader("Path", "Type", "Description")
.row("`a/b`", "`b`", "one").row("`a/c`", "`c`", "two").row("`a`",
"`a`", "three"));
@@ -232,8 +220,7 @@ public class RequestFieldsSnippetTests extends AbstractSnippetTests {
fieldWithPath("a/c").description("two").type("c"),
fieldWithPath("a").description("three").type("a")))
.document(
operationBuilder("xml-request")
.request("http://localhost")
this.operationBuilder.request("http://localhost")
.content("<a><b>5</b><c>charlie</c></a>")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_XML_VALUE)
@@ -242,7 +229,7 @@ public class RequestFieldsSnippetTests extends AbstractSnippetTests {
@Test
public void additionalDescriptors() throws IOException {
this.snippet.expectRequestFields("additional-descriptors")
this.snippet.expectRequestFields()
.withContents(tableWithHeader("Path", "Type", "Description")
.row("`a.b`", "`Number`", "one").row("`a.c`", "`String`", "two")
.row("`a`", "`Object`", "three"));
@@ -251,14 +238,13 @@ public class RequestFieldsSnippetTests extends AbstractSnippetTests {
.requestFields(fieldWithPath("a.b").description("one"),
fieldWithPath("a.c").description("two"))
.and(fieldWithPath("a").description("three"))
.document(operationBuilder("additional-descriptors")
.request("http://localhost")
.document(operationBuilder.request("http://localhost")
.content("{\"a\": {\"b\": 5, \"c\": \"charlie\"}}").build());
}
@Test
public void prefixedAdditionalDescriptors() throws IOException {
this.snippet.expectRequestFields("prefixed-additional-descriptors")
this.snippet.expectRequestFields()
.withContents(tableWithHeader("Path", "Type", "Description")
.row("`a`", "`Object`", "one").row("`a.b`", "`Number`", "two")
.row("`a.c`", "`String`", "three"));
@@ -266,23 +252,21 @@ public class RequestFieldsSnippetTests extends AbstractSnippetTests {
PayloadDocumentation.requestFields(fieldWithPath("a").description("one"))
.andWithPrefix("a.", fieldWithPath("b").description("two"),
fieldWithPath("c").description("three"))
.document(operationBuilder("prefixed-additional-descriptors")
.request("http://localhost")
.document(operationBuilder.request("http://localhost")
.content("{\"a\": {\"b\": 5, \"c\": \"charlie\"}}").build());
}
@Test
public void requestWithFieldsWithEscapedContent() throws IOException {
this.snippet.expectRequestFields("request-fields-with-escaped-content")
this.snippet.expectRequestFields()
.withContents(tableWithHeader("Path", "Type", "Description").row(
escapeIfNecessary("`Foo|Bar`"), escapeIfNecessary("`one|two`"),
escapeIfNecessary("three|four")));
new RequestFieldsSnippet(Arrays.asList(
fieldWithPath("Foo|Bar").type("one|two").description("three|four")))
.document(operationBuilder("request-fields-with-escaped-content")
.request("http://localhost").content("{\"Foo|Bar\": 5}")
.build());
.document(operationBuilder.request("http://localhost")
.content("{\"Foo|Bar\": 5}").build());
}
private String escapeIfNecessary(String input) {

View File

@@ -27,7 +27,6 @@ import org.junit.rules.ExpectedException;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.restdocs.snippet.SnippetException;
import org.springframework.restdocs.templates.TemplateFormats;
import org.springframework.restdocs.test.ExpectedSnippet;
import org.springframework.restdocs.test.OperationBuilder;
@@ -35,6 +34,7 @@ import static org.hamcrest.CoreMatchers.endsWith;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
import static org.springframework.restdocs.templates.TemplateFormats.asciidoctor;
/**
* Tests for failures when rendering {@link ResponseFieldsSnippet} due to missing or
@@ -45,7 +45,10 @@ import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWit
public class ResponseFieldsSnippetFailureTests {
@Rule
public ExpectedSnippet snippet = new ExpectedSnippet(TemplateFormats.asciidoctor());
public OperationBuilder operationBuilder = new OperationBuilder(asciidoctor());
@Rule
public ExpectedSnippet snippet = new ExpectedSnippet(asciidoctor());
@Rule
public ExpectedException thrown = ExpectedException.none();
@@ -56,8 +59,7 @@ public class ResponseFieldsSnippetFailureTests {
this.thrown.expectMessage(
equalTo("Cannot document response fields as the response body is empty"));
new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("a").description("one")))
.document(new OperationBuilder("no-response-body",
this.snippet.getOutputDirectory()).build());
.document(this.operationBuilder.build());
}
@Test
@@ -67,9 +69,8 @@ public class ResponseFieldsSnippetFailureTests {
+ " Object but the actual type is Number"));
new ResponseFieldsSnippet(Arrays
.asList(fieldWithPath("a").description("one").type(JsonFieldType.OBJECT)))
.document(new OperationBuilder("mismatched-field-types",
this.snippet.getOutputDirectory()).response()
.content("{ \"a\": 5 }}").build());
.document(this.operationBuilder.response()
.content("{ \"a\": 5 }}").build());
}
@Test
@@ -79,10 +80,8 @@ public class ResponseFieldsSnippetFailureTests {
+ " Object but the actual type is Varies"));
new ResponseFieldsSnippet(Arrays.asList(
fieldWithPath("[].a").description("one").type(JsonFieldType.OBJECT)))
.document(new OperationBuilder("mismatched-field-types",
this.snippet.getOutputDirectory()).response()
.content("[{ \"a\": 5 },{ \"a\": \"b\" }]")
.build());
.document(this.operationBuilder.response()
.content("[{ \"a\": 5 },{ \"a\": \"b\" }]").build());
}
@Test
@@ -90,15 +89,10 @@ public class ResponseFieldsSnippetFailureTests {
this.thrown.expect(SnippetException.class);
this.thrown.expectMessage(startsWith(
"The following parts of the payload were not" + " documented:"));
new ResponseFieldsSnippet(
Collections.<FieldDescriptor>emptyList())
.document(
new OperationBuilder("undocumented-xml-response-field",
this.snippet.getOutputDirectory()).response()
.content("<a><b>5</b></a>")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_XML_VALUE)
.build());
new ResponseFieldsSnippet(Collections.<FieldDescriptor>emptyList())
.document(this.operationBuilder.response().content("<a><b>5</b></a>")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE)
.build());
}
@Test
@@ -109,8 +103,8 @@ public class ResponseFieldsSnippetFailureTests {
new ResponseFieldsSnippet(
Arrays.asList(fieldWithPath("a").description("one").type("b"),
fieldWithPath("a/@id").description("two").type("c")))
.document(new OperationBuilder("missing-xml-attribute",
this.snippet.getOutputDirectory()).response()
.document(
this.operationBuilder.response()
.content("<a>foo</a>")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_XML_VALUE)
@@ -125,24 +119,20 @@ public class ResponseFieldsSnippetFailureTests {
+ "%n<a>bar</a>%n")));
new ResponseFieldsSnippet(
Arrays.asList(fieldWithPath("a/@id").description("one").type("a")))
.document(
new OperationBuilder("documented-attribute-is-removed",
this.snippet.getOutputDirectory()).response()
.content("<a id=\"foo\">bar</a>")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_XML_VALUE)
.build());
.document(this.operationBuilder.response()
.content("<a id=\"foo\">bar</a>")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_XML_VALUE)
.build());
}
@Test
public void xmlResponseFieldWithNoType() throws IOException {
this.thrown.expect(FieldTypeRequiredException.class);
new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("a").description("one")))
.document(new OperationBuilder("xml-response-no-field-type",
this.snippet.getOutputDirectory()).response().content("<a>5</a>")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_XML_VALUE)
.build());
.document(this.operationBuilder.response().content("<a>5</a>")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE)
.build());
}
@Test
@@ -151,14 +141,10 @@ public class ResponseFieldsSnippetFailureTests {
this.thrown.expectMessage(equalTo("Fields with the following paths were not found"
+ " in the payload: [a/b]"));
new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("a/b").description("one"),
fieldWithPath("a").description("one")))
.document(
new OperationBuilder("missing-xml-response-field",
this.snippet.getOutputDirectory()).response()
.content("<a></a>")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_XML_VALUE)
.build());
fieldWithPath("a").description("one"))).document(this.operationBuilder
.response().content("<a></a>")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE)
.build());
}
@Test
@@ -170,16 +156,10 @@ public class ResponseFieldsSnippetFailureTests {
this.thrown
.expectMessage(endsWith("Fields with the following paths were not found"
+ " in the payload: [a/b]"));
new ResponseFieldsSnippet(
Arrays.asList(fieldWithPath("a/b").description("one")))
.document(
new OperationBuilder(
"undocumented-xml-request-field-and-missing-xml-request-field",
this.snippet.getOutputDirectory()).response()
.content("<a><c>5</c></a>")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_XML_VALUE)
.build());
new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("a/b").description("one")))
.document(this.operationBuilder.response().content("<a><c>5</c></a>")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE)
.build());
}
}

View File

@@ -50,7 +50,7 @@ public class ResponseFieldsSnippetTests extends AbstractSnippetTests {
@Test
public void mapResponseWithFields() throws IOException {
this.snippet.expectResponseFields("map-response-with-fields")
this.snippet.expectResponseFields()
.withContents(tableWithHeader("Path", "Type", "Description")
.row("`id`", "`Number`", "one").row("`date`", "`String`", "two")
.row("`assets`", "`Array`", "three")
@@ -63,7 +63,7 @@ public class ResponseFieldsSnippetTests extends AbstractSnippetTests {
fieldWithPath("assets[]").description("four"),
fieldWithPath("assets[].id").description("five"),
fieldWithPath("assets[].name").description("six")))
.document(operationBuilder("map-response-with-fields").response()
.document(this.operationBuilder.response()
.content(
"{\"id\": 67,\"date\": \"2015-01-20\",\"assets\":"
+ " [{\"id\":356,\"name\": \"sample\"}]}")
@@ -72,15 +72,15 @@ public class ResponseFieldsSnippetTests extends AbstractSnippetTests {
@Test
public void arrayResponseWithFields() throws IOException {
this.snippet.expectResponseFields("array-response-with-fields")
this.snippet.expectResponseFields()
.withContents(tableWithHeader("Path", "Type", "Description")
.row("`[]a.b`", "`Number`", "one")
.row("`[]a.c`", "`String`", "two")
.row("`[]a`", "`Object`", "three"));
new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("[]a.b").description("one"),
fieldWithPath("[]a.c").description("two"),
fieldWithPath("[]a").description("three"))).document(
operationBuilder("array-response-with-fields").response()
fieldWithPath("[]a").description("three")))
.document(this.operationBuilder.response()
.content(
"[{\"a\": {\"b\": 5}},{\"a\": {\"c\": \"charlie\"}}]")
.build());
@@ -88,35 +88,35 @@ public class ResponseFieldsSnippetTests extends AbstractSnippetTests {
@Test
public void arrayResponse() throws IOException {
this.snippet.expectResponseFields("array-response")
this.snippet.expectResponseFields()
.withContents(tableWithHeader("Path", "Type", "Description").row("`[]`",
"`String`", "one"));
new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("[]").description("one")))
.document(operationBuilder("array-response").response()
.document(this.operationBuilder.response()
.content("[\"a\", \"b\", \"c\"]").build());
}
@Test
public void ignoredResponseField() throws IOException {
this.snippet.expectResponseFields("ignored-response-field")
this.snippet.expectResponseFields()
.withContents(tableWithHeader("Path", "Type", "Description").row("`b`",
"`Number`", "Field b"));
new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("a").ignored(),
fieldWithPath("b").description("Field b")))
.document(operationBuilder("ignored-response-field").response()
.document(this.operationBuilder.response()
.content("{\"a\": 5, \"b\": 4}").build());
}
@Test
public void allUndocumentedFieldsCanBeIgnored() throws IOException {
this.snippet.expectResponseFields("ignore-all-undocumented")
this.snippet.expectResponseFields()
.withContents(tableWithHeader("Path", "Type", "Description").row("`b`",
"`Number`", "Field b"));
new ResponseFieldsSnippet(
Arrays.asList(fieldWithPath("b").description("Field b")), true)
.document(operationBuilder("ignore-all-undocumented").response()
.document(this.operationBuilder.response()
.content("{\"a\": 5, \"b\": 4}").build());
}
@@ -125,50 +125,48 @@ public class ResponseFieldsSnippetTests extends AbstractSnippetTests {
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
given(resolver.resolveTemplateResource("response-fields"))
.willReturn(snippetResource("response-fields-with-title"));
this.snippet.expectResponseFields("response-fields-with-custom-attributes")
.withContents(containsString("Custom title"));
this.snippet.expectResponseFields().withContents(containsString("Custom title"));
new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("a").description("one")),
attributes(key("title").value("Custom title"))).document(
operationBuilder("response-fields-with-custom-attributes")
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(resolver))
.response().content("{\"a\": \"foo\"}").build());
attributes(
key("title").value("Custom title")))
.document(
this.operationBuilder
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(
resolver))
.response().content("{\"a\": \"foo\"}")
.build());
}
@Test
public void missingOptionalResponseField() throws IOException {
this.snippet.expectResponseFields("missing-optional-response-field")
this.snippet.expectResponseFields()
.withContents(tableWithHeader("Path", "Type", "Description").row("`a.b`",
"`String`", "one"));
new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("a.b").description("one")
.type(JsonFieldType.STRING).optional()))
.document(operationBuilder("missing-optional-response-field")
.response().content("{}").build());
.document(this.operationBuilder.response().content("{}").build());
}
@Test
public void missingIgnoredOptionalResponseFieldDoesNotRequireAType()
throws IOException {
this.snippet
.expectResponseFields(
"missing-ignored-optional-response-field-does-not-require-a-type")
this.snippet.expectResponseFields()
.withContents(tableWithHeader("Path", "Type", "Description"));
new ResponseFieldsSnippet(Arrays
.asList(fieldWithPath("a.b").description("one").ignored().optional()))
.document(operationBuilder(
"missing-ignored-optional-response-field-does-not-require-a-type")
.response().content("{}").build());
.document(this.operationBuilder.response().content("{}").build());
}
@Test
public void presentOptionalResponseField() throws IOException {
this.snippet.expectResponseFields("present-optional-response-field")
this.snippet.expectResponseFields()
.withContents(tableWithHeader("Path", "Type", "Description").row("`a.b`",
"`String`", "one"));
new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("a.b").description("one")
.type(JsonFieldType.STRING).optional())).document(
operationBuilder("present-optional-response-field").response()
.type(JsonFieldType.STRING).optional()))
.document(this.operationBuilder.response()
.content("{\"a\": { \"b\": \"bravo\"}}").build());
}
@@ -177,58 +175,57 @@ public class ResponseFieldsSnippetTests extends AbstractSnippetTests {
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
given(resolver.resolveTemplateResource("response-fields"))
.willReturn(snippetResource("response-fields-with-extra-column"));
this.snippet.expectResponseFields("response-fields-with-custom-attributes")
this.snippet.expectResponseFields()
.withContents(tableWithHeader("Path", "Type", "Description", "Foo")
.row("a.b", "Number", "one", "alpha")
.row("a.c", "String", "two", "bravo")
.row("a", "Object", "three", "charlie"));
new ResponseFieldsSnippet(Arrays.asList(
fieldWithPath("a.b").description("one").attributes(key("foo")
.value("alpha")),
fieldWithPath("a.b").description("one")
.attributes(key("foo").value("alpha")),
fieldWithPath("a.c").description("two")
.attributes(key("foo").value("bravo")),
fieldWithPath("a").description("three")
.attributes(key("foo").value("charlie")))).document(
operationBuilder("response-fields-with-custom-attributes")
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(resolver))
.response()
.content(
"{\"a\": {\"b\": 5, \"c\": \"charlie\"}}")
.build());
.attributes(key("foo").value("charlie"))))
.document(
this.operationBuilder
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(
resolver))
.response()
.content(
"{\"a\": {\"b\": 5, \"c\": \"charlie\"}}")
.build());
}
@Test
public void fieldWithExplictExactlyMatchingType() throws IOException {
this.snippet
.expectResponseFields(
"response-field-with-explicit-exactly-matching-type")
this.snippet.expectResponseFields()
.withContents(tableWithHeader("Path", "Type", "Description").row("`a`",
"`Number`", "one"));
new ResponseFieldsSnippet(Arrays
.asList(fieldWithPath("a").description("one").type(JsonFieldType.NUMBER)))
.document(operationBuilder(
"response-field-with-explicit-exactly-matching-type")
.response().content("{\"a\": 5 }").build());
.document(this.operationBuilder.response().content("{\"a\": 5 }")
.build());
}
@Test
public void fieldWithExplictVariesType() throws IOException {
this.snippet.expectResponseFields("response-field-with-explicit-varies-type")
this.snippet.expectResponseFields()
.withContents(tableWithHeader("Path", "Type", "Description").row("`a`",
"`Varies`", "one"));
new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("a").description("one")
.type(JsonFieldType.VARIES))).document(
operationBuilder("response-field-with-explicit-varies-type")
.response().content("{\"a\": 5 }").build());
new ResponseFieldsSnippet(Arrays
.asList(fieldWithPath("a").description("one").type(JsonFieldType.VARIES)))
.document(this.operationBuilder.response().content("{\"a\": 5 }")
.build());
}
@Test
public void xmlResponseFields() throws IOException {
this.snippet.expectResponseFields("xml-response")
this.snippet.expectResponseFields()
.withContents(tableWithHeader("Path", "Type", "Description")
.row("`a/b`", "`b`", "one").row("`a/c`", "`c`", "two").row("`a`",
"`a`", "three"));
@@ -237,7 +234,7 @@ public class ResponseFieldsSnippetTests extends AbstractSnippetTests {
fieldWithPath("a/c").description("two").type("c"),
fieldWithPath("a").description("three").type("a")))
.document(
operationBuilder("xml-response").response()
this.operationBuilder.response()
.content("<a><b>5</b><c>charlie</c></a>")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_XML_VALUE)
@@ -246,14 +243,14 @@ public class ResponseFieldsSnippetTests extends AbstractSnippetTests {
@Test
public void xmlAttribute() throws IOException {
this.snippet.expectResponseFields("xml-attribute")
this.snippet.expectResponseFields()
.withContents(tableWithHeader("Path", "Type", "Description")
.row("`a`", "`b`", "one").row("`a/@id`", "`c`", "two"));
new ResponseFieldsSnippet(
Arrays.asList(fieldWithPath("a").description("one").type("b"),
fieldWithPath("a/@id").description("two").type("c")))
.document(
operationBuilder("xml-attribute").response()
this.operationBuilder.response()
.content("<a id=\"1\">foo</a>")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_XML_VALUE)
@@ -262,15 +259,15 @@ public class ResponseFieldsSnippetTests extends AbstractSnippetTests {
@Test
public void missingOptionalXmlAttribute() throws IOException {
this.snippet.expectResponseFields("missing-optional-xml-attribute")
this.snippet.expectResponseFields()
.withContents(tableWithHeader("Path", "Type", "Description")
.row("`a`", "`b`", "one").row("`a/@id`", "`c`", "two"));
new ResponseFieldsSnippet(
Arrays.asList(fieldWithPath("a").description("one").type("b"),
fieldWithPath("a/@id").description("two").type("c").optional()))
.document(
operationBuilder("missing-optional-xml-attribute")
.response().content("<a>foo</a>")
this.operationBuilder.response()
.content("<a>foo</a>")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_XML_VALUE)
.build());
@@ -278,12 +275,11 @@ public class ResponseFieldsSnippetTests extends AbstractSnippetTests {
@Test
public void undocumentedAttributeDoesNotCauseFailure() throws IOException {
this.snippet.expectResponseFields("undocumented-attribute").withContents(
this.snippet.expectResponseFields().withContents(
tableWithHeader("Path", "Type", "Description").row("`a`", "`a`", "one"));
new ResponseFieldsSnippet(
Arrays.asList(fieldWithPath("a").description("one").type("a")))
.document(operationBuilder("undocumented-attribute").response()
.content("<a id=\"foo\">bar</a>")
Arrays.asList(fieldWithPath("a").description("one").type("a"))).document(
this.operationBuilder.response().content("<a id=\"foo\">bar</a>")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_XML_VALUE)
.build());
@@ -291,7 +287,7 @@ public class ResponseFieldsSnippetTests extends AbstractSnippetTests {
@Test
public void additionalDescriptors() throws IOException {
this.snippet.expectResponseFields("additional-descriptors")
this.snippet.expectResponseFields()
.withContents(tableWithHeader("Path", "Type", "Description")
.row("`id`", "`Number`", "one").row("`date`", "`String`", "two")
.row("`assets`", "`Array`", "three")
@@ -305,7 +301,7 @@ public class ResponseFieldsSnippetTests extends AbstractSnippetTests {
.and(fieldWithPath("assets[]").description("four"),
fieldWithPath("assets[].id").description("five"),
fieldWithPath("assets[].name").description("six"))
.document(operationBuilder("additional-descriptors").response()
.document(this.operationBuilder.response()
.content("{\"id\": 67,\"date\": \"2015-01-20\",\"assets\":"
+ " [{\"id\":356,\"name\": \"sample\"}]}")
.build());
@@ -313,7 +309,7 @@ public class ResponseFieldsSnippetTests extends AbstractSnippetTests {
@Test
public void prefixedAdditionalDescriptors() throws IOException {
this.snippet.expectResponseFields("prefixed-additional-descriptors")
this.snippet.expectResponseFields()
.withContents(tableWithHeader("Path", "Type", "Description")
.row("`a`", "`Object`", "one").row("`a.b`", "`Number`", "two")
.row("`a.c`", "`String`", "three"));
@@ -321,21 +317,21 @@ public class ResponseFieldsSnippetTests extends AbstractSnippetTests {
PayloadDocumentation.responseFields(fieldWithPath("a").description("one"))
.andWithPrefix("a.", fieldWithPath("b").description("two"),
fieldWithPath("c").description("three"))
.document(operationBuilder("prefixed-additional-descriptors").response()
.document(this.operationBuilder.response()
.content("{\"a\": {\"b\": 5, \"c\": \"charlie\"}}").build());
}
@Test
public void responseWithFieldsWithEscapedContent() throws IOException {
this.snippet.expectResponseFields("response-fields-with-escaped-content")
this.snippet.expectResponseFields()
.withContents(tableWithHeader("Path", "Type", "Description").row(
escapeIfNecessary("`Foo|Bar`"), escapeIfNecessary("`one|two`"),
escapeIfNecessary("three|four")));
new ResponseFieldsSnippet(Arrays.asList(
fieldWithPath("Foo|Bar").type("one|two").description("three|four")))
.document(operationBuilder("response-fields-with-escaped-content")
.response().content("{\"Foo|Bar\": 5}").build());
.document(this.operationBuilder.response()
.content("{\"Foo|Bar\": 5}").build());
}
private String escapeIfNecessary(String input) {

View File

@@ -26,12 +26,12 @@ import org.junit.rules.ExpectedException;
import org.springframework.restdocs.generate.RestDocumentationGenerator;
import org.springframework.restdocs.snippet.SnippetException;
import org.springframework.restdocs.templates.TemplateFormats;
import org.springframework.restdocs.test.ExpectedSnippet;
import org.springframework.restdocs.test.OperationBuilder;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName;
import static org.springframework.restdocs.templates.TemplateFormats.asciidoctor;
/**
* Tests for failures when rendering {@link PathParametersSnippet} due to missing or
@@ -42,7 +42,10 @@ import static org.springframework.restdocs.request.RequestDocumentation.paramete
public class PathParametersSnippetFailureTests {
@Rule
public ExpectedSnippet snippet = new ExpectedSnippet(TemplateFormats.asciidoctor());
public OperationBuilder operationBuilder = new OperationBuilder(asciidoctor());
@Rule
public ExpectedSnippet snippet = new ExpectedSnippet(asciidoctor());
@Rule
public ExpectedException thrown = ExpectedException.none();
@@ -53,12 +56,10 @@ public class PathParametersSnippetFailureTests {
this.thrown.expectMessage(equalTo("Path parameters with the following names were"
+ " not documented: [a]"));
new PathParametersSnippet(Collections.<ParameterDescriptor>emptyList())
.document(new OperationBuilder("undocumented-path-parameter",
this.snippet.getOutputDirectory())
.attribute(
RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE,
"/{a}/")
.build());
.document(this.operationBuilder
.attribute(RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE,
"/{a}/")
.build());
}
@Test
@@ -68,12 +69,9 @@ public class PathParametersSnippetFailureTests {
+ " not found in the request: [a]"));
new PathParametersSnippet(
Arrays.asList(parameterWithName("a").description("one")))
.document(new OperationBuilder("missing-path-parameter",
this.snippet.getOutputDirectory())
.attribute(
RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE,
"/")
.build());
.document(this.operationBuilder.attribute(
RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE,
"/").build());
}
@Test
@@ -83,13 +81,10 @@ public class PathParametersSnippetFailureTests {
+ " not documented: [b]. Path parameters with the following"
+ " names were not found in the request: [a]"));
new PathParametersSnippet(
Arrays.asList(parameterWithName("a").description("one"))).document(
new OperationBuilder("undocumented-and-missing-path-parameters",
this.snippet.getOutputDirectory())
.attribute(
RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE,
"/{b}")
.build());
Arrays.asList(parameterWithName("a").description("one")))
.document(this.operationBuilder.attribute(
RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE,
"/{b}").build());
}
}

View File

@@ -49,102 +49,94 @@ public class PathParametersSnippetTests extends AbstractSnippetTests {
@Test
public void pathParameters() throws IOException {
this.snippet.expectPathParameters("path-parameters").withContents(
this.snippet.expectPathParameters().withContents(
tableWithTitleAndHeader(getTitle(), "Parameter", "Description")
.row("`a`", "one").row("`b`", "two"));
new PathParametersSnippet(Arrays.asList(parameterWithName("a").description("one"),
parameterWithName("b").description("two")))
.document(operationBuilder("path-parameters").attribute(
RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE,
"/{a}/{b}").build());
.document(this.operationBuilder
.attribute(
RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE,
"/{a}/{b}")
.build());
}
@Test
public void ignoredPathParameter() throws IOException {
this.snippet.expectPathParameters("ignored-path-parameter").withContents(
this.snippet.expectPathParameters().withContents(
tableWithTitleAndHeader(getTitle(), "Parameter", "Description").row("`b`",
"two"));
new PathParametersSnippet(Arrays.asList(parameterWithName("a").ignored(),
parameterWithName("b").description("two")))
.document(operationBuilder("ignored-path-parameter").attribute(
RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE,
"/{a}/{b}").build());
.document(this.operationBuilder
.attribute(
RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE,
"/{a}/{b}")
.build());
}
@Test
public void allUndocumentedPathParametersCanBeIgnored() throws IOException {
this.snippet.expectPathParameters("ignore-all-undocumented").withContents(
this.snippet.expectPathParameters().withContents(
tableWithTitleAndHeader(getTitle(), "Parameter", "Description").row("`b`",
"two"));
new PathParametersSnippet(
Arrays.asList(parameterWithName("b").description("two")), true)
.document(operationBuilder("ignore-all-undocumented").attribute(
.document(this.operationBuilder.attribute(
RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE,
"/{a}/{b}").build());
}
@Test
public void missingOptionalPathParameter() throws IOException {
this.snippet
.expectPathParameters(
"missing-optional-path-parameter")
.withContents(tableWithTitleAndHeader(
this.templateFormat == TemplateFormats.asciidoctor() ? "/{a}"
: "`/{a}`",
"Parameter", "Description").row("`a`", "one").row("`b`", "two"));
this.snippet.expectPathParameters().withContents(tableWithTitleAndHeader(
this.templateFormat == TemplateFormats.asciidoctor() ? "/{a}" : "`/{a}`",
"Parameter", "Description").row("`a`", "one").row("`b`", "two"));
new PathParametersSnippet(Arrays.asList(parameterWithName("a").description("one"),
parameterWithName("b").description("two").optional())).document(
operationBuilder("missing-optional-path-parameter").attribute(
parameterWithName("b").description("two").optional()))
.document(this.operationBuilder.attribute(
RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE,
"/{a}").build());
}
@Test
public void presentOptionalPathParameter() throws IOException {
this.snippet
.expectPathParameters(
"present-optional-path-parameter")
.withContents(tableWithTitleAndHeader(
this.templateFormat == TemplateFormats.asciidoctor() ? "/{a}"
: "`/{a}`",
"Parameter", "Description").row("`a`", "one"));
this.snippet.expectPathParameters().withContents(tableWithTitleAndHeader(
this.templateFormat == TemplateFormats.asciidoctor() ? "/{a}" : "`/{a}`",
"Parameter", "Description").row("`a`", "one"));
new PathParametersSnippet(
Arrays.asList(parameterWithName("a").description("one").optional()))
.document(operationBuilder("present-optional-path-parameter")
.attribute(
RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE,
"/{a}")
.build());
.document(this.operationBuilder.attribute(
RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE,
"/{a}").build());
}
@Test
public void pathParametersWithQueryString() throws IOException {
this.snippet.expectPathParameters("path-parameters-with-query-string")
.withContents(
tableWithTitleAndHeader(getTitle(), "Parameter", "Description")
.row("`a`", "one").row("`b`", "two"));
this.snippet.expectPathParameters().withContents(
tableWithTitleAndHeader(getTitle(), "Parameter", "Description")
.row("`a`", "one").row("`b`", "two"));
new PathParametersSnippet(Arrays.asList(parameterWithName("a").description("one"),
parameterWithName("b").description("two"))).document(
operationBuilder("path-parameters-with-query-string").attribute(
RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE,
"/{a}/{b}?foo=bar").build());
parameterWithName("b").description("two")))
.document(this.operationBuilder
.attribute(
RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE,
"/{a}/{b}?foo=bar")
.build());
}
@Test
public void pathParametersWithQueryStringWithParameters() throws IOException {
this.snippet
.expectPathParameters("path-parameters-with-query-string-with-parameters")
.withContents(
tableWithTitleAndHeader(getTitle(), "Parameter", "Description")
.row("`a`", "one").row("`b`", "two"));
this.snippet.expectPathParameters().withContents(
tableWithTitleAndHeader(getTitle(), "Parameter", "Description")
.row("`a`", "one").row("`b`", "two"));
new PathParametersSnippet(Arrays.asList(parameterWithName("a").description("one"),
parameterWithName("b").description("two")))
.document(operationBuilder(
"path-parameters-with-query-string-with-parameters")
.attribute(
RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE,
"/{a}/{b}?foo={c}")
.build());
.document(this.operationBuilder
.attribute(
RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE,
"/{a}/{b}?foo={c}")
.build());
}
@Test
@@ -152,8 +144,7 @@ public class PathParametersSnippetTests extends AbstractSnippetTests {
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
given(resolver.resolveTemplateResource("path-parameters"))
.willReturn(snippetResource("path-parameters-with-title"));
this.snippet.expectPathParameters("path-parameters-with-custom-attributes")
.withContents(containsString("The title"));
this.snippet.expectPathParameters().withContents(containsString("The title"));
new PathParametersSnippet(
Arrays.asList(
@@ -161,8 +152,8 @@ public class PathParametersSnippetTests extends AbstractSnippetTests {
.attributes(key("foo").value("alpha")),
parameterWithName("b").description("two")
.attributes(key("foo").value("bravo"))),
attributes(key("title").value("The title"))).document(
operationBuilder("path-parameters-with-custom-attributes")
attributes(key("title").value("The title")))
.document(this.operationBuilder
.attribute(
RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE,
"/{a}/{b}")
@@ -177,17 +168,16 @@ public class PathParametersSnippetTests extends AbstractSnippetTests {
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
given(resolver.resolveTemplateResource("path-parameters"))
.willReturn(snippetResource("path-parameters-with-extra-column"));
this.snippet
.expectPathParameters("path-parameters-with-custom-descriptor-attributes")
this.snippet.expectPathParameters()
.withContents(tableWithHeader("Parameter", "Description", "Foo")
.row("a", "one", "alpha").row("b", "two", "bravo"));
new PathParametersSnippet(Arrays.asList(
parameterWithName("a").description("one")
.attributes(key("foo").value("alpha")),
parameterWithName("b").description("two").attributes(
key("foo").value("bravo")))).document(operationBuilder(
"path-parameters-with-custom-descriptor-attributes")
parameterWithName("b").description("two")
.attributes(key("foo").value("bravo"))))
.document(this.operationBuilder
.attribute(
RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE,
"/{a}/{b}")
@@ -198,12 +188,12 @@ public class PathParametersSnippetTests extends AbstractSnippetTests {
@Test
public void additionalDescriptors() throws IOException {
this.snippet.expectPathParameters("additional-descriptors").withContents(
this.snippet.expectPathParameters().withContents(
tableWithTitleAndHeader(getTitle(), "Parameter", "Description")
.row("`a`", "one").row("`b`", "two"));
RequestDocumentation.pathParameters(parameterWithName("a").description("one"))
.and(parameterWithName("b").description("two"))
.document(operationBuilder("additional-descriptors")
.document(this.operationBuilder
.attribute(RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE,
"/{a}/{b}")
.build());
@@ -211,14 +201,14 @@ public class PathParametersSnippetTests extends AbstractSnippetTests {
@Test
public void pathParametersWithEscapedContent() throws IOException {
this.snippet.expectPathParameters("path-parameters-with-escaped-content")
this.snippet.expectPathParameters()
.withContents(tableWithTitleAndHeader(getTitle("{Foo|Bar}"), "Parameter",
"Description").row(escapeIfNecessary("`Foo|Bar`"),
escapeIfNecessary("one|two")));
RequestDocumentation
.pathParameters(parameterWithName("Foo|Bar").description("one|two"))
.document(operationBuilder("path-parameters-with-escaped-content")
.document(this.operationBuilder
.attribute(RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE,
"{Foo|Bar}")
.build());

View File

@@ -25,12 +25,12 @@ import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.restdocs.snippet.SnippetException;
import org.springframework.restdocs.templates.TemplateFormats;
import org.springframework.restdocs.test.ExpectedSnippet;
import org.springframework.restdocs.test.OperationBuilder;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName;
import static org.springframework.restdocs.templates.TemplateFormats.asciidoctor;
/**
* Tests for failures when rendering {@link RequestParametersSnippet} due to missing or
@@ -41,7 +41,10 @@ import static org.springframework.restdocs.request.RequestDocumentation.paramete
public class RequestParametersSnippetFailureTests {
@Rule
public ExpectedSnippet snippet = new ExpectedSnippet(TemplateFormats.asciidoctor());
public OperationBuilder operationBuilder = new OperationBuilder(asciidoctor());
@Rule
public ExpectedSnippet snippet = new ExpectedSnippet(asciidoctor());
@Rule
public ExpectedException thrown = ExpectedException.none();
@@ -53,9 +56,8 @@ public class RequestParametersSnippetFailureTests {
.expectMessage(equalTo("Request parameters with the following names were"
+ " not documented: [a]"));
new RequestParametersSnippet(Collections.<ParameterDescriptor>emptyList())
.document(new OperationBuilder("undocumented-parameter",
this.snippet.getOutputDirectory()).request("http://localhost")
.param("a", "alpha").build());
.document(this.operationBuilder.request("http://localhost")
.param("a", "alpha").build());
}
@Test
@@ -65,10 +67,8 @@ public class RequestParametersSnippetFailureTests {
.expectMessage(equalTo("Request parameters with the following names were"
+ " not found in the request: [a]"));
new RequestParametersSnippet(
Arrays.asList(parameterWithName("a").description("one")))
.document(new OperationBuilder("missing-parameter",
this.snippet.getOutputDirectory())
.request("http://localhost").build());
Arrays.asList(parameterWithName("a").description("one"))).document(
this.operationBuilder.request("http://localhost").build());
}
@Test
@@ -79,11 +79,9 @@ public class RequestParametersSnippetFailureTests {
+ " not documented: [b]. Request parameters with the following"
+ " names were not found in the request: [a]"));
new RequestParametersSnippet(
Arrays.asList(parameterWithName("a").description("one"))).document(
new OperationBuilder("undocumented-and-missing-parameters",
this.snippet.getOutputDirectory())
.request("http://localhost").param("b", "bravo")
.build());
Arrays.asList(parameterWithName("a").description("one")))
.document(this.operationBuilder.request("http://localhost")
.param("b", "bravo").build());
}
}

View File

@@ -48,72 +48,66 @@ public class RequestParametersSnippetTests extends AbstractSnippetTests {
@Test
public void requestParameters() throws IOException {
this.snippet.expectRequestParameters("request-parameters")
this.snippet.expectRequestParameters()
.withContents(tableWithHeader("Parameter", "Description")
.row("`a`", "one").row("`b`", "two"));
new RequestParametersSnippet(
Arrays.asList(parameterWithName("a").description("one"),
parameterWithName("b").description("two")))
.document(operationBuilder("request-parameters")
.request("http://localhost").param("a", "bravo")
.param("b", "bravo").build());
parameterWithName("b").description("two"))).document(
this.operationBuilder.request("http://localhost")
.param("a", "bravo").param("b", "bravo").build());
}
@Test
public void requestParameterWithNoValue() throws IOException {
this.snippet.expectRequestParameters("request-parameter-with-no-value")
.withContents(
tableWithHeader("Parameter", "Description").row("`a`", "one"));
this.snippet.expectRequestParameters().withContents(
tableWithHeader("Parameter", "Description").row("`a`", "one"));
new RequestParametersSnippet(
Arrays.asList(parameterWithName("a").description("one")))
.document(operationBuilder("request-parameter-with-no-value")
.request("http://localhost").param("a").build());
.document(this.operationBuilder.request("http://localhost")
.param("a").build());
}
@Test
public void ignoredRequestParameter() throws IOException {
this.snippet.expectRequestParameters("ignored-request-parameter").withContents(
this.snippet.expectRequestParameters().withContents(
tableWithHeader("Parameter", "Description").row("`b`", "two"));
new RequestParametersSnippet(Arrays.asList(parameterWithName("a").ignored(),
parameterWithName("b").description("two")))
.document(operationBuilder("ignored-request-parameter")
.request("http://localhost").param("a", "bravo")
.param("b", "bravo").build());
.document(this.operationBuilder.request("http://localhost")
.param("a", "bravo").param("b", "bravo").build());
}
@Test
public void allUndocumentedRequestParametersCanBeIgnored() throws IOException {
this.snippet.expectRequestParameters("ignore-all-undocumented").withContents(
this.snippet.expectRequestParameters().withContents(
tableWithHeader("Parameter", "Description").row("`b`", "two"));
new RequestParametersSnippet(
Arrays.asList(parameterWithName("b").description("two")), true)
.document(operationBuilder("ignore-all-undocumented")
.request("http://localhost").param("a", "bravo")
.param("b", "bravo").build());
.document(this.operationBuilder.request("http://localhost")
.param("a", "bravo").param("b", "bravo").build());
}
@Test
public void missingOptionalRequestParameter() throws IOException {
this.snippet.expectRequestParameters("missing-optional-request-parameter")
this.snippet.expectRequestParameters()
.withContents(tableWithHeader("Parameter", "Description")
.row("`a`", "one").row("`b`", "two"));
new RequestParametersSnippet(
Arrays.asList(parameterWithName("a").description("one").optional(),
parameterWithName("b").description("two"))).document(
operationBuilder("missing-optional-request-parameter")
.request("http://localhost").param("b", "bravo")
.build());
this.operationBuilder.request("http://localhost")
.param("b", "bravo").build());
}
@Test
public void presentOptionalRequestParameter() throws IOException {
this.snippet.expectRequestParameters("present-optional-request-parameter")
.withContents(
tableWithHeader("Parameter", "Description").row("`a`", "one"));
this.snippet.expectRequestParameters().withContents(
tableWithHeader("Parameter", "Description").row("`a`", "one"));
new RequestParametersSnippet(
Arrays.asList(parameterWithName("a").description("one").optional()))
.document(operationBuilder("present-optional-request-parameter")
.request("http://localhost").param("a", "one").build());
.document(this.operationBuilder.request("http://localhost")
.param("a", "one").build());
}
@Test
@@ -121,8 +115,7 @@ public class RequestParametersSnippetTests extends AbstractSnippetTests {
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
given(resolver.resolveTemplateResource("request-parameters"))
.willReturn(snippetResource("request-parameters-with-title"));
this.snippet.expectRequestParameters("request-parameters-with-custom-attributes")
.withContents(containsString("The title"));
this.snippet.expectRequestParameters().withContents(containsString("The title"));
new RequestParametersSnippet(
Arrays.asList(
@@ -130,12 +123,16 @@ public class RequestParametersSnippetTests extends AbstractSnippetTests {
.attributes(key("foo").value("alpha")),
parameterWithName("b").description("two")
.attributes(key("foo").value("bravo"))),
attributes(key("title").value("The title"))).document(
operationBuilder("request-parameters-with-custom-attributes")
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(resolver))
.request("http://localhost").param("a", "alpha")
.param("b", "bravo").build());
attributes(
key("title").value("The title")))
.document(
this.operationBuilder
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(
resolver))
.request("http://localhost")
.param("a", "alpha").param("b", "bravo")
.build());
}
@Test
@@ -143,9 +140,7 @@ public class RequestParametersSnippetTests extends AbstractSnippetTests {
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
given(resolver.resolveTemplateResource("request-parameters"))
.willReturn(snippetResource("request-parameters-with-extra-column"));
this.snippet
.expectRequestParameters(
"request-parameters-with-custom-descriptor-attributes")
this.snippet.expectRequestParameters()
.withContents(tableWithHeader("Parameter", "Description", "Foo")
.row("a", "one", "alpha").row("b", "two", "bravo"));
@@ -154,8 +149,8 @@ public class RequestParametersSnippetTests extends AbstractSnippetTests {
.attributes(key("foo").value("alpha")),
parameterWithName("b").description("two")
.attributes(key("foo").value("bravo"))))
.document(operationBuilder(
"request-parameters-with-custom-descriptor-attributes")
.document(
this.operationBuilder
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(
resolver))
@@ -169,42 +164,44 @@ public class RequestParametersSnippetTests extends AbstractSnippetTests {
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
given(resolver.resolveTemplateResource("request-parameters"))
.willReturn(snippetResource("request-parameters-with-optional-column"));
this.snippet.expectRequestParameters("request-parameters-with-optional-column")
this.snippet.expectRequestParameters()
.withContents(tableWithHeader("Parameter", "Optional", "Description")
.row("a", "true", "one").row("b", "false", "two"));
new RequestParametersSnippet(Arrays.asList(
parameterWithName("a").description("one").optional(),
parameterWithName("b").description("two"))).document(
operationBuilder("request-parameters-with-optional-column")
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(resolver))
.request("http://localhost").param("a", "alpha")
.param("b", "bravo").build());
new RequestParametersSnippet(
Arrays.asList(parameterWithName("a").description("one").optional(),
parameterWithName("b").description("two")))
.document(
this.operationBuilder
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(
resolver))
.request("http://localhost")
.param("a", "alpha").param("b", "bravo")
.build());
}
@Test
public void additionalDescriptors() throws IOException {
this.snippet.expectRequestParameters("additional-descriptors")
this.snippet.expectRequestParameters()
.withContents(tableWithHeader("Parameter", "Description")
.row("`a`", "one").row("`b`", "two"));
RequestDocumentation.requestParameters(parameterWithName("a").description("one"))
.and(parameterWithName("b").description("two"))
.document(operationBuilder("additional-descriptors")
.request("http://localhost").param("a", "bravo")
.param("b", "bravo").build());
.document(this.operationBuilder.request("http://localhost")
.param("a", "bravo").param("b", "bravo").build());
}
@Test
public void requestParametersWithEscapedContent() throws IOException {
this.snippet.expectRequestParameters("request-parameters-with-escaped-content")
this.snippet.expectRequestParameters()
.withContents(tableWithHeader("Parameter", "Description").row(
escapeIfNecessary("`Foo|Bar`"), escapeIfNecessary("one|two")));
RequestDocumentation
.requestParameters(parameterWithName("Foo|Bar").description("one|two"))
.document(operationBuilder("request-parameters-with-escaped-content")
.request("http://localhost").param("Foo|Bar", "baz").build());
.document(this.operationBuilder.request("http://localhost")
.param("Foo|Bar", "baz").build());
}
private String escapeIfNecessary(String input) {

View File

@@ -25,12 +25,12 @@ import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.restdocs.snippet.SnippetException;
import org.springframework.restdocs.templates.TemplateFormats;
import org.springframework.restdocs.test.ExpectedSnippet;
import org.springframework.restdocs.test.OperationBuilder;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.springframework.restdocs.request.RequestDocumentation.partWithName;
import static org.springframework.restdocs.templates.TemplateFormats.asciidoctor;
/**
* Tests for failures when rendering {@link RequestPartsSnippet} due to missing or
@@ -41,7 +41,10 @@ import static org.springframework.restdocs.request.RequestDocumentation.partWith
public class RequestPartsSnippetFailureTests {
@Rule
public ExpectedSnippet snippet = new ExpectedSnippet(TemplateFormats.asciidoctor());
public OperationBuilder operationBuilder = new OperationBuilder(asciidoctor());
@Rule
public ExpectedSnippet snippet = new ExpectedSnippet(asciidoctor());
@Rule
public ExpectedException thrown = ExpectedException.none();
@@ -52,9 +55,8 @@ public class RequestPartsSnippetFailureTests {
this.thrown.expectMessage(equalTo(
"Request parts with the following names were" + " not documented: [a]"));
new RequestPartsSnippet(Collections.<RequestPartDescriptor>emptyList())
.document(new OperationBuilder("undocumented-part",
this.snippet.getOutputDirectory()).request("http://localhost")
.part("a", "alpha".getBytes()).build());
.document(this.operationBuilder.request("http://localhost")
.part("a", "alpha".getBytes()).build());
}
@Test
@@ -63,9 +65,7 @@ public class RequestPartsSnippetFailureTests {
this.thrown.expectMessage(equalTo("Request parts with the following names were"
+ " not found in the request: [a]"));
new RequestPartsSnippet(Arrays.asList(partWithName("a").description("one")))
.document(new OperationBuilder("missing-part",
this.snippet.getOutputDirectory()).request("http://localhost")
.build());
.document(this.operationBuilder.request("http://localhost").build());
}
@Test
@@ -75,9 +75,8 @@ public class RequestPartsSnippetFailureTests {
+ " not documented: [b]. Request parts with the following"
+ " names were not found in the request: [a]"));
new RequestPartsSnippet(Arrays.asList(partWithName("a").description("one")))
.document(new OperationBuilder("undocumented-and-missing-parts",
this.snippet.getOutputDirectory()).request("http://localhost")
.part("b", "bravo".getBytes()).build());
.document(this.operationBuilder.request("http://localhost")
.part("b", "bravo".getBytes()).build());
}
}

View File

@@ -48,59 +48,57 @@ public class RequestPartsSnippetTests extends AbstractSnippetTests {
@Test
public void requestParts() throws IOException {
this.snippet.expectRequestParts("request-parts")
this.snippet.expectRequestParts()
.withContents(tableWithHeader("Part", "Description").row("`a`", "one")
.row("`b`", "two"));
new RequestPartsSnippet(Arrays.asList(partWithName("a").description("one"),
partWithName("b").description("two")))
.document(operationBuilder("request-parts")
.request("http://localhost").part("a", "bravo".getBytes())
.and().part("b", "bravo".getBytes()).build());
.document(this.operationBuilder.request("http://localhost")
.part("a", "bravo".getBytes()).and()
.part("b", "bravo".getBytes()).build());
}
@Test
public void ignoredRequestPart() throws IOException {
this.snippet.expectRequestParts("ignored-request-part")
this.snippet.expectRequestParts()
.withContents(tableWithHeader("Part", "Description").row("`b`", "two"));
new RequestPartsSnippet(Arrays.asList(partWithName("a").ignored(),
partWithName("b").description("two")))
.document(operationBuilder("ignored-request-part")
.request("http://localhost").part("a", "bravo".getBytes())
.and().part("b", "bravo".getBytes()).build());
.document(this.operationBuilder.request("http://localhost")
.part("a", "bravo".getBytes()).and()
.part("b", "bravo".getBytes()).build());
}
@Test
public void allUndocumentedRequestPartsCanBeIgnored() throws IOException {
this.snippet.expectRequestParts("ignore-all-undocumented")
this.snippet.expectRequestParts()
.withContents(tableWithHeader("Part", "Description").row("`b`", "two"));
new RequestPartsSnippet(Arrays.asList(partWithName("b").description("two")), true)
.document(operationBuilder("ignore-all-undocumented")
.request("http://localhost").part("a", "bravo".getBytes()).and()
.part("b", "bravo".getBytes()).build());
.document(this.operationBuilder.request("http://localhost")
.part("a", "bravo".getBytes()).and().part("b", "bravo".getBytes())
.build());
}
@Test
public void missingOptionalRequestPart() throws IOException {
this.snippet.expectRequestParts("missing-optional-request-parts")
this.snippet.expectRequestParts()
.withContents(tableWithHeader("Part", "Description").row("`a`", "one")
.row("`b`", "two"));
new RequestPartsSnippet(
Arrays.asList(partWithName("a").description("one").optional(),
partWithName("b").description("two"))).document(
operationBuilder("missing-optional-request-parts")
.request("http://localhost")
this.operationBuilder.request("http://localhost")
.part("b", "bravo".getBytes()).build());
}
@Test
public void presentOptionalRequestPart() throws IOException {
this.snippet.expectRequestParts("present-optional-request-part")
this.snippet.expectRequestParts()
.withContents(tableWithHeader("Part", "Description").row("`a`", "one"));
new RequestPartsSnippet(
Arrays.asList(partWithName("a").description("one").optional()))
.document(operationBuilder("present-optional-request-part")
.request("http://localhost").part("a", "one".getBytes())
.build());
.document(this.operationBuilder.request("http://localhost")
.part("a", "one".getBytes()).build());
}
@Test
@@ -108,8 +106,7 @@ public class RequestPartsSnippetTests extends AbstractSnippetTests {
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
given(resolver.resolveTemplateResource("request-parts"))
.willReturn(snippetResource("request-parts-with-title"));
this.snippet.expectRequestParts("request-parts-with-custom-attributes")
.withContents(containsString("The title"));
this.snippet.expectRequestParts().withContents(containsString("The title"));
new RequestPartsSnippet(
Arrays.asList(
@@ -117,12 +114,16 @@ public class RequestPartsSnippetTests extends AbstractSnippetTests {
.attributes(key("foo").value("alpha")),
partWithName("b").description("two")
.attributes(key("foo").value("bravo"))),
attributes(key("title").value("The title")))
.document(operationBuilder("request-parts-with-custom-attributes")
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(resolver))
.request("http://localhost").part("a", "alpha".getBytes())
.and().part("b", "bravo".getBytes()).build());
attributes(
key("title").value("The title")))
.document(
this.operationBuilder
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(
resolver))
.request("http://localhost")
.part("a", "alpha".getBytes()).and()
.part("b", "bravo".getBytes()).build());
}
@Test
@@ -130,7 +131,7 @@ public class RequestPartsSnippetTests extends AbstractSnippetTests {
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
given(resolver.resolveTemplateResource("request-parts"))
.willReturn(snippetResource("request-parts-with-extra-column"));
this.snippet.expectRequestParts("request-parts-with-custom-descriptor-attributes")
this.snippet.expectRequestParts()
.withContents(tableWithHeader("Part", "Description", "Foo")
.row("a", "one", "alpha").row("b", "two", "bravo"));
@@ -139,8 +140,8 @@ public class RequestPartsSnippetTests extends AbstractSnippetTests {
.attributes(key("foo").value("alpha")),
partWithName("b").description("two")
.attributes(key("foo").value("bravo"))))
.document(operationBuilder(
"request-parts-with-custom-descriptor-attributes")
.document(
this.operationBuilder
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(
resolver))
@@ -154,43 +155,44 @@ public class RequestPartsSnippetTests extends AbstractSnippetTests {
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
given(resolver.resolveTemplateResource("request-parts"))
.willReturn(snippetResource("request-parts-with-optional-column"));
this.snippet.expectRequestParts("request-parts-with-optional-column")
this.snippet.expectRequestParts()
.withContents(tableWithHeader("Part", "Optional", "Description")
.row("a", "true", "one").row("b", "false", "two"));
new RequestPartsSnippet(
Arrays.asList(partWithName("a").description("one").optional(),
partWithName("b").description("two"))).document(
operationBuilder("request-parts-with-optional-column")
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(resolver))
.request("http://localhost")
.part("a", "alpha".getBytes()).and()
.part("b", "bravo".getBytes()).build());
partWithName("b").description("two")))
.document(
this.operationBuilder
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(
resolver))
.request("http://localhost")
.part("a", "alpha".getBytes()).and()
.part("b", "bravo".getBytes()).build());
}
@Test
public void additionalDescriptors() throws IOException {
this.snippet.expectRequestParts("additional-descriptors")
this.snippet.expectRequestParts()
.withContents(tableWithHeader("Part", "Description").row("`a`", "one")
.row("`b`", "two"));
RequestDocumentation.requestParts(partWithName("a").description("one"))
.and(partWithName("b").description("two"))
.document(operationBuilder("additional-descriptors")
.request("http://localhost").part("a", "bravo".getBytes()).and()
.part("b", "bravo".getBytes()).build());
.document(this.operationBuilder.request("http://localhost")
.part("a", "bravo".getBytes()).and().part("b", "bravo".getBytes())
.build());
}
@Test
public void requestPartsWithEscapedContent() throws IOException {
this.snippet.expectRequestParts("request-parts-with-escaped-content")
.withContents(tableWithHeader("Part", "Description").row(
escapeIfNecessary("`Foo|Bar`"), escapeIfNecessary("one|two")));
this.snippet.expectRequestParts().withContents(
tableWithHeader("Part", "Description").row(escapeIfNecessary("`Foo|Bar`"),
escapeIfNecessary("one|two")));
RequestDocumentation.requestParts(partWithName("Foo|Bar").description("one|two"))
.document(operationBuilder("request-parts-with-escaped-content")
.request("http://localhost").part("Foo|Bar", "baz".getBytes())
.build());
.document(this.operationBuilder.request("http://localhost")
.part("Foo|Bar", "baz".getBytes()).build());
}
private String escapeIfNecessary(String input) {

View File

@@ -71,68 +71,67 @@ public class ExpectedSnippet implements TestRule {
}
}
public ExpectedSnippet expectCurlRequest(String name) {
expect(name, "curl-request");
public ExpectedSnippet expectCurlRequest() {
expect("curl-request");
return this;
}
public ExpectedSnippet expectHttpieRequest(String name) {
expect(name, "httpie-request");
public ExpectedSnippet expectHttpieRequest() {
expect("httpie-request");
return this;
}
public ExpectedSnippet expectRequestFields(String name) {
expect(name, "request-fields");
public ExpectedSnippet expectRequestFields() {
expect("request-fields");
return this;
}
public ExpectedSnippet expectResponseFields(String name) {
expect(name, "response-fields");
public ExpectedSnippet expectResponseFields() {
expect("response-fields");
return this;
}
public ExpectedSnippet expectRequestHeaders(String name) {
expect(name, "request-headers");
public ExpectedSnippet expectRequestHeaders() {
expect("request-headers");
return this;
}
public ExpectedSnippet expectResponseHeaders(String name) {
expect(name, "response-headers");
public ExpectedSnippet expectResponseHeaders() {
expect("response-headers");
return this;
}
public ExpectedSnippet expectLinks(String name) {
expect(name, "links");
public ExpectedSnippet expectLinks() {
expect("links");
return this;
}
public ExpectedSnippet expectHttpRequest(String name) {
expect(name, "http-request");
public ExpectedSnippet expectHttpRequest() {
expect("http-request");
return this;
}
public ExpectedSnippet expectHttpResponse(String name) {
expect(name, "http-response");
public ExpectedSnippet expectHttpResponse() {
expect("http-response");
return this;
}
public ExpectedSnippet expectRequestParameters(String name) {
expect(name, "request-parameters");
public ExpectedSnippet expectRequestParameters() {
expect("request-parameters");
return this;
}
public ExpectedSnippet expectPathParameters(String name) {
expect(name, "path-parameters");
public ExpectedSnippet expectPathParameters() {
expect("path-parameters");
return this;
}
public ExpectedSnippet expectRequestParts(String name) {
expect(name, "request-parts");
public ExpectedSnippet expectRequestParts() {
expect("request-parts");
return this;
}
private ExpectedSnippet expect(String name, String type) {
this.expectedName = name;
private ExpectedSnippet expect(String type) {
this.expectedType = type;
return this;
}

View File

@@ -24,6 +24,10 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
@@ -45,7 +49,6 @@ import org.springframework.restdocs.snippet.WriterResolver;
import org.springframework.restdocs.templates.StandardTemplateResourceResolver;
import org.springframework.restdocs.templates.TemplateEngine;
import org.springframework.restdocs.templates.TemplateFormat;
import org.springframework.restdocs.templates.TemplateFormats;
import org.springframework.restdocs.templates.mustache.AsciidoctorTableCellContentLambda;
import org.springframework.restdocs.templates.mustache.MustacheTemplateEngine;
@@ -54,28 +57,21 @@ import org.springframework.restdocs.templates.mustache.MustacheTemplateEngine;
*
* @author Andy Wilkinson
*/
public class OperationBuilder {
public class OperationBuilder implements TestRule {
private final Map<String, Object> attributes = new HashMap<>();
private final OperationResponseBuilder responseBuilder = new OperationResponseBuilder();
private OperationResponseBuilder responseBuilder;
private final String name;
private String name;
private final File outputDirectory;
private File outputDirectory;
private final TemplateFormat templateFormat;
private OperationRequestBuilder requestBuilder;
public OperationBuilder(String name, File outputDirectory) {
this(name, outputDirectory, TemplateFormats.asciidoctor());
}
public OperationBuilder(String name, File outputDirectory,
TemplateFormat templateFormat) {
this.name = name;
this.outputDirectory = outputDirectory;
public OperationBuilder(TemplateFormat templateFormat) {
this.templateFormat = templateFormat;
}
@@ -85,6 +81,7 @@ public class OperationBuilder {
}
public OperationResponseBuilder response() {
this.responseBuilder = new OperationResponseBuilder();
return this.responseBuilder;
}
@@ -93,6 +90,14 @@ public class OperationBuilder {
return this;
}
private void prepare(String operationName, File outputDirectory) {
this.name = operationName;
this.outputDirectory = outputDirectory;
this.requestBuilder = null;
this.requestBuilder = null;
this.attributes.clear();
}
public Operation build() {
if (this.attributes.get(TemplateEngine.class.getName()) == null) {
Map<String, Object> templateContext = new HashMap<>();
@@ -113,7 +118,10 @@ public class OperationBuilder {
(this.requestBuilder == null
? new OperationRequestBuilder("http://localhost/").buildRequest()
: this.requestBuilder.buildRequest()),
this.responseBuilder.buildResponse(), this.attributes);
this.responseBuilder == null
? new OperationResponseBuilder().buildResponse()
: this.responseBuilder.buildResponse(),
this.attributes);
}
private RestDocumentationContext createContext() {
@@ -124,6 +132,25 @@ public class OperationBuilder {
return context;
}
@Override
public Statement apply(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
String operationName = description.getMethodName();
int index = operationName.indexOf('[');
if (index > 0) {
operationName = operationName.substring(0, index);
}
OperationBuilder.this.prepare(operationName,
new File("build/" + description.getTestClass().getSimpleName()));
base.evaluate();
}
};
}
/**
* Basic builder API for creating an {@link OperationRequest}.
*/