|
16 | 16 |
|
17 | 17 | package com.linecorp.armeria.internal.server.annotation; |
18 | 18 |
|
| 19 | +import static com.google.common.collect.ImmutableList.toImmutableList; |
19 | 20 | import static com.google.common.collect.ImmutableMap.toImmutableMap; |
20 | 21 | import static net.javacrumbs.jsonunit.fluent.JsonFluentAssert.assertThatJson; |
21 | 22 | import static org.assertj.core.api.Assertions.assertThat; |
22 | 23 |
|
23 | 24 | import java.io.File; |
24 | 25 | import java.io.IOException; |
25 | 26 | import java.nio.charset.StandardCharsets; |
| 27 | +import java.util.List; |
26 | 28 | import java.util.function.Function; |
27 | 29 |
|
28 | 30 | import org.junit.jupiter.api.Test; |
29 | 31 | import org.junit.jupiter.api.extension.RegisterExtension; |
30 | 32 | import org.junit.jupiter.params.ParameterizedTest; |
31 | 33 | import org.junit.jupiter.params.provider.ValueSource; |
32 | 34 |
|
| 35 | +import com.google.common.collect.ImmutableList; |
33 | 36 | import com.google.common.collect.ImmutableMap; |
34 | 37 | import com.google.common.io.Files; |
35 | 38 |
|
@@ -89,6 +92,80 @@ void testUploadFile(String path) throws Exception { |
89 | 92 | "\"param1\":\"armeria\"}"); |
90 | 93 | } |
91 | 94 |
|
| 95 | + @Test |
| 96 | + void testUploadMultipleFilesWithSameNameMultipart() { |
| 97 | + final Multipart multipart = Multipart.of( |
| 98 | + BodyPart.of(ContentDisposition.of("form-data", "files", "bar.txt"), "bar"), |
| 99 | + BodyPart.of(ContentDisposition.of("form-data", "files", "qux.txt"), "qux"), |
| 100 | + BodyPart.of(ContentDisposition.of("form-data", "files", "quz.txt"), MediaType.PLAIN_TEXT, |
| 101 | + "quz"), |
| 102 | + BodyPart.of(ContentDisposition.of("form-data", "params"), "hello"), |
| 103 | + BodyPart.of(ContentDisposition.of("form-data", "params"), "armeria") |
| 104 | + ); |
| 105 | + |
| 106 | + final AggregatedHttpResponse response = |
| 107 | + server.blockingWebClient().execute( |
| 108 | + multipart.toHttpRequest("/uploadWithFileParamSameNameMultipart") |
| 109 | + ); |
| 110 | + |
| 111 | + final ImmutableMap<String, List<String>> expected = ImmutableMap.of( |
| 112 | + "files", ImmutableList.of("fileName bar.txt data bar contentType application/octet-stream", |
| 113 | + "fileName qux.txt data qux contentType application/octet-stream", |
| 114 | + "fileName quz.txt data quz contentType text/plain"), |
| 115 | + "params", ImmutableList.of("hello", |
| 116 | + "armeria" |
| 117 | + ) |
| 118 | + ); |
| 119 | + assertThatJson(response.contentUtf8()).isEqualTo(expected); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + void testUploadMultipleFilesWithSameNamePath() { |
| 124 | + final Multipart multipart = Multipart.of( |
| 125 | + BodyPart.of(ContentDisposition.of("form-data", "files", "bar.txt"), "bar"), |
| 126 | + BodyPart.of(ContentDisposition.of("form-data", "files", "qux.txt"), "qux"), |
| 127 | + BodyPart.of(ContentDisposition.of("form-data", "files", "quz.txt"), MediaType.PLAIN_TEXT, |
| 128 | + "quz"), |
| 129 | + BodyPart.of(ContentDisposition.of("form-data", "params"), "hello"), |
| 130 | + BodyPart.of(ContentDisposition.of("form-data", "params"), "armeria") |
| 131 | + ); |
| 132 | + |
| 133 | + final AggregatedHttpResponse response = |
| 134 | + server.blockingWebClient().execute( |
| 135 | + multipart.toHttpRequest("/uploadWithFileParamSameNamePath") |
| 136 | + ); |
| 137 | + |
| 138 | + final ImmutableMap<String, List<String>> expected = ImmutableMap.of( |
| 139 | + "files", ImmutableList.of("data bar", "data qux", "data quz"), |
| 140 | + "params", ImmutableList.of("hello", "armeria" |
| 141 | + ) |
| 142 | + ); |
| 143 | + assertThatJson(response.contentUtf8()).isEqualTo(expected); |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + void testUploadMultipleFilesWithSameNameFile() { |
| 148 | + final Multipart multipart = Multipart.of( |
| 149 | + BodyPart.of(ContentDisposition.of("form-data", "files", "bar.txt"), "bar"), |
| 150 | + BodyPart.of(ContentDisposition.of("form-data", "files", "qux.txt"), "qux"), |
| 151 | + BodyPart.of(ContentDisposition.of("form-data", "files", "quz.txt"), MediaType.PLAIN_TEXT, |
| 152 | + "quz"), |
| 153 | + BodyPart.of(ContentDisposition.of("form-data", "params"), "hello"), |
| 154 | + BodyPart.of(ContentDisposition.of("form-data", "params"), "armeria") |
| 155 | + ); |
| 156 | + |
| 157 | + final AggregatedHttpResponse response = |
| 158 | + server.blockingWebClient().execute( |
| 159 | + multipart.toHttpRequest("/uploadWithFileParamSameNameFile") |
| 160 | + ); |
| 161 | + |
| 162 | + final ImmutableMap<String, List<String>> expected = ImmutableMap.of( |
| 163 | + "files", ImmutableList.of("data bar", "data qux", "data quz"), |
| 164 | + "params", ImmutableList.of("hello", "armeria") |
| 165 | + ); |
| 166 | + assertThatJson(response.contentUtf8()).isEqualTo(expected); |
| 167 | + } |
| 168 | + |
92 | 169 | @Test |
93 | 170 | void emptyBodyPart() { |
94 | 171 | final Multipart multipart = Multipart.of(); |
@@ -190,5 +267,78 @@ public HttpResponse uploadWithMultipartObject(Multipart multipart) { |
190 | 267 | return HttpResponse.ofJson(body); |
191 | 268 | })); |
192 | 269 | } |
| 270 | + |
| 271 | + @Blocking |
| 272 | + @Post |
| 273 | + @Path("/uploadWithFileParamSameNameMultipart") |
| 274 | + public HttpResponse uploadWithFileParamSameNameMultipart( |
| 275 | + @Param("params") List<String> params, |
| 276 | + @Param("files") List<MultipartFile> files) { |
| 277 | + final List<String> fileData = files |
| 278 | + .stream() |
| 279 | + .map(multipartFile -> { |
| 280 | + try { |
| 281 | + final String data = Files.asCharSource(multipartFile.file(), |
| 282 | + StandardCharsets.UTF_8) |
| 283 | + .read(); |
| 284 | + return String.format("fileName %s data %s contentType %s", |
| 285 | + multipartFile.filename(), data, |
| 286 | + multipartFile.headers().contentType()); |
| 287 | + } catch (IOException e) { |
| 288 | + throw new RuntimeException(e); |
| 289 | + } |
| 290 | + }).collect(toImmutableList()); |
| 291 | + final ImmutableMap<String, List<String>> content = |
| 292 | + ImmutableMap.of("files", fileData, |
| 293 | + "params", params); |
| 294 | + return HttpResponse.ofJson(content); |
| 295 | + } |
| 296 | + |
| 297 | + @Blocking |
| 298 | + @Post |
| 299 | + @Path("/uploadWithFileParamSameNamePath") |
| 300 | + public HttpResponse uploadWithFileParamSameNamePath( |
| 301 | + @Param("params") List<String> params, |
| 302 | + @Param("files") List<java.nio.file.Path> files) { |
| 303 | + final List<String> fileData = files |
| 304 | + .stream() |
| 305 | + .map(path -> { |
| 306 | + try { |
| 307 | + final String data = Files.asCharSource(path.toFile(), StandardCharsets.UTF_8) |
| 308 | + .read(); |
| 309 | + return String.format("data %s", data); |
| 310 | + } catch (IOException e) { |
| 311 | + throw new RuntimeException(e); |
| 312 | + } |
| 313 | + }).collect(toImmutableList()); |
| 314 | + final ImmutableMap<String, List<String>> content = |
| 315 | + ImmutableMap.of("files", fileData, |
| 316 | + "params", params); |
| 317 | + return HttpResponse.ofJson(content); |
| 318 | + } |
| 319 | + |
| 320 | + @Blocking |
| 321 | + @Post |
| 322 | + @Path("/uploadWithFileParamSameNameFile") |
| 323 | + public HttpResponse uploadWithFileParamSameNameFile( |
| 324 | + @Param("params") List<String> params, |
| 325 | + @Param("files") List<File> files, |
| 326 | + RequestHeaders headers) { |
| 327 | + final List<String> fileData = files |
| 328 | + .stream() |
| 329 | + .map(file -> { |
| 330 | + try { |
| 331 | + final String data = Files.asCharSource(file, StandardCharsets.UTF_8) |
| 332 | + .read(); |
| 333 | + return String.format("data %s", data); |
| 334 | + } catch (IOException e) { |
| 335 | + throw new RuntimeException(e); |
| 336 | + } |
| 337 | + }).collect(toImmutableList()); |
| 338 | + final ImmutableMap<String, List<String>> content = |
| 339 | + ImmutableMap.of("files", fileData, |
| 340 | + "params", params); |
| 341 | + return HttpResponse.ofJson(content); |
| 342 | + } |
193 | 343 | } |
194 | 344 | } |
0 commit comments