Merge branch '1.2.x'

This commit is contained in:
Andy Wilkinson
2017-09-23 17:40:29 +01:00
2 changed files with 28 additions and 7 deletions

View File

@@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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<String, List<String>> parameter : request.getParameters().entrySet()) { for (Entry<String, List<String>> parameter : request.getParameters().entrySet()) {
if (parameter.getValue().isEmpty()) { if (parameter.getValue().isEmpty()) {
writePartBoundary(writer); writePartBoundary(writer);
writePart(parameter.getKey(), "", null, writer); writePart(parameter.getKey(), "", null, null, writer);
} }
else { else {
for (String value : parameter.getValue()) { for (String value : parameter.getValue()) {
writePartBoundary(writer); writePartBoundary(writer);
writePart(parameter.getKey(), value, null, writer); writePart(parameter.getKey(), value, null, null, writer);
writer.println(); writer.println();
} }
} }
@@ -181,13 +181,17 @@ public class HttpRequestSnippet extends TemplatedSnippet {
} }
private void writePart(OperationRequestPart part, PrintWriter writer) { private void writePart(OperationRequestPart part, PrintWriter writer) {
writePart(part.getName(), part.getContentAsString(), writePart(part.getName(), part.getContentAsString(), part.getSubmittedFileName(),
part.getHeaders().getContentType(), writer); part.getHeaders().getContentType(), writer);
} }
private void writePart(String name, String value, MediaType contentType, private void writePart(String name, String value, String filename,
PrintWriter writer) { MediaType contentType, PrintWriter writer) {
writer.printf("Content-Disposition: form-data; name=%s%n", name); writer.printf("Content-Disposition: form-data; name=%s", name);
if (StringUtils.hasText(filename)) {
writer.printf("; filename=%s", filename);
}
writer.printf("%n");
if (contentType != null) { if (contentType != null) {
writer.printf("Content-Type: %s%n", contentType); writer.printf("Content-Type: %s%n", contentType);
} }

View File

@@ -285,6 +285,23 @@ public class HttpRequestSnippetTests extends AbstractSnippetTests {
.part("image", "<< data >>".getBytes()).build()); .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 @Test
public void multipartPostWithParameters() throws IOException { public void multipartPostWithParameters() throws IOException {
String param1Part = createPart( String param1Part = createPart(