|
17 | 17 | package org.springframework.http.converter;
|
18 | 18 |
|
19 | 19 | import java.io.ByteArrayInputStream;
|
| 20 | +import java.io.IOException; |
20 | 21 | import java.lang.reflect.Type;
|
21 | 22 | import java.nio.charset.StandardCharsets;
|
22 | 23 | import java.util.ArrayList;
|
|
27 | 28 |
|
28 | 29 | import org.springframework.core.ParameterizedTypeReference;
|
29 | 30 | import org.springframework.core.io.ClassPathResource;
|
| 31 | +import org.springframework.core.io.InputStreamResource; |
30 | 32 | import org.springframework.core.io.Resource;
|
31 | 33 | import org.springframework.core.io.support.ResourceRegion;
|
32 | 34 | import org.springframework.http.HttpHeaders;
|
|
36 | 38 | import org.springframework.web.testfixture.http.MockHttpOutputMessage;
|
37 | 39 |
|
38 | 40 | import static org.assertj.core.api.Assertions.assertThat;
|
| 41 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; |
39 | 42 | import static org.mockito.BDDMockito.given;
|
40 | 43 | import static org.mockito.Mockito.mock;
|
41 | 44 |
|
@@ -198,4 +201,86 @@ public void applicationOctetStreamDefaultContentType() throws Exception {
|
198 | 201 | assertThat(outputMessage.getBodyAsString(StandardCharsets.UTF_8)).isEqualTo("Spring");
|
199 | 202 | }
|
200 | 203 |
|
| 204 | + @Test |
| 205 | + void shouldNotWriteForUnsupportedType() throws Exception { |
| 206 | + MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); |
| 207 | + Object unsupportedBody = new Object(); |
| 208 | + |
| 209 | + assertThatThrownBy(() -> converter.write(unsupportedBody, null, outputMessage)) |
| 210 | + .isInstanceOf(HttpMessageNotWritableException.class); |
| 211 | + } |
| 212 | + |
| 213 | + @Test |
| 214 | + void shouldHandleEmptyResourceRegionCollection() throws Exception { |
| 215 | + MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); |
| 216 | + List<ResourceRegion> emptyRegions = Collections.emptyList(); |
| 217 | + |
| 218 | + assertThatThrownBy(() -> converter.write(emptyRegions, null, outputMessage)) |
| 219 | + .isInstanceOf(HttpMessageNotWritableException.class); |
| 220 | + } |
| 221 | + |
| 222 | + @Test |
| 223 | + void shouldGetDefaultContentTypeForResourceRegion() { |
| 224 | + Resource resource = new ClassPathResource("byterangeresource.txt", getClass()); |
| 225 | + ResourceRegion region = new ResourceRegion(resource, 0, 10); |
| 226 | + |
| 227 | + MediaType contentType = converter.getDefaultContentType(region); |
| 228 | + assertThat(contentType).isEqualTo(MediaType.TEXT_PLAIN); |
| 229 | + } |
| 230 | + |
| 231 | + @Test |
| 232 | + void shouldGetDefaultOctetStreamContentTypeForUnknownResource() { |
| 233 | + Resource resource = mock(Resource.class); |
| 234 | + given(resource.getFilename()).willReturn("unknown.dat"); |
| 235 | + ResourceRegion region = new ResourceRegion(resource, 0, 10); |
| 236 | + |
| 237 | + MediaType contentType = converter.getDefaultContentType(region); |
| 238 | + assertThat(contentType).isEqualTo(MediaType.APPLICATION_OCTET_STREAM); |
| 239 | + } |
| 240 | + |
| 241 | + @Test |
| 242 | + void shouldSupportRepeatableWritesForNonInputStreamResource() { |
| 243 | + Resource resource = new ClassPathResource("byterangeresource.txt", getClass()); |
| 244 | + ResourceRegion region = new ResourceRegion(resource, 0, 10); |
| 245 | + |
| 246 | + assertThat(converter.supportsRepeatableWrites(region)).isTrue(); |
| 247 | + } |
| 248 | + |
| 249 | + @Test |
| 250 | + void shouldNotSupportRepeatableWritesForInputStreamResource() { |
| 251 | + Resource resource = mock(InputStreamResource.class); |
| 252 | + ResourceRegion region = new ResourceRegion(resource, 0, 10); |
| 253 | + |
| 254 | + assertThat(converter.supportsRepeatableWrites(region)).isFalse(); |
| 255 | + } |
| 256 | + |
| 257 | + @Test |
| 258 | + void shouldHandleIOExceptionWhenWritingRegion() throws Exception { |
| 259 | + MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); |
| 260 | + Resource resource = mock(Resource.class); |
| 261 | + given(resource.contentLength()).willReturn(10L); |
| 262 | + given(resource.getInputStream()).willThrow(new IOException("Simulated error")); |
| 263 | + ResourceRegion region = new ResourceRegion(resource, 0, 5); |
| 264 | + |
| 265 | + // Should not throw exception |
| 266 | + converter.write(region, MediaType.TEXT_PLAIN, outputMessage); |
| 267 | + |
| 268 | + assertThat(outputMessage.getHeaders().getRange()).isEqualTo(HttpRange.parseRanges("bytes 0-4/10")); |
| 269 | + } |
| 270 | + |
| 271 | + @Test |
| 272 | + void shouldHandleIOExceptionWhenWritingRegionCollection() throws Exception { |
| 273 | + MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); |
| 274 | + Resource resource = mock(Resource.class); |
| 275 | + given(resource.contentLength()).willReturn(10L); |
| 276 | + given(resource.getInputStream()).willThrow(new IOException("Simulated error")); |
| 277 | + ResourceRegion region = new ResourceRegion(resource, 0, 5); |
| 278 | + List<ResourceRegion> regions = Collections.singletonList(region); |
| 279 | + |
| 280 | + // Should not throw exception |
| 281 | + converter.write(regions, MediaType.TEXT_PLAIN, outputMessage); |
| 282 | + |
| 283 | + assertThat(outputMessage.getHeaders().getContentType().toString()) |
| 284 | + .startsWith("multipart/byteranges;boundary="); |
| 285 | + } |
201 | 286 | }
|
0 commit comments