|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2017 the original author or authors. |
| 2 | + * Copyright 2002-2018 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
32 | 32 | import org.springframework.core.codec.StringDecoder;
|
33 | 33 | import org.springframework.core.io.ClassPathResource;
|
34 | 34 | import org.springframework.core.io.Resource;
|
| 35 | +import org.springframework.core.io.buffer.DataBuffer; |
| 36 | +import org.springframework.core.io.buffer.DataBufferUtils; |
| 37 | +import org.springframework.core.io.buffer.DefaultDataBufferFactory; |
35 | 38 | import org.springframework.http.HttpEntity;
|
36 | 39 | import org.springframework.http.MediaType;
|
37 | 40 | import org.springframework.http.client.MultipartBodyBuilder;
|
@@ -191,6 +194,42 @@ public void singleSubscriberWithStrings() {
|
191 | 194 | this.writer.write(result, null, MediaType.MULTIPART_FORM_DATA, response, hints).block();
|
192 | 195 | }
|
193 | 196 |
|
| 197 | + @Test // SPR-16376 |
| 198 | + public void customContentDisposition() throws IOException { |
| 199 | + Resource logo = new ClassPathResource("/org/springframework/http/converter/logo.jpg"); |
| 200 | + Flux<DataBuffer> buffers = DataBufferUtils.read(logo, new DefaultDataBufferFactory(), 1024); |
| 201 | + long contentLength = logo.contentLength(); |
| 202 | + |
| 203 | + MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder(); |
| 204 | + bodyBuilder.part("resource", logo) |
| 205 | + .headers(h -> h.setContentDispositionFormData("resource", "spring.jpg")); |
| 206 | + bodyBuilder.asyncPart("buffers", buffers, DataBuffer.class) |
| 207 | + .headers(h -> { |
| 208 | + h.setContentDispositionFormData("buffers", "buffers.jpg"); |
| 209 | + h.setContentType(MediaType.IMAGE_JPEG); |
| 210 | + h.setContentLength(contentLength); |
| 211 | + }); |
| 212 | + |
| 213 | + MultiValueMap<String, HttpEntity<?>> multipartData = bodyBuilder.build(); |
| 214 | + |
| 215 | + MockServerHttpResponse response = new MockServerHttpResponse(); |
| 216 | + Map<String, Object> hints = Collections.emptyMap(); |
| 217 | + this.writer.write(Mono.just(multipartData), null, MediaType.MULTIPART_FORM_DATA, response, hints).block(); |
| 218 | + |
| 219 | + MultiValueMap<String, Part> requestParts = parse(response, hints); |
| 220 | + assertEquals(2, requestParts.size()); |
| 221 | + |
| 222 | + Part part = requestParts.getFirst("resource"); |
| 223 | + assertTrue(part instanceof FilePart); |
| 224 | + assertEquals("spring.jpg", ((FilePart) part).filename()); |
| 225 | + assertEquals(logo.getFile().length(), part.headers().getContentLength()); |
| 226 | + |
| 227 | + part = requestParts.getFirst("buffers"); |
| 228 | + assertTrue(part instanceof FilePart); |
| 229 | + assertEquals("buffers.jpg", ((FilePart) part).filename()); |
| 230 | + assertEquals(logo.getFile().length(), part.headers().getContentLength()); |
| 231 | + } |
| 232 | + |
194 | 233 | private MultiValueMap<String, Part> parse(MockServerHttpResponse response, Map<String, Object> hints) {
|
195 | 234 | MediaType contentType = response.getHeaders().getContentType();
|
196 | 235 | assertNotNull("No boundary found", contentType.getParameter("boundary"));
|
|
0 commit comments