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);
}
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();
}
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();
}
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);
}
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);
}
Aggregations