Skip to content

Commit 638ebef

Browse files
banseok1216wilkinsona
authored andcommitted
Fix incorrect Content-Type for POST/PUT/PATCH requests without body
Signed-off-by: banseok1216 <bansuk1216@naver.com> See gh-1018
1 parent 5c5b5b4 commit 638ebef

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

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
@@ -175,7 +175,7 @@ private void writeMultipartEnd(PrintWriter writer) {
175175

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

181181
private Map<String, String> header(String name, String value) {

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,13 @@ public void postRequestWithCharset() throws IOException {
138138
.content(japaneseContent));
139139
}
140140

141+
@Test
142+
public void postRequestWithNoContentTypeAndNoBodyDoesNotIncludeContentTypeHeader() throws IOException {
143+
new HttpRequestSnippet().document(this.operationBuilder.request("http://localhost/foo").method("POST").build());
144+
assertThat(this.generatedSnippets.httpRequest())
145+
.is(httpRequest(RequestMethod.POST, "/foo").header(HttpHeaders.HOST, "localhost"));
146+
}
147+
141148
@Test
142149
public void putRequestWithContent() throws IOException {
143150
String content = "Hello, world";

0 commit comments

Comments
 (0)