Skip to content

Commit 60f12e4

Browse files
committed
Fix HTTP request snippet's body for multipart PATCH requests
Closes gh-933
1 parent b6df324 commit 60f12e4

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2023 the original author or authors.
2+
* Copyright 2014-2024 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.
@@ -120,16 +120,17 @@ private String getRequestBody(OperationRequest request) {
120120
if (StringUtils.hasText(content)) {
121121
writer.printf("%n%s", content);
122122
}
123-
else if (isPutOrPost(request)) {
123+
else if (isPutPostOrPatch(request)) {
124124
if (!request.getParts().isEmpty()) {
125125
writeParts(request, writer);
126126
}
127127
}
128128
return httpRequest.toString();
129129
}
130130

131-
private boolean isPutOrPost(OperationRequest request) {
132-
return HttpMethod.PUT.equals(request.getMethod()) || HttpMethod.POST.equals(request.getMethod());
131+
private boolean isPutPostOrPatch(OperationRequest request) {
132+
return HttpMethod.PUT.equals(request.getMethod()) || HttpMethod.POST.equals(request.getMethod())
133+
|| HttpMethod.PATCH.equals(request.getMethod());
133134
}
134135

135136
private void writeParts(OperationRequest request, PrintWriter writer) {
@@ -169,7 +170,7 @@ private void writeMultipartEnd(PrintWriter writer) {
169170
}
170171

171172
private boolean requiresFormEncodingContentTypeHeader(OperationRequest request) {
172-
return request.getHeaders().get(HttpHeaders.CONTENT_TYPE) == null && isPutOrPost(request)
173+
return request.getHeaders().get(HttpHeaders.CONTENT_TYPE) == null && isPutPostOrPatch(request)
173174
&& !includeParametersInUri(request);
174175
}
175176

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2023 the original author or authors.
2+
* Copyright 2014-2024 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.
@@ -165,6 +165,36 @@ public void multipartPost() throws IOException {
165165
.content(expectedContent));
166166
}
167167

168+
@Test
169+
public void multipartPut() throws IOException {
170+
new HttpRequestSnippet().document(this.operationBuilder.request("http://localhost/upload")
171+
.method("PUT")
172+
.header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE)
173+
.part("image", "<< data >>".getBytes())
174+
.build());
175+
String expectedContent = createPart(
176+
String.format("Content-Disposition: " + "form-data; " + "name=image%n%n<< data >>"));
177+
assertThat(this.generatedSnippets.httpRequest()).is(httpRequest(RequestMethod.PUT, "/upload")
178+
.header("Content-Type", "multipart/form-data; boundary=" + BOUNDARY)
179+
.header(HttpHeaders.HOST, "localhost")
180+
.content(expectedContent));
181+
}
182+
183+
@Test
184+
public void multipartPatch() throws IOException {
185+
new HttpRequestSnippet().document(this.operationBuilder.request("http://localhost/upload")
186+
.method("PATCH")
187+
.header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE)
188+
.part("image", "<< data >>".getBytes())
189+
.build());
190+
String expectedContent = createPart(
191+
String.format("Content-Disposition: " + "form-data; " + "name=image%n%n<< data >>"));
192+
assertThat(this.generatedSnippets.httpRequest()).is(httpRequest(RequestMethod.PATCH, "/upload")
193+
.header("Content-Type", "multipart/form-data; boundary=" + BOUNDARY)
194+
.header(HttpHeaders.HOST, "localhost")
195+
.content(expectedContent));
196+
}
197+
168198
@Test
169199
public void multipartPostWithFilename() throws IOException {
170200
new HttpRequestSnippet().document(this.operationBuilder.request("http://localhost/upload")

0 commit comments

Comments
 (0)