Search in sources :

Example 76 with DataBuffer

use of org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.

the class DefaultClientResponseTests method toEntityListTypeReference.

@Test
public void toEntityListTypeReference() {
    byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
    DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
    Flux<DataBuffer> body = Flux.just(dataBuffer);
    mockTextPlainResponse(body);
    List<HttpMessageReader<?>> messageReaders = Collections.singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes()));
    given(mockExchangeStrategies.messageReaders()).willReturn(messageReaders);
    ResponseEntity<List<String>> result = defaultClientResponse.toEntityList(new ParameterizedTypeReference<String>() {
    }).block();
    assertThat(result.getBody()).isEqualTo(Collections.singletonList("foo"));
    assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
    assertThat(result.getStatusCodeValue()).isEqualTo(HttpStatus.OK.value());
    assertThat(result.getHeaders().getContentType()).isEqualTo(MediaType.TEXT_PLAIN);
}
Also used : DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) HttpMessageReader(org.springframework.http.codec.HttpMessageReader) DecoderHttpMessageReader(org.springframework.http.codec.DecoderHttpMessageReader) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) List(java.util.List) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 77 with DataBuffer

use of org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.

the class DefaultClientResponseTests method toEntityListWithUnknownStatusCode.

@Test
public void toEntityListWithUnknownStatusCode() {
    byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
    DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
    Flux<DataBuffer> body = Flux.just(dataBuffer);
    httpHeaders.setContentType(MediaType.TEXT_PLAIN);
    given(mockResponse.getHeaders()).willReturn(httpHeaders);
    given(mockResponse.getStatusCode()).willThrow(new IllegalArgumentException("999"));
    given(mockResponse.getRawStatusCode()).willReturn(999);
    given(mockResponse.getBody()).willReturn(body);
    List<HttpMessageReader<?>> messageReaders = Collections.singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes()));
    given(mockExchangeStrategies.messageReaders()).willReturn(messageReaders);
    ResponseEntity<List<String>> result = defaultClientResponse.toEntityList(String.class).block();
    assertThat(result.getBody()).isEqualTo(Collections.singletonList("foo"));
    assertThatIllegalArgumentException().isThrownBy(result::getStatusCode);
    assertThat(result.getStatusCodeValue()).isEqualTo(999);
    assertThat(result.getHeaders().getContentType()).isEqualTo(MediaType.TEXT_PLAIN);
}
Also used : DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) HttpMessageReader(org.springframework.http.codec.HttpMessageReader) DecoderHttpMessageReader(org.springframework.http.codec.DecoderHttpMessageReader) List(java.util.List) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 78 with DataBuffer

use of org.springframework.core.io.buffer.DataBuffer 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 79 with DataBuffer

use of org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.

the class BodyExtractorsTests method toMonoVoidAsClientShouldConsumeAndCancel.

@Test
public void toMonoVoidAsClientShouldConsumeAndCancel() {
    byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
    DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
    TestPublisher<DataBuffer> body = TestPublisher.create();
    BodyExtractor<Mono<Void>, ReactiveHttpInputMessage> extractor = BodyExtractors.toMono(Void.class);
    MockClientHttpResponse response = new MockClientHttpResponse(HttpStatus.OK);
    response.setBody(body.flux());
    StepVerifier.create(extractor.extract(response, this.context)).then(() -> {
        body.assertWasSubscribed();
        body.emit(dataBuffer);
    }).verifyComplete();
    body.assertCancelled();
}
Also used : DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) Mono(reactor.core.publisher.Mono) ReactiveHttpInputMessage(org.springframework.http.ReactiveHttpInputMessage) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) NettyDataBuffer(org.springframework.core.io.buffer.NettyDataBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer) MockClientHttpResponse(org.springframework.web.testfixture.http.client.reactive.MockClientHttpResponse) Test(org.junit.jupiter.api.Test)

Example 80 with DataBuffer

use of org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.

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(org.springframework.core.io.buffer.DefaultDataBuffer) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) Flux(reactor.core.publisher.Flux) ReactiveHttpInputMessage(org.springframework.http.ReactiveHttpInputMessage) 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)

Aggregations

DataBuffer (org.springframework.core.io.buffer.DataBuffer)230 Test (org.junit.jupiter.api.Test)111 Mono (reactor.core.publisher.Mono)55 Flux (reactor.core.publisher.Flux)52 DefaultDataBuffer (org.springframework.core.io.buffer.DefaultDataBuffer)49 ResolvableType (org.springframework.core.ResolvableType)41 DefaultDataBufferFactory (org.springframework.core.io.buffer.DefaultDataBufferFactory)36 StepVerifier (reactor.test.StepVerifier)36 List (java.util.List)34 Test (org.junit.Test)31 DataBufferUtils (org.springframework.core.io.buffer.DataBufferUtils)29 IOException (java.io.IOException)28 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)27 MockServerHttpRequest (org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest)27 Map (java.util.Map)25 NettyDataBuffer (org.springframework.core.io.buffer.NettyDataBuffer)25 DataBufferFactory (org.springframework.core.io.buffer.DataBufferFactory)24 HttpHeaders (org.springframework.http.HttpHeaders)24 MediaType (org.springframework.http.MediaType)24 ByteBuffer (java.nio.ByteBuffer)23