Polish "Provide more control of formatting of curl and HTTPie commands"
See gh-348 Closes gh-260
This commit is contained in:
@@ -27,16 +27,17 @@ import org.springframework.restdocs.snippet.Snippet;
|
||||
* @author Andy Wilkinson
|
||||
* @author Paul-Christian Volkmer
|
||||
* @author Raman Gupta
|
||||
* @author Tomasz Kopczynski
|
||||
* @since 1.1.0
|
||||
*/
|
||||
public abstract class CliDocumentation {
|
||||
|
||||
static final CommandFormatter DEFAULT_COMMAND_FORMATTER = multiLineFormat();
|
||||
|
||||
private CliDocumentation() {
|
||||
|
||||
}
|
||||
|
||||
private static final CommandFormatter defaultCommandFormatter = multiLineFormat();
|
||||
|
||||
/**
|
||||
* Returns a new {@code Snippet} that will document the curl request for the API
|
||||
* operation.
|
||||
@@ -44,7 +45,7 @@ public abstract class CliDocumentation {
|
||||
* @return the snippet that will document the curl request
|
||||
*/
|
||||
public static Snippet curlRequest() {
|
||||
return curlRequest(defaultCommandFormatter);
|
||||
return curlRequest(DEFAULT_COMMAND_FORMATTER);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -56,15 +57,17 @@ public abstract class CliDocumentation {
|
||||
* @return the snippet that will document the curl request
|
||||
*/
|
||||
public static Snippet curlRequest(Map<String, Object> attributes) {
|
||||
return curlRequest(attributes, defaultCommandFormatter);
|
||||
return curlRequest(attributes, DEFAULT_COMMAND_FORMATTER);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new {@code Snippet} that will document the curl request for the API
|
||||
* operation. The given {@code commandFormatter} will be used for formatting the snippet.
|
||||
*
|
||||
* operation. The given {@code commandFormatter} will be used to format the curl
|
||||
* command in the snippet.
|
||||
*
|
||||
* @param commandFormatter the command formatter
|
||||
* @return the snippet that will document the curl request
|
||||
* @since 1.2.0
|
||||
*/
|
||||
public static Snippet curlRequest(CommandFormatter commandFormatter) {
|
||||
return curlRequest(null, commandFormatter);
|
||||
@@ -73,13 +76,16 @@ public abstract class CliDocumentation {
|
||||
/**
|
||||
* Returns a new {@code Snippet} that will document the curl request for the API
|
||||
* operation. The given {@code attributes} will be available during snippet
|
||||
* generation. The given {@code commandFormatter} will be used for formatting the snippet.
|
||||
* generation. The given {@code commandFormatter} will be used to format the curl
|
||||
* command in the snippet.
|
||||
*
|
||||
* @param attributes the attributes
|
||||
* @param commandFormatter the command formatter
|
||||
* @return the snippet that will document the curl request
|
||||
* @since 1.2.0
|
||||
*/
|
||||
public static Snippet curlRequest(Map<String, Object> attributes, CommandFormatter commandFormatter) {
|
||||
public static Snippet curlRequest(Map<String, Object> attributes,
|
||||
CommandFormatter commandFormatter) {
|
||||
return new CurlRequestSnippet(attributes, commandFormatter);
|
||||
}
|
||||
|
||||
@@ -90,7 +96,7 @@ public abstract class CliDocumentation {
|
||||
* @return the snippet that will document the HTTPie request
|
||||
*/
|
||||
public static Snippet httpieRequest() {
|
||||
return httpieRequest(defaultCommandFormatter);
|
||||
return httpieRequest(DEFAULT_COMMAND_FORMATTER);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -102,47 +108,54 @@ public abstract class CliDocumentation {
|
||||
* @return the snippet that will document the HTTPie request
|
||||
*/
|
||||
public static Snippet httpieRequest(Map<String, Object> attributes) {
|
||||
return httpieRequest(attributes, defaultCommandFormatter);
|
||||
return httpieRequest(attributes, DEFAULT_COMMAND_FORMATTER);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new {@code Snippet} that will document the HTTPie request for the API
|
||||
* operation. The given {@code commandFormatter} will be used for formatting the snippet.
|
||||
* operation. The given {@code commandFormatter} will be used to format the HTTPie
|
||||
* command in the snippet.
|
||||
*
|
||||
* @param commandFormatter the command formatter
|
||||
* @return the snippet that will document the HTTPie request
|
||||
* @since 1.2.0
|
||||
*/
|
||||
public static Snippet httpieRequest(CommandFormatter commandFormatter) {
|
||||
return httpieRequest(null, defaultCommandFormatter);
|
||||
return httpieRequest(null, commandFormatter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new {@code Snippet} that will document the HTTPie request for the API
|
||||
* operation. The given {@code attributes} will be available during snippet
|
||||
* generation. The given {@code commandFormatter} will be used for formatting the snippet.
|
||||
* generation. The given {@code commandFormatter} will be used to format the HTTPie
|
||||
* command in the snippet snippet.
|
||||
*
|
||||
* @param attributes the attributes
|
||||
* @param commandFormatter the command formatter
|
||||
* @return the snippet that will document the HTTPie request
|
||||
* @since 1.2.0
|
||||
*/
|
||||
public static Snippet httpieRequest(Map<String, Object> attributes, CommandFormatter commandFormatter) {
|
||||
public static Snippet httpieRequest(Map<String, Object> attributes,
|
||||
CommandFormatter commandFormatter) {
|
||||
return new HttpieRequestSnippet(attributes, commandFormatter);
|
||||
}
|
||||
/**
|
||||
* Creates a new {@code CommandFormatter} which formats input to a multi line output.
|
||||
*
|
||||
* @return A multi line {@code commandFormatter}
|
||||
*/
|
||||
public static CommandFormatter multiLineFormat() {
|
||||
return new ConcatenatingCommandFormatter(" \\%n ");
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@code CommandFormatter} which formats input to a single line output.
|
||||
* Creates a new {@code CommandFormatter} that produces multi-line output.
|
||||
*
|
||||
* @return A single line {@code CommandFormatter}
|
||||
* @return A multi-line {@code CommandFormatter}
|
||||
*/
|
||||
public static CommandFormatter multiLineFormat() {
|
||||
return new ConcatenatingCommandFormatter(" \\%n ");
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@code CommandFormatter} that produces single-line output.
|
||||
*
|
||||
* @return A single-line {@code CommandFormatter}
|
||||
*/
|
||||
public static CommandFormatter singleLineFormat() {
|
||||
return new ConcatenatingCommandFormatter(" ");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -19,19 +19,20 @@ package org.springframework.restdocs.cli;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Formatter for {@link CurlRequestSnippet} and {@link HttpieRequestSnippet}.
|
||||
* Its purpose is to format a command snippet from a list of its parts represented
|
||||
* as {@code String}s.
|
||||
* Formatter for CLI commands such as those included in {@link CurlRequestSnippet} and
|
||||
* {@link HttpieRequestSnippet}.
|
||||
*
|
||||
* @author Tomasz Kopczynski
|
||||
* @since 1.2.0
|
||||
*/
|
||||
public interface CommandFormatter {
|
||||
|
||||
/**
|
||||
* Formats a list of {@code String}s into a single {@code String}.
|
||||
* Formats a list of {@code elements} into a single {@code String}.
|
||||
*
|
||||
* @param elements A list of {@code String}s to be formatted
|
||||
* @return A list of {@code String}s formatted as one {@code String}
|
||||
* @param elements The {@code String} elements to be formatted
|
||||
* @return A single formatted {@code String}
|
||||
*/
|
||||
String format(List<String> elements);
|
||||
|
||||
}
|
||||
|
||||
@@ -39,9 +39,12 @@ import org.springframework.util.StringUtils;
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Paul-Christian Volkmer
|
||||
* @author Tomasz Kopczynski
|
||||
* @since 1.1.0
|
||||
* @see CliDocumentation#curlRequest()
|
||||
* @see CliDocumentation#curlRequest(CommandFormatter)
|
||||
* @see CliDocumentation#curlRequest(Map)
|
||||
* @see CliDocumentation#curlRequest(Map, CommandFormatter)
|
||||
*/
|
||||
public class CurlRequestSnippet extends TemplatedSnippet {
|
||||
|
||||
@@ -49,16 +52,19 @@ public class CurlRequestSnippet extends TemplatedSnippet {
|
||||
|
||||
/**
|
||||
* Creates a new {@code CurlRequestSnippet} with no additional attributes.
|
||||
*
|
||||
* @deprecated since 1.2.0 in favor of {@link #CurlRequestSnippet(CommandFormatter)}
|
||||
*/
|
||||
@Deprecated
|
||||
protected CurlRequestSnippet() {
|
||||
this(null, null);
|
||||
this(null, CliDocumentation.DEFAULT_COMMAND_FORMATTER);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@code CurlRequestSnippet} with a given {@link CommandFormatter}.
|
||||
* Creates a new {@code CurlRequestSnippet} that will use the given
|
||||
* {@code commandFormatter} to format the curl command.
|
||||
*
|
||||
* @param commandFormatter The formatter for generating the snippet
|
||||
* @param commandFormatter The formatter
|
||||
*/
|
||||
protected CurlRequestSnippet(CommandFormatter commandFormatter) {
|
||||
this(null, commandFormatter);
|
||||
@@ -69,24 +75,26 @@ public class CurlRequestSnippet extends TemplatedSnippet {
|
||||
* {@code attributes} that will be included in the model during template rendering.
|
||||
*
|
||||
* @param attributes The additional attributes
|
||||
* @deprecated since 1.2.0 in favor of
|
||||
* {@link #CurlRequestSnippet(Map, CommandFormatter)}
|
||||
*/
|
||||
@Deprecated
|
||||
protected CurlRequestSnippet(Map<String, Object> attributes) {
|
||||
this(attributes, null);
|
||||
this(attributes, CliDocumentation.DEFAULT_COMMAND_FORMATTER);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@code CurlRequestSnippet} with the given additional
|
||||
* {@code attributes} that will be included in the model during template rendering
|
||||
* and the given {@link CommandFormatter}.
|
||||
* {@code attributes} that will be included in the model during template rendering.
|
||||
* The given {@code commandFormaatter} will be used to format the curl command.
|
||||
*
|
||||
* @param attributes The additional attributes
|
||||
* @param commandFormatter The formatter for generating the snippet
|
||||
*/
|
||||
protected CurlRequestSnippet(Map<String, Object> attributes, CommandFormatter commandFormatter) {
|
||||
protected CurlRequestSnippet(Map<String, Object> attributes,
|
||||
CommandFormatter commandFormatter) {
|
||||
super("curl-request", attributes);
|
||||
|
||||
Assert.notNull(commandFormatter, "Command formatter must be set");
|
||||
Assert.notNull(commandFormatter, "Command formatter must not be null");
|
||||
this.commandFormatter = commandFormatter;
|
||||
}
|
||||
|
||||
|
||||
@@ -42,6 +42,7 @@ import org.springframework.util.StringUtils;
|
||||
*
|
||||
* @author Raman Gupta
|
||||
* @author Andy Wilkinson
|
||||
* @author Tomasz Kopczynski
|
||||
* @since 1.1.0
|
||||
* @see CliDocumentation#httpieRequest()
|
||||
* @see CliDocumentation#httpieRequest(Map)
|
||||
@@ -52,6 +53,8 @@ public class HttpieRequestSnippet extends TemplatedSnippet {
|
||||
|
||||
/**
|
||||
* Creates a new {@code HttpieRequestSnippet} with no additional attributes.
|
||||
*
|
||||
* @deprecated since 1.2.0 in favor of {@link #HttpieRequestSnippet(CommandFormatter)}
|
||||
*/
|
||||
@Deprecated
|
||||
protected HttpieRequestSnippet() {
|
||||
@@ -59,9 +62,10 @@ public class HttpieRequestSnippet extends TemplatedSnippet {
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@code HttpieRequestSnippet} with the given {@link CommandFormatter}.
|
||||
* Creates a new {@code HttpieRequestSnippet} that will use the given
|
||||
* {@code commandFormatter} to format the HTTPie command.
|
||||
*
|
||||
* @param commandFormatter The formatter for generating the snippet
|
||||
* @param commandFormatter The formatter
|
||||
*/
|
||||
protected HttpieRequestSnippet(CommandFormatter commandFormatter) {
|
||||
this(null, commandFormatter);
|
||||
@@ -72,6 +76,8 @@ public class HttpieRequestSnippet extends TemplatedSnippet {
|
||||
* {@code attributes} that will be included in the model during template rendering.
|
||||
*
|
||||
* @param attributes The additional attributes
|
||||
* @deprecated since 1.2.0 in favor of
|
||||
* {@link #HttpieRequestSnippet(Map, CommandFormatter)}
|
||||
*/
|
||||
@Deprecated
|
||||
protected HttpieRequestSnippet(Map<String, Object> attributes) {
|
||||
@@ -80,16 +86,16 @@ public class HttpieRequestSnippet extends TemplatedSnippet {
|
||||
|
||||
/**
|
||||
* Creates a new {@code HttpieRequestSnippet} with the given additional
|
||||
* {@code attributes} that will be included in the model during template rendering
|
||||
* and the given {@link CommandFormatter}.
|
||||
* {@code attributes} that will be included in the model during template rendering.
|
||||
* The given {@code commandFormaatter} will be used to format the HTTPie command.
|
||||
*
|
||||
* @param attributes The additional attributes
|
||||
* @param commandFormatter The formatter for generating the snippet
|
||||
*/
|
||||
protected HttpieRequestSnippet(Map<String, Object> attributes, CommandFormatter commandFormatter) {
|
||||
protected HttpieRequestSnippet(Map<String, Object> attributes,
|
||||
CommandFormatter commandFormatter) {
|
||||
super("httpie-request", attributes);
|
||||
|
||||
Assert.notNull(commandFormatter, "Command formatter must be set");
|
||||
Assert.notNull(commandFormatter, "Command formatter must not be null");
|
||||
this.commandFormatter = commandFormatter;
|
||||
}
|
||||
|
||||
@@ -200,8 +206,8 @@ public class HttpieRequestSnippet extends TemplatedSnippet {
|
||||
|
||||
private void writeCookies(OperationRequest request, List<String> lines) {
|
||||
for (RequestCookie cookie : request.getCookies()) {
|
||||
lines.add(String.format("'Cookie:%s=%s'", cookie.getName(),
|
||||
cookie.getValue()));
|
||||
lines.add(
|
||||
String.format("'Cookie:%s=%s'", cookie.getName(), cookie.getValue()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -215,8 +221,7 @@ public class HttpieRequestSnippet extends TemplatedSnippet {
|
||||
}
|
||||
else if (request.isPutOrPost()) {
|
||||
writeContentUsingParameters(
|
||||
request.getParameters().getUniqueParameters(request.getUri()),
|
||||
lines);
|
||||
request.getParameters().getUniqueParameters(request.getUri()), lines);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -29,27 +29,33 @@ import static org.junit.Assert.assertThat;
|
||||
* Tests for {@link CommandFormatter}.
|
||||
*
|
||||
* @author Tomasz Kopczynski
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
public class ConcatenatingCommandFormatterTests {
|
||||
|
||||
private CommandFormatter singleLineFormat = CliDocumentation.singleLineFormat();
|
||||
|
||||
private static final String STR = "test";
|
||||
private CommandFormatter singleLineFormat = new ConcatenatingCommandFormatter(" ");
|
||||
|
||||
@Test
|
||||
public void noElementsTest() {
|
||||
assertThat(this.singleLineFormat.format(Collections.<String>emptyList()), is(equalTo("")));
|
||||
public void formattingAnEmptyListProducesAnEmptyString() {
|
||||
assertThat(this.singleLineFormat.format(Collections.<String>emptyList()),
|
||||
is(equalTo("")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void formattingNullProducesAnEmptyString() {
|
||||
assertThat(this.singleLineFormat.format(null), is(equalTo("")));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void singleElementTest() {
|
||||
assertThat(this.singleLineFormat.format(Collections.singletonList(STR)), is(equalTo(String.format(" %s", STR))));
|
||||
public void formattingASingleElement() {
|
||||
assertThat(this.singleLineFormat.format(Collections.singletonList("alpha")),
|
||||
is(equalTo(" alpha")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void twoElementsTest() {
|
||||
assertThat(this.singleLineFormat.format(Arrays.asList(STR, STR)), is(equalTo(String.format(" %s %s", STR, STR))));
|
||||
public void formattingMultipleElements() {
|
||||
assertThat(this.singleLineFormat.format(Arrays.asList("alpha", "bravo")),
|
||||
is(equalTo(String.format(" alpha bravo"))));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -45,6 +45,7 @@ import static org.springframework.restdocs.snippet.Attributes.key;
|
||||
* @author Dmitriy Mayboroda
|
||||
* @author Jonathan Pearlin
|
||||
* @author Paul-Christian Volkmer
|
||||
* @author Tomasz Kopczynski
|
||||
*/
|
||||
@RunWith(Parameterized.class)
|
||||
public class CurlRequestSnippetTests extends AbstractSnippetTests {
|
||||
@@ -256,8 +257,10 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
|
||||
@Test
|
||||
public void requestWithHeadersMultiline() throws IOException {
|
||||
this.snippets.expectCurlRequest()
|
||||
.withContents(codeBlock("bash").content(String.format("$ curl 'http://localhost/foo' -i"
|
||||
+ " \\%n -H 'Content-Type: application/json' \\%n -H 'a: alpha'")));
|
||||
.withContents(codeBlock("bash")
|
||||
.content(String.format("$ curl 'http://localhost/foo' -i \\%n"
|
||||
+ " -H 'Content-Type: application/json' \\%n"
|
||||
+ " -H 'a: alpha'")));
|
||||
new CurlRequestSnippet(CliDocumentation.multiLineFormat())
|
||||
.document(this.operationBuilder.request("http://localhost/foo")
|
||||
.header(HttpHeaders.CONTENT_TYPE,
|
||||
@@ -360,11 +363,9 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
|
||||
attributes(
|
||||
key("title").value("curl request title")),
|
||||
this.commandFormatter)
|
||||
.document(
|
||||
this.operationBuilder
|
||||
.document(this.operationBuilder
|
||||
.attribute(TemplateEngine.class.getName(),
|
||||
new MustacheTemplateEngine(
|
||||
resolver))
|
||||
new MustacheTemplateEngine(resolver))
|
||||
.request("http://localhost/foo").build());
|
||||
}
|
||||
|
||||
|
||||
@@ -46,6 +46,8 @@ import static org.springframework.restdocs.snippet.Attributes.key;
|
||||
* @author Jonathan Pearlin
|
||||
* @author Paul-Christian Volkmer
|
||||
* @author Raman Gupta
|
||||
* @author Tomasz Kopczynski
|
||||
*
|
||||
*/
|
||||
@RunWith(Parameterized.class)
|
||||
public class HttpieRequestSnippetTests extends AbstractSnippetTests {
|
||||
@@ -256,9 +258,9 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void requestWithHeadersMultiline() throws IOException {
|
||||
this.snippets.expectHttpieRequest().withContents(
|
||||
codeBlock("bash").content(String.format("$ http GET 'http://localhost/foo'"
|
||||
+ " \\%n 'Content-Type:application/json' \\%n 'a:alpha'")));
|
||||
this.snippets.expectHttpieRequest().withContents(codeBlock("bash")
|
||||
.content(String.format("$ http GET 'http://localhost/foo' \\%n"
|
||||
+ " 'Content-Type:application/json' \\%n 'a:alpha'")));
|
||||
new HttpieRequestSnippet(CliDocumentation.multiLineFormat())
|
||||
.document(this.operationBuilder.request("http://localhost/foo")
|
||||
.header(HttpHeaders.CONTENT_TYPE,
|
||||
@@ -279,7 +281,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
|
||||
@Test
|
||||
public void multipartPostWithNoSubmittedFileName() throws IOException {
|
||||
String expectedContent = "$ http --form POST 'http://localhost/upload'"
|
||||
+ " 'metadata'@<(echo '{\"description\": \"foo\"}')";
|
||||
+ " 'metadata'@<(echo '{\"description\": \"foo\"}')";
|
||||
this.snippets.expectHttpieRequest()
|
||||
.withContents(codeBlock("bash").content(expectedContent));
|
||||
new HttpieRequestSnippet(this.commandFormatter).document(
|
||||
@@ -293,7 +295,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
|
||||
public void multipartPostWithContentType() throws IOException {
|
||||
// httpie does not yet support manually set content type by part
|
||||
String expectedContent = "$ http --form POST 'http://localhost/upload'"
|
||||
+ " 'image'@'documents/images/example.png'";
|
||||
+ " 'image'@'documents/images/example.png'";
|
||||
this.snippets.expectHttpieRequest()
|
||||
.withContents(codeBlock("bash").content(expectedContent));
|
||||
new HttpieRequestSnippet(this.commandFormatter).document(
|
||||
@@ -308,7 +310,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
|
||||
@Test
|
||||
public void multipartPost() throws IOException {
|
||||
String expectedContent = "$ http --form POST 'http://localhost/upload'"
|
||||
+ " 'image'@'documents/images/example.png'";
|
||||
+ " 'image'@'documents/images/example.png'";
|
||||
this.snippets.expectHttpieRequest()
|
||||
.withContents(codeBlock("bash").content(expectedContent));
|
||||
new HttpieRequestSnippet(this.commandFormatter).document(
|
||||
@@ -322,8 +324,8 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
|
||||
@Test
|
||||
public void multipartPostWithParameters() throws IOException {
|
||||
String expectedContent = "$ http --form POST 'http://localhost/upload'"
|
||||
+ " 'image'@'documents/images/example.png' 'a=apple' 'a=avocado'"
|
||||
+ " 'b=banana'";
|
||||
+ " 'image'@'documents/images/example.png' 'a=apple' 'a=avocado'"
|
||||
+ " 'b=banana'";
|
||||
this.snippets.expectHttpieRequest()
|
||||
.withContents(codeBlock("bash").content(expectedContent));
|
||||
new HttpieRequestSnippet(this.commandFormatter).document(
|
||||
@@ -355,14 +357,12 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
|
||||
given(resolver.resolveTemplateResource("httpie-request"))
|
||||
.willReturn(snippetResource("httpie-request-with-title"));
|
||||
new HttpieRequestSnippet(
|
||||
attributes(
|
||||
key("title").value("httpie request title")),
|
||||
attributes(key("title")
|
||||
.value("httpie request title")),
|
||||
this.commandFormatter)
|
||||
.document(
|
||||
this.operationBuilder
|
||||
.document(this.operationBuilder
|
||||
.attribute(TemplateEngine.class.getName(),
|
||||
new MustacheTemplateEngine(
|
||||
resolver))
|
||||
new MustacheTemplateEngine(resolver))
|
||||
.request("http://localhost/foo").build());
|
||||
}
|
||||
|
||||
|
||||
@@ -102,6 +102,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Dewet Diener
|
||||
* @author Tomasz Kopczynski
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@WebAppConfiguration
|
||||
@@ -161,9 +162,11 @@ public class MockMvcRestDocumentationIntegrationTests {
|
||||
assertThat(
|
||||
new File(
|
||||
"build/generated-snippets/curl-snippet-with-content/curl-request.adoc"),
|
||||
is(snippet(asciidoctor()).withContents(codeBlock(asciidoctor(), "bash")
|
||||
.content(String.format("$ curl " + "'http://localhost:8080/' -i -X POST "
|
||||
+ "\\%n -H 'Accept: application/json' \\%n -d 'content'")))));
|
||||
is(snippet(asciidoctor())
|
||||
.withContents(codeBlock(asciidoctor(), "bash").content(String
|
||||
.format("$ curl 'http://localhost:8080/' -i -X POST \\%n"
|
||||
+ " -H 'Accept: application/json' \\%n"
|
||||
+ " -d 'content'")))));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -178,8 +181,9 @@ public class MockMvcRestDocumentationIntegrationTests {
|
||||
new File(
|
||||
"build/generated-snippets/curl-snippet-with-cookies/curl-request.adoc"),
|
||||
is(snippet(asciidoctor()).withContents(codeBlock(asciidoctor(), "bash")
|
||||
.content(String.format("$ curl " + "'http://localhost:8080/' -i "
|
||||
+ "\\%n -H 'Accept: application/json' \\%n --cookie 'cookieName=cookieVal'")))));
|
||||
.content(String.format("$ curl 'http://localhost:8080/' -i \\%n"
|
||||
+ " -H 'Accept: application/json' \\%n"
|
||||
+ " --cookie 'cookieName=cookieVal'")))));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -192,10 +196,11 @@ public class MockMvcRestDocumentationIntegrationTests {
|
||||
assertThat(
|
||||
new File(
|
||||
"build/generated-snippets/curl-snippet-with-query-string/curl-request.adoc"),
|
||||
is(snippet(asciidoctor())
|
||||
.withContents(codeBlock(asciidoctor(), "bash").content(String.format("$ curl "
|
||||
+ "'http://localhost:8080/?foo=bar' -i -X POST "
|
||||
+ "\\%n -H 'Accept: application/json' \\%n -d 'a=alpha'")))));
|
||||
is(snippet(asciidoctor()).withContents(
|
||||
codeBlock(asciidoctor(), "bash").content(String.format("$ curl "
|
||||
+ "'http://localhost:8080/?foo=bar' -i -X POST \\%n"
|
||||
+ " -H 'Accept: application/json' \\%n"
|
||||
+ " -d 'a=alpha'")))));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -209,9 +214,10 @@ public class MockMvcRestDocumentationIntegrationTests {
|
||||
new File(
|
||||
"build/generated-snippets/curl-snippet-with-content-and-parameters/curl-request.adoc"),
|
||||
is(snippet(asciidoctor())
|
||||
.withContents(codeBlock(asciidoctor(), "bash").content(String.format("$ curl "
|
||||
+ "'http://localhost:8080/?a=alpha' -i -X POST "
|
||||
+ "\\%n -H 'Accept: application/json' \\%n -d 'some content'")))));
|
||||
.withContents(codeBlock(asciidoctor(), "bash").content(String
|
||||
.format("$ curl 'http://localhost:8080/?a=alpha' -i -X POST \\%n"
|
||||
+ " -H 'Accept: application/json' \\%n"
|
||||
+ " -d 'some content'")))));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -226,8 +232,9 @@ public class MockMvcRestDocumentationIntegrationTests {
|
||||
new File(
|
||||
"build/generated-snippets/httpie-snippet-with-content/httpie-request.adoc"),
|
||||
is(snippet(asciidoctor()).withContents(codeBlock(asciidoctor(), "bash")
|
||||
.content(String.format("$ echo 'content' | http POST 'http://localhost:8080/'"
|
||||
+ " \\%n 'Accept:application/json'")))));
|
||||
.content(String.format("$ echo 'content' | "
|
||||
+ "http POST 'http://localhost:8080/' \\%n"
|
||||
+ " 'Accept:application/json'")))));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -242,8 +249,9 @@ public class MockMvcRestDocumentationIntegrationTests {
|
||||
new File(
|
||||
"build/generated-snippets/httpie-snippet-with-cookies/httpie-request.adoc"),
|
||||
is(snippet(asciidoctor()).withContents(codeBlock(asciidoctor(), "bash")
|
||||
.content(String.format("$ http GET 'http://localhost:8080/'"
|
||||
+ " \\%n 'Accept:application/json' \\%n 'Cookie:cookieName=cookieVal'")))));
|
||||
.content(String.format("$ http GET 'http://localhost:8080/' \\%n"
|
||||
+ " 'Accept:application/json' \\%n"
|
||||
+ " 'Cookie:cookieName=cookieVal'")))));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -256,10 +264,10 @@ public class MockMvcRestDocumentationIntegrationTests {
|
||||
assertThat(
|
||||
new File(
|
||||
"build/generated-snippets/httpie-snippet-with-query-string/httpie-request.adoc"),
|
||||
is(snippet(asciidoctor())
|
||||
.withContents(codeBlock(asciidoctor(), "bash").content(String.format("$ http "
|
||||
+ "--form POST 'http://localhost:8080/?foo=bar' "
|
||||
+ "\\%n 'Accept:application/json' \\%n 'a=alpha'")))));
|
||||
is(snippet(asciidoctor()).withContents(codeBlock(asciidoctor(),
|
||||
"bash").content(String.format("$ http "
|
||||
+ "--form POST 'http://localhost:8080/?foo=bar' \\%n"
|
||||
+ " 'Accept:application/json' \\%n 'a=alpha'")))));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -274,8 +282,8 @@ public class MockMvcRestDocumentationIntegrationTests {
|
||||
"build/generated-snippets/httpie-snippet-post-with-content-and-parameters/httpie-request.adoc"),
|
||||
is(snippet(asciidoctor()).withContents(codeBlock(asciidoctor(), "bash")
|
||||
.content(String.format("$ echo " + "'some content' | http POST "
|
||||
+ "'http://localhost:8080/?a=alpha' "
|
||||
+ "\\%n 'Accept:application/json'")))));
|
||||
+ "'http://localhost:8080/?a=alpha' \\%n"
|
||||
+ " 'Accept:application/json'")))));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -560,8 +568,9 @@ public class MockMvcRestDocumentationIntegrationTests {
|
||||
new File(
|
||||
"build/generated-snippets/custom-context-path/curl-request.adoc"),
|
||||
is(snippet(asciidoctor())
|
||||
.withContents(codeBlock(asciidoctor(), "bash").content(String.format(
|
||||
"$ curl 'http://localhost:8080/custom/' -i \\%n -H 'Accept: application/json'")))));
|
||||
.withContents(codeBlock(asciidoctor(), "bash").content(String
|
||||
.format("$ curl 'http://localhost:8080/custom/' -i \\%n"
|
||||
+ " -H 'Accept: application/json'")))));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -70,6 +70,7 @@ import static org.springframework.restdocs.test.SnippetMatchers.snippet;
|
||||
* Integration tests for using Spring REST Docs with REST Assured.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Tomasz Kopczynski
|
||||
*/
|
||||
public class RestAssuredRestDocumentationIntegrationTests {
|
||||
|
||||
@@ -101,10 +102,11 @@ public class RestAssuredRestDocumentationIntegrationTests {
|
||||
new File(
|
||||
"build/generated-snippets/curl-snippet-with-content/curl-request.adoc"),
|
||||
is(snippet(asciidoctor()).withContents(codeBlock(asciidoctor(), "bash")
|
||||
.content(String.format("$ curl 'http://localhost:" + tomcat.getPort() + "/' -i "
|
||||
+ "-X POST \\%n -H 'Accept: application/json' "
|
||||
+ "\\%n -H 'Content-Type: " + contentType + "' "
|
||||
+ "\\%n -d 'content'")))));
|
||||
.content(String.format("$ curl 'http://localhost:"
|
||||
+ tomcat.getPort() + "/' -i -X POST \\%n"
|
||||
+ " -H 'Accept: application/json' \\%n"
|
||||
+ " -H 'Content-Type: " + contentType + "' \\%n"
|
||||
+ " -d 'content'")))));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -118,11 +120,12 @@ public class RestAssuredRestDocumentationIntegrationTests {
|
||||
assertThat(
|
||||
new File(
|
||||
"build/generated-snippets/curl-snippet-with-cookies/curl-request.adoc"),
|
||||
is(snippet(asciidoctor()).withContents(codeBlock(asciidoctor(), "bash")
|
||||
.content(String.format("$ curl 'http://localhost:" + tomcat.getPort() + "/' -i "
|
||||
+ "\\%n -H 'Accept: application/json' " + "\\%n -H 'Content-Type: "
|
||||
+ contentType + "' "
|
||||
+ "\\%n --cookie 'cookieName=cookieVal'")))));
|
||||
is(snippet(asciidoctor()).withContents(codeBlock(asciidoctor(),
|
||||
"bash").content(String.format("$ curl 'http://localhost:"
|
||||
+ tomcat.getPort() + "/' -i \\%n"
|
||||
+ " -H 'Accept: application/json' \\%n"
|
||||
+ " -H 'Content-Type: " + contentType + "' \\%n"
|
||||
+ " --cookie 'cookieName=cookieVal'")))));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -137,10 +140,11 @@ public class RestAssuredRestDocumentationIntegrationTests {
|
||||
new File(
|
||||
"build/generated-snippets/curl-snippet-with-query-string/curl-request.adoc"),
|
||||
is(snippet(asciidoctor()).withContents(codeBlock(asciidoctor(), "bash")
|
||||
.content(String.format("$ curl " + "'http://localhost:" + tomcat.getPort()
|
||||
+ "/?foo=bar' -i -X POST "
|
||||
+ "\\%n -H 'Accept: application/json' " + "\\%n -H 'Content-Type: "
|
||||
+ contentType + "' " + "\\%n -d 'a=alpha'")))));
|
||||
.content(String.format("$ curl " + "'http://localhost:"
|
||||
+ tomcat.getPort() + "/?foo=bar' -i -X POST \\%n"
|
||||
+ " -H 'Accept: application/json' \\%n"
|
||||
+ " -H 'Content-Type: " + contentType + "' \\%n"
|
||||
+ " -d 'a=alpha'")))));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user