Search in sources :

Example 1 with ReactiveHttpInputMessage

use of cn.taketoday.http.ReactiveHttpInputMessage in project today-infrastructure by TAKETODAY.

the class BodyExtractorsTests method toMonoParameterizedTypeReference.

@Test
public void toMonoParameterizedTypeReference() {
    BodyExtractor<Mono<Map<String, String>>, ReactiveHttpInputMessage> extractor = BodyExtractors.toMono(new TypeReference<Map<String, String>>() {
    });
    byte[] bytes = "{\"username\":\"foo\",\"password\":\"bar\"}".getBytes(StandardCharsets.UTF_8);
    DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
    Flux<DataBuffer> body = Flux.just(dataBuffer);
    MockServerHttpRequest request = MockServerHttpRequest.post("/").contentType(MediaType.APPLICATION_JSON).body(body);
    Mono<Map<String, String>> result = extractor.extract(request, this.context);
    Map<String, String> expected = new LinkedHashMap<>();
    expected.put("username", "foo");
    expected.put("password", "bar");
    StepVerifier.create(result).expectNext(expected).expectComplete().verify();
}
Also used : DefaultDataBuffer(cn.taketoday.core.io.buffer.DefaultDataBuffer) Mono(reactor.core.publisher.Mono) LinkedHashMap(java.util.LinkedHashMap) MockServerHttpRequest(cn.taketoday.web.testfixture.http.server.reactive.MockServerHttpRequest) ReactiveHttpInputMessage(cn.taketoday.http.ReactiveHttpInputMessage) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) MultiValueMap(cn.taketoday.core.MultiValueMap) 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 ReactiveHttpInputMessage

use of cn.taketoday.http.ReactiveHttpInputMessage in project today-framework by TAKETODAY.

the class BodyExtractorsTests method unsupportedMediaTypeShouldConsumeAndCancel.

// SPR-17054
@Test
public void unsupportedMediaTypeShouldConsumeAndCancel() {
    NettyDataBufferFactory factory = new NettyDataBufferFactory(new PooledByteBufAllocator(true));
    NettyDataBuffer buffer = factory.wrap(ByteBuffer.wrap("spring".getBytes(StandardCharsets.UTF_8)));
    TestPublisher<DataBuffer> body = TestPublisher.create();
    MockClientHttpResponse response = new MockClientHttpResponse(HttpStatus.OK);
    response.getHeaders().setContentType(MediaType.APPLICATION_PDF);
    response.setBody(body.flux());
    BodyExtractor<Mono<User>, ReactiveHttpInputMessage> extractor = BodyExtractors.toMono(User.class);
    StepVerifier.create(extractor.extract(response, this.context)).then(() -> {
        body.assertWasSubscribed();
        body.emit(buffer);
    }).expectErrorSatisfies(throwable -> {
        boolean condition = throwable instanceof UnsupportedMediaTypeException;
        assertThat(condition).isTrue();
        assertThatExceptionOfType(IllegalReferenceCountException.class).isThrownBy(buffer::release);
        body.assertCancelled();
    }).verify();
}
Also used : 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) Mono(reactor.core.publisher.Mono) NettyDataBuffer(cn.taketoday.core.io.buffer.NettyDataBuffer) NettyDataBufferFactory(cn.taketoday.core.io.buffer.NettyDataBufferFactory) ReactiveHttpInputMessage(cn.taketoday.http.ReactiveHttpInputMessage) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) NettyDataBuffer(cn.taketoday.core.io.buffer.NettyDataBuffer) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) DefaultDataBuffer(cn.taketoday.core.io.buffer.DefaultDataBuffer) MockClientHttpResponse(cn.taketoday.web.testfixture.http.client.reactive.MockClientHttpResponse) Test(org.junit.jupiter.api.Test)

Example 3 with ReactiveHttpInputMessage

use of cn.taketoday.http.ReactiveHttpInputMessage in project today-framework by TAKETODAY.

the class BodyExtractorsTests method toDataBuffers.

@Test
public void toDataBuffers() {
    BodyExtractor<Flux<DataBuffer>, ReactiveHttpInputMessage> extractor = BodyExtractors.toDataBuffers();
    byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
    DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
    Flux<DataBuffer> body = Flux.just(dataBuffer);
    MockServerHttpRequest request = MockServerHttpRequest.post("/").body(body);
    Flux<DataBuffer> result = extractor.extract(request, this.context);
    StepVerifier.create(result).expectNext(dataBuffer).expectComplete().verify();
}
Also used : DefaultDataBuffer(cn.taketoday.core.io.buffer.DefaultDataBuffer) MockServerHttpRequest(cn.taketoday.web.testfixture.http.server.reactive.MockServerHttpRequest) Flux(reactor.core.publisher.Flux) ReactiveHttpInputMessage(cn.taketoday.http.ReactiveHttpInputMessage) 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 4 with ReactiveHttpInputMessage

use of cn.taketoday.http.ReactiveHttpInputMessage in project today-framework by TAKETODAY.

