diff --git a/spring-restdocs-core/src/main/java/org/springframework/restdocs/http/HttpRequestSnippet.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/http/HttpRequestSnippet.java index f72337af..aedebdf7 100644 --- a/spring-restdocs-core/src/main/java/org/springframework/restdocs/http/HttpRequestSnippet.java +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/http/HttpRequestSnippet.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2016 the original author or authors. + * Copyright 2014-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -158,12 +158,12 @@ public class HttpRequestSnippet extends TemplatedSnippet { for (Entry> parameter : request.getParameters().entrySet()) { if (parameter.getValue().isEmpty()) { writePartBoundary(writer); - writePart(parameter.getKey(), "", null, writer); + writePart(parameter.getKey(), "", null, null, writer); } else { for (String value : parameter.getValue()) { writePartBoundary(writer); - writePart(parameter.getKey(), value, null, writer); + writePart(parameter.getKey(), value, null, null, writer); writer.println(); } } @@ -181,13 +181,17 @@ public class HttpRequestSnippet extends TemplatedSnippet { } private void writePart(OperationRequestPart part, PrintWriter writer) { - writePart(part.getName(), part.getContentAsString(), + writePart(part.getName(), part.getContentAsString(), part.getSubmittedFileName(), part.getHeaders().getContentType(), writer); } - private void writePart(String name, String value, MediaType contentType, - PrintWriter writer) { - writer.printf("Content-Disposition: form-data; name=%s%n", name); + private void writePart(String name, String value, String filename, + MediaType contentType, PrintWriter writer) { + writer.printf("Content-Disposition: form-data; name=%s", name); + if (StringUtils.hasText(filename)) { + writer.printf("; filename=%s", filename); + } + writer.printf("%n"); if (contentType != null) { writer.printf("Content-Type: %s%n", contentType); } diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/http/HttpRequestSnippetTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/http/HttpRequestSnippetTests.java index 4312770e..32687d60 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/http/HttpRequestSnippetTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/http/HttpRequestSnippetTests.java @@ -285,6 +285,23 @@ public class HttpRequestSnippetTests extends AbstractSnippetTests { .part("image", "<< data >>".getBytes()).build()); } + @Test + public void multipartPostWithFilename() throws IOException { + String expectedContent = createPart(String.format("Content-Disposition: " + + "form-data; " + "name=image; filename=image.png%n%n<< data >>")); + this.snippets.expectHttpRequest() + .withContents(httpRequest(RequestMethod.POST, "/upload") + .header("Content-Type", + "multipart/form-data; boundary=" + BOUNDARY) + .header(HttpHeaders.HOST, "localhost").content(expectedContent)); + new HttpRequestSnippet().document( + this.operationBuilder.request("http://localhost/upload").method("POST") + .header(HttpHeaders.CONTENT_TYPE, + MediaType.MULTIPART_FORM_DATA_VALUE) + .part("image", "<< data >>".getBytes()).submittedFileName("image.png") + .build()); + } + @Test public void multipartPostWithParameters() throws IOException { String param1Part = createPart(