Search in sources :

Example 96 with DataBuffer

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

the class DefaultMetadataExtractorTests method routeAsText.

@Test
public void routeAsText() {
    this.extractor.metadataToExtract(TEXT_PLAIN, String.class, ROUTE_KEY);
    MetadataEncoder metadataEncoder = new MetadataEncoder(TEXT_PLAIN, this.strategies).route("toA");
    DataBuffer metadata = metadataEncoder.encode().block();
    Payload payload = createPayload(metadata);
    Map<String, Object> result = this.extractor.extract(payload, TEXT_PLAIN);
    payload.release();
    assertThat(result).hasSize(1).containsEntry(ROUTE_KEY, "toA");
}
Also used : Payload(io.rsocket.Payload) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 97 with DataBuffer

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

the class DefaultMetadataExtractorTests method compositeMetadataWithMimeTypeRegistrations.

@Test
public void compositeMetadataWithMimeTypeRegistrations() {
    this.extractor.metadataToExtract(TEXT_PLAIN, String.class, "text-entry");
    this.extractor.metadataToExtract(TEXT_HTML, String.class, "html-entry");
    this.extractor.metadataToExtract(TEXT_XML, String.class, "xml-entry");
    MetadataEncoder metadataEncoder = new MetadataEncoder(COMPOSITE_METADATA, this.strategies).route("toA").metadata("text data", TEXT_PLAIN).metadata("html data", TEXT_HTML).metadata("xml data", TEXT_XML);
    DataBuffer metadata = metadataEncoder.encode().block();
    Payload payload = createPayload(metadata);
    Map<String, Object> result = this.extractor.extract(payload, COMPOSITE_METADATA);
    payload.release();
    assertThat(result).hasSize(4).containsEntry(ROUTE_KEY, "toA").containsEntry("text-entry", "text data").containsEntry("html-entry", "html data").containsEntry("xml-entry", "xml data");
}
Also used : Payload(io.rsocket.Payload) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 98 with DataBuffer

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

the class PayloadUtilsTests method createWithDefaultBuffer.

@Test
public void createWithDefaultBuffer() {
    DataBuffer data = createDefaultDataBuffer("sample data");
    Payload payload = PayloadUtils.createPayload(data);
    assertThat(payload).isInstanceOf(DefaultPayload.class);
    assertThat(payload.getDataUtf8()).isEqualTo(data.toString(UTF_8));
}
Also used : Payload(io.rsocket.Payload) DefaultPayload(io.rsocket.util.DefaultPayload) ByteBufPayload(io.rsocket.util.ByteBufPayload) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer) NettyDataBuffer(org.springframework.core.io.buffer.NettyDataBuffer) Test(org.junit.jupiter.api.Test)

Example 99 with DataBuffer

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

the class HttpHandlerConnectorTests method adaptRequest.

@Test
public void adaptRequest() {
    TestHttpHandler handler = new TestHttpHandler(response -> {
        response.setStatusCode(HttpStatus.OK);
        return response.setComplete();
    });
    new HttpHandlerConnector(handler).connect(HttpMethod.POST, URI.create("/custom-path"), request -> {
        request.getHeaders().put("custom-header", Arrays.asList("h0", "h1"));
        request.getCookies().add("custom-cookie", new HttpCookie("custom-cookie", "c0"));
        return request.writeWith(Mono.just(toDataBuffer("Custom body")));
    }).block(Duration.ofSeconds(5));
    MockServerHttpRequest request = (MockServerHttpRequest) handler.getSavedRequest();
    assertThat(request.getMethod()).isEqualTo(HttpMethod.POST);
    assertThat(request.getURI().toString()).isEqualTo("/custom-path");
    HttpHeaders headers = request.getHeaders();
    assertThat(headers.get("custom-header")).isEqualTo(Arrays.asList("h0", "h1"));
    assertThat(request.getCookies().getFirst("custom-cookie")).isEqualTo(new HttpCookie("custom-cookie", "c0"));
    assertThat(headers.get(HttpHeaders.COOKIE)).isEqualTo(Collections.singletonList("custom-cookie=c0"));
    DataBuffer buffer = request.getBody().blockFirst(Duration.ZERO);
    assertThat(buffer.toString(UTF_8)).isEqualTo("Custom body");
}
Also used : Arrays(java.util.Arrays) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) HttpHeaders(org.springframework.http.HttpHeaders) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HttpMethod(org.springframework.http.HttpMethod) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Function(java.util.function.Function) ReactiveHttpOutputMessage(org.springframework.http.ReactiveHttpOutputMessage) ClientHttpResponse(org.springframework.http.client.reactive.ClientHttpResponse) Test(org.junit.jupiter.api.Test) HttpCookie(org.springframework.http.HttpCookie) HttpStatus(org.springframework.http.HttpStatus) HttpHandler(org.springframework.http.server.reactive.HttpHandler) Duration(java.time.Duration) Schedulers(reactor.core.scheduler.Schedulers) URI(java.net.URI) Collections(java.util.Collections) ResponseCookie(org.springframework.http.ResponseCookie) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) HttpHeaders(org.springframework.http.HttpHeaders) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) HttpCookie(org.springframework.http.HttpCookie) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 100 with DataBuffer

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

the class DataBufferTestUtilsTests method dumpBytes.

@ParameterizedDataBufferAllocatingTest
void dumpBytes(String displayName, DataBufferFactory bufferFactory) {
    this.bufferFactory = bufferFactory;
    DataBuffer buffer = this.bufferFactory.allocateBuffer(4);
    byte[] source = { 'a', 'b', 'c', 'd' };
    buffer.write(source);
    byte[] result = DataBufferTestUtils.dumpBytes(buffer);
    assertThat(result).isEqualTo(source);
    release(buffer);
}
Also used : DataBuffer(org.springframework.core.io.buffer.DataBuffer)

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