|
| 1 | +/* |
| 2 | + * Copyright 2002-2020 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.http.codec; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.nio.charset.StandardCharsets; |
| 20 | +import java.util.Collections; |
| 21 | + |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import reactor.core.publisher.Mono; |
| 24 | + |
| 25 | +import org.springframework.core.ResolvableType; |
| 26 | +import org.springframework.core.io.ByteArrayResource; |
| 27 | +import org.springframework.core.io.Resource; |
| 28 | +import org.springframework.core.io.buffer.DataBuffer; |
| 29 | +import org.springframework.core.testfixture.io.buffer.AbstractLeakCheckingTests; |
| 30 | +import org.springframework.http.ContentDisposition; |
| 31 | +import org.springframework.http.HttpStatus; |
| 32 | +import org.springframework.http.MediaType; |
| 33 | +import org.springframework.web.testfixture.http.client.reactive.MockClientHttpResponse; |
| 34 | + |
| 35 | +import static org.assertj.core.api.Assertions.assertThat; |
| 36 | + |
| 37 | +/** |
| 38 | + * Unit tests for {@link ResourceHttpMessageReader}. |
| 39 | + * @author Rossen Stoyanchev |
| 40 | + */ |
| 41 | +public class ResourceHttpMessageReaderTests extends AbstractLeakCheckingTests { |
| 42 | + |
| 43 | + private final ResourceHttpMessageReader reader = new ResourceHttpMessageReader(); |
| 44 | + |
| 45 | + @Test |
| 46 | + void readResourceAsMono() throws IOException { |
| 47 | + String filename = "test.txt"; |
| 48 | + String body = "Test resource content"; |
| 49 | + |
| 50 | + ContentDisposition contentDisposition = |
| 51 | + ContentDisposition.builder("attachment").name("file").filename(filename).build(); |
| 52 | + |
| 53 | + MockClientHttpResponse response = new MockClientHttpResponse(HttpStatus.OK); |
| 54 | + response.getHeaders().setContentType(MediaType.TEXT_PLAIN); |
| 55 | + response.getHeaders().setContentDisposition(contentDisposition); |
| 56 | + response.setBody(Mono.just(stringBuffer(body))); |
| 57 | + |
| 58 | + Resource resource = reader.readMono( |
| 59 | + ResolvableType.forClass(ByteArrayResource.class), response, Collections.emptyMap()).block(); |
| 60 | + |
| 61 | + assertThat(resource).isNotNull(); |
| 62 | + assertThat(resource.getFilename()).isEqualTo(filename); |
| 63 | + assertThat(resource.getInputStream()).hasContent(body); |
| 64 | + } |
| 65 | + |
| 66 | + private DataBuffer stringBuffer(String value) { |
| 67 | + byte[] bytes = value.getBytes(StandardCharsets.UTF_8); |
| 68 | + DataBuffer buffer = this.bufferFactory.allocateBuffer(bytes.length); |
| 69 | + buffer.write(bytes); |
| 70 | + return buffer; |
| 71 | + } |
| 72 | + |
| 73 | +} |
0 commit comments