Allow a TemplatedSnippet to produce multiple snippets from same template
Previously a TemplatedSnippet could only write snippets with the same name as its template. This meant that the same template could not be used to produce multiple snippets with different names. This commit separates the snippet name from the template name, thereby allowing the snippet name to vary while the template name remains the same. For backwards compatibility, the default behaviour is for the template name and snippet name to remain the same. The new overloaded constructor can be used when the names need to differ. Closes gh-320
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2015 the original author or authors.
|
||||
* Copyright 2014-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -38,15 +38,34 @@ public abstract class TemplatedSnippet implements Snippet {
|
||||
|
||||
private final String snippetName;
|
||||
|
||||
private final String templateName;
|
||||
|
||||
/**
|
||||
* Creates a new {@code TemplatedSnippet} that will produce a snippet with the given
|
||||
* {@code snippetName}. The given {@code attributes} will be included in the model
|
||||
* during rendering of the template.
|
||||
* {@code snippetName}. The {@code snippetName} will also be used as the name of the
|
||||
* template. The given {@code attributes} will be included in the model during
|
||||
* rendering of the template.
|
||||
*
|
||||
* @param snippetName The name of the snippet
|
||||
* @param attributes The additional attributes
|
||||
* @see #TemplatedSnippet(String, String, Map)
|
||||
*/
|
||||
protected TemplatedSnippet(String snippetName, Map<String, Object> attributes) {
|
||||
this(snippetName, snippetName, attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@code TemplatedSnippet} that will produce a snippet with the given
|
||||
* {@code snippetName} using a template with the given {@code templateName}. The given
|
||||
* {@code attributes} will be included in the model during rendering of the template.
|
||||
*
|
||||
* @param snippetName The name of the snippet
|
||||
* @param templateName The name of the template
|
||||
* @param attributes The additional attributes
|
||||
*/
|
||||
protected TemplatedSnippet(String snippetName, String templateName,
|
||||
Map<String, Object> attributes) {
|
||||
this.templateName = templateName;
|
||||
this.snippetName = snippetName;
|
||||
if (attributes != null) {
|
||||
this.attributes.putAll(attributes);
|
||||
@@ -65,7 +84,8 @@ public abstract class TemplatedSnippet implements Snippet {
|
||||
model.putAll(this.attributes);
|
||||
TemplateEngine templateEngine = (TemplateEngine) operation.getAttributes()
|
||||
.get(TemplateEngine.class.getName());
|
||||
writer.append(templateEngine.compileTemplate(this.snippetName).render(model));
|
||||
writer.append(
|
||||
templateEngine.compileTemplate(this.templateName).render(model));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ import org.junit.runners.Parameterized.Parameters;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.restdocs.templates.TemplateFormat;
|
||||
import org.springframework.restdocs.test.ExpectedSnippet;
|
||||
import org.springframework.restdocs.test.ExpectedSnippets;
|
||||
import org.springframework.restdocs.test.OperationBuilder;
|
||||
import org.springframework.restdocs.test.SnippetMatchers;
|
||||
import org.springframework.restdocs.test.SnippetMatchers.CodeBlockMatcher;
|
||||
@@ -50,7 +50,7 @@ public abstract class AbstractSnippetTests {
|
||||
protected final TemplateFormat templateFormat;
|
||||
|
||||
@Rule
|
||||
public ExpectedSnippet snippet;
|
||||
public ExpectedSnippets snippets;
|
||||
|
||||
@Rule
|
||||
public OperationBuilder operationBuilder;
|
||||
@@ -62,7 +62,7 @@ public abstract class AbstractSnippetTests {
|
||||
}
|
||||
|
||||
public AbstractSnippetTests(String name, TemplateFormat templateFormat) {
|
||||
this.snippet = new ExpectedSnippet(templateFormat);
|
||||
this.snippets = new ExpectedSnippets(templateFormat);
|
||||
this.templateFormat = templateFormat;
|
||||
this.operationBuilder = new OperationBuilder(this.templateFormat);
|
||||
}
|
||||
|
||||
@@ -55,7 +55,7 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void getRequest() throws IOException {
|
||||
this.snippet.expectCurlRequest().withContents(
|
||||
this.snippets.expectCurlRequest().withContents(
|
||||
codeBlock("bash").content("$ curl 'http://localhost/foo' -i"));
|
||||
new CurlRequestSnippet()
|
||||
.document(this.operationBuilder.request("http://localhost/foo").build());
|
||||
@@ -63,7 +63,7 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void getRequestWithParameter() throws IOException {
|
||||
this.snippet.expectCurlRequest().withContents(
|
||||
this.snippets.expectCurlRequest().withContents(
|
||||
codeBlock("bash").content("$ curl 'http://localhost/foo?a=alpha' -i"));
|
||||
new CurlRequestSnippet().document(this.operationBuilder
|
||||
.request("http://localhost/foo").param("a", "alpha").build());
|
||||
@@ -71,7 +71,7 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void nonGetRequest() throws IOException {
|
||||
this.snippet.expectCurlRequest().withContents(
|
||||
this.snippets.expectCurlRequest().withContents(
|
||||
codeBlock("bash").content("$ curl 'http://localhost/foo' -i -X POST"));
|
||||
new CurlRequestSnippet().document(this.operationBuilder
|
||||
.request("http://localhost/foo").method("POST").build());
|
||||
@@ -79,7 +79,7 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void requestWithContent() throws IOException {
|
||||
this.snippet.expectCurlRequest().withContents(codeBlock("bash")
|
||||
this.snippets.expectCurlRequest().withContents(codeBlock("bash")
|
||||
.content("$ curl 'http://localhost/foo' -i -d 'content'"));
|
||||
new CurlRequestSnippet().document(this.operationBuilder
|
||||
.request("http://localhost/foo").content("content").build());
|
||||
@@ -87,7 +87,7 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void getRequestWithQueryString() throws IOException {
|
||||
this.snippet.expectCurlRequest().withContents(codeBlock("bash")
|
||||
this.snippets.expectCurlRequest().withContents(codeBlock("bash")
|
||||
.content("$ curl 'http://localhost/foo?param=value' -i"));
|
||||
new CurlRequestSnippet().document(this.operationBuilder
|
||||
.request("http://localhost/foo?param=value").build());
|
||||
@@ -96,7 +96,7 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
|
||||
@Test
|
||||
public void getRequestWithTotallyOverlappingQueryStringAndParameters()
|
||||
throws IOException {
|
||||
this.snippet.expectCurlRequest().withContents(codeBlock("bash")
|
||||
this.snippets.expectCurlRequest().withContents(codeBlock("bash")
|
||||
.content("$ curl 'http://localhost/foo?param=value' -i"));
|
||||
new CurlRequestSnippet().document(
|
||||
this.operationBuilder.request("http://localhost/foo?param=value")
|
||||
@@ -106,7 +106,7 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
|
||||
@Test
|
||||
public void getRequestWithPartiallyOverlappingQueryStringAndParameters()
|
||||
throws IOException {
|
||||
this.snippet.expectCurlRequest().withContents(codeBlock("bash")
|
||||
this.snippets.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")
|
||||
@@ -115,7 +115,7 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void getRequestWithDisjointQueryStringAndParameters() throws IOException {
|
||||
this.snippet.expectCurlRequest().withContents(codeBlock("bash")
|
||||
this.snippets.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());
|
||||
@@ -123,7 +123,7 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void getRequestWithQueryStringWithNoValue() throws IOException {
|
||||
this.snippet.expectCurlRequest().withContents(
|
||||
this.snippets.expectCurlRequest().withContents(
|
||||
codeBlock("bash").content("$ curl 'http://localhost/foo?param' -i"));
|
||||
new CurlRequestSnippet().document(
|
||||
this.operationBuilder.request("http://localhost/foo?param").build());
|
||||
@@ -131,7 +131,7 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void postRequestWithQueryString() throws IOException {
|
||||
this.snippet.expectCurlRequest().withContents(codeBlock("bash")
|
||||
this.snippets.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());
|
||||
@@ -139,7 +139,7 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void postRequestWithQueryStringWithNoValue() throws IOException {
|
||||
this.snippet.expectCurlRequest().withContents(codeBlock("bash")
|
||||
this.snippets.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());
|
||||
@@ -147,7 +147,7 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void postRequestWithOneParameter() throws IOException {
|
||||
this.snippet.expectCurlRequest().withContents(codeBlock("bash")
|
||||
this.snippets.expectCurlRequest().withContents(codeBlock("bash")
|
||||
.content("$ curl 'http://localhost/foo' -i -X POST -d 'k1=v1'"));
|
||||
new CurlRequestSnippet()
|
||||
.document(this.operationBuilder.request("http://localhost/foo")
|
||||
@@ -156,7 +156,7 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void postRequestWithOneParameterWithNoValue() throws IOException {
|
||||
this.snippet.expectCurlRequest().withContents(codeBlock("bash")
|
||||
this.snippets.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());
|
||||
@@ -164,7 +164,7 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void postRequestWithMultipleParameters() throws IOException {
|
||||
this.snippet.expectCurlRequest()
|
||||
this.snippets.expectCurlRequest()
|
||||
.withContents(codeBlock("bash")
|
||||
.content("$ curl 'http://localhost/foo' -i -X POST"
|
||||
+ " -d 'k1=v1&k1=v1-bis&k2=v2'"));
|
||||
@@ -175,7 +175,7 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void postRequestWithUrlEncodedParameter() throws IOException {
|
||||
this.snippet.expectCurlRequest().withContents(codeBlock("bash")
|
||||
this.snippets.expectCurlRequest().withContents(codeBlock("bash")
|
||||
.content("$ curl 'http://localhost/foo' -i -X POST -d 'k1=a%26b'"));
|
||||
new CurlRequestSnippet()
|
||||
.document(this.operationBuilder.request("http://localhost/foo")
|
||||
@@ -184,7 +184,7 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void postRequestWithDisjointQueryStringAndParameter() throws IOException {
|
||||
this.snippet.expectCurlRequest().withContents(codeBlock("bash").content(
|
||||
this.snippets.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")
|
||||
@@ -194,7 +194,7 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
|
||||
@Test
|
||||
public void postRequestWithTotallyOverlappingQueryStringAndParameters()
|
||||
throws IOException {
|
||||
this.snippet.expectCurlRequest().withContents(codeBlock("bash")
|
||||
this.snippets.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")
|
||||
@@ -204,7 +204,7 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
|
||||
@Test
|
||||
public void postRequestWithPartiallyOverlappingQueryStringAndParameters()
|
||||
throws IOException {
|
||||
this.snippet.expectCurlRequest().withContents(codeBlock("bash").content(
|
||||
this.snippets.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")
|
||||
@@ -213,7 +213,7 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void putRequestWithOneParameter() throws IOException {
|
||||
this.snippet.expectCurlRequest().withContents(codeBlock("bash")
|
||||
this.snippets.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());
|
||||
@@ -221,7 +221,7 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void putRequestWithMultipleParameters() throws IOException {
|
||||
this.snippet.expectCurlRequest()
|
||||
this.snippets.expectCurlRequest()
|
||||
.withContents(codeBlock("bash")
|
||||
.content("$ curl 'http://localhost/foo' -i -X PUT"
|
||||
+ " -d 'k1=v1&k1=v1-bis&k2=v2'"));
|
||||
@@ -232,7 +232,7 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void putRequestWithUrlEncodedParameter() throws IOException {
|
||||
this.snippet.expectCurlRequest().withContents(codeBlock("bash")
|
||||
this.snippets.expectCurlRequest().withContents(codeBlock("bash")
|
||||
.content("$ curl 'http://localhost/foo' -i -X PUT -d 'k1=a%26b'"));
|
||||
new CurlRequestSnippet()
|
||||
.document(this.operationBuilder.request("http://localhost/foo")
|
||||
@@ -241,7 +241,7 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void requestWithHeaders() throws IOException {
|
||||
this.snippet.expectCurlRequest()
|
||||
this.snippets.expectCurlRequest()
|
||||
.withContents(codeBlock("bash").content("$ curl 'http://localhost/foo' -i"
|
||||
+ " -H 'Content-Type: application/json' -H 'a: alpha'"));
|
||||
new CurlRequestSnippet()
|
||||
@@ -256,7 +256,7 @@ 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()
|
||||
this.snippets.expectCurlRequest()
|
||||
.withContents(codeBlock("bash").content(expectedContent));
|
||||
new CurlRequestSnippet().document(this.operationBuilder
|
||||
.request("http://localhost/upload").method("POST")
|
||||
@@ -269,7 +269,7 @@ 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()
|
||||
this.snippets.expectCurlRequest()
|
||||
.withContents(codeBlock("bash").content(expectedContent));
|
||||
new CurlRequestSnippet().document(
|
||||
this.operationBuilder.request("http://localhost/upload").method("POST")
|
||||
@@ -285,7 +285,7 @@ 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()
|
||||
this.snippets.expectCurlRequest()
|
||||
.withContents(codeBlock("bash").content(expectedContent));
|
||||
new CurlRequestSnippet().document(
|
||||
this.operationBuilder.request("http://localhost/upload").method("POST")
|
||||
@@ -301,7 +301,7 @@ 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()
|
||||
this.snippets.expectCurlRequest()
|
||||
.withContents(codeBlock("bash").content(expectedContent));
|
||||
new CurlRequestSnippet().document(
|
||||
this.operationBuilder.request("http://localhost/upload").method("POST")
|
||||
@@ -314,7 +314,7 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void basicAuthCredentialsAreSuppliedUsingUserOption() throws IOException {
|
||||
this.snippet.expectCurlRequest().withContents(codeBlock("bash")
|
||||
this.snippets.expectCurlRequest().withContents(codeBlock("bash")
|
||||
.content("$ curl 'http://localhost/foo' -i -u 'user:secret'"));
|
||||
new CurlRequestSnippet()
|
||||
.document(this.operationBuilder.request("http://localhost/foo")
|
||||
@@ -326,7 +326,7 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void customAttributes() throws IOException {
|
||||
this.snippet.expectCurlRequest()
|
||||
this.snippets.expectCurlRequest()
|
||||
.withContents(containsString("curl request title"));
|
||||
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
|
||||
given(resolver.resolveTemplateResource("curl-request"))
|
||||
@@ -344,7 +344,7 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void customHostHeaderIsIncluded() throws IOException {
|
||||
this.snippet.expectCurlRequest()
|
||||
this.snippets.expectCurlRequest()
|
||||
.withContents(codeBlock("bash").content(
|
||||
"$ curl 'http://localhost/foo' -i" + " -H 'Host: api.example.com'"
|
||||
+ " -H 'Content-Type: application/json' -H 'a: alpha'"));
|
||||
@@ -358,7 +358,7 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void postWithContentAndParameters() throws IOException {
|
||||
this.snippet.expectCurlRequest()
|
||||
this.snippets.expectCurlRequest()
|
||||
.withContents(codeBlock("bash")
|
||||
.content("$ curl 'http://localhost/foo?a=alpha&b=bravo' -i "
|
||||
+ "-X POST -d 'Some content'"));
|
||||
|
||||
@@ -56,7 +56,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void getRequest() throws IOException {
|
||||
this.snippet.expectHttpieRequest().withContents(
|
||||
this.snippets.expectHttpieRequest().withContents(
|
||||
codeBlock("bash").content("$ http GET 'http://localhost/foo'"));
|
||||
new HttpieRequestSnippet()
|
||||
.document(this.operationBuilder.request("http://localhost/foo").build());
|
||||
@@ -64,7 +64,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void getRequestWithParameter() throws IOException {
|
||||
this.snippet.expectHttpieRequest().withContents(
|
||||
this.snippets.expectHttpieRequest().withContents(
|
||||
codeBlock("bash").content("$ http GET 'http://localhost/foo?a=alpha'"));
|
||||
new HttpieRequestSnippet().document(this.operationBuilder
|
||||
.request("http://localhost/foo").param("a", "alpha").build());
|
||||
@@ -72,7 +72,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void nonGetRequest() throws IOException {
|
||||
this.snippet.expectHttpieRequest().withContents(
|
||||
this.snippets.expectHttpieRequest().withContents(
|
||||
codeBlock("bash").content("$ http POST 'http://localhost/foo'"));
|
||||
new HttpieRequestSnippet().document(this.operationBuilder
|
||||
.request("http://localhost/foo").method("POST").build());
|
||||
@@ -80,7 +80,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void requestWithContent() throws IOException {
|
||||
this.snippet.expectHttpieRequest().withContents(codeBlock("bash")
|
||||
this.snippets.expectHttpieRequest().withContents(codeBlock("bash")
|
||||
.content("$ echo 'content' | http GET 'http://localhost/foo'"));
|
||||
new HttpieRequestSnippet().document(this.operationBuilder
|
||||
.request("http://localhost/foo").content("content").build());
|
||||
@@ -88,7 +88,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void getRequestWithQueryString() throws IOException {
|
||||
this.snippet.expectHttpieRequest().withContents(codeBlock("bash")
|
||||
this.snippets.expectHttpieRequest().withContents(codeBlock("bash")
|
||||
.content("$ http GET 'http://localhost/foo?param=value'"));
|
||||
new HttpieRequestSnippet().document(this.operationBuilder
|
||||
.request("http://localhost/foo?param=value").build());
|
||||
@@ -97,7 +97,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
|
||||
@Test
|
||||
public void getRequestWithTotallyOverlappingQueryStringAndParameters()
|
||||
throws IOException {
|
||||
this.snippet.expectHttpieRequest().withContents(codeBlock("bash")
|
||||
this.snippets.expectHttpieRequest().withContents(codeBlock("bash")
|
||||
.content("$ http GET 'http://localhost/foo?param=value'"));
|
||||
new HttpieRequestSnippet().document(
|
||||
this.operationBuilder.request("http://localhost/foo?param=value")
|
||||
@@ -107,7 +107,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
|
||||
@Test
|
||||
public void getRequestWithPartiallyOverlappingQueryStringAndParameters()
|
||||
throws IOException {
|
||||
this.snippet.expectHttpieRequest().withContents(codeBlock("bash")
|
||||
this.snippets.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")
|
||||
@@ -116,7 +116,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void getRequestWithDisjointQueryStringAndParameters() throws IOException {
|
||||
this.snippet.expectHttpieRequest().withContents(codeBlock("bash")
|
||||
this.snippets.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());
|
||||
@@ -124,7 +124,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void getRequestWithQueryStringWithNoValue() throws IOException {
|
||||
this.snippet.expectHttpieRequest().withContents(
|
||||
this.snippets.expectHttpieRequest().withContents(
|
||||
codeBlock("bash").content("$ http GET 'http://localhost/foo?param'"));
|
||||
new HttpieRequestSnippet().document(
|
||||
this.operationBuilder.request("http://localhost/foo?param").build());
|
||||
@@ -132,7 +132,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void postRequestWithQueryString() throws IOException {
|
||||
this.snippet.expectHttpieRequest().withContents(codeBlock("bash")
|
||||
this.snippets.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());
|
||||
@@ -140,7 +140,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void postRequestWithQueryStringWithNoValue() throws IOException {
|
||||
this.snippet.expectHttpieRequest().withContents(
|
||||
this.snippets.expectHttpieRequest().withContents(
|
||||
codeBlock("bash").content("$ http POST 'http://localhost/foo?param'"));
|
||||
new HttpieRequestSnippet().document(this.operationBuilder
|
||||
.request("http://localhost/foo?param").method("POST").build());
|
||||
@@ -148,7 +148,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void postRequestWithOneParameter() throws IOException {
|
||||
this.snippet.expectHttpieRequest().withContents(codeBlock("bash")
|
||||
this.snippets.expectHttpieRequest().withContents(codeBlock("bash")
|
||||
.content("$ http --form POST 'http://localhost/foo' 'k1=v1'"));
|
||||
new HttpieRequestSnippet()
|
||||
.document(this.operationBuilder.request("http://localhost/foo")
|
||||
@@ -157,7 +157,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void postRequestWithOneParameterWithNoValue() throws IOException {
|
||||
this.snippet.expectHttpieRequest().withContents(codeBlock("bash")
|
||||
this.snippets.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());
|
||||
@@ -165,7 +165,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void postRequestWithMultipleParameters() throws IOException {
|
||||
this.snippet.expectHttpieRequest()
|
||||
this.snippets.expectHttpieRequest()
|
||||
.withContents(codeBlock("bash")
|
||||
.content("$ http --form POST 'http://localhost/foo'"
|
||||
+ " 'k1=v1' 'k1=v1-bis' 'k2=v2'"));
|
||||
@@ -176,7 +176,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void postRequestWithUrlEncodedParameter() throws IOException {
|
||||
this.snippet.expectHttpieRequest().withContents(codeBlock("bash")
|
||||
this.snippets.expectHttpieRequest().withContents(codeBlock("bash")
|
||||
.content("$ http --form POST 'http://localhost/foo' 'k1=a&b'"));
|
||||
new HttpieRequestSnippet()
|
||||
.document(this.operationBuilder.request("http://localhost/foo")
|
||||
@@ -185,7 +185,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void postRequestWithDisjointQueryStringAndParameter() throws IOException {
|
||||
this.snippet.expectHttpieRequest().withContents(codeBlock("bash")
|
||||
this.snippets.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")
|
||||
@@ -195,7 +195,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
|
||||
@Test
|
||||
public void postRequestWithTotallyOverlappingQueryStringAndParameters()
|
||||
throws IOException {
|
||||
this.snippet.expectHttpieRequest().withContents(codeBlock("bash")
|
||||
this.snippets.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")
|
||||
@@ -205,7 +205,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
|
||||
@Test
|
||||
public void postRequestWithPartiallyOverlappingQueryStringAndParameters()
|
||||
throws IOException {
|
||||
this.snippet.expectHttpieRequest().withContents(codeBlock("bash")
|
||||
this.snippets.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")
|
||||
@@ -214,7 +214,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void putRequestWithOneParameter() throws IOException {
|
||||
this.snippet.expectHttpieRequest().withContents(codeBlock("bash")
|
||||
this.snippets.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());
|
||||
@@ -222,7 +222,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void putRequestWithMultipleParameters() throws IOException {
|
||||
this.snippet.expectHttpieRequest()
|
||||
this.snippets.expectHttpieRequest()
|
||||
.withContents(codeBlock("bash")
|
||||
.content("$ http --form PUT 'http://localhost/foo'"
|
||||
+ " 'k1=v1' 'k1=v1-bis' 'k2=v2'"));
|
||||
@@ -233,7 +233,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void putRequestWithUrlEncodedParameter() throws IOException {
|
||||
this.snippet.expectHttpieRequest().withContents(codeBlock("bash")
|
||||
this.snippets.expectHttpieRequest().withContents(codeBlock("bash")
|
||||
.content("$ http --form PUT 'http://localhost/foo' 'k1=a&b'"));
|
||||
new HttpieRequestSnippet()
|
||||
.document(this.operationBuilder.request("http://localhost/foo")
|
||||
@@ -242,7 +242,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void requestWithHeaders() throws IOException {
|
||||
this.snippet.expectHttpieRequest().withContents(
|
||||
this.snippets.expectHttpieRequest().withContents(
|
||||
codeBlock("bash").content("$ http GET 'http://localhost/foo'"
|
||||
+ " 'Content-Type:application/json' 'a:alpha'"));
|
||||
new HttpieRequestSnippet()
|
||||
@@ -257,7 +257,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
|
||||
String expectedContent = String
|
||||
.format("$ http --form POST 'http://localhost/upload' \\%n"
|
||||
+ " 'metadata'@<(echo '{\"description\": \"foo\"}')");
|
||||
this.snippet.expectHttpieRequest()
|
||||
this.snippets.expectHttpieRequest()
|
||||
.withContents(codeBlock("bash").content(expectedContent));
|
||||
new HttpieRequestSnippet().document(this.operationBuilder
|
||||
.request("http://localhost/upload").method("POST")
|
||||
@@ -271,7 +271,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
|
||||
String expectedContent = String
|
||||
.format("$ http --form POST 'http://localhost/upload' \\%n"
|
||||
+ " 'image'@'documents/images/example.png'");
|
||||
this.snippet.expectHttpieRequest()
|
||||
this.snippets.expectHttpieRequest()
|
||||
.withContents(codeBlock("bash").content(expectedContent));
|
||||
new HttpieRequestSnippet().document(
|
||||
this.operationBuilder.request("http://localhost/upload").method("POST")
|
||||
@@ -287,7 +287,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
|
||||
String expectedContent = String
|
||||
.format("$ http --form POST 'http://localhost/upload' \\%n"
|
||||
+ " 'image'@'documents/images/example.png'");
|
||||
this.snippet.expectHttpieRequest()
|
||||
this.snippets.expectHttpieRequest()
|
||||
.withContents(codeBlock("bash").content(expectedContent));
|
||||
new HttpieRequestSnippet().document(
|
||||
this.operationBuilder.request("http://localhost/upload").method("POST")
|
||||
@@ -303,7 +303,7 @@ 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()
|
||||
this.snippets.expectHttpieRequest()
|
||||
.withContents(codeBlock("bash").content(expectedContent));
|
||||
new HttpieRequestSnippet().document(
|
||||
this.operationBuilder.request("http://localhost/upload").method("POST")
|
||||
@@ -316,7 +316,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void basicAuthCredentialsAreSuppliedUsingAuthOption() throws IOException {
|
||||
this.snippet.expectHttpieRequest().withContents(codeBlock("bash")
|
||||
this.snippets.expectHttpieRequest().withContents(codeBlock("bash")
|
||||
.content("$ http --auth 'user:secret' GET 'http://localhost/foo'"));
|
||||
new HttpieRequestSnippet()
|
||||
.document(this.operationBuilder.request("http://localhost/foo")
|
||||
@@ -328,7 +328,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void customAttributes() throws IOException {
|
||||
this.snippet.expectHttpieRequest()
|
||||
this.snippets.expectHttpieRequest()
|
||||
.withContents(containsString("httpie request title"));
|
||||
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
|
||||
given(resolver.resolveTemplateResource("httpie-request"))
|
||||
@@ -346,7 +346,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void customHostHeaderIsIncluded() throws IOException {
|
||||
this.snippet.expectHttpieRequest()
|
||||
this.snippets.expectHttpieRequest()
|
||||
.withContents(codeBlock("bash").content(
|
||||
"$ http GET 'http://localhost/foo' 'Host:api.example.com'"
|
||||
+ " 'Content-Type:application/json' 'a:alpha'"));
|
||||
@@ -360,7 +360,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void postWithContentAndParameters() throws IOException {
|
||||
this.snippet.expectHttpieRequest().withContents(
|
||||
this.snippets.expectHttpieRequest().withContents(
|
||||
codeBlock("bash").content("$ echo 'Some content' | http POST "
|
||||
+ "'http://localhost/foo?a=alpha&b=bravo'"));
|
||||
new HttpieRequestSnippet().document(this.operationBuilder
|
||||
|
||||
@@ -24,7 +24,7 @@ import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import org.springframework.restdocs.snippet.SnippetException;
|
||||
import org.springframework.restdocs.test.ExpectedSnippet;
|
||||
import org.springframework.restdocs.test.ExpectedSnippets;
|
||||
import org.springframework.restdocs.test.OperationBuilder;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.endsWith;
|
||||
@@ -44,7 +44,7 @@ public class RequestHeadersSnippetFailureTests {
|
||||
public OperationBuilder operationBuilder = new OperationBuilder(asciidoctor());
|
||||
|
||||
@Rule
|
||||
public ExpectedSnippet snippet = new ExpectedSnippet(asciidoctor());
|
||||
public ExpectedSnippets snippets = new ExpectedSnippets(asciidoctor());
|
||||
|
||||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
@@ -49,7 +49,7 @@ public class RequestHeadersSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void requestWithHeaders() throws IOException {
|
||||
this.snippet.expectRequestHeaders()
|
||||
this.snippets.expectRequestHeaders()
|
||||
.withContents(tableWithHeader("Name", "Description")
|
||||
.row("`X-Test`", "one").row("`Accept`", "two")
|
||||
.row("`Accept-Encoding`", "three")
|
||||
@@ -83,7 +83,7 @@ public class RequestHeadersSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void caseInsensitiveRequestHeaders() throws IOException {
|
||||
this.snippet.expectRequestHeaders().withContents(
|
||||
this.snippets.expectRequestHeaders().withContents(
|
||||
tableWithHeader("Name", "Description").row("`X-Test`", "one"));
|
||||
new RequestHeadersSnippet(
|
||||
Arrays.asList(headerWithName("X-Test").description("one")))
|
||||
@@ -93,7 +93,7 @@ public class RequestHeadersSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void undocumentedRequestHeader() throws IOException {
|
||||
this.snippet.expectRequestHeaders().withContents(
|
||||
this.snippets.expectRequestHeaders().withContents(
|
||||
tableWithHeader("Name", "Description").row("`X-Test`", "one"));
|
||||
new RequestHeadersSnippet(
|
||||
Arrays.asList(headerWithName("X-Test").description("one")))
|
||||
@@ -104,7 +104,7 @@ public class RequestHeadersSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void requestHeadersWithCustomAttributes() throws IOException {
|
||||
this.snippet.expectRequestHeaders().withContents(containsString("Custom title"));
|
||||
this.snippets.expectRequestHeaders().withContents(containsString("Custom title"));
|
||||
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
|
||||
given(resolver.resolveTemplateResource("request-headers"))
|
||||
.willReturn(snippetResource("request-headers-with-title"));
|
||||
@@ -123,7 +123,7 @@ public class RequestHeadersSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void requestHeadersWithCustomDescriptorAttributes() throws IOException {
|
||||
this.snippet.expectRequestHeaders().withContents(//
|
||||
this.snippets.expectRequestHeaders().withContents(//
|
||||
tableWithHeader("Name", "Description", "Foo")
|
||||
.row("X-Test", "one", "alpha")
|
||||
.row("Accept-Encoding", "two", "bravo")
|
||||
@@ -152,7 +152,7 @@ public class RequestHeadersSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void additionalDescriptors() throws IOException {
|
||||
this.snippet.expectRequestHeaders()
|
||||
this.snippets.expectRequestHeaders()
|
||||
.withContents(tableWithHeader("Name", "Description")
|
||||
.row("`X-Test`", "one").row("`Accept`", "two")
|
||||
.row("`Accept-Encoding`", "three")
|
||||
@@ -178,7 +178,7 @@ public class RequestHeadersSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void tableCellContentIsEscapedWhenNecessary() throws IOException {
|
||||
this.snippet.expectRequestHeaders().withContents(
|
||||
this.snippets.expectRequestHeaders().withContents(
|
||||
tableWithHeader("Name", "Description").row(escapeIfNecessary("`Foo|Bar`"),
|
||||
escapeIfNecessary("one|two")));
|
||||
new RequestHeadersSnippet(
|
||||
|
||||
@@ -24,7 +24,7 @@ import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import org.springframework.restdocs.snippet.SnippetException;
|
||||
import org.springframework.restdocs.test.ExpectedSnippet;
|
||||
import org.springframework.restdocs.test.ExpectedSnippets;
|
||||
import org.springframework.restdocs.test.OperationBuilder;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.endsWith;
|
||||
@@ -44,7 +44,7 @@ public class ResponseHeadersSnippetFailureTests {
|
||||
public OperationBuilder operationBuilder = new OperationBuilder(asciidoctor());
|
||||
|
||||
@Rule
|
||||
public ExpectedSnippet snippet = new ExpectedSnippet(asciidoctor());
|
||||
public ExpectedSnippets snippets = new ExpectedSnippets(asciidoctor());
|
||||
|
||||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
@@ -49,7 +49,7 @@ public class ResponseHeadersSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void responseWithHeaders() throws IOException {
|
||||
this.snippet.expectResponseHeaders().withContents(
|
||||
this.snippets.expectResponseHeaders().withContents(
|
||||
tableWithHeader("Name", "Description").row("`X-Test`", "one")
|
||||
.row("`Content-Type`", "two").row("`Etag`", "three")
|
||||
.row("`Cache-Control`", "five").row("`Vary`", "six"));
|
||||
@@ -71,7 +71,7 @@ public class ResponseHeadersSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void caseInsensitiveResponseHeaders() throws IOException {
|
||||
this.snippet.expectResponseHeaders().withContents(
|
||||
this.snippets.expectResponseHeaders().withContents(
|
||||
tableWithHeader("Name", "Description").row("`X-Test`", "one"));
|
||||
new ResponseHeadersSnippet(
|
||||
Arrays.asList(headerWithName("X-Test").description("one")))
|
||||
@@ -81,7 +81,7 @@ public class ResponseHeadersSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void undocumentedResponseHeader() throws IOException {
|
||||
this.snippet.expectResponseHeaders().withContents(
|
||||
this.snippets.expectResponseHeaders().withContents(
|
||||
tableWithHeader("Name", "Description").row("`X-Test`", "one"));
|
||||
new ResponseHeadersSnippet(
|
||||
Arrays.asList(headerWithName("X-Test").description("one"))).document(
|
||||
@@ -91,7 +91,7 @@ public class ResponseHeadersSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void responseHeadersWithCustomAttributes() throws IOException {
|
||||
this.snippet.expectResponseHeaders().withContents(containsString("Custom title"));
|
||||
this.snippets.expectResponseHeaders().withContents(containsString("Custom title"));
|
||||
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
|
||||
given(resolver.resolveTemplateResource("response-headers"))
|
||||
.willReturn(snippetResource("response-headers-with-title"));
|
||||
@@ -110,7 +110,7 @@ public class ResponseHeadersSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void responseHeadersWithCustomDescriptorAttributes() throws IOException {
|
||||
this.snippet.expectResponseHeaders()
|
||||
this.snippets.expectResponseHeaders()
|
||||
.withContents(tableWithHeader("Name", "Description", "Foo")
|
||||
.row("X-Test", "one", "alpha").row("Content-Type", "two", "bravo")
|
||||
.row("Etag", "three", "charlie"));
|
||||
@@ -138,7 +138,7 @@ public class ResponseHeadersSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void additionalDescriptors() throws IOException {
|
||||
this.snippet.expectResponseHeaders().withContents(
|
||||
this.snippets.expectResponseHeaders().withContents(
|
||||
tableWithHeader("Name", "Description").row("`X-Test`", "one")
|
||||
.row("`Content-Type`", "two").row("`Etag`", "three")
|
||||
.row("`Cache-Control`", "five").row("`Vary`", "six"));
|
||||
@@ -157,7 +157,7 @@ public class ResponseHeadersSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void tableCellContentIsEscapedWhenNecessary() throws IOException {
|
||||
this.snippet.expectResponseHeaders().withContents(
|
||||
this.snippets.expectResponseHeaders().withContents(
|
||||
tableWithHeader("Name", "Description").row(escapeIfNecessary("`Foo|Bar`"),
|
||||
escapeIfNecessary("one|two")));
|
||||
new ResponseHeadersSnippet(
|
||||
|
||||
@@ -51,7 +51,7 @@ public class HttpRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void getRequest() throws IOException {
|
||||
this.snippet.expectHttpRequest()
|
||||
this.snippets.expectHttpRequest()
|
||||
.withContents(httpRequest(RequestMethod.GET, "/foo").header("Alpha", "a")
|
||||
.header(HttpHeaders.HOST, "localhost"));
|
||||
|
||||
@@ -61,7 +61,7 @@ public class HttpRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void getRequestWithParameters() throws IOException {
|
||||
this.snippet.expectHttpRequest()
|
||||
this.snippets.expectHttpRequest()
|
||||
.withContents(httpRequest(RequestMethod.GET, "/foo?b=bravo")
|
||||
.header("Alpha", "a").header(HttpHeaders.HOST, "localhost"));
|
||||
|
||||
@@ -72,7 +72,7 @@ public class HttpRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void getRequestWithPort() throws IOException {
|
||||
this.snippet.expectHttpRequest()
|
||||
this.snippets.expectHttpRequest()
|
||||
.withContents(httpRequest(RequestMethod.GET, "/foo").header("Alpha", "a")
|
||||
.header(HttpHeaders.HOST, "localhost:8080"));
|
||||
|
||||
@@ -82,7 +82,7 @@ public class HttpRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void getRequestWithQueryString() throws IOException {
|
||||
this.snippet.expectHttpRequest()
|
||||
this.snippets.expectHttpRequest()
|
||||
.withContents(httpRequest(RequestMethod.GET, "/foo?bar=baz")
|
||||
.header(HttpHeaders.HOST, "localhost"));
|
||||
|
||||
@@ -92,7 +92,7 @@ public class HttpRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void getRequestWithQueryStringWithNoValue() throws IOException {
|
||||
this.snippet.expectHttpRequest()
|
||||
this.snippets.expectHttpRequest()
|
||||
.withContents(httpRequest(RequestMethod.GET, "/foo?bar")
|
||||
.header(HttpHeaders.HOST, "localhost"));
|
||||
|
||||
@@ -102,7 +102,7 @@ public class HttpRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void getWithPartiallyOverlappingQueryStringAndParameters() throws IOException {
|
||||
this.snippet.expectHttpRequest()
|
||||
this.snippets.expectHttpRequest()
|
||||
.withContents(httpRequest(RequestMethod.GET, "/foo?a=alpha&b=bravo")
|
||||
.header(HttpHeaders.HOST, "localhost"));
|
||||
|
||||
@@ -113,7 +113,7 @@ public class HttpRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void getWithTotallyOverlappingQueryStringAndParameters() throws IOException {
|
||||
this.snippet.expectHttpRequest()
|
||||
this.snippets.expectHttpRequest()
|
||||
.withContents(httpRequest(RequestMethod.GET, "/foo?a=alpha&b=bravo")
|
||||
.header(HttpHeaders.HOST, "localhost"));
|
||||
|
||||
@@ -125,7 +125,7 @@ public class HttpRequestSnippetTests extends AbstractSnippetTests {
|
||||
@Test
|
||||
public void postRequestWithContent() throws IOException {
|
||||
String content = "Hello, world";
|
||||
this.snippet.expectHttpRequest()
|
||||
this.snippets.expectHttpRequest()
|
||||
.withContents(httpRequest(RequestMethod.POST, "/foo")
|
||||
.header(HttpHeaders.HOST, "localhost").content(content).header(
|
||||
HttpHeaders.CONTENT_LENGTH, content.getBytes().length));
|
||||
@@ -137,7 +137,7 @@ public class HttpRequestSnippetTests extends AbstractSnippetTests {
|
||||
@Test
|
||||
public void postRequestWithContentAndParameters() throws IOException {
|
||||
String content = "Hello, world";
|
||||
this.snippet.expectHttpRequest()
|
||||
this.snippets.expectHttpRequest()
|
||||
.withContents(httpRequest(RequestMethod.POST, "/foo?a=alpha")
|
||||
.header(HttpHeaders.HOST, "localhost").content(content).header(
|
||||
HttpHeaders.CONTENT_LENGTH, content.getBytes().length));
|
||||
@@ -151,7 +151,7 @@ public class HttpRequestSnippetTests extends AbstractSnippetTests {
|
||||
public void postRequestWithContentAndDisjointQueryStringAndParameters()
|
||||
throws IOException {
|
||||
String content = "Hello, world";
|
||||
this.snippet.expectHttpRequest()
|
||||
this.snippets.expectHttpRequest()
|
||||
.withContents(httpRequest(RequestMethod.POST, "/foo?b=bravo&a=alpha")
|
||||
.header(HttpHeaders.HOST, "localhost").content(content).header(
|
||||
HttpHeaders.CONTENT_LENGTH, content.getBytes().length));
|
||||
@@ -165,7 +165,7 @@ public class HttpRequestSnippetTests extends AbstractSnippetTests {
|
||||
public void postRequestWithContentAndPartiallyOverlappingQueryStringAndParameters()
|
||||
throws IOException {
|
||||
String content = "Hello, world";
|
||||
this.snippet.expectHttpRequest()
|
||||
this.snippets.expectHttpRequest()
|
||||
.withContents(httpRequest(RequestMethod.POST, "/foo?b=bravo&a=alpha")
|
||||
.header(HttpHeaders.HOST, "localhost").content(content).header(
|
||||
HttpHeaders.CONTENT_LENGTH, content.getBytes().length));
|
||||
@@ -179,7 +179,7 @@ public class HttpRequestSnippetTests extends AbstractSnippetTests {
|
||||
public void postRequestWithContentAndTotallyOverlappingQueryStringAndParameters()
|
||||
throws IOException {
|
||||
String content = "Hello, world";
|
||||
this.snippet.expectHttpRequest()
|
||||
this.snippets.expectHttpRequest()
|
||||
.withContents(httpRequest(RequestMethod.POST, "/foo?b=bravo&a=alpha")
|
||||
.header(HttpHeaders.HOST, "localhost").content(content).header(
|
||||
HttpHeaders.CONTENT_LENGTH, content.getBytes().length));
|
||||
@@ -193,7 +193,7 @@ public class HttpRequestSnippetTests extends AbstractSnippetTests {
|
||||
public void postRequestWithCharset() throws IOException {
|
||||
String japaneseContent = "\u30b3\u30f3\u30c6\u30f3\u30c4";
|
||||
byte[] contentBytes = japaneseContent.getBytes("UTF-8");
|
||||
this.snippet.expectHttpRequest()
|
||||
this.snippets.expectHttpRequest()
|
||||
.withContents(httpRequest(RequestMethod.POST, "/foo")
|
||||
.header("Content-Type", "text/plain;charset=UTF-8")
|
||||
.header(HttpHeaders.HOST, "localhost")
|
||||
@@ -208,7 +208,7 @@ public class HttpRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void postRequestWithParameter() throws IOException {
|
||||
this.snippet.expectHttpRequest()
|
||||
this.snippets.expectHttpRequest()
|
||||
.withContents(httpRequest(RequestMethod.POST, "/foo")
|
||||
.header(HttpHeaders.HOST, "localhost")
|
||||
.header("Content-Type", "application/x-www-form-urlencoded")
|
||||
@@ -221,7 +221,7 @@ public class HttpRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void postRequestWithParameterWithNoValue() throws IOException {
|
||||
this.snippet.expectHttpRequest()
|
||||
this.snippets.expectHttpRequest()
|
||||
.withContents(httpRequest(RequestMethod.POST, "/foo")
|
||||
.header(HttpHeaders.HOST, "localhost")
|
||||
.header("Content-Type", "application/x-www-form-urlencoded")
|
||||
@@ -234,7 +234,7 @@ public class HttpRequestSnippetTests extends AbstractSnippetTests {
|
||||
@Test
|
||||
public void putRequestWithContent() throws IOException {
|
||||
String content = "Hello, world";
|
||||
this.snippet.expectHttpRequest()
|
||||
this.snippets.expectHttpRequest()
|
||||
.withContents(httpRequest(RequestMethod.PUT, "/foo")
|
||||
.header(HttpHeaders.HOST, "localhost").content(content).header(
|
||||
HttpHeaders.CONTENT_LENGTH, content.getBytes().length));
|
||||
@@ -245,7 +245,7 @@ public class HttpRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void putRequestWithParameter() throws IOException {
|
||||
this.snippet.expectHttpRequest()
|
||||
this.snippets.expectHttpRequest()
|
||||
.withContents(httpRequest(RequestMethod.PUT, "/foo")
|
||||
.header(HttpHeaders.HOST, "localhost")
|
||||
.header("Content-Type", "application/x-www-form-urlencoded")
|
||||
@@ -260,7 +260,7 @@ public class HttpRequestSnippetTests extends AbstractSnippetTests {
|
||||
public void multipartPost() throws IOException {
|
||||
String expectedContent = createPart(String.format(
|
||||
"Content-Disposition: " + "form-data; " + "name=image%n%n<< data >>"));
|
||||
this.snippet.expectHttpRequest()
|
||||
this.snippets.expectHttpRequest()
|
||||
.withContents(httpRequest(RequestMethod.POST, "/upload")
|
||||
.header("Content-Type",
|
||||
"multipart/form-data; boundary=" + BOUNDARY)
|
||||
@@ -286,7 +286,7 @@ 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()
|
||||
this.snippets.expectHttpRequest()
|
||||
.withContents(httpRequest(RequestMethod.POST, "/upload")
|
||||
.header("Content-Type",
|
||||
"multipart/form-data; boundary=" + BOUNDARY)
|
||||
@@ -306,7 +306,7 @@ 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()
|
||||
this.snippets.expectHttpRequest()
|
||||
.withContents(httpRequest(RequestMethod.POST, "/upload")
|
||||
.header("Content-Type",
|
||||
"multipart/form-data; boundary=" + BOUNDARY)
|
||||
@@ -323,7 +323,7 @@ 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()
|
||||
this.snippets.expectHttpRequest()
|
||||
.withContents(httpRequest(RequestMethod.POST, "/upload")
|
||||
.header("Content-Type",
|
||||
"multipart/form-data; boundary=" + BOUNDARY)
|
||||
@@ -339,7 +339,7 @@ public class HttpRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void getRequestWithCustomHost() throws IOException {
|
||||
this.snippet.expectHttpRequest()
|
||||
this.snippets.expectHttpRequest()
|
||||
.withContents(httpRequest(RequestMethod.GET, "/foo")
|
||||
.header(HttpHeaders.HOST, "api.example.com"));
|
||||
new HttpRequestSnippet()
|
||||
@@ -349,7 +349,7 @@ public class HttpRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void requestWithCustomSnippetAttributes() throws IOException {
|
||||
this.snippet.expectHttpRequest()
|
||||
this.snippets.expectHttpRequest()
|
||||
.withContents(containsString("Title for the request"));
|
||||
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
|
||||
given(resolver.resolveTemplateResource("http-request"))
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2015 the original author or authors.
|
||||
* Copyright 2014-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -49,13 +49,13 @@ public class HttpResponseSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void basicResponse() throws IOException {
|
||||
this.snippet.expectHttpResponse().withContents(httpResponse(HttpStatus.OK));
|
||||
this.snippets.expectHttpResponse().withContents(httpResponse(HttpStatus.OK));
|
||||
new HttpResponseSnippet().document(this.operationBuilder.build());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nonOkResponse() throws IOException {
|
||||
this.snippet.expectHttpResponse()
|
||||
this.snippets.expectHttpResponse()
|
||||
.withContents(httpResponse(HttpStatus.BAD_REQUEST));
|
||||
new HttpResponseSnippet().document(this.operationBuilder.response()
|
||||
.status(HttpStatus.BAD_REQUEST.value()).build());
|
||||
@@ -63,7 +63,7 @@ public class HttpResponseSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void responseWithHeaders() throws IOException {
|
||||
this.snippet.expectHttpResponse().withContents(httpResponse(HttpStatus.OK)
|
||||
this.snippets.expectHttpResponse().withContents(httpResponse(HttpStatus.OK)
|
||||
.header("Content-Type", "application/json").header("a", "alpha"));
|
||||
new HttpResponseSnippet()
|
||||
.document(this.operationBuilder.response()
|
||||
@@ -75,7 +75,7 @@ public class HttpResponseSnippetTests extends AbstractSnippetTests {
|
||||
@Test
|
||||
public void responseWithContent() throws IOException {
|
||||
String content = "content";
|
||||
this.snippet.expectHttpResponse()
|
||||
this.snippets.expectHttpResponse()
|
||||
.withContents(httpResponse(HttpStatus.OK).content(content)
|
||||
.header(HttpHeaders.CONTENT_LENGTH, content.getBytes().length));
|
||||
new HttpResponseSnippet()
|
||||
@@ -86,7 +86,7 @@ public class HttpResponseSnippetTests extends AbstractSnippetTests {
|
||||
public void responseWithCharset() throws IOException {
|
||||
String japaneseContent = "\u30b3\u30f3\u30c6\u30f3\u30c4";
|
||||
byte[] contentBytes = japaneseContent.getBytes("UTF-8");
|
||||
this.snippet.expectHttpResponse()
|
||||
this.snippets.expectHttpResponse()
|
||||
.withContents(httpResponse(HttpStatus.OK)
|
||||
.header("Content-Type", "text/plain;charset=UTF-8")
|
||||
.content(japaneseContent)
|
||||
@@ -98,7 +98,7 @@ public class HttpResponseSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void responseWithCustomSnippetAttributes() throws IOException {
|
||||
this.snippet.expectHttpResponse()
|
||||
this.snippets.expectHttpResponse()
|
||||
.withContents(containsString("Title for the response"));
|
||||
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
|
||||
given(resolver.resolveTemplateResource("http-response"))
|
||||
|
||||
@@ -25,7 +25,7 @@ import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import org.springframework.restdocs.snippet.SnippetException;
|
||||
import org.springframework.restdocs.test.ExpectedSnippet;
|
||||
import org.springframework.restdocs.test.ExpectedSnippets;
|
||||
import org.springframework.restdocs.test.OperationBuilder;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
@@ -43,7 +43,7 @@ public class LinksSnippetFailureTests {
|
||||
public OperationBuilder operationBuilder = new OperationBuilder(asciidoctor());
|
||||
|
||||
@Rule
|
||||
public ExpectedSnippet snippet = new ExpectedSnippet(asciidoctor());
|
||||
public ExpectedSnippets snippets = new ExpectedSnippets(asciidoctor());
|
||||
|
||||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
@@ -47,7 +47,7 @@ public class LinksSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void ignoredLink() throws IOException {
|
||||
this.snippet.expectLinks().withContents(
|
||||
this.snippets.expectLinks().withContents(
|
||||
tableWithHeader("Relation", "Description").row("`b`", "Link b"));
|
||||
new LinksSnippet(
|
||||
new StubLinkExtractor().withLinks(new Link("a", "alpha"),
|
||||
@@ -59,7 +59,7 @@ public class LinksSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void allUndocumentedLinksCanBeIgnored() throws IOException {
|
||||
this.snippet.expectLinks().withContents(
|
||||
this.snippets.expectLinks().withContents(
|
||||
tableWithHeader("Relation", "Description").row("`b`", "Link b"));
|
||||
new LinksSnippet(
|
||||
new StubLinkExtractor().withLinks(new Link("a", "alpha"),
|
||||
@@ -70,7 +70,7 @@ public class LinksSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void presentOptionalLink() throws IOException {
|
||||
this.snippet.expectLinks().withContents(
|
||||
this.snippets.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()))
|
||||
@@ -79,7 +79,7 @@ public class LinksSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void missingOptionalLink() throws IOException {
|
||||
this.snippet.expectLinks().withContents(
|
||||
this.snippets.expectLinks().withContents(
|
||||
tableWithHeader("Relation", "Description").row("`foo`", "bar"));
|
||||
new LinksSnippet(new StubLinkExtractor(),
|
||||
Arrays.asList(new LinkDescriptor("foo").description("bar").optional()))
|
||||
@@ -88,7 +88,7 @@ public class LinksSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void documentedLinks() throws IOException {
|
||||
this.snippet.expectLinks().withContents(tableWithHeader("Relation", "Description")
|
||||
this.snippets.expectLinks().withContents(tableWithHeader("Relation", "Description")
|
||||
.row("`a`", "one").row("`b`", "two"));
|
||||
new LinksSnippet(
|
||||
new StubLinkExtractor().withLinks(new Link("a", "alpha"),
|
||||
@@ -100,7 +100,7 @@ public class LinksSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void linkDescriptionFromTitleInPayload() throws IOException {
|
||||
this.snippet.expectLinks().withContents(tableWithHeader("Relation", "Description")
|
||||
this.snippets.expectLinks().withContents(tableWithHeader("Relation", "Description")
|
||||
.row("`a`", "one").row("`b`", "Link b"));
|
||||
new LinksSnippet(
|
||||
new StubLinkExtractor().withLinks(new Link("a", "alpha", "Link a"),
|
||||
@@ -114,7 +114,7 @@ public class LinksSnippetTests extends AbstractSnippetTests {
|
||||
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
|
||||
given(resolver.resolveTemplateResource("links"))
|
||||
.willReturn(snippetResource("links-with-title"));
|
||||
this.snippet.expectLinks().withContents(containsString("Title for the links"));
|
||||
this.snippets.expectLinks().withContents(containsString("Title for the links"));
|
||||
|
||||
new LinksSnippet(
|
||||
new StubLinkExtractor().withLinks(new Link("a", "alpha"),
|
||||
@@ -134,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()
|
||||
this.snippets.expectLinks()
|
||||
.withContents(tableWithHeader("Relation", "Description", "Foo")
|
||||
.row("a", "one", "alpha").row("b", "two", "bravo"));
|
||||
|
||||
@@ -154,7 +154,7 @@ public class LinksSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void additionalDescriptors() throws IOException {
|
||||
this.snippet.expectLinks().withContents(tableWithHeader("Relation", "Description")
|
||||
this.snippets.expectLinks().withContents(tableWithHeader("Relation", "Description")
|
||||
.row("`a`", "one").row("`b`", "two"));
|
||||
HypermediaDocumentation
|
||||
.links(new StubLinkExtractor().withLinks(new Link("a", "alpha"),
|
||||
@@ -166,7 +166,7 @@ public class LinksSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void tableCellContentIsEscapedWhenNecessary() throws IOException {
|
||||
this.snippet.expectLinks().withContents(tableWithHeader("Relation", "Description")
|
||||
this.snippets.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")))
|
||||
|
||||
@@ -26,7 +26,7 @@ import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.restdocs.templates.TemplateEngine;
|
||||
import org.springframework.restdocs.templates.TemplateResourceResolver;
|
||||
import org.springframework.restdocs.templates.mustache.MustacheTemplateEngine;
|
||||
import org.springframework.restdocs.test.ExpectedSnippet;
|
||||
import org.springframework.restdocs.test.ExpectedSnippets;
|
||||
import org.springframework.restdocs.test.OperationBuilder;
|
||||
|
||||
import static org.mockito.BDDMockito.given;
|
||||
@@ -46,14 +46,14 @@ public class AsciidoctorRequestFieldsSnippetTests {
|
||||
public OperationBuilder operationBuilder = new OperationBuilder(asciidoctor());
|
||||
|
||||
@Rule
|
||||
public ExpectedSnippet snippet = new ExpectedSnippet(asciidoctor());
|
||||
public ExpectedSnippets snippets = new ExpectedSnippets(asciidoctor());
|
||||
|
||||
@Test
|
||||
public void requestFieldsWithListDescription() throws IOException {
|
||||
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
|
||||
given(resolver.resolveTemplateResource("request-fields"))
|
||||
.willReturn(snippetResource("request-fields-with-list-description"));
|
||||
this.snippet.expectRequestFields().withContents(
|
||||
this.snippets.expectRequestFields().withContents(
|
||||
tableWithHeader(asciidoctor(), "Path", "Type", "Description")
|
||||
//
|
||||
.row("a", "String", String.format(" - one%n - two"))
|
||||
|
||||
@@ -28,7 +28,7 @@ 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.ExpectedSnippets;
|
||||
import org.springframework.restdocs.test.OperationBuilder;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.endsWith;
|
||||
@@ -45,7 +45,8 @@ import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWit
|
||||
public class RequestFieldsSnippetFailureTests {
|
||||
|
||||
@Rule
|
||||
public ExpectedSnippet snippet = new ExpectedSnippet(TemplateFormats.asciidoctor());
|
||||
public ExpectedSnippets snippets = new ExpectedSnippets(
|
||||
TemplateFormats.asciidoctor());
|
||||
|
||||
@Rule
|
||||
public OperationBuilder operationBuilder = new OperationBuilder(
|
||||
|
||||
@@ -50,7 +50,7 @@ public class RequestFieldsSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void mapRequestWithFields() throws IOException {
|
||||
this.snippet.expectRequestFields()
|
||||
this.snippets.expectRequestFields()
|
||||
.withContents(tableWithHeader("Path", "Type", "Description")
|
||||
.row("`a.b`", "`Number`", "one").row("`a.c`", "`String`", "two")
|
||||
.row("`a`", "`Object`", "three"));
|
||||
@@ -65,7 +65,7 @@ public class RequestFieldsSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void arrayRequestWithFields() throws IOException {
|
||||
this.snippet.expectRequestFields()
|
||||
this.snippets.expectRequestFields()
|
||||
.withContents(tableWithHeader("Path", "Type", "Description")
|
||||
.row("`[]`", "`Array`", "one").row("`[]a.b`", "`Number`", "two")
|
||||
.row("`[]a.c`", "`String`", "three")
|
||||
@@ -83,7 +83,7 @@ public class RequestFieldsSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void ignoredRequestField() throws IOException {
|
||||
this.snippet.expectRequestFields()
|
||||
this.snippets.expectRequestFields()
|
||||
.withContents(tableWithHeader("Path", "Type", "Description").row("`b`",
|
||||
"`Number`", "Field b"));
|
||||
|
||||
@@ -95,7 +95,7 @@ public class RequestFieldsSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void allUndocumentedRequestFieldsCanBeIgnored() throws IOException {
|
||||
this.snippet.expectRequestFields()
|
||||
this.snippets.expectRequestFields()
|
||||
.withContents(tableWithHeader("Path", "Type", "Description").row("`b`",
|
||||
"`Number`", "Field b"));
|
||||
new RequestFieldsSnippet(Arrays.asList(fieldWithPath("b").description("Field b")),
|
||||
@@ -106,7 +106,7 @@ public class RequestFieldsSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void missingOptionalRequestField() throws IOException {
|
||||
this.snippet.expectRequestFields()
|
||||
this.snippets.expectRequestFields()
|
||||
.withContents(tableWithHeader("Path", "Type", "Description").row("`a.b`",
|
||||
"`String`", "one"));
|
||||
new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a.b").description("one")
|
||||
@@ -118,7 +118,7 @@ public class RequestFieldsSnippetTests extends AbstractSnippetTests {
|
||||
@Test
|
||||
public void missingIgnoredOptionalRequestFieldDoesNotRequireAType()
|
||||
throws IOException {
|
||||
this.snippet.expectRequestFields()
|
||||
this.snippets.expectRequestFields()
|
||||
.withContents(tableWithHeader("Path", "Type", "Description"));
|
||||
new RequestFieldsSnippet(Arrays
|
||||
.asList(fieldWithPath("a.b").description("one").ignored().optional()))
|
||||
@@ -128,7 +128,7 @@ public class RequestFieldsSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void presentOptionalRequestField() throws IOException {
|
||||
this.snippet.expectRequestFields()
|
||||
this.snippets.expectRequestFields()
|
||||
.withContents(tableWithHeader("Path", "Type", "Description").row("`a.b`",
|
||||
"`String`", "one"));
|
||||
new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a.b").description("one")
|
||||
@@ -142,7 +142,7 @@ public class RequestFieldsSnippetTests extends AbstractSnippetTests {
|
||||
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
|
||||
given(resolver.resolveTemplateResource("request-fields"))
|
||||
.willReturn(snippetResource("request-fields-with-title"));
|
||||
this.snippet.expectRequestFields().withContents(containsString("Custom title"));
|
||||
this.snippets.expectRequestFields().withContents(containsString("Custom title"));
|
||||
|
||||
new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a").description("one")),
|
||||
attributes(
|
||||
@@ -161,7 +161,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()
|
||||
this.snippets.expectRequestFields()
|
||||
.withContents(tableWithHeader("Path", "Type", "Description", "Foo")
|
||||
.row("a.b", "Number", "one", "alpha")
|
||||
.row("a.c", "String", "two", "bravo")
|
||||
@@ -187,7 +187,7 @@ public class RequestFieldsSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void fieldWithExplictExactlyMatchingType() throws IOException {
|
||||
this.snippet.expectRequestFields()
|
||||
this.snippets.expectRequestFields()
|
||||
.withContents(tableWithHeader("Path", "Type", "Description").row("`a`",
|
||||
"`Number`", "one"));
|
||||
|
||||
@@ -199,7 +199,7 @@ public class RequestFieldsSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void fieldWithExplictVariesType() throws IOException {
|
||||
this.snippet.expectRequestFields()
|
||||
this.snippets.expectRequestFields()
|
||||
.withContents(tableWithHeader("Path", "Type", "Description").row("`a`",
|
||||
"`Varies`", "one"));
|
||||
|
||||
@@ -211,7 +211,7 @@ public class RequestFieldsSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void xmlRequestFields() throws IOException {
|
||||
this.snippet.expectRequestFields()
|
||||
this.snippets.expectRequestFields()
|
||||
.withContents(tableWithHeader("Path", "Type", "Description")
|
||||
.row("`a/b`", "`b`", "one").row("`a/c`", "`c`", "two").row("`a`",
|
||||
"`a`", "three"));
|
||||
@@ -230,7 +230,7 @@ public class RequestFieldsSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void additionalDescriptors() throws IOException {
|
||||
this.snippet.expectRequestFields()
|
||||
this.snippets.expectRequestFields()
|
||||
.withContents(tableWithHeader("Path", "Type", "Description")
|
||||
.row("`a.b`", "`Number`", "one").row("`a.c`", "`String`", "two")
|
||||
.row("`a`", "`Object`", "three"));
|
||||
@@ -245,7 +245,7 @@ public class RequestFieldsSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void prefixedAdditionalDescriptors() throws IOException {
|
||||
this.snippet.expectRequestFields()
|
||||
this.snippets.expectRequestFields()
|
||||
.withContents(tableWithHeader("Path", "Type", "Description")
|
||||
.row("`a`", "`Object`", "one").row("`a.b`", "`Number`", "two")
|
||||
.row("`a.c`", "`String`", "three"));
|
||||
@@ -259,7 +259,7 @@ public class RequestFieldsSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void requestWithFieldsWithEscapedContent() throws IOException {
|
||||
this.snippet.expectRequestFields()
|
||||
this.snippets.expectRequestFields()
|
||||
.withContents(tableWithHeader("Path", "Type", "Description").row(
|
||||
escapeIfNecessary("`Foo|Bar`"), escapeIfNecessary("`one|two`"),
|
||||
escapeIfNecessary("three|four")));
|
||||
|
||||
@@ -27,7 +27,7 @@ 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.test.ExpectedSnippet;
|
||||
import org.springframework.restdocs.test.ExpectedSnippets;
|
||||
import org.springframework.restdocs.test.OperationBuilder;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.endsWith;
|
||||
@@ -48,7 +48,7 @@ public class ResponseFieldsSnippetFailureTests {
|
||||
public OperationBuilder operationBuilder = new OperationBuilder(asciidoctor());
|
||||
|
||||
@Rule
|
||||
public ExpectedSnippet snippet = new ExpectedSnippet(asciidoctor());
|
||||
public ExpectedSnippets snippets = new ExpectedSnippets(asciidoctor());
|
||||
|
||||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
@@ -50,7 +50,7 @@ public class ResponseFieldsSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void mapResponseWithFields() throws IOException {
|
||||
this.snippet.expectResponseFields()
|
||||
this.snippets.expectResponseFields()
|
||||
.withContents(tableWithHeader("Path", "Type", "Description")
|
||||
.row("`id`", "`Number`", "one").row("`date`", "`String`", "two")
|
||||
.row("`assets`", "`Array`", "three")
|
||||
@@ -72,7 +72,7 @@ public class ResponseFieldsSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void arrayResponseWithFields() throws IOException {
|
||||
this.snippet.expectResponseFields()
|
||||
this.snippets.expectResponseFields()
|
||||
.withContents(tableWithHeader("Path", "Type", "Description")
|
||||
.row("`[]a.b`", "`Number`", "one")
|
||||
.row("`[]a.c`", "`String`", "two")
|
||||
@@ -88,7 +88,7 @@ public class ResponseFieldsSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void arrayResponse() throws IOException {
|
||||
this.snippet.expectResponseFields()
|
||||
this.snippets.expectResponseFields()
|
||||
.withContents(tableWithHeader("Path", "Type", "Description").row("`[]`",
|
||||
"`Array`", "one"));
|
||||
new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("[]").description("one")))
|
||||
@@ -98,7 +98,7 @@ public class ResponseFieldsSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void ignoredResponseField() throws IOException {
|
||||
this.snippet.expectResponseFields()
|
||||
this.snippets.expectResponseFields()
|
||||
.withContents(tableWithHeader("Path", "Type", "Description").row("`b`",
|
||||
"`Number`", "Field b"));
|
||||
|
||||
@@ -110,7 +110,7 @@ public class ResponseFieldsSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void allUndocumentedFieldsCanBeIgnored() throws IOException {
|
||||
this.snippet.expectResponseFields()
|
||||
this.snippets.expectResponseFields()
|
||||
.withContents(tableWithHeader("Path", "Type", "Description").row("`b`",
|
||||
"`Number`", "Field b"));
|
||||
|
||||
@@ -125,7 +125,7 @@ public class ResponseFieldsSnippetTests extends AbstractSnippetTests {
|
||||
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
|
||||
given(resolver.resolveTemplateResource("response-fields"))
|
||||
.willReturn(snippetResource("response-fields-with-title"));
|
||||
this.snippet.expectResponseFields().withContents(containsString("Custom title"));
|
||||
this.snippets.expectResponseFields().withContents(containsString("Custom title"));
|
||||
|
||||
new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("a").description("one")),
|
||||
attributes(
|
||||
@@ -141,7 +141,7 @@ public class ResponseFieldsSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void missingOptionalResponseField() throws IOException {
|
||||
this.snippet.expectResponseFields()
|
||||
this.snippets.expectResponseFields()
|
||||
.withContents(tableWithHeader("Path", "Type", "Description").row("`a.b`",
|
||||
"`String`", "one"));
|
||||
new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("a.b").description("one")
|
||||
@@ -152,7 +152,7 @@ public class ResponseFieldsSnippetTests extends AbstractSnippetTests {
|
||||
@Test
|
||||
public void missingIgnoredOptionalResponseFieldDoesNotRequireAType()
|
||||
throws IOException {
|
||||
this.snippet.expectResponseFields()
|
||||
this.snippets.expectResponseFields()
|
||||
.withContents(tableWithHeader("Path", "Type", "Description"));
|
||||
new ResponseFieldsSnippet(Arrays
|
||||
.asList(fieldWithPath("a.b").description("one").ignored().optional()))
|
||||
@@ -161,7 +161,7 @@ public class ResponseFieldsSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void presentOptionalResponseField() throws IOException {
|
||||
this.snippet.expectResponseFields()
|
||||
this.snippets.expectResponseFields()
|
||||
.withContents(tableWithHeader("Path", "Type", "Description").row("`a.b`",
|
||||
"`String`", "one"));
|
||||
new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("a.b").description("one")
|
||||
@@ -175,7 +175,7 @@ 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()
|
||||
this.snippets.expectResponseFields()
|
||||
.withContents(tableWithHeader("Path", "Type", "Description", "Foo")
|
||||
.row("a.b", "Number", "one", "alpha")
|
||||
.row("a.c", "String", "two", "bravo")
|
||||
@@ -201,7 +201,7 @@ public class ResponseFieldsSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void fieldWithExplictExactlyMatchingType() throws IOException {
|
||||
this.snippet.expectResponseFields()
|
||||
this.snippets.expectResponseFields()
|
||||
.withContents(tableWithHeader("Path", "Type", "Description").row("`a`",
|
||||
"`Number`", "one"));
|
||||
|
||||
@@ -213,7 +213,7 @@ public class ResponseFieldsSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void fieldWithExplictVariesType() throws IOException {
|
||||
this.snippet.expectResponseFields()
|
||||
this.snippets.expectResponseFields()
|
||||
.withContents(tableWithHeader("Path", "Type", "Description").row("`a`",
|
||||
"`Varies`", "one"));
|
||||
|
||||
@@ -225,7 +225,7 @@ public class ResponseFieldsSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void xmlResponseFields() throws IOException {
|
||||
this.snippet.expectResponseFields()
|
||||
this.snippets.expectResponseFields()
|
||||
.withContents(tableWithHeader("Path", "Type", "Description")
|
||||
.row("`a/b`", "`b`", "one").row("`a/c`", "`c`", "two").row("`a`",
|
||||
"`a`", "three"));
|
||||
@@ -243,7 +243,7 @@ public class ResponseFieldsSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void xmlAttribute() throws IOException {
|
||||
this.snippet.expectResponseFields()
|
||||
this.snippets.expectResponseFields()
|
||||
.withContents(tableWithHeader("Path", "Type", "Description")
|
||||
.row("`a`", "`b`", "one").row("`a/@id`", "`c`", "two"));
|
||||
new ResponseFieldsSnippet(
|
||||
@@ -259,7 +259,7 @@ public class ResponseFieldsSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void missingOptionalXmlAttribute() throws IOException {
|
||||
this.snippet.expectResponseFields()
|
||||
this.snippets.expectResponseFields()
|
||||
.withContents(tableWithHeader("Path", "Type", "Description")
|
||||
.row("`a`", "`b`", "one").row("`a/@id`", "`c`", "two"));
|
||||
new ResponseFieldsSnippet(
|
||||
@@ -275,7 +275,7 @@ public class ResponseFieldsSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void undocumentedAttributeDoesNotCauseFailure() throws IOException {
|
||||
this.snippet.expectResponseFields().withContents(
|
||||
this.snippets.expectResponseFields().withContents(
|
||||
tableWithHeader("Path", "Type", "Description").row("`a`", "`a`", "one"));
|
||||
new ResponseFieldsSnippet(
|
||||
Arrays.asList(fieldWithPath("a").description("one").type("a"))).document(
|
||||
@@ -287,7 +287,7 @@ public class ResponseFieldsSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void additionalDescriptors() throws IOException {
|
||||
this.snippet.expectResponseFields()
|
||||
this.snippets.expectResponseFields()
|
||||
.withContents(tableWithHeader("Path", "Type", "Description")
|
||||
.row("`id`", "`Number`", "one").row("`date`", "`String`", "two")
|
||||
.row("`assets`", "`Array`", "three")
|
||||
@@ -309,7 +309,7 @@ public class ResponseFieldsSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void prefixedAdditionalDescriptors() throws IOException {
|
||||
this.snippet.expectResponseFields()
|
||||
this.snippets.expectResponseFields()
|
||||
.withContents(tableWithHeader("Path", "Type", "Description")
|
||||
.row("`a`", "`Object`", "one").row("`a.b`", "`Number`", "two")
|
||||
.row("`a.c`", "`String`", "three"));
|
||||
@@ -323,7 +323,7 @@ public class ResponseFieldsSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void responseWithFieldsWithEscapedContent() throws IOException {
|
||||
this.snippet.expectResponseFields()
|
||||
this.snippets.expectResponseFields()
|
||||
.withContents(tableWithHeader("Path", "Type", "Description").row(
|
||||
escapeIfNecessary("`Foo|Bar`"), escapeIfNecessary("`one|two`"),
|
||||
escapeIfNecessary("three|four")));
|
||||
|
||||
@@ -26,7 +26,7 @@ import org.junit.rules.ExpectedException;
|
||||
|
||||
import org.springframework.restdocs.generate.RestDocumentationGenerator;
|
||||
import org.springframework.restdocs.snippet.SnippetException;
|
||||
import org.springframework.restdocs.test.ExpectedSnippet;
|
||||
import org.springframework.restdocs.test.ExpectedSnippets;
|
||||
import org.springframework.restdocs.test.OperationBuilder;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
@@ -45,7 +45,7 @@ public class PathParametersSnippetFailureTests {
|
||||
public OperationBuilder operationBuilder = new OperationBuilder(asciidoctor());
|
||||
|
||||
@Rule
|
||||
public ExpectedSnippet snippet = new ExpectedSnippet(asciidoctor());
|
||||
public ExpectedSnippets snippets = new ExpectedSnippets(asciidoctor());
|
||||
|
||||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
@@ -49,7 +49,7 @@ public class PathParametersSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void pathParameters() throws IOException {
|
||||
this.snippet.expectPathParameters().withContents(
|
||||
this.snippets.expectPathParameters().withContents(
|
||||
tableWithTitleAndHeader(getTitle(), "Parameter", "Description")
|
||||
.row("`a`", "one").row("`b`", "two"));
|
||||
new PathParametersSnippet(Arrays.asList(parameterWithName("a").description("one"),
|
||||
@@ -63,7 +63,7 @@ public class PathParametersSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void ignoredPathParameter() throws IOException {
|
||||
this.snippet.expectPathParameters().withContents(
|
||||
this.snippets.expectPathParameters().withContents(
|
||||
tableWithTitleAndHeader(getTitle(), "Parameter", "Description").row("`b`",
|
||||
"two"));
|
||||
new PathParametersSnippet(Arrays.asList(parameterWithName("a").ignored(),
|
||||
@@ -77,7 +77,7 @@ public class PathParametersSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void allUndocumentedPathParametersCanBeIgnored() throws IOException {
|
||||
this.snippet.expectPathParameters().withContents(
|
||||
this.snippets.expectPathParameters().withContents(
|
||||
tableWithTitleAndHeader(getTitle(), "Parameter", "Description").row("`b`",
|
||||
"two"));
|
||||
new PathParametersSnippet(
|
||||
@@ -89,7 +89,7 @@ public class PathParametersSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void missingOptionalPathParameter() throws IOException {
|
||||
this.snippet.expectPathParameters().withContents(tableWithTitleAndHeader(
|
||||
this.snippets.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"),
|
||||
@@ -101,7 +101,7 @@ public class PathParametersSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void presentOptionalPathParameter() throws IOException {
|
||||
this.snippet.expectPathParameters().withContents(tableWithTitleAndHeader(
|
||||
this.snippets.expectPathParameters().withContents(tableWithTitleAndHeader(
|
||||
this.templateFormat == TemplateFormats.asciidoctor() ? "/{a}" : "`/{a}`",
|
||||
"Parameter", "Description").row("`a`", "one"));
|
||||
new PathParametersSnippet(
|
||||
@@ -113,7 +113,7 @@ public class PathParametersSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void pathParametersWithQueryString() throws IOException {
|
||||
this.snippet.expectPathParameters().withContents(
|
||||
this.snippets.expectPathParameters().withContents(
|
||||
tableWithTitleAndHeader(getTitle(), "Parameter", "Description")
|
||||
.row("`a`", "one").row("`b`", "two"));
|
||||
new PathParametersSnippet(Arrays.asList(parameterWithName("a").description("one"),
|
||||
@@ -127,7 +127,7 @@ public class PathParametersSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void pathParametersWithQueryStringWithParameters() throws IOException {
|
||||
this.snippet.expectPathParameters().withContents(
|
||||
this.snippets.expectPathParameters().withContents(
|
||||
tableWithTitleAndHeader(getTitle(), "Parameter", "Description")
|
||||
.row("`a`", "one").row("`b`", "two"));
|
||||
new PathParametersSnippet(Arrays.asList(parameterWithName("a").description("one"),
|
||||
@@ -144,7 +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().withContents(containsString("The title"));
|
||||
this.snippets.expectPathParameters().withContents(containsString("The title"));
|
||||
|
||||
new PathParametersSnippet(
|
||||
Arrays.asList(
|
||||
@@ -168,7 +168,7 @@ 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()
|
||||
this.snippets.expectPathParameters()
|
||||
.withContents(tableWithHeader("Parameter", "Description", "Foo")
|
||||
.row("a", "one", "alpha").row("b", "two", "bravo"));
|
||||
|
||||
@@ -188,7 +188,7 @@ public class PathParametersSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void additionalDescriptors() throws IOException {
|
||||
this.snippet.expectPathParameters().withContents(
|
||||
this.snippets.expectPathParameters().withContents(
|
||||
tableWithTitleAndHeader(getTitle(), "Parameter", "Description")
|
||||
.row("`a`", "one").row("`b`", "two"));
|
||||
RequestDocumentation.pathParameters(parameterWithName("a").description("one"))
|
||||
@@ -201,7 +201,7 @@ public class PathParametersSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void pathParametersWithEscapedContent() throws IOException {
|
||||
this.snippet.expectPathParameters()
|
||||
this.snippets.expectPathParameters()
|
||||
.withContents(tableWithTitleAndHeader(getTitle("{Foo|Bar}"), "Parameter",
|
||||
"Description").row(escapeIfNecessary("`Foo|Bar`"),
|
||||
escapeIfNecessary("one|two")));
|
||||
|
||||
@@ -25,7 +25,7 @@ import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import org.springframework.restdocs.snippet.SnippetException;
|
||||
import org.springframework.restdocs.test.ExpectedSnippet;
|
||||
import org.springframework.restdocs.test.ExpectedSnippets;
|
||||
import org.springframework.restdocs.test.OperationBuilder;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
@@ -44,7 +44,7 @@ public class RequestParametersSnippetFailureTests {
|
||||
public OperationBuilder operationBuilder = new OperationBuilder(asciidoctor());
|
||||
|
||||
@Rule
|
||||
public ExpectedSnippet snippet = new ExpectedSnippet(asciidoctor());
|
||||
public ExpectedSnippets snippets = new ExpectedSnippets(asciidoctor());
|
||||
|
||||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
@@ -48,7 +48,7 @@ public class RequestParametersSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void requestParameters() throws IOException {
|
||||
this.snippet.expectRequestParameters()
|
||||
this.snippets.expectRequestParameters()
|
||||
.withContents(tableWithHeader("Parameter", "Description")
|
||||
.row("`a`", "one").row("`b`", "two"));
|
||||
new RequestParametersSnippet(
|
||||
@@ -60,7 +60,7 @@ public class RequestParametersSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void requestParameterWithNoValue() throws IOException {
|
||||
this.snippet.expectRequestParameters().withContents(
|
||||
this.snippets.expectRequestParameters().withContents(
|
||||
tableWithHeader("Parameter", "Description").row("`a`", "one"));
|
||||
new RequestParametersSnippet(
|
||||
Arrays.asList(parameterWithName("a").description("one")))
|
||||
@@ -70,7 +70,7 @@ public class RequestParametersSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void ignoredRequestParameter() throws IOException {
|
||||
this.snippet.expectRequestParameters().withContents(
|
||||
this.snippets.expectRequestParameters().withContents(
|
||||
tableWithHeader("Parameter", "Description").row("`b`", "two"));
|
||||
new RequestParametersSnippet(Arrays.asList(parameterWithName("a").ignored(),
|
||||
parameterWithName("b").description("two")))
|
||||
@@ -80,7 +80,7 @@ public class RequestParametersSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void allUndocumentedRequestParametersCanBeIgnored() throws IOException {
|
||||
this.snippet.expectRequestParameters().withContents(
|
||||
this.snippets.expectRequestParameters().withContents(
|
||||
tableWithHeader("Parameter", "Description").row("`b`", "two"));
|
||||
new RequestParametersSnippet(
|
||||
Arrays.asList(parameterWithName("b").description("two")), true)
|
||||
@@ -90,7 +90,7 @@ public class RequestParametersSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void missingOptionalRequestParameter() throws IOException {
|
||||
this.snippet.expectRequestParameters()
|
||||
this.snippets.expectRequestParameters()
|
||||
.withContents(tableWithHeader("Parameter", "Description")
|
||||
.row("`a`", "one").row("`b`", "two"));
|
||||
new RequestParametersSnippet(
|
||||
@@ -102,7 +102,7 @@ public class RequestParametersSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void presentOptionalRequestParameter() throws IOException {
|
||||
this.snippet.expectRequestParameters().withContents(
|
||||
this.snippets.expectRequestParameters().withContents(
|
||||
tableWithHeader("Parameter", "Description").row("`a`", "one"));
|
||||
new RequestParametersSnippet(
|
||||
Arrays.asList(parameterWithName("a").description("one").optional()))
|
||||
@@ -115,7 +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().withContents(containsString("The title"));
|
||||
this.snippets.expectRequestParameters().withContents(containsString("The title"));
|
||||
|
||||
new RequestParametersSnippet(
|
||||
Arrays.asList(
|
||||
@@ -140,7 +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()
|
||||
this.snippets.expectRequestParameters()
|
||||
.withContents(tableWithHeader("Parameter", "Description", "Foo")
|
||||
.row("a", "one", "alpha").row("b", "two", "bravo"));
|
||||
|
||||
@@ -164,7 +164,7 @@ 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()
|
||||
this.snippets.expectRequestParameters()
|
||||
.withContents(tableWithHeader("Parameter", "Optional", "Description")
|
||||
.row("a", "true", "one").row("b", "false", "two"));
|
||||
|
||||
@@ -183,7 +183,7 @@ public class RequestParametersSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void additionalDescriptors() throws IOException {
|
||||
this.snippet.expectRequestParameters()
|
||||
this.snippets.expectRequestParameters()
|
||||
.withContents(tableWithHeader("Parameter", "Description")
|
||||
.row("`a`", "one").row("`b`", "two"));
|
||||
RequestDocumentation.requestParameters(parameterWithName("a").description("one"))
|
||||
@@ -194,7 +194,7 @@ public class RequestParametersSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void requestParametersWithEscapedContent() throws IOException {
|
||||
this.snippet.expectRequestParameters()
|
||||
this.snippets.expectRequestParameters()
|
||||
.withContents(tableWithHeader("Parameter", "Description").row(
|
||||
escapeIfNecessary("`Foo|Bar`"), escapeIfNecessary("one|two")));
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import org.springframework.restdocs.snippet.SnippetException;
|
||||
import org.springframework.restdocs.test.ExpectedSnippet;
|
||||
import org.springframework.restdocs.test.ExpectedSnippets;
|
||||
import org.springframework.restdocs.test.OperationBuilder;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
@@ -44,7 +44,7 @@ public class RequestPartsSnippetFailureTests {
|
||||
public OperationBuilder operationBuilder = new OperationBuilder(asciidoctor());
|
||||
|
||||
@Rule
|
||||
public ExpectedSnippet snippet = new ExpectedSnippet(asciidoctor());
|
||||
public ExpectedSnippets snippets = new ExpectedSnippets(asciidoctor());
|
||||
|
||||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
@@ -48,7 +48,7 @@ public class RequestPartsSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void requestParts() throws IOException {
|
||||
this.snippet.expectRequestParts()
|
||||
this.snippets.expectRequestParts()
|
||||
.withContents(tableWithHeader("Part", "Description").row("`a`", "one")
|
||||
.row("`b`", "two"));
|
||||
new RequestPartsSnippet(Arrays.asList(partWithName("a").description("one"),
|
||||
@@ -60,7 +60,7 @@ public class RequestPartsSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void ignoredRequestPart() throws IOException {
|
||||
this.snippet.expectRequestParts()
|
||||
this.snippets.expectRequestParts()
|
||||
.withContents(tableWithHeader("Part", "Description").row("`b`", "two"));
|
||||
new RequestPartsSnippet(Arrays.asList(partWithName("a").ignored(),
|
||||
partWithName("b").description("two")))
|
||||
@@ -71,7 +71,7 @@ public class RequestPartsSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void allUndocumentedRequestPartsCanBeIgnored() throws IOException {
|
||||
this.snippet.expectRequestParts()
|
||||
this.snippets.expectRequestParts()
|
||||
.withContents(tableWithHeader("Part", "Description").row("`b`", "two"));
|
||||
new RequestPartsSnippet(Arrays.asList(partWithName("b").description("two")), true)
|
||||
.document(this.operationBuilder.request("http://localhost")
|
||||
@@ -81,7 +81,7 @@ public class RequestPartsSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void missingOptionalRequestPart() throws IOException {
|
||||
this.snippet.expectRequestParts()
|
||||
this.snippets.expectRequestParts()
|
||||
.withContents(tableWithHeader("Part", "Description").row("`a`", "one")
|
||||
.row("`b`", "two"));
|
||||
new RequestPartsSnippet(
|
||||
@@ -93,7 +93,7 @@ public class RequestPartsSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void presentOptionalRequestPart() throws IOException {
|
||||
this.snippet.expectRequestParts()
|
||||
this.snippets.expectRequestParts()
|
||||
.withContents(tableWithHeader("Part", "Description").row("`a`", "one"));
|
||||
new RequestPartsSnippet(
|
||||
Arrays.asList(partWithName("a").description("one").optional()))
|
||||
@@ -106,7 +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().withContents(containsString("The title"));
|
||||
this.snippets.expectRequestParts().withContents(containsString("The title"));
|
||||
|
||||
new RequestPartsSnippet(
|
||||
Arrays.asList(
|
||||
@@ -131,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()
|
||||
this.snippets.expectRequestParts()
|
||||
.withContents(tableWithHeader("Part", "Description", "Foo")
|
||||
.row("a", "one", "alpha").row("b", "two", "bravo"));
|
||||
|
||||
@@ -155,7 +155,7 @@ 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()
|
||||
this.snippets.expectRequestParts()
|
||||
.withContents(tableWithHeader("Part", "Optional", "Description")
|
||||
.row("a", "true", "one").row("b", "false", "two"));
|
||||
|
||||
@@ -174,7 +174,7 @@ public class RequestPartsSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void additionalDescriptors() throws IOException {
|
||||
this.snippet.expectRequestParts()
|
||||
this.snippets.expectRequestParts()
|
||||
.withContents(tableWithHeader("Part", "Description").row("`a`", "one")
|
||||
.row("`b`", "two"));
|
||||
RequestDocumentation.requestParts(partWithName("a").description("one"))
|
||||
@@ -186,7 +186,7 @@ public class RequestPartsSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void requestPartsWithEscapedContent() throws IOException {
|
||||
this.snippet.expectRequestParts().withContents(
|
||||
this.snippets.expectRequestParts().withContents(
|
||||
tableWithHeader("Part", "Description").row(escapeIfNecessary("`Foo|Bar`"),
|
||||
escapeIfNecessary("one|two")));
|
||||
|
||||
|
||||
@@ -16,13 +16,18 @@
|
||||
|
||||
package org.springframework.restdocs.snippet;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.restdocs.operation.Operation;
|
||||
import org.springframework.restdocs.templates.TemplateFormats;
|
||||
import org.springframework.restdocs.test.ExpectedSnippets;
|
||||
import org.springframework.restdocs.test.OperationBuilder;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
@@ -38,6 +43,13 @@ import static org.junit.Assert.assertThat;
|
||||
*/
|
||||
public class TemplatedSnippetTests {
|
||||
|
||||
@Rule
|
||||
public OperationBuilder operationBuilder = new OperationBuilder(
|
||||
TemplateFormats.asciidoctor());
|
||||
|
||||
@Rule
|
||||
public ExpectedSnippets snippets = new ExpectedSnippets(TemplateFormats.asciidoctor());
|
||||
|
||||
@Test
|
||||
public void attributesAreCopied() {
|
||||
Map<String, Object> attributes = new HashMap<>();
|
||||
@@ -60,15 +72,30 @@ public class TemplatedSnippetTests {
|
||||
.getSnippetName(), is(equalTo("test")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multipleSnippetsCanBeProducedFromTheSameTemplate() throws IOException {
|
||||
this.snippets.expect("multiple-snippets-one");
|
||||
this.snippets.expect("multiple-snippets-two");
|
||||
new TestTemplatedSnippet("one", "multiple-snippets")
|
||||
.document(this.operationBuilder.build());
|
||||
new TestTemplatedSnippet("two", "multiple-snippets")
|
||||
.document(this.operationBuilder.build());
|
||||
}
|
||||
|
||||
private static class TestTemplatedSnippet extends TemplatedSnippet {
|
||||
|
||||
protected TestTemplatedSnippet(String snippetName, String templateName) {
|
||||
super(templateName + "-" + snippetName, templateName,
|
||||
Collections.<String, Object>emptyMap());
|
||||
}
|
||||
|
||||
protected TestTemplatedSnippet(Map<String, Object> attributes) {
|
||||
super("test", attributes);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Map<String, Object> createModel(Operation operation) {
|
||||
return null;
|
||||
return new HashMap<>();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,162 +0,0 @@
|
||||
/*
|
||||
* Copyright 2014-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.restdocs.test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.hamcrest.Matcher;
|
||||
import org.junit.runners.model.Statement;
|
||||
|
||||
import org.springframework.restdocs.snippet.TemplatedSnippet;
|
||||
import org.springframework.restdocs.templates.TemplateFormat;
|
||||
import org.springframework.restdocs.test.SnippetMatchers.SnippetMatcher;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* The {@code ExpectedSnippet} rule is used to verify that a {@link TemplatedSnippet} has
|
||||
* generated the expected snippet.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Andreas Evers
|
||||
*/
|
||||
public class ExpectedSnippet extends OperationTestRule {
|
||||
|
||||
private final TemplateFormat templateFormat;
|
||||
|
||||
private final SnippetMatcher snippet;
|
||||
|
||||
private String expectedName;
|
||||
|
||||
private String expectedType;
|
||||
|
||||
private File outputDirectory;
|
||||
|
||||
public ExpectedSnippet(TemplateFormat templateFormat) {
|
||||
this.templateFormat = templateFormat;
|
||||
this.snippet = SnippetMatchers.snippet(templateFormat);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Statement apply(final Statement base, File outputDirectory,
|
||||
String operationName) {
|
||||
this.outputDirectory = outputDirectory;
|
||||
this.expectedName = operationName;
|
||||
return new ExpectedSnippetStatement(base);
|
||||
}
|
||||
|
||||
private void verifySnippet() throws IOException {
|
||||
if (this.outputDirectory != null && this.expectedName != null) {
|
||||
File snippetDir = new File(this.outputDirectory, this.expectedName);
|
||||
File snippetFile = new File(snippetDir,
|
||||
this.expectedType + "." + this.templateFormat.getFileExtension());
|
||||
assertThat(snippetFile, is(this.snippet));
|
||||
}
|
||||
}
|
||||
|
||||
public ExpectedSnippet expectCurlRequest() {
|
||||
expect("curl-request");
|
||||
return this;
|
||||
}
|
||||
|
||||
public ExpectedSnippet expectHttpieRequest() {
|
||||
expect("httpie-request");
|
||||
return this;
|
||||
}
|
||||
|
||||
public ExpectedSnippet expectRequestFields() {
|
||||
expect("request-fields");
|
||||
return this;
|
||||
}
|
||||
|
||||
public ExpectedSnippet expectResponseFields() {
|
||||
expect("response-fields");
|
||||
return this;
|
||||
}
|
||||
|
||||
public ExpectedSnippet expectRequestHeaders() {
|
||||
expect("request-headers");
|
||||
return this;
|
||||
}
|
||||
|
||||
public ExpectedSnippet expectResponseHeaders() {
|
||||
expect("response-headers");
|
||||
return this;
|
||||
}
|
||||
|
||||
public ExpectedSnippet expectLinks() {
|
||||
expect("links");
|
||||
return this;
|
||||
}
|
||||
|
||||
public ExpectedSnippet expectHttpRequest() {
|
||||
expect("http-request");
|
||||
return this;
|
||||
}
|
||||
|
||||
public ExpectedSnippet expectHttpResponse() {
|
||||
expect("http-response");
|
||||
return this;
|
||||
}
|
||||
|
||||
public ExpectedSnippet expectRequestParameters() {
|
||||
expect("request-parameters");
|
||||
return this;
|
||||
}
|
||||
|
||||
public ExpectedSnippet expectPathParameters() {
|
||||
expect("path-parameters");
|
||||
return this;
|
||||
}
|
||||
|
||||
public ExpectedSnippet expectRequestParts() {
|
||||
expect("request-parts");
|
||||
return this;
|
||||
}
|
||||
|
||||
private ExpectedSnippet expect(String type) {
|
||||
this.expectedType = type;
|
||||
return this;
|
||||
}
|
||||
|
||||
public void withContents(Matcher<String> matcher) {
|
||||
this.snippet.withContents(matcher);
|
||||
}
|
||||
|
||||
public File getOutputDirectory() {
|
||||
return this.outputDirectory;
|
||||
}
|
||||
|
||||
private final class ExpectedSnippetStatement extends Statement {
|
||||
|
||||
private final Statement delegate;
|
||||
|
||||
private ExpectedSnippetStatement(Statement delegate) {
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void evaluate() throws Throwable {
|
||||
this.delegate.evaluate();
|
||||
verifySnippet();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
/*
|
||||
* Copyright 2014-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.restdocs.test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.hamcrest.Matcher;
|
||||
import org.junit.runners.model.Statement;
|
||||
|
||||
import org.springframework.restdocs.snippet.TemplatedSnippet;
|
||||
import org.springframework.restdocs.templates.TemplateFormat;
|
||||
import org.springframework.restdocs.test.SnippetMatchers.SnippetMatcher;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* The {@code ExpectedSnippets} rule is used to verify that a test of a
|
||||
* {@link TemplatedSnippet} has generated the expected snippets.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Andreas Evers
|
||||
*/
|
||||
public class ExpectedSnippets extends OperationTestRule {
|
||||
|
||||
private final TemplateFormat templateFormat;
|
||||
|
||||
private String operationName;
|
||||
|
||||
private File outputDirectory;
|
||||
|
||||
private List<ExpectedSnippet> expectations = new ArrayList<>();
|
||||
|
||||
public ExpectedSnippets(TemplateFormat templateFormat) {
|
||||
this.templateFormat = templateFormat;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Statement apply(final Statement base, File outputDirectory,
|
||||
String operationName) {
|
||||
this.outputDirectory = outputDirectory;
|
||||
this.operationName = operationName;
|
||||
return new ExpectedSnippetsStatement(base);
|
||||
}
|
||||
|
||||
private void verifySnippets() throws IOException {
|
||||
if (this.outputDirectory != null && this.operationName != null) {
|
||||
File snippetDir = new File(this.outputDirectory, this.operationName);
|
||||
for (ExpectedSnippet expectation : this.expectations) {
|
||||
expectation.verify(snippetDir);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public ExpectedSnippet expectCurlRequest() {
|
||||
return expect("curl-request");
|
||||
}
|
||||
|
||||
public ExpectedSnippet expectHttpieRequest() {
|
||||
return expect("httpie-request");
|
||||
}
|
||||
|
||||
public ExpectedSnippet expectRequestFields() {
|
||||
return expect("request-fields");
|
||||
}
|
||||
|
||||
public ExpectedSnippet expectResponseFields() {
|
||||
return expect("response-fields");
|
||||
}
|
||||
|
||||
public ExpectedSnippet expectRequestHeaders() {
|
||||
return expect("request-headers");
|
||||
}
|
||||
|
||||
public ExpectedSnippet expectResponseHeaders() {
|
||||
return expect("response-headers");
|
||||
}
|
||||
|
||||
public ExpectedSnippet expectLinks() {
|
||||
return expect("links");
|
||||
}
|
||||
|
||||
public ExpectedSnippet expectHttpRequest() {
|
||||
return expect("http-request");
|
||||
}
|
||||
|
||||
public ExpectedSnippet expectHttpResponse() {
|
||||
return expect("http-response");
|
||||
}
|
||||
|
||||
public ExpectedSnippet expectRequestParameters() {
|
||||
return expect("request-parameters");
|
||||
}
|
||||
|
||||
public ExpectedSnippet expectPathParameters() {
|
||||
return expect("path-parameters");
|
||||
}
|
||||
|
||||
public ExpectedSnippet expectRequestParts() {
|
||||
return expect("request-parts");
|
||||
}
|
||||
|
||||
public ExpectedSnippet expect(String type) {
|
||||
ExpectedSnippet expectedSnippet = new ExpectedSnippet(
|
||||
SnippetMatchers.snippet(this.templateFormat), type);
|
||||
this.expectations.add(expectedSnippet);
|
||||
return expectedSnippet;
|
||||
}
|
||||
|
||||
public File getOutputDirectory() {
|
||||
return this.outputDirectory;
|
||||
}
|
||||
|
||||
public String getOperationName() {
|
||||
return this.operationName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Expecations for a particular snippet.
|
||||
*/
|
||||
public final class ExpectedSnippet {
|
||||
|
||||
private final SnippetMatcher snippetMatcher;
|
||||
|
||||
private final String snippetName;
|
||||
|
||||
private ExpectedSnippet(SnippetMatcher snippetMatcher, String snippetName) {
|
||||
this.snippetMatcher = snippetMatcher;
|
||||
this.snippetName = snippetName;
|
||||
}
|
||||
|
||||
private void verify(File snippetDir) {
|
||||
File snippetFile = new File(snippetDir, this.snippetName + "."
|
||||
+ ExpectedSnippets.this.templateFormat.getFileExtension());
|
||||
assertThat(snippetFile, is(this.snippetMatcher));
|
||||
}
|
||||
|
||||
public void withContents(Matcher<String> matcher) {
|
||||
this.snippetMatcher.withContents(matcher);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private final class ExpectedSnippetsStatement extends Statement {
|
||||
|
||||
private final Statement delegate;
|
||||
|
||||
private ExpectedSnippetsStatement(Statement delegate) {
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void evaluate() throws Throwable {
|
||||
this.delegate.evaluate();
|
||||
verifySnippets();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user