Skip to content

Commit ba17f1c

Browse files
committed
Fix incorrect Content-Type inference for POST/PUT/PATCH requests without body
Signed-off-by: banseok1216 <bansuk1216@naver.com>
1 parent b77e80e commit ba17f1c

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ private void writeMultipartEnd(PrintWriter writer) {
178178

179179
private boolean requiresFormEncodingContentTypeHeader(OperationRequest request) {
180180
return request.getHeaders().get(HttpHeaders.CONTENT_TYPE) == null && isPutPostOrPatch(request)
181-
&& !includeParametersInUri(request);
181+
&& request.getContent().length > 0 && !includeParametersInUri(request);
182182
}
183183

184184
private Map<String, String> header(String name, String value) {
@@ -188,4 +188,4 @@ private Map<String, String> header(String name, String value) {
188188
return header;
189189
}
190190

191-
}
191+
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,22 @@ void deleteWithQueryString(OperationBuilder operationBuilder, AssertableSnippets
244244
.isHttpRequest((request) -> request.delete("/foo?a=alpha&b=bravo").header("Host", "localhost"));
245245
}
246246

247+
@RenderedSnippetTest
248+
void postFormUrlEncodedRequestIsDocumentedWhenContentTypeIsExplicit(OperationBuilder operationBuilder,
249+
AssertableSnippets snippets) throws IOException {
250+
String body = "a=alpha&b=bravo";
251+
new HttpRequestSnippet().document(operationBuilder.request("http://localhost/foo")
252+
.method("POST")
253+
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE)
254+
.content(body)
255+
.build());
256+
assertThat(snippets.httpRequest()).isHttpRequest((request) -> request.post("/foo")
257+
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE)
258+
.header(HttpHeaders.HOST, "localhost")
259+
.header(HttpHeaders.CONTENT_LENGTH, body.getBytes().length)
260+
.content(body));
261+
}
262+
247263
private String createPart(String content) {
248264
return this.createPart(content, true);
249265
}

0 commit comments

Comments
 (0)