Search in sources :

Example 1 with ServerHttpRequest

use of cn.taketoday.http.server.reactive.ServerHttpRequest in project today-infrastructure by TAKETODAY.

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

Example 2 with ServerHttpRequest

use of cn.taketoday.http.server.reactive.ServerHttpRequest in project today-framework by TAKETODAY.

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

Aggregations

MultiValueMap (cn.taketoday.core.MultiValueMap)2 TypeReference (cn.taketoday.core.TypeReference)2 ByteBufferDecoder (cn.taketoday.core.codec.ByteBufferDecoder)2 StringDecoder (cn.taketoday.core.codec.StringDecoder)2 DataBuffer (cn.taketoday.core.io.buffer.DataBuffer)2 DefaultDataBuffer (cn.taketoday.core.io.buffer.DefaultDataBuffer)2 DefaultDataBufferFactory (cn.taketoday.core.io.buffer.DefaultDataBufferFactory)2 NettyDataBuffer (cn.taketoday.core.io.buffer.NettyDataBuffer)2 NettyDataBufferFactory (cn.taketoday.core.io.buffer.NettyDataBufferFactory)2 HttpStatus (cn.taketoday.http.HttpStatus)2 MediaType (cn.taketoday.http.MediaType)2 ReactiveHttpInputMessage (cn.taketoday.http.ReactiveHttpInputMessage)2 DecoderHttpMessageReader (cn.taketoday.http.codec.DecoderHttpMessageReader)2 FormHttpMessageReader (cn.taketoday.http.codec.FormHttpMessageReader)2 HttpMessageReader (cn.taketoday.http.codec.HttpMessageReader)2 JSON_VIEW_HINT (cn.taketoday.http.codec.json.Jackson2CodecSupport.JSON_VIEW_HINT)2 Jackson2JsonDecoder (cn.taketoday.http.codec.json.Jackson2JsonDecoder)2 DefaultPartHttpMessageReader (cn.taketoday.http.codec.multipart.DefaultPartHttpMessageReader)2 FilePart (cn.taketoday.http.codec.multipart.FilePart)2 FormFieldPart (cn.taketoday.http.codec.multipart.FormFieldPart)2