|
16 | 16 |
|
17 | 17 | package org.springframework.http.codec.multipart;
|
18 | 18 |
|
| 19 | +import java.io.IOException; |
19 | 20 | import java.time.Duration;
|
20 | 21 | import java.util.Collections;
|
21 | 22 | import java.util.List;
|
|
25 | 26 | import org.reactivestreams.Publisher;
|
26 | 27 | import reactor.core.publisher.Flux;
|
27 | 28 | import reactor.core.publisher.Mono;
|
| 29 | +import reactor.core.publisher.UnicastProcessor; |
28 | 30 |
|
29 | 31 | import org.springframework.core.ResolvableType;
|
30 | 32 | import org.springframework.core.codec.StringDecoder;
|
@@ -146,9 +148,48 @@ public String getFilename() {
|
146 | 148 | Collections.emptyMap()).block(Duration.ZERO);
|
147 | 149 |
|
148 | 150 | assertEquals("foobarbaz", value);
|
| 151 | + } |
| 152 | + |
| 153 | + @Test // SPR-16402 |
| 154 | + public void singleSubscriberWithResource() throws IOException { |
| 155 | + UnicastProcessor<Resource> processor = UnicastProcessor.create(); |
| 156 | + Resource logo = new ClassPathResource("/org/springframework/http/converter/logo.jpg"); |
| 157 | + Mono.just(logo).subscribe(processor); |
| 158 | + |
| 159 | + MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder(); |
| 160 | + bodyBuilder.asyncPart("logo", processor, Resource.class); |
| 161 | + |
| 162 | + Mono<MultiValueMap<String, HttpEntity<?>>> result = Mono.just(bodyBuilder.build()); |
| 163 | + |
| 164 | + MockServerHttpResponse response = new MockServerHttpResponse(); |
| 165 | + Map<String, Object> hints = Collections.emptyMap(); |
| 166 | + this.writer.write(result, null, MediaType.MULTIPART_FORM_DATA, response, hints).block(); |
| 167 | + |
| 168 | + MultiValueMap<String, Part> requestParts = parse(response, hints); |
| 169 | + assertEquals(1, requestParts.size()); |
149 | 170 |
|
| 171 | + Part part = requestParts.getFirst("logo"); |
| 172 | + assertEquals("logo", part.name()); |
| 173 | +// TODO: a Resource written as an async part doesn't have a file name in the contentDisposition |
| 174 | +// assertTrue(part instanceof FilePart); |
| 175 | +// assertEquals("logo.jpg", ((FilePart) part).filename()); |
| 176 | + assertEquals(MediaType.IMAGE_JPEG, part.headers().getContentType()); |
| 177 | + assertEquals(logo.getFile().length(), part.headers().getContentLength()); |
| 178 | + } |
150 | 179 |
|
| 180 | + @Test // SPR-16402 |
| 181 | + public void singleSubscriberWithStrings() { |
| 182 | + UnicastProcessor<String> processor = UnicastProcessor.create(); |
| 183 | + Flux.just("foo", "bar", "baz").subscribe(processor); |
151 | 184 |
|
| 185 | + MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder(); |
| 186 | + bodyBuilder.asyncPart("name", processor, String.class); |
| 187 | + |
| 188 | + Mono<MultiValueMap<String, HttpEntity<?>>> result = Mono.just(bodyBuilder.build()); |
| 189 | + |
| 190 | + MockServerHttpResponse response = new MockServerHttpResponse(); |
| 191 | + Map<String, Object> hints = Collections.emptyMap(); |
| 192 | + this.writer.write(result, null, MediaType.MULTIPART_FORM_DATA, response, hints).block(); |
152 | 193 | }
|
153 | 194 |
|
154 | 195 | private MultiValueMap<String, Part> parse(MockServerHttpResponse response, Map<String, Object> hints) {
|
|
0 commit comments