Skip to content

Commit 33a16d3

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 33a16d3

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

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

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,6 @@ private String getPath(OperationRequest request) {
8383
return path;
8484
}
8585

86-
private boolean includeParametersInUri(OperationRequest request) {
87-
HttpMethod method = request.getMethod();
88-
return (method != HttpMethod.PUT && method != HttpMethod.POST && method != HttpMethod.PATCH)
89-
|| (request.getContent().length > 0 && !MediaType.APPLICATION_FORM_URLENCODED
90-
.isCompatibleWith(request.getHeaders().getContentType()));
91-
}
92-
9386
private List<Map<String, String>> getHeaders(OperationRequest request) {
9487
List<Map<String, String>> headers = new ArrayList<>();
9588

@@ -113,9 +106,6 @@ private List<Map<String, String>> getHeaders(OperationRequest request) {
113106
headers.add(header(HttpHeaders.COOKIE, String.join("; ", cookies)));
114107
}
115108

116-
if (requiresFormEncodingContentTypeHeader(request)) {
117-
headers.add(header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE));
118-
}
119109
return headers;
120110
}
121111

@@ -176,11 +166,6 @@ private void writeMultipartEnd(PrintWriter writer) {
176166
writer.printf("--%s--", MULTIPART_BOUNDARY);
177167
}
178168

179-
private boolean requiresFormEncodingContentTypeHeader(OperationRequest request) {
180-
return request.getHeaders().get(HttpHeaders.CONTENT_TYPE) == null && isPutPostOrPatch(request)
181-
&& !includeParametersInUri(request);
182-
}
183-
184169
private Map<String, String> header(String name, String value) {
185170
Map<String, String> header = new HashMap<>();
186171
header.put("name", name);

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,24 @@ 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(
249+
OperationBuilder operationBuilder, 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
257+
.post("/foo")
258+
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE)
259+
.header(HttpHeaders.HOST, "localhost")
260+
.header(HttpHeaders.CONTENT_LENGTH, body.getBytes().length)
261+
.content(body)
262+
);
263+
}
264+
247265
private String createPart(String content) {
248266
return this.createPart(content, true);
249267
}

0 commit comments

Comments
 (0)