Skip to content

Commit 0146f1a

Browse files
committed
Fix handling of parameters when documenting non-GET requests
Fixes gh-683
1 parent f5dda62 commit 0146f1a

File tree

6 files changed

+66
-12
lines changed

6 files changed

+66
-12
lines changed

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2020 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.
@@ -91,8 +91,10 @@ private String getUrl(Operation operation) {
9191
}
9292

9393
private boolean includeParametersInUri(OperationRequest request) {
94-
return request.getMethod() == HttpMethod.GET || (request.getContent().length > 0
95-
&& !MediaType.APPLICATION_FORM_URLENCODED.isCompatibleWith(request.getHeaders().getContentType()));
94+
HttpMethod method = request.getMethod();
95+
return (method != HttpMethod.PUT && method != HttpMethod.POST && method != HttpMethod.PATCH)
96+
|| (request.getContent().length > 0 && !MediaType.APPLICATION_FORM_URLENCODED
97+
.isCompatibleWith(request.getHeaders().getContentType()));
9698
}
9799

98100
private String getOptions(Operation operation) {

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2020 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.
@@ -129,8 +129,10 @@ private void writeOptions(OperationRequest request, PrintWriter writer) {
129129
}
130130

131131
private boolean includeParametersInUri(OperationRequest request) {
132-
return request.getMethod() == HttpMethod.GET || (request.getContent().length > 0
133-
&& !MediaType.APPLICATION_FORM_URLENCODED.isCompatibleWith(request.getHeaders().getContentType()));
132+
HttpMethod method = request.getMethod();
133+
return (method != HttpMethod.PUT && method != HttpMethod.POST && method != HttpMethod.PATCH)
134+
|| (request.getContent().length > 0 && !MediaType.APPLICATION_FORM_URLENCODED
135+
.isCompatibleWith(request.getHeaders().getContentType()));
134136
}
135137

136138
private boolean includeParametersAsFormOptions(OperationRequest request) {

spring-restdocs-core/src/main/java/org/springframework/restdocs/http/HttpRequestSnippet.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2020 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.
@@ -92,8 +92,10 @@ private String getPath(OperationRequest request) {
9292
}
9393

9494
private boolean includeParametersInUri(OperationRequest request) {
95-
return request.getMethod() == HttpMethod.GET || (request.getContent().length > 0
96-
&& !MediaType.APPLICATION_FORM_URLENCODED.isCompatibleWith(request.getHeaders().getContentType()));
95+
HttpMethod method = request.getMethod();
96+
return (method != HttpMethod.PUT && method != HttpMethod.POST && method != HttpMethod.PATCH)
97+
|| (request.getContent().length > 0 && !MediaType.APPLICATION_FORM_URLENCODED
98+
.isCompatibleWith(request.getHeaders().getContentType()));
9799
}
98100

99101
private List<Map<String, String>> getHeaders(OperationRequest request) {

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

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2020 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.
@@ -326,4 +326,20 @@ public void postWithContentAndParameters() throws IOException {
326326
.withContent("$ curl 'http://localhost/foo?a=alpha&b=bravo' -i " + "-X POST -d 'Some content'"));
327327
}
328328

329+
@Test
330+
public void deleteWithParameters() throws IOException {
331+
new CurlRequestSnippet(this.commandFormatter).document(this.operationBuilder.request("http://localhost/foo")
332+
.method("DELETE").param("a", "alpha").param("b", "bravo").build());
333+
assertThat(this.generatedSnippets.curlRequest())
334+
.is(codeBlock("bash").withContent("$ curl 'http://localhost/foo?a=alpha&b=bravo' -i " + "-X DELETE"));
335+
}
336+
337+
@Test
338+
public void deleteWithQueryString() throws IOException {
339+
new CurlRequestSnippet(this.commandFormatter).document(
340+
this.operationBuilder.request("http://localhost/foo?a=alpha&b=bravo").method("DELETE").build());
341+
assertThat(this.generatedSnippets.curlRequest())
342+
.is(codeBlock("bash").withContent("$ curl 'http://localhost/foo?a=alpha&b=bravo' -i " + "-X DELETE"));
343+
}
344+
329345
}

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

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2020 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.
@@ -330,4 +330,20 @@ public void postWithContentAndParameters() throws IOException {
330330
.withContent("$ echo 'Some content' | http POST " + "'http://localhost/foo?a=alpha&b=bravo'"));
331331
}
332332

333+
@Test
334+
public void deleteWithParameters() throws IOException {
335+
new HttpieRequestSnippet(this.commandFormatter).document(this.operationBuilder.request("http://localhost/foo")
336+
.method("DELETE").param("a", "alpha").param("b", "bravo").build());
337+
assertThat(this.generatedSnippets.httpieRequest())
338+
.is(codeBlock("bash").withContent("$ http DELETE 'http://localhost/foo?a=alpha&b=bravo'"));
339+
}
340+
341+
@Test
342+
public void deleteWithQueryString() throws IOException {
343+
new HttpieRequestSnippet(this.commandFormatter).document(
344+
this.operationBuilder.request("http://localhost/foo?a=alpha&b=bravo").method("DELETE").build());
345+
assertThat(this.generatedSnippets.httpieRequest())
346+
.is(codeBlock("bash").withContent("$ http DELETE 'http://localhost/foo?a=alpha&b=bravo'"));
347+
}
348+
333349
}

spring-restdocs-core/src/test/java/org/springframework/restdocs/http/HttpRequestSnippetTests.java

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2020 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.
@@ -307,6 +307,22 @@ public void requestWithCustomSnippetAttributes() throws IOException {
307307
assertThat(this.generatedSnippets.httpRequest()).contains("Title for the request");
308308
}
309309

310+
@Test
311+
public void deleteWithParameters() throws IOException {
312+
new HttpRequestSnippet().document(this.operationBuilder.request("http://localhost/foo").method("DELETE")
313+
.param("a", "alpha").param("b", "bravo").build());
314+
assertThat(this.generatedSnippets.httpRequest())
315+
.is(httpRequest(RequestMethod.DELETE, "/foo?a=alpha&b=bravo").header("Host", "localhost"));
316+
}
317+
318+
@Test
319+
public void deleteWithQueryString() throws IOException {
320+
new HttpRequestSnippet().document(
321+
this.operationBuilder.request("http://localhost/foo?a=alpha&b=bravo").method("DELETE").build());
322+
assertThat(this.generatedSnippets.httpRequest())
323+
.is(httpRequest(RequestMethod.DELETE, "/foo?a=alpha&b=bravo").header("Host", "localhost"));
324+
}
325+
310326
private String createPart(String content) {
311327
return this.createPart(content, true);
312328
}

0 commit comments

Comments
 (0)