the class BodyExtractorsTests method toMono.

@Test
public void toMono() {
    BodyExtractor<Mono<String>, ReactiveHttpInputMessage> extractor = BodyExtractors.toMono(String.class);
    DefaultDataBufferFactory factory = DefaultDataBufferFactory.sharedInstance;
    DefaultDataBuffer dataBuffer = factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
    Flux<DataBuffer> body = Flux.just(dataBuffer);
    MockServerHttpRequest request = MockServerHttpRequest.post("/").body(body);
    Mono<String> result = extractor.extract(request, this.context);
    StepVerifier.create(result).expectNext("foo").expectComplete().verify();
}
Also used : DefaultDataBuffer(cn.taketoday.core.io.buffer.DefaultDataBuffer) MockServerHttpRequest(cn.taketoday.web.testfixture.http.server.reactive.MockServerHttpRequest) DefaultDataBufferFactory(cn.taketoday.core.io.buffer.DefaultDataBufferFactory) Mono(reactor.core.publisher.Mono) ReactiveHttpInputMessage(cn.taketoday.http.ReactiveHttpInputMessage) 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 5 with ReactiveHttpInputMessage

use of cn.taketoday.http.ReactiveHttpInputMessage in project today-framework by TAKETODAY.

the class BodyExtractorsTests method toFluxUnacceptable.

@Test
public void toFluxUnacceptable() {
    BodyExtractor<Flux<String>, ReactiveHttpInputMessage> extractor = BodyExtractors.toFlux(String.class);
    byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
    DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
    Flux<DataBuffer> body = Flux.just(dataBuffer);
    MockServerHttpRequest request = MockServerHttpRequest.post("/").contentType(MediaType.APPLICATION_JSON).body(body);
    BodyExtractor.Context emptyContext = new BodyExtractor.Context() {

        @Override
        public List<HttpMessageReader<?>> messageReaders() {
            return Collections.emptyList();
        }

        @Override
        public Optional<ServerHttpResponse> serverResponse() {
            return Optional.empty();
        }

        @Override
        public Map<String, Object> hints() {
            return Collections.emptyMap();
        }
    };
    Flux<String> result = extractor.extract(request, emptyContext);
    StepVerifier.create(result).expectError(UnsupportedMediaTypeException.class).verify();
}
Also used : DefaultDataBuffer(cn.taketoday.core.io.buffer.DefaultDataBuffer) Flux(reactor.core.publisher.Flux) ServerHttpResponse(cn.taketoday.http.server.reactive.ServerHttpResponse) DefaultPartHttpMessageReader(cn.taketoday.http.codec.multipart.DefaultPartHttpMessageReader) FormHttpMessageReader(cn.taketoday.http.codec.FormHttpMessageReader) MultipartHttpMessageReader(cn.taketoday.http.codec.multipart.MultipartHttpMessageReader) DecoderHttpMessageReader(cn.taketoday.http.codec.DecoderHttpMessageReader) HttpMessageReader(cn.taketoday.http.codec.HttpMessageReader) MockServerHttpRequest(cn.taketoday.web.testfixture.http.server.reactive.MockServerHttpRequest) ReactiveHttpInputMessage(cn.taketoday.http.ReactiveHttpInputMessage) 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

ReactiveHttpInputMessage (cn.taketoday.http.ReactiveHttpInputMessage)22 Test (org.junit.jupiter.api.Test)22 DataBuffer (cn.taketoday.core.io.buffer.DataBuffer)20 DefaultDataBuffer (cn.taketoday.core.io.buffer.DefaultDataBuffer)20 NettyDataBuffer (cn.taketoday.core.io.buffer.NettyDataBuffer)20 MockServerHttpRequest (cn.taketoday.web.testfixture.http.server.reactive.MockServerHttpRequest)18 Mono (reactor.core.publisher.Mono)16 Flux (reactor.core.publisher.Flux)10 MultiValueMap (cn.taketoday.core.MultiValueMap)8 MockClientHttpResponse (cn.taketoday.web.testfixture.http.client.reactive.MockClientHttpResponse)8 HashMap (java.util.HashMap)8 LinkedHashMap (java.util.LinkedHashMap)8 Map (java.util.Map)8 DefaultDataBufferFactory (cn.taketoday.core.io.buffer.DefaultDataBufferFactory)6 DecoderHttpMessageReader (cn.taketoday.http.codec.DecoderHttpMessageReader)6 FormHttpMessageReader (cn.taketoday.http.codec.FormHttpMessageReader)6 HttpMessageReader (cn.taketoday.http.codec.HttpMessageReader)6 DefaultPartHttpMessageReader (cn.taketoday.http.codec.multipart.DefaultPartHttpMessageReader)6 MultipartHttpMessageReader (cn.taketoday.http.codec.multipart.MultipartHttpMessageReader)6 ServerHttpResponse (cn.taketoday.http.server.reactive.ServerHttpResponse)6