Search in sources :

Example 1 with FormFieldPart

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();
}
Also used : DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) URISyntaxException(java.net.URISyntaxException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) ByteBuffer(java.nio.ByteBuffer) HttpCookie(org.springframework.http.HttpCookie) UnsupportedMediaTypeStatusException(org.springframework.web.server.UnsupportedMediaTypeStatusException) Part(org.springframework.http.codec.multipart.Part) Map(java.util.Map) URI(java.net.URI) Jackson2JsonDecoder(org.springframework.http.codec.json.Jackson2JsonDecoder) BodyExtractors.toMono(org.springframework.web.reactive.function.BodyExtractors.toMono) StringDecoder(org.springframework.core.codec.StringDecoder) HttpHeaders(org.springframework.http.HttpHeaders) MediaType(org.springframework.http.MediaType) Instant(java.time.Instant) InetSocketAddress(java.net.InetSocketAddress) StandardCharsets(java.nio.charset.StandardCharsets) DecoderHttpMessageReader(org.springframework.http.codec.DecoderHttpMessageReader) Test(org.junit.jupiter.api.Test) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) List(java.util.List) Optional(java.util.Optional) FormFieldPart(org.springframework.http.codec.multipart.FormFieldPart) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) ServerWebInputException(org.springframework.web.server.ServerWebInputException) HttpMessageReader(org.springframework.http.codec.HttpMessageReader) Retention(java.lang.annotation.Retention) OptionalLong(java.util.OptionalLong) Charset(java.nio.charset.Charset) ValueSource(org.junit.jupiter.params.provider.ValueSource) HttpMethod(org.springframework.http.HttpMethod) MultiValueMap(org.springframework.util.MultiValueMap) Target(java.lang.annotation.Target) Mono(reactor.core.publisher.Mono) HttpRange(org.springframework.http.HttpRange) ElementType(java.lang.annotation.ElementType) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Flux(reactor.core.publisher.Flux) HttpStatus(org.springframework.http.HttpStatus) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) ChronoUnit(java.time.temporal.ChronoUnit) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) Collections(java.util.Collections) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) RetentionPolicy(java.lang.annotation.RetentionPolicy) HttpHeaders(org.springframework.http.HttpHeaders) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) Part(org.springframework.http.codec.multipart.Part) FormFieldPart(org.springframework.http.codec.multipart.FormFieldPart) FormFieldPart(org.springframework.http.codec.multipart.FormFieldPart) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with FormFieldPart

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();
}
Also used : DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) BeforeEach(org.junit.jupiter.api.BeforeEach) TestPublisher(reactor.test.publisher.TestPublisher) FilePart(org.springframework.http.codec.multipart.FilePart) FormFieldPart(org.springframework.http.codec.multipart.FormFieldPart) JsonView(com.fasterxml.jackson.annotation.JsonView) StepVerifier(reactor.test.StepVerifier) NettyDataBufferFactory(org.springframework.core.io.buffer.NettyDataBufferFactory) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) IllegalReferenceCountException(io.netty.util.IllegalReferenceCountException) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) FormHttpMessageReader(org.springframework.http.codec.FormHttpMessageReader) HttpMessageReader(org.springframework.http.codec.HttpMessageReader) HashMap(java.util.HashMap) JSON_VIEW_HINT(org.springframework.http.codec.json.Jackson2CodecSupport.JSON_VIEW_HINT) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Part(org.springframework.http.codec.multipart.Part) Map(java.util.Map) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Jaxb2XmlDecoder(org.springframework.http.codec.xml.Jaxb2XmlDecoder) NettyDataBuffer(org.springframework.core.io.buffer.NettyDataBuffer) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) Jackson2JsonDecoder(org.springframework.http.codec.json.Jackson2JsonDecoder) DefaultPartHttpMessageReader(org.springframework.http.codec.multipart.DefaultPartHttpMessageReader) MockClientHttpResponse(org.springframework.web.testfixture.http.client.reactive.MockClientHttpResponse) StringDecoder(org.springframework.core.codec.StringDecoder) MediaType(org.springframework.http.MediaType) MultiValueMap(org.springframework.util.MultiValueMap) Mono(reactor.core.publisher.Mono) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) ByteBufferDecoder(org.springframework.core.codec.ByteBufferDecoder) DataBuffer(org.springframework.core.io.buffer.DataBuffer) StandardCharsets(java.nio.charset.StandardCharsets) DecoderHttpMessageReader(org.springframework.http.codec.DecoderHttpMessageReader) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) HttpStatus(org.springframework.http.HttpStatus) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) List(java.util.List) ReactiveHttpInputMessage(org.springframework.http.ReactiveHttpInputMessage) MultipartHttpMessageReader(org.springframework.http.codec.multipart.MultipartHttpMessageReader) Optional(java.util.Optional) Collections(java.util.Collections) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) Flux(reactor.core.publisher.Flux) FilePart(org.springframework.http.codec.multipart.FilePart) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) FilePart(org.springframework.http.codec.multipart.FilePart) FormFieldPart(org.springframework.http.codec.multipart.FormFieldPart) Part(org.springframework.http.codec.multipart.Part) FormFieldPart(org.springframework.http.codec.multipart.FormFieldPart) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) NettyDataBuffer(org.springframework.core.io.buffer.NettyDataBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 3 with FormFieldPart

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);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) MediaType(org.springframework.http.MediaType) FormFieldPart(org.springframework.http.codec.multipart.FormFieldPart)

Aggregations

MediaType (org.springframework.http.MediaType)3 FormFieldPart (org.springframework.http.codec.multipart.FormFieldPart)3 ByteBuffer (java.nio.ByteBuffer)2 StandardCharsets (java.nio.charset.StandardCharsets)2 Collections (java.util.Collections)2 List (java.util.List)2 Map (java.util.Map)2 Optional (java.util.Optional)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Test (org.junit.jupiter.api.Test)2 ParameterizedTypeReference (org.springframework.core.ParameterizedTypeReference)2 StringDecoder (org.springframework.core.codec.StringDecoder)2 DataBuffer (org.springframework.core.io.buffer.DataBuffer)2 DefaultDataBuffer (org.springframework.core.io.buffer.DefaultDataBuffer)2 DefaultDataBufferFactory (org.springframework.core.io.buffer.DefaultDataBufferFactory)2 HttpStatus (org.springframework.http.HttpStatus)2 DecoderHttpMessageReader (org.springframework.http.codec.DecoderHttpMessageReader)2 HttpMessageReader (org.springframework.http.codec.HttpMessageReader)2 Jackson2JsonDecoder (org.springframework.http.codec.json.Jackson2JsonDecoder)2 Part (org.springframework.http.codec.multipart.Part)2