Skip to content

Commit faaf3a6

Browse files
committed
Add FormEvent stream event test
Add sample that shows how FormEvents can be used to create a form submit from a stream of key/value pairs. See gh-30131
1 parent 298d6e4 commit faaf3a6

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

spring-web/src/test/java/org/springframework/http/codec/multipart/PartEventHttpMessageWriterTests.java

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,7 +18,9 @@
1818

1919
import java.nio.charset.StandardCharsets;
2020
import java.time.Duration;
21+
import java.util.AbstractMap;
2122
import java.util.Collections;
23+
import java.util.List;
2224
import java.util.Map;
2325

2426
import org.junit.jupiter.api.Test;
@@ -93,6 +95,46 @@ void write() {
9395
assertThat(decodeToString(part)).isEqualTo("AaBbCc");
9496
}
9597

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+
96138
@SuppressWarnings("ConstantConditions")
97139
private String decodeToString(Part part) {
98140
return StringDecoder.textPlainOnly().decodeToMono(part.content(),

0 commit comments

Comments
 (0)