use of org.springframework.http.codec.multipart.FilePart in project spring-framework by spring-projects.
the class BodyExtractorsTests method toParts.
@Test
public void toParts() {
BodyExtractor<Flux<Part>, ServerHttpRequest> extractor = BodyExtractors.toParts();
String bodyContents = "-----------------------------9051914041544843365972754266\r\n" + "Content-Disposition: form-data; name=\"text\"\r\n" + "\r\n" + "text default\r\n" + "-----------------------------9051914041544843365972754266\r\n" + "Content-Disposition: form-data; name=\"file1\"; filename=\"a.txt\"\r\n" + "Content-Type: text/plain\r\n" + "\r\n" + "Content of a.txt.\r\n" + "\r\n" + "-----------------------------9051914041544843365972754266\r\n" + "Content-Disposition: form-data; name=\"file2\"; filename=\"a.html\"\r\n" + "Content-Type: text/html\r\n" + "\r\n" + "<!DOCTYPE html><title>Content of a.html.</title>\r\n" + "\r\n" + "-----------------------------9051914041544843365972754266--\r\n";
byte[] bytes = bodyContents.getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
Flux<DataBuffer> body = Flux.just(dataBuffer);
MockServerHttpRequest request = MockServerHttpRequest.post("/").header("Content-Type", "multipart/form-data; boundary=---------------------------9051914041544843365972754266").body(body);
Flux<Part> result = extractor.extract(request, this.context);
StepVerifier.create(result).consumeNextWith(part -> {
assertThat(part.name()).isEqualTo("text");
boolean condition = part instanceof FormFieldPart;
assertThat(condition).isTrue();
FormFieldPart formFieldPart = (FormFieldPart) part;
assertThat(formFieldPart.value()).isEqualTo("text default");
}).consumeNextWith(part -> {
assertThat(part.name()).isEqualTo("file1");
boolean condition = part instanceof FilePart;
assertThat(condition).isTrue();
FilePart filePart = (FilePart) part;
assertThat(filePart.filename()).isEqualTo("a.txt");
assertThat(filePart.headers().getContentType()).isEqualTo(MediaType.TEXT_PLAIN);
}).consumeNextWith(part -> {
assertThat(part.name()).isEqualTo("file2");
boolean condition = part instanceof FilePart;
assertThat(condition).isTrue();
FilePart filePart = (FilePart) part;
assertThat(filePart.filename()).isEqualTo("a.html");
assertThat(filePart.headers().getContentType()).isEqualTo(MediaType.TEXT_HTML);
}).expectComplete().verify();
}
use of org.springframework.http.codec.multipart.FilePart in project spring-framework by spring-projects.
the class MockMvcHttpConnector method initRequestBuilder.
private MockHttpServletRequestBuilder initRequestBuilder(HttpMethod httpMethod, URI uri, MockClientHttpRequest httpRequest, @Nullable byte[] bytes) {
String contentType = httpRequest.getHeaders().getFirst(HttpHeaders.CONTENT_TYPE);
if (!StringUtils.startsWithIgnoreCase(contentType, "multipart/")) {
MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.request(httpMethod, uri);
if (!ObjectUtils.isEmpty(bytes)) {
requestBuilder.content(bytes);
}
return requestBuilder;
}
// Parse the multipart request in order to adapt to Servlet Part's
MockMultipartHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.multipart(uri);
Assert.notNull(bytes, "No multipart content");
ReactiveHttpInputMessage inputMessage = MockServerHttpRequest.post(uri.toString()).headers(httpRequest.getHeaders()).body(Mono.just(DefaultDataBufferFactory.sharedInstance.wrap(bytes)));
MULTIPART_READER.read(ResolvableType.forClass(Part.class), inputMessage, Collections.emptyMap()).flatMap(part -> DataBufferUtils.join(part.content()).doOnNext(buffer -> {
byte[] partBytes = new byte[buffer.readableByteCount()];
buffer.read(partBytes);
DataBufferUtils.release(buffer);
// Adapt to jakarta.servlet.http.Part...
MockPart mockPart = (part instanceof FilePart ? new MockPart(part.name(), ((FilePart) part).filename(), partBytes) : new MockPart(part.name(), partBytes));
mockPart.getHeaders().putAll(part.headers());
requestBuilder.part(mockPart);
})).blockLast(TIMEOUT);
return requestBuilder;
}
Aggregations