Allow client-side use of BodyExtractors#toFormData
Issue: SPR-16804
This commit is contained in:
parent
020c6c0102
commit
4ff4d5a181
|
|
@ -165,19 +165,22 @@ public abstract class BodyExtractors {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return a {@code BodyExtractor} that reads form data into a {@link MultiValueMap}.
|
* Return a {@code BodyExtractor} that reads form data into a {@link MultiValueMap}.
|
||||||
|
* <p>As of 5.1 this method can also be used on the client side to read form
|
||||||
|
* data from a server response (e.g. OAuth).
|
||||||
* @return a {@code BodyExtractor} that reads form data
|
* @return a {@code BodyExtractor} that reads form data
|
||||||
*/
|
*/
|
||||||
// Note that the returned BodyExtractor is parameterized to ServerHttpRequest, not
|
public static BodyExtractor<Mono<MultiValueMap<String, String>>, ReactiveHttpInputMessage> toFormData() {
|
||||||
// ReactiveHttpInputMessage like other methods, since reading form data only typically happens on
|
return (message, context) -> {
|
||||||
// the server-side
|
|
||||||
public static BodyExtractor<Mono<MultiValueMap<String, String>>, ServerHttpRequest> toFormData() {
|
|
||||||
return (request, context) -> {
|
|
||||||
ResolvableType type = FORM_MAP_TYPE;
|
ResolvableType type = FORM_MAP_TYPE;
|
||||||
HttpMessageReader<MultiValueMap<String, String>> reader =
|
HttpMessageReader<MultiValueMap<String, String>> reader =
|
||||||
messageReader(type, MediaType.APPLICATION_FORM_URLENCODED, context);
|
messageReader(type, MediaType.APPLICATION_FORM_URLENCODED, context);
|
||||||
return context.serverResponse()
|
Optional<ServerHttpResponse> response = context.serverResponse();
|
||||||
.map(response -> reader.readMono(type, type, request, response, context.hints()))
|
if (response.isPresent() && message instanceof ServerHttpRequest) {
|
||||||
.orElseGet(() -> reader.readMono(type, request, context.hints()));
|
return reader.readMono(type, type, (ServerHttpRequest) message, response.get(), context.hints());
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return reader.readMono(type, message, context.hints());
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -107,7 +107,7 @@ public class BodyExtractorsTests {
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void toMono() throws Exception {
|
public void toMono() {
|
||||||
BodyExtractor<Mono<String>, ReactiveHttpInputMessage> extractor = BodyExtractors.toMono(String.class);
|
BodyExtractor<Mono<String>, ReactiveHttpInputMessage> extractor = BodyExtractors.toMono(String.class);
|
||||||
|
|
||||||
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
|
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
|
||||||
|
|
@ -125,7 +125,7 @@ public class BodyExtractorsTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void toMonoParameterizedTypeReference() throws Exception {
|
public void toMonoParameterizedTypeReference() {
|
||||||
BodyExtractor<Mono<Map<String, String>>, ReactiveHttpInputMessage> extractor =
|
BodyExtractor<Mono<Map<String, String>>, ReactiveHttpInputMessage> extractor =
|
||||||
BodyExtractors.toMono(new ParameterizedTypeReference<Map<String, String>>() {});
|
BodyExtractors.toMono(new ParameterizedTypeReference<Map<String, String>>() {});
|
||||||
|
|
||||||
|
|
@ -147,7 +147,7 @@ public class BodyExtractorsTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void toMonoWithHints() throws Exception {
|
public void toMonoWithHints() {
|
||||||
BodyExtractor<Mono<User>, ReactiveHttpInputMessage> extractor = BodyExtractors.toMono(User.class);
|
BodyExtractor<Mono<User>, ReactiveHttpInputMessage> extractor = BodyExtractors.toMono(User.class);
|
||||||
this.hints.put(JSON_VIEW_HINT, SafeToDeserialize.class);
|
this.hints.put(JSON_VIEW_HINT, SafeToDeserialize.class);
|
||||||
|
|
||||||
|
|
@ -172,7 +172,7 @@ public class BodyExtractorsTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test // SPR-15758
|
@Test // SPR-15758
|
||||||
public void toMonoWithEmptyBodyAndNoContentType() throws Exception {
|
public void toMonoWithEmptyBodyAndNoContentType() {
|
||||||
BodyExtractor<Mono<Map<String, String>>, ReactiveHttpInputMessage> extractor =
|
BodyExtractor<Mono<Map<String, String>>, ReactiveHttpInputMessage> extractor =
|
||||||
BodyExtractors.toMono(new ParameterizedTypeReference<Map<String, String>>() {});
|
BodyExtractors.toMono(new ParameterizedTypeReference<Map<String, String>>() {});
|
||||||
|
|
||||||
|
|
@ -183,7 +183,7 @@ public class BodyExtractorsTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void toFlux() throws Exception {
|
public void toFlux() {
|
||||||
BodyExtractor<Flux<String>, ReactiveHttpInputMessage> extractor = BodyExtractors.toFlux(String.class);
|
BodyExtractor<Flux<String>, ReactiveHttpInputMessage> extractor = BodyExtractors.toFlux(String.class);
|
||||||
|
|
||||||
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
|
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
|
||||||
|
|
@ -201,7 +201,7 @@ public class BodyExtractorsTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void toFluxWithHints() throws Exception {
|
public void toFluxWithHints() {
|
||||||
BodyExtractor<Flux<User>, ReactiveHttpInputMessage> extractor = BodyExtractors.toFlux(User.class);
|
BodyExtractor<Flux<User>, ReactiveHttpInputMessage> extractor = BodyExtractors.toFlux(User.class);
|
||||||
this.hints.put(JSON_VIEW_HINT, SafeToDeserialize.class);
|
this.hints.put(JSON_VIEW_HINT, SafeToDeserialize.class);
|
||||||
|
|
||||||
|
|
@ -230,7 +230,7 @@ public class BodyExtractorsTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void toFluxUnacceptable() throws Exception {
|
public void toFluxUnacceptable() {
|
||||||
BodyExtractor<Flux<String>, ReactiveHttpInputMessage> extractor = BodyExtractors.toFlux(String.class);
|
BodyExtractor<Flux<String>, ReactiveHttpInputMessage> extractor = BodyExtractors.toFlux(String.class);
|
||||||
|
|
||||||
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
|
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
|
||||||
|
|
@ -266,9 +266,7 @@ public class BodyExtractorsTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void toFormData() throws Exception {
|
public void toFormData() {
|
||||||
BodyExtractor<Mono<MultiValueMap<String, String>>, ServerHttpRequest> extractor = BodyExtractors.toFormData();
|
|
||||||
|
|
||||||
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
|
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
|
||||||
String text = "name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3";
|
String text = "name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3";
|
||||||
DefaultDataBuffer dataBuffer = factory.wrap(ByteBuffer.wrap(text.getBytes(StandardCharsets.UTF_8)));
|
DefaultDataBuffer dataBuffer = factory.wrap(ByteBuffer.wrap(text.getBytes(StandardCharsets.UTF_8)));
|
||||||
|
|
@ -278,7 +276,7 @@ public class BodyExtractorsTests {
|
||||||
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
|
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
|
||||||
.body(body);
|
.body(body);
|
||||||
|
|
||||||
Mono<MultiValueMap<String, String>> result = extractor.extract(request, this.context);
|
Mono<MultiValueMap<String, String>> result = BodyExtractors.toFormData().extract(request, this.context);
|
||||||
|
|
||||||
StepVerifier.create(result)
|
StepVerifier.create(result)
|
||||||
.consumeNextWith(form -> {
|
.consumeNextWith(form -> {
|
||||||
|
|
@ -295,7 +293,7 @@ public class BodyExtractorsTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void toParts() throws Exception {
|
public void toParts() {
|
||||||
BodyExtractor<Flux<Part>, ServerHttpRequest> extractor = BodyExtractors.toParts();
|
BodyExtractor<Flux<Part>, ServerHttpRequest> extractor = BodyExtractors.toParts();
|
||||||
|
|
||||||
String bodyContents = "-----------------------------9051914041544843365972754266\r\n" +
|
String bodyContents = "-----------------------------9051914041544843365972754266\r\n" +
|
||||||
|
|
@ -353,7 +351,7 @@ public class BodyExtractorsTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void toDataBuffers() throws Exception {
|
public void toDataBuffers() {
|
||||||
BodyExtractor<Flux<DataBuffer>, ReactiveHttpInputMessage> extractor = BodyExtractors.toDataBuffers();
|
BodyExtractor<Flux<DataBuffer>, ReactiveHttpInputMessage> extractor = BodyExtractors.toDataBuffers();
|
||||||
|
|
||||||
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
|
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue