Skip to content

Commit a7e91ad

Browse files
committed
Merge pull request #488 from Johnny Lim
* gh-488: Add HTTP method option unconditionally in curl command
2 parents 95c4234 + da64c55 commit a7e91ad

File tree

5 files changed

+34
-37
lines changed

5 files changed

+34
-37
lines changed

spring-restdocs-core/src/main/java/org/springframework/restdocs/cli/CurlRequestSnippet.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2017 the original author or authors.
2+
* Copyright 2014-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -128,7 +128,7 @@ private String getOptions(Operation operation) {
128128

129129
CliOperationRequest request = new CliOperationRequest(operation.getRequest());
130130
writeUserOptionIfNecessary(request, builder);
131-
writeHttpMethodIfNecessary(request, builder);
131+
writeHttpMethod(request, builder);
132132

133133
List<String> additionalLines = new ArrayList<>();
134134
writeHeaders(request, additionalLines);
@@ -167,11 +167,8 @@ private void writeUserOptionIfNecessary(CliOperationRequest request,
167167
}
168168
}
169169

170-
private void writeHttpMethodIfNecessary(OperationRequest request,
171-
StringBuilder builder) {
172-
if (!HttpMethod.GET.equals(request.getMethod())) {
173-
builder.append(String.format(" -X %s", request.getMethod()));
174-
}
170+
private void writeHttpMethod(OperationRequest request, StringBuilder builder) {
171+
builder.append(String.format(" -X %s", request.getMethod()));
175172
}
176173

177174
private void writeHeaders(CliOperationRequest request, List<String> lines) {

spring-restdocs-core/src/test/java/org/springframework/restdocs/cli/CurlRequestSnippetTests.java

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,15 @@ public CurlRequestSnippetTests(String name, TemplateFormat templateFormat) {
5959
@Test
6060
public void getRequest() throws IOException {
6161
this.snippets.expectCurlRequest().withContents(
62-
codeBlock("bash").content("$ curl 'http://localhost/foo' -i"));
62+
codeBlock("bash").content("$ curl 'http://localhost/foo' -i -X GET"));
6363
new CurlRequestSnippet(this.commandFormatter)
6464
.document(this.operationBuilder.request("http://localhost/foo").build());
6565
}
6666

6767
@Test
6868
public void getRequestWithParameter() throws IOException {
69-
this.snippets.expectCurlRequest().withContents(
70-
codeBlock("bash").content("$ curl 'http://localhost/foo?a=alpha' -i"));
69+
this.snippets.expectCurlRequest().withContents(codeBlock("bash")
70+
.content("$ curl 'http://localhost/foo?a=alpha' -i -X GET"));
7171
new CurlRequestSnippet(this.commandFormatter).document(this.operationBuilder
7272
.request("http://localhost/foo").param("a", "alpha").build());
7373
}
@@ -83,15 +83,15 @@ public void nonGetRequest() throws IOException {
8383
@Test
8484
public void requestWithContent() throws IOException {
8585
this.snippets.expectCurlRequest().withContents(codeBlock("bash")
86-
.content("$ curl 'http://localhost/foo' -i -d 'content'"));
86+
.content("$ curl 'http://localhost/foo' -i -X GET -d 'content'"));
8787
new CurlRequestSnippet(this.commandFormatter).document(this.operationBuilder
8888
.request("http://localhost/foo").content("content").build());
8989
}
9090

9191
@Test
9292
public void getRequestWithQueryString() throws IOException {
9393
this.snippets.expectCurlRequest().withContents(codeBlock("bash")
94-
.content("$ curl 'http://localhost/foo?param=value' -i"));
94+
.content("$ curl 'http://localhost/foo?param=value' -i -X GET"));
9595
new CurlRequestSnippet(this.commandFormatter).document(this.operationBuilder
9696
.request("http://localhost/foo?param=value").build());
9797
}
@@ -100,7 +100,7 @@ public void getRequestWithQueryString() throws IOException {
100100
public void getRequestWithTotallyOverlappingQueryStringAndParameters()
101101
throws IOException {
102102
this.snippets.expectCurlRequest().withContents(codeBlock("bash")
103-
.content("$ curl 'http://localhost/foo?param=value' -i"));
103+
.content("$ curl 'http://localhost/foo?param=value' -i -X GET"));
104104
new CurlRequestSnippet(this.commandFormatter).document(
105105
this.operationBuilder.request("http://localhost/foo?param=value")
106106
.param("param", "value").build());
@@ -110,7 +110,7 @@ public void getRequestWithTotallyOverlappingQueryStringAndParameters()
110110
public void getRequestWithPartiallyOverlappingQueryStringAndParameters()
111111
throws IOException {
112112
this.snippets.expectCurlRequest().withContents(codeBlock("bash")
113-
.content("$ curl 'http://localhost/foo?a=alpha&b=bravo' -i"));
113+
.content("$ curl 'http://localhost/foo?a=alpha&b=bravo' -i -X GET"));
114114
new CurlRequestSnippet(this.commandFormatter)
115115
.document(this.operationBuilder.request("http://localhost/foo?a=alpha")
116116
.param("a", "alpha").param("b", "bravo").build());
@@ -119,15 +119,15 @@ public void getRequestWithPartiallyOverlappingQueryStringAndParameters()
119119
@Test
120120
public void getRequestWithDisjointQueryStringAndParameters() throws IOException {
121121
this.snippets.expectCurlRequest().withContents(codeBlock("bash")
122-
.content("$ curl 'http://localhost/foo?a=alpha&b=bravo' -i"));
122+
.content("$ curl 'http://localhost/foo?a=alpha&b=bravo' -i -X GET"));
123123
new CurlRequestSnippet(this.commandFormatter).document(this.operationBuilder
124124
.request("http://localhost/foo?a=alpha").param("b", "bravo").build());
125125
}
126126

127127
@Test
128128
public void getRequestWithQueryStringWithNoValue() throws IOException {
129-
this.snippets.expectCurlRequest().withContents(
130-
codeBlock("bash").content("$ curl 'http://localhost/foo?param' -i"));
129+
this.snippets.expectCurlRequest().withContents(codeBlock("bash")
130+
.content("$ curl 'http://localhost/foo?param' -i -X GET"));
131131
new CurlRequestSnippet(this.commandFormatter).document(
132132
this.operationBuilder.request("http://localhost/foo?param").build());
133133
}
@@ -242,8 +242,8 @@ public void putRequestWithUrlEncodedParameter() throws IOException {
242242

243243
@Test
244244
public void requestWithHeaders() throws IOException {
245-
this.snippets.expectCurlRequest()
246-
.withContents(codeBlock("bash").content("$ curl 'http://localhost/foo' -i"
245+
this.snippets.expectCurlRequest().withContents(
246+
codeBlock("bash").content("$ curl 'http://localhost/foo' -i -X GET"
247247
+ " -H 'Content-Type: application/json' -H 'a: alpha'"));
248248
new CurlRequestSnippet(this.commandFormatter).document(this.operationBuilder
249249
.request("http://localhost/foo")
@@ -254,8 +254,8 @@ public void requestWithHeaders() throws IOException {
254254
@Test
255255
public void requestWithHeadersMultiline() throws IOException {
256256
this.snippets.expectCurlRequest()
257-
.withContents(codeBlock("bash")
258-
.content(String.format("$ curl 'http://localhost/foo' -i \\%n"
257+
.withContents(codeBlock("bash").content(
258+
String.format("$ curl 'http://localhost/foo' -i -X GET \\%n"
259259
+ " -H 'Content-Type: application/json' \\%n"
260260
+ " -H 'a: alpha'")));
261261
new CurlRequestSnippet(CliDocumentation.multiLineFormat())
@@ -267,8 +267,8 @@ public void requestWithHeadersMultiline() throws IOException {
267267

268268
@Test
269269
public void requestWithCookies() throws IOException {
270-
this.snippets.expectCurlRequest()
271-
.withContents(codeBlock("bash").content("$ curl 'http://localhost/foo' -i"
270+
this.snippets.expectCurlRequest().withContents(
271+
codeBlock("bash").content("$ curl 'http://localhost/foo' -i -X GET"
272272
+ " --cookie 'name1=value1;name2=value2'"));
273273
new CurlRequestSnippet(this.commandFormatter)
274274
.document(this.operationBuilder.request("http://localhost/foo")
@@ -336,7 +336,7 @@ public void multipartPostWithParameters() throws IOException {
336336
@Test
337337
public void basicAuthCredentialsAreSuppliedUsingUserOption() throws IOException {
338338
this.snippets.expectCurlRequest().withContents(codeBlock("bash")
339-
.content("$ curl 'http://localhost/foo' -i -u 'user:secret'"));
339+
.content("$ curl 'http://localhost/foo' -i -u 'user:secret' -X GET"));
340340
new CurlRequestSnippet(this.commandFormatter).document(this.operationBuilder
341341
.request("http://localhost/foo")
342342
.header(HttpHeaders.AUTHORIZATION,
@@ -363,10 +363,9 @@ public void customAttributes() throws IOException {
363363

364364
@Test
365365
public void customHostHeaderIsIncluded() throws IOException {
366-
this.snippets.expectCurlRequest()
367-
.withContents(codeBlock("bash").content(
368-
"$ curl 'http://localhost/foo' -i" + " -H 'Host: api.example.com'"
369-
+ " -H 'Content-Type: application/json' -H 'a: alpha'"));
366+
this.snippets.expectCurlRequest().withContents(codeBlock("bash").content(
367+
"$ curl 'http://localhost/foo' -i -X GET -H 'Host: api.example.com'"
368+
+ " -H 'Content-Type: application/json' -H 'a: alpha'"));
370369
new CurlRequestSnippet(this.commandFormatter).document(this.operationBuilder
371370
.request("http://localhost/foo")
372371
.header(HttpHeaders.HOST, "api.example.com")

spring-restdocs-mockmvc/src/test/java/org/springframework/restdocs/mockmvc/MockMvcRestDocumentationIntegrationTests.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,11 @@ public void curlSnippetWithCookies() throws Exception {
178178
.andDo(document("curl-snippet-with-cookies"));
179179
assertThat(new File(
180180
"build/generated-snippets/curl-snippet-with-cookies/curl-request.adoc"),
181-
is(snippet(asciidoctor()).withContents(codeBlock(asciidoctor(), "bash")
182-
.content(String.format("$ curl 'http://localhost:8080/' -i \\%n"
183-
+ " -H 'Accept: application/json' \\%n"
184-
+ " --cookie 'cookieName=cookieVal'")))));
181+
is(snippet(asciidoctor())
182+
.withContents(codeBlock(asciidoctor(), "bash").content(String
183+
.format("$ curl 'http://localhost:8080/' -i -X GET \\%n"
184+
+ " -H 'Accept: application/json' \\%n"
185+
+ " --cookie 'cookieName=cookieVal'")))));
185186
}
186187

187188
@Test
@@ -556,9 +557,9 @@ public void customContextPath() throws Exception {
556557
assertThat(
557558
new File(
558559
"build/generated-snippets/custom-context-path/curl-request.adoc"),
559-
is(snippet(asciidoctor())
560-
.withContents(codeBlock(asciidoctor(), "bash").content(String
561-
.format("$ curl 'http://localhost:8080/custom/' -i \\%n"
560+
is(snippet(asciidoctor()).withContents(
561+
codeBlock(asciidoctor(), "bash").content(String.format(
562+
"$ curl 'http://localhost:8080/custom/' -i -X GET \\%n"
562563
+ " -H 'Accept: application/json'")))));
563564
}
564565

spring-restdocs-restassured/src/test/java/org/springframework/restdocs/restassured/RestAssuredRestDocumentationIntegrationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public void curlSnippetWithCookies() throws Exception {
121121
"build/generated-snippets/curl-snippet-with-cookies/curl-request.adoc"),
122122
is(snippet(asciidoctor()).withContents(codeBlock(asciidoctor(), "bash")
123123
.content(String.format("$ curl 'http://localhost:"
124-
+ tomcat.getPort() + "/' -i \\%n"
124+
+ tomcat.getPort() + "/' -i -X GET \\%n"
125125
+ " -H 'Accept: application/json' \\%n"
126126
+ " -H 'Content-Type: " + contentType + "' \\%n"
127127
+ " --cookie 'cookieName=cookieVal'")))));

spring-restdocs-restassured/src/test/java/org/springframework/restdocs/restassured3/RestAssuredRestDocumentationIntegrationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public void curlSnippetWithCookies() throws Exception {
120120
"build/generated-snippets/curl-snippet-with-cookies/curl-request.adoc"),
121121
is(snippet(asciidoctor()).withContents(codeBlock(asciidoctor(), "bash")
122122
.content(String.format("$ curl 'http://localhost:"
123-
+ tomcat.getPort() + "/' -i \\%n"
123+
+ tomcat.getPort() + "/' -i -X GET \\%n"
124124
+ " -H 'Accept: application/json' \\%n"
125125
+ " -H 'Content-Type: " + contentType + "' \\%n"
126126
+ " --cookie 'cookieName=cookieVal'")))));

0 commit comments

Comments
 (0)