Support filename hint for client side too

Closes gh-25516
This commit is contained in:
Rossen Stoyanchev
2020-08-14 08:15:49 +01:00
parent 36553264ce
commit a7f71f4d9b
3 changed files with 96 additions and 6 deletions

View File

@@ -0,0 +1,73 @@
/*
* Copyright 2002-2020 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.http.codec;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Mono;
import org.springframework.core.ResolvableType;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.testfixture.io.buffer.AbstractLeakCheckingTests;
import org.springframework.http.ContentDisposition;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.testfixture.http.client.reactive.MockClientHttpResponse;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Unit tests for {@link ResourceHttpMessageReader}.
* @author Rossen Stoyanchev
*/
public class ResourceHttpMessageReaderTests extends AbstractLeakCheckingTests {
private final ResourceHttpMessageReader reader = new ResourceHttpMessageReader();
@Test
void readResourceAsMono() throws IOException {
String filename = "test.txt";
String body = "Test resource content";
ContentDisposition contentDisposition =
ContentDisposition.builder("attachment").name("file").filename(filename).build();
MockClientHttpResponse response = new MockClientHttpResponse(HttpStatus.OK);
response.getHeaders().setContentType(MediaType.TEXT_PLAIN);
response.getHeaders().setContentDisposition(contentDisposition);
response.setBody(Mono.just(stringBuffer(body)));
Resource resource = reader.readMono(
ResolvableType.forClass(ByteArrayResource.class), response, Collections.emptyMap()).block();
assertThat(resource).isNotNull();
assertThat(resource.getFilename()).isEqualTo(filename);
assertThat(resource.getInputStream()).hasContent(body);
}
private DataBuffer stringBuffer(String value) {
byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
DataBuffer buffer = this.bufferFactory.allocateBuffer(bytes.length);
buffer.write(bytes);
return buffer;
}
}