Search in sources :

Example 16 with DataBuffer

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

the class DataBufferEncoderTests method encode.

@Test
public void encode() {
    DataBuffer fooBuffer = stringBuffer("foo");
    DataBuffer barBuffer = stringBuffer("bar");
    Flux<DataBuffer> source = Flux.just(fooBuffer, barBuffer);
    Flux<DataBuffer> output = this.encoder.encode(source, this.bufferFactory, ResolvableType.forClassWithGenerics(Publisher.class, ByteBuffer.class), null, Collections.emptyMap());
    assertSame(source, output);
}
Also used : Publisher(org.reactivestreams.Publisher) ByteBuffer(java.nio.ByteBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Example 17 with DataBuffer

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

the class ResourceDecoderTests method decode.

@Test
public void decode() throws Exception {
    DataBuffer fooBuffer = stringBuffer("foo");
    DataBuffer barBuffer = stringBuffer("bar");
    Flux<DataBuffer> source = Flux.just(fooBuffer, barBuffer);
    Flux<Resource> result = this.decoder.decode(source, ResolvableType.forClass(Resource.class), null, Collections.emptyMap());
    StepVerifier.create(result).consumeNextWith(resource -> {
        try {
            byte[] bytes = StreamUtils.copyToByteArray(resource.getInputStream());
            assertEquals("foobar", new String(bytes));
        } catch (IOException e) {
            fail(e.getMessage());
        }
    }).expectComplete().verify();
}
Also used : ByteArrayResource(org.springframework.core.io.ByteArrayResource) InputStreamResource(org.springframework.core.io.InputStreamResource) Resource(org.springframework.core.io.Resource) IOException(java.io.IOException) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Example 18 with DataBuffer

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

the class ResourceRegionEncoderTests method shouldEncodeResourceRegion.

private void shouldEncodeResourceRegion(Resource resource) {
    ResourceRegion region = new ResourceRegion(resource, 0, 6);
    Flux<DataBuffer> result = this.encoder.encode(Mono.just(region), this.bufferFactory, ResolvableType.forClass(ResourceRegion.class), MimeTypeUtils.APPLICATION_OCTET_STREAM, Collections.emptyMap());
    StepVerifier.create(result).consumeNextWith(stringConsumer("Spring")).expectComplete().verify();
}
Also used : ResourceRegion(org.springframework.core.io.support.ResourceRegion) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Example 19 with DataBuffer

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

the class DataBufferTestUtilsTests method dumpBytes.

@Test
public void dumpBytes() {
    DataBuffer buffer = this.bufferFactory.allocateBuffer(4);
    byte[] source = { 'a', 'b', 'c', 'd' };
    buffer.write(source);
    byte[] result = DataBufferTestUtils.dumpBytes(buffer);
    assertArrayEquals(source, result);
    release(buffer);
}
Also used : DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Example 20 with DataBuffer

use of org.springframework.core.io.buffer.DataBuffer in project thymeleaf-tests by thymeleaf.

the class AbstractSpring5ReactiveTest method testTemplateDirectExecution.

private static void testTemplateDirectExecution(final String template, final Set<String> markupSelectors, final IContext context, final String result, final boolean sse, final int responseMaxChunkSizeBytes) throws Exception {
    final String dataDriverVariableName = detectDataDriver(context);
    final boolean isDataDriven = dataDriverVariableName != null;
    List<DataBuffer> resultBuffers = null;
    try {
        final Publisher<DataBuffer> resultStream = templateEngine.processStream(template, markupSelectors, context, bufferFactory, (sse ? sseMediaType : htmlMediaType), charset, responseMaxChunkSizeBytes);
        resultBuffers = Flux.from(resultStream).collectList().block();
    } catch (final Exception e) {
        throw new TemplateProcessingException("Error happened while executing reactive test for template " + template + " with markup " + "selectors " + markupSelectors + ", context with variables " + context.getVariableNames() + " and " + "response chunk size of " + responseMaxChunkSizeBytes + " bytes.", e);
    }
    if (responseMaxChunkSizeBytes != Integer.MAX_VALUE) {
        for (final DataBuffer resultBuffer : resultBuffers) {
            Assert.assertTrue("Buffer returned by stream is of size larger than " + responseMaxChunkSizeBytes, resultBuffer.readableByteCount() <= responseMaxChunkSizeBytes);
        }
    } else {
        if (!isDataDriven) {
            final int bufferCount = resultBuffers.size();
            Assert.assertTrue("No limit set on buffer size, and non-data-driven: there should only be one result buffer instead of " + bufferCount, bufferCount == 1);
        }
    }
    final String resultStr = resultBuffers.stream().map((buffer) -> ReactiveTestUtils.bufferAsString(buffer, charset)).map(// Note we NORMALIZE before joining it all
    ReactiveTestUtils::normalizeResult).collect(Collectors.joining());
    final String expected = ReactiveTestUtils.readExpectedNormalizedResults(result, charset);
    Assert.assertEquals(expected, resultStr);
}
Also used : TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException) 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)30 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