|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2022 the original author or authors. |
| 2 | + * Copyright 2002-2023 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.
|
|
18 | 18 |
|
19 | 19 | import java.nio.charset.StandardCharsets;
|
20 | 20 | import java.time.Duration;
|
| 21 | +import java.util.AbstractMap; |
21 | 22 | import java.util.Collections;
|
| 23 | +import java.util.List; |
22 | 24 | import java.util.Map;
|
23 | 25 |
|
24 | 26 | import org.junit.jupiter.api.Test;
|
@@ -93,6 +95,46 @@ void write() {
|
93 | 95 | assertThat(decodeToString(part)).isEqualTo("AaBbCc");
|
94 | 96 | }
|
95 | 97 |
|
| 98 | + @Test |
| 99 | + void writeFormEventStream() { |
| 100 | + Flux<Map.Entry<String, String>> body = Flux.just( |
| 101 | + new AbstractMap.SimpleEntry<>("name 1", "value 1"), |
| 102 | + new AbstractMap.SimpleEntry<>("name 2", "value 2+1"), |
| 103 | + new AbstractMap.SimpleEntry<>("name 2", "value 2+2") |
| 104 | + ); |
| 105 | + |
| 106 | + Flux<PartEvent> partEvents = body |
| 107 | + .concatMap(entry -> FormPartEvent.create(entry.getKey(), entry.getValue())); |
| 108 | + |
| 109 | + Map<String, Object> hints = Collections.emptyMap(); |
| 110 | + this.writer.write(partEvents, null, MediaType.MULTIPART_FORM_DATA, this.response, hints) |
| 111 | + .block(Duration.ofSeconds(5)); |
| 112 | + |
| 113 | + MultiValueMap<String, Part> requestParts = parse(this.response, hints); |
| 114 | + assertThat(requestParts).hasSize(2); |
| 115 | + |
| 116 | + Part part = requestParts.getFirst("name 1"); |
| 117 | + assertThat(part.name()).isEqualTo("name 1"); |
| 118 | + assertThat(part.headers().getContentType().isCompatibleWith(MediaType.TEXT_PLAIN)).isTrue(); |
| 119 | + String value = decodeToString(part); |
| 120 | + assertThat(value).isEqualTo("value 1"); |
| 121 | + |
| 122 | + List<Part> parts = requestParts.get("name 2"); |
| 123 | + assertThat(parts).hasSize(2); |
| 124 | + |
| 125 | + part = parts.get(0); |
| 126 | + assertThat(part.name()).isEqualTo("name 2"); |
| 127 | + assertThat(part.headers().getContentType().isCompatibleWith(MediaType.TEXT_PLAIN)).isTrue(); |
| 128 | + value = decodeToString(part); |
| 129 | + assertThat(value).isEqualTo("value 2+1"); |
| 130 | + |
| 131 | + part = parts.get(1); |
| 132 | + assertThat(part.name()).isEqualTo("name 2"); |
| 133 | + assertThat(part.headers().getContentType().isCompatibleWith(MediaType.TEXT_PLAIN)).isTrue(); |
| 134 | + value = decodeToString(part); |
| 135 | + assertThat(value).isEqualTo("value 2+2"); |
| 136 | + } |
| 137 | + |
96 | 138 | @SuppressWarnings("ConstantConditions")
|
97 | 139 | private String decodeToString(Part part) {
|
98 | 140 | return StringDecoder.textPlainOnly().decodeToMono(part.content(),
|
|
0 commit comments