use of org.springframework.http.codec.multipart.FormFieldPart in project spring-framework by spring-projects.
the class DefaultServerRequestTests method multipartData.
@Test
public void multipartData() {
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";
byte[] bytes = data.getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
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, "https://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 -> {
assertThat(formData.size()).isEqualTo(2);
Part part = formData.getFirst("foo");
boolean condition1 = part instanceof FormFieldPart;
assertThat(condition1).isTrue();
FormFieldPart formFieldPart = (FormFieldPart) part;
assertThat(formFieldPart.value()).isEqualTo("bar");
part = formData.getFirst("baz");
boolean condition = part instanceof FormFieldPart;
assertThat(condition).isTrue();
formFieldPart = (FormFieldPart) part;
assertThat(formFieldPart.value()).isEqualTo("qux");
}).verifyComplete();
}
use of org.springframework.http.codec.multipart.FormFieldPart 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.FormFieldPart in project spring-security by spring-projects.
the class CsrfWebFilter method tokenFromMultipartData.
private Mono<String> tokenFromMultipartData(ServerWebExchange exchange, CsrfToken expected) {
if (!this.isTokenFromMultipartDataEnabled) {
return Mono.empty();
}
ServerHttpRequest request = exchange.getRequest();
HttpHeaders headers = request.getHeaders();
MediaType contentType = headers.getContentType();
if (!contentType.includes(MediaType.MULTIPART_FORM_DATA)) {
return Mono.empty();
}
return exchange.getMultipartData().map((d) -> d.getFirst(expected.getParameterName())).cast(FormFieldPart.class).map(FormFieldPart::value);
}
Aggregations