ResourceDecoder supports filename hint

Closes gh-22267
This commit is contained in:
Rossen Stoyanchev
2019-03-19 17:50:06 -04:00
parent 5a3ff35215
commit 4a397f108a
7 changed files with 109 additions and 32 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@ package org.springframework.core.codec;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.Map;
import org.junit.Test;
@@ -100,23 +101,28 @@ public class ResourceDecoderTests extends AbstractDecoderTestCase<ResourceDecode
}
@Override
public void decodeToMono() throws Exception {
public void decodeToMono() {
Flux<DataBuffer> input = Flux.concat(
dataBuffer(this.fooBytes),
dataBuffer(this.barBytes));
testDecodeToMonoAll(input, Resource.class, step -> step
.consumeNextWith(resource -> {
try {
byte[] bytes = StreamUtils.copyToByteArray(resource.getInputStream());
assertEquals("foobar", new String(bytes));
}
catch (IOException e) {
fail(e.getMessage());
}
})
.expectComplete()
.verify());
testDecodeToMonoAll(input, ResolvableType.forClass(Resource.class),
step -> step
.consumeNextWith(value -> {
Resource resource = (Resource) value;
try {
byte[] bytes = StreamUtils.copyToByteArray(resource.getInputStream());
assertEquals("foobar", new String(bytes));
assertEquals("testFile", resource.getFilename());
}
catch (IOException e) {
fail(e.getMessage());
}
})
.expectComplete()
.verify(),
null,
Collections.singletonMap(ResourceDecoder.FILENAME_HINT, "testFile"));
}
}