Line breaks in cURL and HTTPie snippets

Closes gh-260
This commit is contained in:
Tomasz Kopczynski
2017-02-12 21:51:17 +01:00
committed by Andy Wilkinson
parent 82164348f3
commit 2cb9d36ef1
10 changed files with 508 additions and 199 deletions

View File

@@ -0,0 +1,55 @@
/*
* Copyright 2014-2017 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.cli;
import java.util.Arrays;
import java.util.Collections;
import org.junit.Test;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
/**
* Tests for {@link CommandFormatter}.
*
* @author Tomasz Kopczynski
*/
public class ConcatenatingCommandFormatterTests {
private CommandFormatter singleLineFormat = CliDocumentation.singleLineFormat();
private static final String STR = "test";
@Test
public void noElementsTest() {
assertThat(this.singleLineFormat.format(Collections.<String>emptyList()), is(equalTo("")));
assertThat(this.singleLineFormat.format(null), is(equalTo("")));
}
@Test
public void singleElementTest() {
assertThat(this.singleLineFormat.format(Collections.singletonList(STR)), is(equalTo(String.format(" %s", STR))));
}
@Test
public void twoElementsTest() {
assertThat(this.singleLineFormat.format(Arrays.asList(STR, STR)), is(equalTo(String.format(" %s %s", STR, STR))));
}
}

View File

