Add formData() and multipartData() to ServerRequest

Issue: SPR-16551
This commit is contained in:
Arjen Poutsma
2018-03-23 09:58:59 +01:00
parent 1b83f129a2
commit c56317928f
7 changed files with 160 additions and 6 deletions

View File

@@ -44,13 +44,15 @@ import org.springframework.http.HttpRange;
import org.springframework.http.MediaType;
import org.springframework.http.codec.DecoderHttpMessageReader;
import org.springframework.http.codec.HttpMessageReader;
import org.springframework.http.codec.multipart.FormFieldPart;
import org.springframework.http.codec.multipart.Part;
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
import org.springframework.mock.web.test.server.MockServerWebExchange;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.UnsupportedMediaTypeStatusException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.*;
import static org.springframework.web.reactive.function.BodyExtractors.toMono;
/**
@@ -336,4 +338,70 @@ public class DefaultServerRequestTests {
.verify();
}
@Test
public void formData() throws Exception {
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
DefaultDataBuffer dataBuffer =
factory.wrap(ByteBuffer.wrap("foo=bar&baz=qux".getBytes(StandardCharsets.UTF_8)));
Flux<DataBuffer> body = Flux.just(dataBuffer);
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MockServerHttpRequest mockRequest = MockServerHttpRequest
.method(HttpMethod.GET, "http://example.com")
.headers(httpHeaders)
.body(body);
DefaultServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
Mono<MultiValueMap<String, String>> resultData = request.formData();
StepVerifier.create(resultData)
.consumeNextWith(formData -> {
assertEquals(2, formData.size());
assertEquals("bar", formData.getFirst("foo"));
assertEquals("qux", formData.getFirst("baz"));
})
.verifyComplete();
}
@Test
public void multipartData() throws Exception {
String data = "--12345\r\n" +
"Content-Disposition: form-data; name=\"foo\"\r\n" +
"\r\n" +
"bar\r\n" +
"--12345\r\n" +
"Content-Disposition: form-data; name=\"baz\"\r\n" +
"\r\n" +
"qux\r\n" +
"--12345--\r\n";
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
DefaultDataBuffer dataBuffer =
factory.wrap(ByteBuffer.wrap(data.getBytes(StandardCharsets.UTF_8)));
Flux<DataBuffer> body = Flux.just(dataBuffer);
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.set(HttpHeaders.CONTENT_TYPE, "multipart/form-data; boundary=12345");
MockServerHttpRequest mockRequest = MockServerHttpRequest
.method(HttpMethod.GET, "http://example.com")
.headers(httpHeaders)
.body(body);
DefaultServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
Mono<MultiValueMap<String, Part>> resultData = request.multipartData();
StepVerifier.create(resultData)
.consumeNextWith(formData -> {
assertEquals(2, formData.size());
Part part = formData.getFirst("foo");
assertTrue(part instanceof FormFieldPart);
FormFieldPart formFieldPart = (FormFieldPart) part;
assertEquals("bar", formFieldPart.value());
part = formData.getFirst("baz");
assertTrue(part instanceof FormFieldPart);
formFieldPart = (FormFieldPart) part;
assertEquals("qux", formFieldPart.value());
})
.verifyComplete();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -40,6 +40,7 @@ import org.springframework.http.HttpMethod;
import org.springframework.http.HttpRange;
import org.springframework.http.HttpRequest;
import org.springframework.http.MediaType;
import org.springframework.http.codec.multipart.Part;
import org.springframework.http.server.PathContainer;
import org.springframework.http.server.RequestPath;
import org.springframework.http.server.reactive.ServerHttpRequest;
@@ -208,6 +209,20 @@ public class MockServerRequest implements ServerRequest {
return Mono.justOrEmpty(this.principal);
}
@Override
@SuppressWarnings("unchecked")
public Mono<MultiValueMap<String, String>> formData() {
Assert.state(this.body != null, "No body");
return (Mono<MultiValueMap<String, String>>) this.body;
}
@Override
@SuppressWarnings("unchecked")
public Mono<MultiValueMap<String, Part>> multipartData() {
Assert.state(this.body != null, "No body");
return (Mono<MultiValueMap<String, Part>>) this.body;
}
public static Builder builder() {
return new BuilderImpl();
}