Skip to content

Commit de67160

Browse files
committed
Merge branch '1.2.x'
2 parents 4e99df6 + d22dc1e commit de67160

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2016 the original author or authors.
2+
* Copyright 2014-2017 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.
@@ -158,12 +158,12 @@ private void writeParts(OperationRequest request, PrintWriter writer) {
158158
for (Entry<String, List<String>> parameter : request.getParameters().entrySet()) {
159159
if (parameter.getValue().isEmpty()) {
160160
writePartBoundary(writer);
161-
writePart(parameter.getKey(), "", null, writer);
161+
writePart(parameter.getKey(), "", null, null, writer);
162162
}
163163
else {
164164
for (String value : parameter.getValue()) {
165165
writePartBoundary(writer);
166-
writePart(parameter.getKey(), value, null, writer);
166+
writePart(parameter.getKey(), value, null, null, writer);
167167
writer.println();
168168
}
169169
}
@@ -181,13 +181,17 @@ private void writePartBoundary(PrintWriter writer) {
181181
}
182182

183183
private void writePart(OperationRequestPart part, PrintWriter writer) {
184-
writePart(part.getName(), part.getContentAsString(),
184+
writePart(part.getName(), part.getContentAsString(), part.getSubmittedFileName(),
185185
part.getHeaders().getContentType(), writer);
186186
}
187187

188-
private void writePart(String name, String value, MediaType contentType,
189-
PrintWriter writer) {
190-
writer.printf("Content-Disposition: form-data; name=%s%n", name);
188+
private void writePart(String name, String value, String filename,
189+
MediaType contentType, PrintWriter writer) {
190+
writer.printf("Content-Disposition: form-data; name=%s", name);
191+
if (StringUtils.hasText(filename)) {
192+
writer.printf("; filename=%s", filename);
193+
}
194+
writer.printf("%n");
191195
if (contentType != null) {
192196
writer.printf("Content-Type: %s%n", contentType);
193197
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,23 @@ public void multipartPost() throws IOException {
285285
.part("image", "<< data >>".getBytes()).build());
286286
}
287287

288+
@Test
289+
public void multipartPostWithFilename() throws IOException {
290+
String expectedContent = createPart(String.format("Content-Disposition: "
291+
+ "form-data; " + "name=image; filename=image.png%n%n<< data >>"));
292+
this.snippets.expectHttpRequest()
293+
.withContents(httpRequest(RequestMethod.POST, "/upload")
294+
.header("Content-Type",
295+
"multipart/form-data; boundary=" + BOUNDARY)
296+
.header(HttpHeaders.HOST, "localhost").content(expectedContent));
297+
new HttpRequestSnippet().document(
298+
this.operationBuilder.request("http://localhost/upload").method("POST")
299+
.header(HttpHeaders.CONTENT_TYPE,
300+
MediaType.MULTIPART_FORM_DATA_VALUE)
301+
.part("image", "<< data >>".getBytes()).submittedFileName("image.png")
302+
.build());
303+
}
304+
288305
@Test
289306
public void multipartPostWithParameters() throws IOException {
290307
String param1Part = createPart(

0 commit comments

Comments
 (0)