@@ -49,6 +49,8 @@ import static org.springframework.restdocs.snippet.Attributes.key;
@RunWith(Parameterized.class)
public class CurlRequestSnippetTests extends AbstractSnippetTests {
private CommandFormatter commandFormatter = CliDocumentation.singleLineFormat();
public CurlRequestSnippetTests(String name, TemplateFormat templateFormat) {
super(name, templateFormat);
}
@@ -57,7 +59,7 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
public void getRequest() throws IOException {
this.snippets.expectCurlRequest().withContents(
codeBlock("bash").content("$ curl 'http://localhost/foo' -i"));
new CurlRequestSnippet()
new CurlRequestSnippet(this.commandFormatter)
.document(this.operationBuilder.request("http://localhost/foo").build());
}
@@ -65,7 +67,7 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
public void getRequestWithParameter() throws IOException {
this.snippets.expectCurlRequest().withContents(
codeBlock("bash").content("$ curl 'http://localhost/foo?a=alpha' -i"));
new CurlRequestSnippet().document(this.operationBuilder
new CurlRequestSnippet(this.commandFormatter).document(this.operationBuilder
.request("http://localhost/foo").param("a", "alpha").build());
}
@@ -73,7 +75,7 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
public void nonGetRequest() throws IOException {
this.snippets.expectCurlRequest().withContents(
codeBlock("bash").content("$ curl 'http://localhost/foo' -i -X POST"));
new CurlRequestSnippet().document(this.operationBuilder
new CurlRequestSnippet(this.commandFormatter).document(this.operationBuilder
.request("http://localhost/foo").method("POST").build());
}
@@ -81,7 +83,7 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
public void requestWithContent() throws IOException {
this.snippets.expectCurlRequest().withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo' -i -d 'content'"));
new CurlRequestSnippet().document(this.operationBuilder
new CurlRequestSnippet(this.commandFormatter).document(this.operationBuilder
.request("http://localhost/foo").content("content").build());
}
@@ -89,7 +91,7 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
public void getRequestWithQueryString() throws IOException {
this.snippets.expectCurlRequest().withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo?param=value' -i"));
new CurlRequestSnippet().document(this.operationBuilder
new CurlRequestSnippet(this.commandFormatter).document(this.operationBuilder
.request("http://localhost/foo?param=value").build());
}
@@ -98,7 +100,7 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
throws IOException {
this.snippets.expectCurlRequest().withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo?param=value' -i"));
new CurlRequestSnippet().document(
new CurlRequestSnippet(this.commandFormatter).document(
this.operationBuilder.request("http://localhost/foo?param=value")
.param("param", "value").build());
}
@@ -108,7 +110,7 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
throws IOException {
this.snippets.expectCurlRequest().withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo?a=alpha&b=bravo' -i"));
new CurlRequestSnippet()
new CurlRequestSnippet(this.commandFormatter)
.document(this.operationBuilder.request("http://localhost/foo?a=alpha")
.param("a", "alpha").param("b", "bravo").build());
}
@@ -117,7 +119,7 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
public void getRequestWithDisjointQueryStringAndParameters() throws IOException {
this.snippets.expectCurlRequest().withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo?a=alpha&b=bravo' -i"));
new CurlRequestSnippet().document(this.operationBuilder
new CurlRequestSnippet(this.commandFormatter).document(this.operationBuilder
.request("http://localhost/foo?a=alpha").param("b", "bravo").build());
}
@@ -125,7 +127,7 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
public void getRequestWithQueryStringWithNoValue() throws IOException {
this.snippets.expectCurlRequest().withContents(
codeBlock("bash").content("$ curl 'http://localhost/foo?param' -i"));
new CurlRequestSnippet().document(
new CurlRequestSnippet(this.commandFormatter).document(
this.operationBuilder.request("http://localhost/foo?param").build());
}
@@ -133,7 +135,7 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
public void postRequestWithQueryString() throws IOException {
this.snippets.expectCurlRequest().withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo?param=value' -i -X POST"));
new CurlRequestSnippet().document(this.operationBuilder
new CurlRequestSnippet(this.commandFormatter).document(this.operationBuilder
.request("http://localhost/foo?param=value").method("POST").build());
}
@@ -141,7 +143,7 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
public void postRequestWithQueryStringWithNoValue() throws IOException {
this.snippets.expectCurlRequest().withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo?param' -i -X POST"));
new CurlRequestSnippet().document(this.operationBuilder
new CurlRequestSnippet(this.commandFormatter).document(this.operationBuilder
.request("http://localhost/foo?param").method("POST").build());
}
@@ -149,7 +151,7 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
public void postRequestWithOneParameter() throws IOException {
this.snippets.expectCurlRequest().withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo' -i -X POST -d 'k1=v1'"));
new CurlRequestSnippet()
new CurlRequestSnippet(this.commandFormatter)
.document(this.operationBuilder.request("http://localhost/foo")
.method("POST").param("k1", "v1").build());
}
@@ -158,7 +160,7 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
public void postRequestWithOneParameterWithNoValue() throws IOException {
this.snippets.expectCurlRequest().withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo' -i -X POST -d 'k1='"));
new CurlRequestSnippet().document(this.operationBuilder
new CurlRequestSnippet(this.commandFormatter).document(this.operationBuilder
.request("http://localhost/foo").method("POST").param("k1").build());
}
@@ -168,7 +170,7 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
.withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo' -i -X POST"
+ " -d 'k1=v1&k1=v1-bis&k2=v2'"));
new CurlRequestSnippet().document(
new CurlRequestSnippet(this.commandFormatter).document(
this.operationBuilder.request("http://localhost/foo").method("POST")
.param("k1", "v1", "v1-bis").param("k2", "v2").build());
}
@@ -177,7 +179,7 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
public void postRequestWithUrlEncodedParameter() throws IOException {
this.snippets.expectCurlRequest().withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo' -i -X POST -d 'k1=a%26b'"));
new CurlRequestSnippet()
new CurlRequestSnippet(this.commandFormatter)
.document(this.operationBuilder.request("http://localhost/foo")
.method("POST").param("k1", "a&b").build());
}
@@ -186,7 +188,7 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
public void postRequestWithDisjointQueryStringAndParameter() throws IOException {
this.snippets.expectCurlRequest().withContents(codeBlock("bash").content(
"$ curl 'http://localhost/foo?a=alpha' -i -X POST -d 'b=bravo'"));
new CurlRequestSnippet()
new CurlRequestSnippet(this.commandFormatter)
.document(this.operationBuilder.request("http://localhost/foo?a=alpha")
.method("POST").param("b", "bravo").build());
}
@@ -196,7 +198,7 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
throws IOException {
this.snippets.expectCurlRequest().withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo?a=alpha&b=bravo' -i -X POST"));
new CurlRequestSnippet().document(
new CurlRequestSnippet(this.commandFormatter).document(
this.operationBuilder.request("http://localhost/foo?a=alpha&b=bravo")
.method("POST").param("a", "alpha").param("b", "bravo").build());
}
@@ -206,7 +208,7 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
throws IOException {
this.snippets.expectCurlRequest().withContents(codeBlock("bash").content(
"$ curl 'http://localhost/foo?a=alpha' -i -X POST -d 'b=bravo'"));
new CurlRequestSnippet()
new CurlRequestSnippet(this.commandFormatter)
.document(this.operationBuilder.request("http://localhost/foo?a=alpha")
.method("POST").param("a", "alpha").param("b", "bravo").build());
}
@@ -215,7 +217,7 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
public void putRequestWithOneParameter() throws IOException {
this.snippets.expectCurlRequest().withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo' -i -X PUT -d 'k1=v1'"));
new CurlRequestSnippet().document(this.operationBuilder
new CurlRequestSnippet(this.commandFormatter).document(this.operationBuilder
.request("http://localhost/foo").method("PUT").param("k1", "v1").build());
}
@@ -225,7 +227,7 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
.withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo' -i -X PUT"
+ " -d 'k1=v1&k1=v1-bis&k2=v2'"));
new CurlRequestSnippet().document(this.operationBuilder
new CurlRequestSnippet(this.commandFormatter).document(this.operationBuilder
.request("http://localhost/foo").method("PUT").param("k1", "v1")
.param("k1", "v1-bis").param("k2", "v2").build());
}
@@ -234,7 +236,7 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
public void putRequestWithUrlEncodedParameter() throws IOException {
this.snippets.expectCurlRequest().withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo' -i -X PUT -d 'k1=a%26b'"));
new CurlRequestSnippet()
new CurlRequestSnippet(this.commandFormatter)
.document(this.operationBuilder.request("http://localhost/foo")
.method("PUT").param("k1", "a&b").build());
}
@@ -244,7 +246,19 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
this.snippets.expectCurlRequest()
.withContents(codeBlock("bash").content("$ curl 'http://localhost/foo' -i"
+ " -H 'Content-Type: application/json' -H 'a: alpha'"));
new CurlRequestSnippet()
new CurlRequestSnippet(this.commandFormatter)
.document(this.operationBuilder.request("http://localhost/foo")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_JSON_VALUE)
.header("a", "alpha").build());
}
@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'")));
new CurlRequestSnippet(CliDocumentation.multiLineFormat())
.document(this.operationBuilder.request("http://localhost/foo")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_JSON_VALUE)
@@ -256,7 +270,7 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
this.snippets.expectCurlRequest()
.withContents(codeBlock("bash").content("$ curl 'http://localhost/foo' -i"
+ " --cookie 'name1=value1;name2=value2'"));
new CurlRequestSnippet()
new CurlRequestSnippet(this.commandFormatter)
.document(this.operationBuilder.request("http://localhost/foo")
.cookie("name1", "value1").cookie("name2", "value2").build());
}
@@ -268,11 +282,11 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
+ "'metadata={\"description\": \"foo\"}'";
this.snippets.expectCurlRequest()
.withContents(codeBlock("bash").content(expectedContent));
new CurlRequestSnippet().document(
new CurlRequestSnippet(this.commandFormatter).document(
this.operationBuilder.request("http://localhost/upload").method("POST")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.MULTIPART_FORM_DATA_VALUE)
.part("metadata", "{\"description\": \"foo\"}".getBytes()).build());
.part("metadata", "{\"description\": \"foo\"}".getBytes()).build());
}
@Test
@@ -282,13 +296,13 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
+ "'image=@documents/images/example.png;type=image/png'";
this.snippets.expectCurlRequest()
.withContents(codeBlock("bash").content(expectedContent));
new CurlRequestSnippet().document(
new CurlRequestSnippet(this.commandFormatter).document(
this.operationBuilder.request("http://localhost/upload").method("POST")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.MULTIPART_FORM_DATA_VALUE)
.part("image", new byte[0])
.header(HttpHeaders.CONTENT_TYPE, MediaType.IMAGE_PNG_VALUE)
.submittedFileName("documents/images/example.png").build());
.part("image", new byte[0])
.header(HttpHeaders.CONTENT_TYPE, MediaType.IMAGE_PNG_VALUE)
.submittedFileName("documents/images/example.png").build());
}
@Test
@@ -298,12 +312,12 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
+ "'image=@documents/images/example.png'";
this.snippets.expectCurlRequest()
.withContents(codeBlock("bash").content(expectedContent));
new CurlRequestSnippet().document(
new CurlRequestSnippet(this.commandFormatter).document(
this.operationBuilder.request("http://localhost/upload").method("POST")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.MULTIPART_FORM_DATA_VALUE)
.part("image", new byte[0])
.submittedFileName("documents/images/example.png").build());
.part("image", new byte[0])
.submittedFileName("documents/images/example.png").build());
}
@Test
@@ -314,25 +328,25 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
+ "-F 'b=banana'";
this.snippets.expectCurlRequest()
.withContents(codeBlock("bash").content(expectedContent));
new CurlRequestSnippet().document(
new CurlRequestSnippet(this.commandFormatter).document(
this.operationBuilder.request("http://localhost/upload").method("POST")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.MULTIPART_FORM_DATA_VALUE)
.part("image", new byte[0])
.submittedFileName("documents/images/example.png").and()
.param("a", "apple", "avocado").param("b", "banana").build());
.part("image", new byte[0])
.submittedFileName("documents/images/example.png").and()
.param("a", "apple", "avocado").param("b", "banana").build());
}
@Test
public void basicAuthCredentialsAreSuppliedUsingUserOption() throws IOException {
this.snippets.expectCurlRequest().withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo' -i -u 'user:secret'"));
new CurlRequestSnippet()
new CurlRequestSnippet(this.commandFormatter)
.document(this.operationBuilder.request("http://localhost/foo")
.header(HttpHeaders.AUTHORIZATION,
"Basic " + Base64Utils
.encodeToString("user:secret".getBytes()))
.build());
.build());
}
@Test
@@ -344,13 +358,14 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
.willReturn(snippetResource("curl-request-with-title"));
new CurlRequestSnippet(
attributes(
key("title").value("curl request title")))
.document(
this.operationBuilder
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(
resolver))
.request("http://localhost/foo").build());
key("title").value("curl request title")),
this.commandFormatter)
.document(
this.operationBuilder
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(
resolver))
.request("http://localhost/foo").build());
}
@Test
@@ -359,7 +374,7 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
.withContents(codeBlock("bash").content(
"$ curl 'http://localhost/foo' -i" + " -H 'Host: api.example.com'"
+ " -H 'Content-Type: application/json' -H 'a: alpha'"));
new CurlRequestSnippet()
new CurlRequestSnippet(this.commandFormatter)
.document(this.operationBuilder.request("http://localhost/foo")
.header(HttpHeaders.HOST, "api.example.com")
.header(HttpHeaders.CONTENT_TYPE,
@@ -373,9 +388,8 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
.withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo?a=alpha&b=bravo' -i "
+ "-X POST -d 'Some content'"));
new CurlRequestSnippet().document(this.operationBuilder
new CurlRequestSnippet(this.commandFormatter).document(this.operationBuilder
.request("http://localhost/foo").param("a", "alpha").method("POST")
.param("b", "bravo").content("Some content").build());
}
}

