Skip to content

Commit d0f11e8

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 d0f11e8

4 files changed

Lines changed: 9 additions & 1 deletion

File tree

.DS_Store

10 KB
Binary file not shown.

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

Lines changed: 1 addition & 1 deletion
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) {

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

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

247+
@RenderedSnippetTest
248+
void postRequestWithNoContentTypeAndNoBodyDoesNotIncludeContentTypeHeader(OperationBuilder operationBuilder,
249+
AssertableSnippets snippets) throws IOException {
250+
new HttpRequestSnippet().document(operationBuilder.request("http://localhost/foo").method("POST").build());
251+
assertThat(snippets.httpRequest())
252+
.isHttpRequest((request) -> request.post("/foo").header(HttpHeaders.HOST, "localhost"));
253+
}
254+
247255
private String createPart(String content) {
248256
return this.createPart(content, true);
249257
}

spring-restdocs-docs/.DS_Store

6 KB
Binary file not shown.

0 commit comments

Comments
 (0)