Polish "Provide more control of formatting of curl and HTTPie commands"

See gh-348
Closes gh-260
This commit is contained in:
Andy Wilkinson
2017-02-12 21:51:17 +01:00
parent 2cb9d36ef1
commit a6d322007b
9 changed files with 164 additions and 117 deletions

View File

@@ -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"))));
}
}

View File

@@ -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());
}

View File

@@ -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());
}