View File

@@ -50,6 +50,8 @@ import static org.springframework.restdocs.snippet.Attributes.key;
@RunWith(Parameterized.class)
public class HttpieRequestSnippetTests extends AbstractSnippetTests {
private CommandFormatter commandFormatter = CliDocumentation.singleLineFormat();
public HttpieRequestSnippetTests(String name, TemplateFormat templateFormat) {
super(name, templateFormat);
}
@@ -58,7 +60,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
public void getRequest() throws IOException {
this.snippets.expectHttpieRequest().withContents(
codeBlock("bash").content("$ http GET 'http://localhost/foo'"));
new HttpieRequestSnippet()
new HttpieRequestSnippet(this.commandFormatter)
.document(this.operationBuilder.request("http://localhost/foo").build());
}
@@ -66,7 +68,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
public void getRequestWithParameter() throws IOException {
this.snippets.expectHttpieRequest().withContents(
codeBlock("bash").content("$ http GET 'http://localhost/foo?a=alpha'"));
new HttpieRequestSnippet().document(this.operationBuilder
new HttpieRequestSnippet(this.commandFormatter).document(this.operationBuilder
.request("http://localhost/foo").param("a", "alpha").build());
}
@@ -74,7 +76,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
public void nonGetRequest() throws IOException {
this.snippets.expectHttpieRequest().withContents(
codeBlock("bash").content("$ http POST 'http://localhost/foo'"));
new HttpieRequestSnippet().document(this.operationBuilder
new HttpieRequestSnippet(this.commandFormatter).document(this.operationBuilder
.request("http://localhost/foo").method("POST").build());
}
@@ -82,7 +84,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
public void requestWithContent() throws IOException {
this.snippets.expectHttpieRequest().withContents(codeBlock("bash")
.content("$ echo 'content' | http GET 'http://localhost/foo'"));
new HttpieRequestSnippet().document(this.operationBuilder
new HttpieRequestSnippet(this.commandFormatter).document(this.operationBuilder
.request("http://localhost/foo").content("content").build());
}
@@ -90,7 +92,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
public void getRequestWithQueryString() throws IOException {
this.snippets.expectHttpieRequest().withContents(codeBlock("bash")
.content("$ http GET 'http://localhost/foo?param=value'"));
new HttpieRequestSnippet().document(this.operationBuilder
new HttpieRequestSnippet(this.commandFormatter).document(this.operationBuilder
.request("http://localhost/foo?param=value").build());
}
@@ -99,7 +101,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
throws IOException {
this.snippets.expectHttpieRequest().withContents(codeBlock("bash")
.content("$ http GET 'http://localhost/foo?param=value'"));
new HttpieRequestSnippet().document(
new HttpieRequestSnippet(this.commandFormatter).document(
this.operationBuilder.request("http://localhost/foo?param=value")
.param("param", "value").build());
}
@@ -109,7 +111,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
throws IOException {
this.snippets.expectHttpieRequest().withContents(codeBlock("bash")
.content("$ http GET 'http://localhost/foo?a=alpha&b=bravo'"));
new HttpieRequestSnippet()
new HttpieRequestSnippet(this.commandFormatter)
.document(this.operationBuilder.request("http://localhost/foo?a=alpha")
.param("a", "alpha").param("b", "bravo").build());
}
@@ -118,7 +120,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
public void getRequestWithDisjointQueryStringAndParameters() throws IOException {
this.snippets.expectHttpieRequest().withContents(codeBlock("bash")
.content("$ http GET 'http://localhost/foo?a=alpha&b=bravo'"));
new HttpieRequestSnippet().document(this.operationBuilder
new HttpieRequestSnippet(this.commandFormatter).document(this.operationBuilder
.request("http://localhost/foo?a=alpha").param("b", "bravo").build());
}
@@ -126,7 +128,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
public void getRequestWithQueryStringWithNoValue() throws IOException {
this.snippets.expectHttpieRequest().withContents(
codeBlock("bash").content("$ http GET 'http://localhost/foo?param'"));
new HttpieRequestSnippet().document(
new HttpieRequestSnippet(this.commandFormatter).document(
this.operationBuilder.request("http://localhost/foo?param").build());
}
@@ -134,7 +136,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
public void postRequestWithQueryString() throws IOException {
this.snippets.expectHttpieRequest().withContents(codeBlock("bash")
.content("$ http POST 'http://localhost/foo?param=value'"));
new HttpieRequestSnippet().document(this.operationBuilder
new HttpieRequestSnippet(this.commandFormatter).document(this.operationBuilder
.request("http://localhost/foo?param=value").method("POST").build());
}
@@ -142,7 +144,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
public void postRequestWithQueryStringWithNoValue() throws IOException {
this.snippets.expectHttpieRequest().withContents(
codeBlock("bash").content("$ http POST 'http://localhost/foo?param'"));
new HttpieRequestSnippet().document(this.operationBuilder
new HttpieRequestSnippet(this.commandFormatter).document(this.operationBuilder
.request("http://localhost/foo?param").method("POST").build());
}
@@ -150,7 +152,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
public void postRequestWithOneParameter() throws IOException {
this.snippets.expectHttpieRequest().withContents(codeBlock("bash")
.content("$ http --form POST 'http://localhost/foo' 'k1=v1'"));
new HttpieRequestSnippet()
new HttpieRequestSnippet(this.commandFormatter)
.document(this.operationBuilder.request("http://localhost/foo")
.method("POST").param("k1", "v1").build());
}
@@ -159,7 +161,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
public void postRequestWithOneParameterWithNoValue() throws IOException {
this.snippets.expectHttpieRequest().withContents(codeBlock("bash")
.content("$ http --form POST 'http://localhost/foo' 'k1='"));
new HttpieRequestSnippet().document(this.operationBuilder
new HttpieRequestSnippet(this.commandFormatter).document(this.operationBuilder
.request("http://localhost/foo").method("POST").param("k1").build());
}
@@ -169,7 +171,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
.withContents(codeBlock("bash")
.content("$ http --form POST 'http://localhost/foo'"
+ " 'k1=v1' 'k1=v1-bis' 'k2=v2'"));
new HttpieRequestSnippet().document(
new HttpieRequestSnippet(this.commandFormatter).document(
this.operationBuilder.request("http://localhost/foo").method("POST")
.param("k1", "v1", "v1-bis").param("k2", "v2").build());
}
@@ -178,7 +180,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
public void postRequestWithUrlEncodedParameter() throws IOException {
this.snippets.expectHttpieRequest().withContents(codeBlock("bash")
.content("$ http --form POST 'http://localhost/foo' 'k1=a&b'"));
new HttpieRequestSnippet()
new HttpieRequestSnippet(this.commandFormatter)
.document(this.operationBuilder.request("http://localhost/foo")
.method("POST").param("k1", "a&b").build());
}
@@ -187,7 +189,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
public void postRequestWithDisjointQueryStringAndParameter() throws IOException {
this.snippets.expectHttpieRequest().withContents(codeBlock("bash")
.content("$ http --form POST 'http://localhost/foo?a=alpha' 'b=bravo'"));
new HttpieRequestSnippet()
new HttpieRequestSnippet(this.commandFormatter)
.document(this.operationBuilder.request("http://localhost/foo?a=alpha")
.method("POST").param("b", "bravo").build());
}
@@ -197,7 +199,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
throws IOException {
this.snippets.expectHttpieRequest().withContents(codeBlock("bash")
.content("$ http POST 'http://localhost/foo?a=alpha&b=bravo'"));
new HttpieRequestSnippet().document(
new HttpieRequestSnippet(this.commandFormatter).document(
this.operationBuilder.request("http://localhost/foo?a=alpha&b=bravo")
.method("POST").param("a", "alpha").param("b", "bravo").build());
}
@@ -207,7 +209,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
throws IOException {
this.snippets.expectHttpieRequest().withContents(codeBlock("bash")
.content("$ http --form POST 'http://localhost/foo?a=alpha' 'b=bravo'"));
new HttpieRequestSnippet()
new HttpieRequestSnippet(this.commandFormatter)
.document(this.operationBuilder.request("http://localhost/foo?a=alpha")
.method("POST").param("a", "alpha").param("b", "bravo").build());
}
@@ -216,7 +218,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
public void putRequestWithOneParameter() throws IOException {
this.snippets.expectHttpieRequest().withContents(codeBlock("bash")
.content("$ http --form PUT 'http://localhost/foo' 'k1=v1'"));
new HttpieRequestSnippet().document(this.operationBuilder
new HttpieRequestSnippet(this.commandFormatter).document(this.operationBuilder
.request("http://localhost/foo").method("PUT").param("k1", "v1").build());
}
@@ -226,7 +228,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
.withContents(codeBlock("bash")
.content("$ http --form PUT 'http://localhost/foo'"
+ " 'k1=v1' 'k1=v1-bis' 'k2=v2'"));
new HttpieRequestSnippet().document(this.operationBuilder
new HttpieRequestSnippet(this.commandFormatter).document(this.operationBuilder
.request("http://localhost/foo").method("PUT").param("k1", "v1")
.param("k1", "v1-bis").param("k2", "v2").build());
}
@@ -235,7 +237,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
public void putRequestWithUrlEncodedParameter() throws IOException {
this.snippets.expectHttpieRequest().withContents(codeBlock("bash")
.content("$ http --form PUT 'http://localhost/foo' 'k1=a&b'"));
new HttpieRequestSnippet()
new HttpieRequestSnippet(this.commandFormatter)
.document(this.operationBuilder.request("http://localhost/foo")
.method("PUT").param("k1", "a&b").build());
}
@@ -245,7 +247,19 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
this.snippets.expectHttpieRequest().withContents(
codeBlock("bash").content("$ http GET 'http://localhost/foo'"
+ " 'Content-Type:application/json' 'a:alpha'"));
new HttpieRequestSnippet()
new HttpieRequestSnippet(this.commandFormatter)
.document(this.operationBuilder.request("http://localhost/foo")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_JSON_VALUE)
.header("a", "alpha").build());
}
@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'")));
new HttpieRequestSnippet(CliDocumentation.multiLineFormat())
.document(this.operationBuilder.request("http://localhost/foo")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_JSON_VALUE)
@@ -257,84 +271,80 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
this.snippets.expectHttpieRequest().withContents(
codeBlock("bash").content("$ http GET 'http://localhost/foo'"
+ " 'Cookie:name1=value1' 'Cookie:name2=value2'"));
new HttpieRequestSnippet()
new HttpieRequestSnippet(this.commandFormatter)
.document(this.operationBuilder.request("http://localhost/foo")
.cookie("name1", "value1").cookie("name2", "value2").build());
}
@Test
public void multipartPostWithNoSubmittedFileName() throws IOException {
String expectedContent = String
.format("$ http --form POST 'http://localhost/upload' \\%n"
+ " 'metadata'@<(echo '{\"description\": \"foo\"}')");
String expectedContent = "$ http --form POST 'http://localhost/upload'"
+ " 'metadata'@<(echo '{\"description\": \"foo\"}')";
this.snippets.expectHttpieRequest()
.withContents(codeBlock("bash").content(expectedContent));
new HttpieRequestSnippet().document(
new HttpieRequestSnippet(this.commandFormatter).document(
this.operationBuilder.request("http://localhost/upload").method("POST")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.MULTIPART_FORM_DATA_VALUE)
.part("metadata", "{\"description\": \"foo\"}".getBytes()).build());
.part("metadata", "{\"description\": \"foo\"}".getBytes()).build());
}
@Test
public void multipartPostWithContentType() throws IOException {
// httpie does not yet support manually set content type by part
String expectedContent = String
.format("$ http --form POST 'http://localhost/upload' \\%n"
+ " 'image'@'documents/images/example.png'");
String expectedContent = "$ http --form POST 'http://localhost/upload'"
+ " 'image'@'documents/images/example.png'";
this.snippets.expectHttpieRequest()
.withContents(codeBlock("bash").content(expectedContent));
new HttpieRequestSnippet().document(
new HttpieRequestSnippet(this.commandFormatter).document(
this.operationBuilder.request("http://localhost/upload").method("POST")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.MULTIPART_FORM_DATA_VALUE)
.part("image", new byte[0])
.header(HttpHeaders.CONTENT_TYPE, MediaType.IMAGE_PNG_VALUE)
.submittedFileName("documents/images/example.png").build());
.part("image", new byte[0])
.header(HttpHeaders.CONTENT_TYPE, MediaType.IMAGE_PNG_VALUE)
.submittedFileName("documents/images/example.png").build());
}
@Test
public void multipartPost() throws IOException {
String expectedContent = String
.format("$ http --form POST 'http://localhost/upload' \\%n"
+ " 'image'@'documents/images/example.png'");
String expectedContent = "$ http --form POST 'http://localhost/upload'"
+ " 'image'@'documents/images/example.png'";
this.snippets.expectHttpieRequest()
.withContents(codeBlock("bash").content(expectedContent));
new HttpieRequestSnippet().document(
new HttpieRequestSnippet(this.commandFormatter).document(
this.operationBuilder.request("http://localhost/upload").method("POST")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.MULTIPART_FORM_DATA_VALUE)
.part("image", new byte[0])
.submittedFileName("documents/images/example.png").build());
.part("image", new byte[0])
.submittedFileName("documents/images/example.png").build());
}
@Test
public void multipartPostWithParameters() throws IOException {
String expectedContent = String
.format("$ http --form POST 'http://localhost/upload' \\%n"
+ " 'image'@'documents/images/example.png' 'a=apple' 'a=avocado'"
+ " 'b=banana'");
String expectedContent = "$ http --form POST 'http://localhost/upload'"
+ " 'image'@'documents/images/example.png' 'a=apple' 'a=avocado'"
+ " 'b=banana'";
this.snippets.expectHttpieRequest()
.withContents(codeBlock("bash").content(expectedContent));
new HttpieRequestSnippet().document(
new HttpieRequestSnippet(this.commandFormatter).document(
this.operationBuilder.request("http://localhost/upload").method("POST")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.MULTIPART_FORM_DATA_VALUE)
.part("image", new byte[0])
.submittedFileName("documents/images/example.png").and()
.param("a", "apple", "avocado").param("b", "banana").build());
.part("image", new byte[0])
.submittedFileName("documents/images/example.png").and()
.param("a", "apple", "avocado").param("b", "banana").build());
}
@Test
public void basicAuthCredentialsAreSuppliedUsingAuthOption() throws IOException {
this.snippets.expectHttpieRequest().withContents(codeBlock("bash")
.content("$ http --auth 'user:secret' GET 'http://localhost/foo'"));
new HttpieRequestSnippet()
new HttpieRequestSnippet(this.commandFormatter)
.document(this.operationBuilder.request("http://localhost/foo")
.header(HttpHeaders.AUTHORIZATION,
"Basic " + Base64Utils
.encodeToString("user:secret".getBytes()))
.build());
.build());
}
@Test
@@ -346,13 +356,14 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
.willReturn(snippetResource("httpie-request-with-title"));
new HttpieRequestSnippet(
attributes(
key("title").value("httpie request title")))
.document(
this.operationBuilder
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(
resolver))
.request("http://localhost/foo").build());
key("title").value("httpie request title")),
this.commandFormatter)
.document(
this.operationBuilder
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(
resolver))
.request("http://localhost/foo").build());
}
@Test
@@ -361,7 +372,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
.withContents(codeBlock("bash").content(
"$ http GET 'http://localhost/foo' 'Host:api.example.com'"
+ " 'Content-Type:application/json' 'a:alpha'"));
new HttpieRequestSnippet()
new HttpieRequestSnippet(this.commandFormatter)
.document(this.operationBuilder.request("http://localhost/foo")
.header(HttpHeaders.HOST, "api.example.com")
.header(HttpHeaders.CONTENT_TYPE,
@@ -374,7 +385,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
this.snippets.expectHttpieRequest().withContents(
codeBlock("bash").content("$ echo 'Some content' | http POST "
+ "'http://localhost/foo?a=alpha&b=bravo'"));
new HttpieRequestSnippet().document(this.operationBuilder
new HttpieRequestSnippet(this.commandFormatter).document(this.operationBuilder
.request("http://localhost/foo").method("POST").param("a", "alpha")
.param("b", "bravo").content("Some content").build());
}