|
17 | 17 | package org.springframework.web.util;
|
18 | 18 |
|
19 | 19 | import java.io.UnsupportedEncodingException;
|
| 20 | +import java.lang.reflect.Field; |
20 | 21 | import java.nio.charset.StandardCharsets;
|
21 | 22 |
|
22 | 23 | import org.junit.jupiter.api.Test;
|
23 | 24 |
|
24 | 25 | import org.springframework.http.HttpMethod;
|
25 | 26 | import org.springframework.http.MediaType;
|
| 27 | +import org.springframework.util.FastByteArrayOutputStream; |
| 28 | +import org.springframework.util.ReflectionUtils; |
26 | 29 | import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
|
27 | 30 |
|
28 | 31 | import static org.assertj.core.api.Assertions.assertThat;
|
@@ -89,6 +92,19 @@ void cachedContentToStringWithLimit() throws Exception {
|
89 | 92 | assertThat(wrapper.getContentAsString()).isEqualTo(new String("Hel".getBytes(CHARSET), CHARSET));
|
90 | 93 | }
|
91 | 94 |
|
| 95 | + @Test |
| 96 | + void shouldNotAllocateMoreThanCacheLimit() throws Exception { |
| 97 | + ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper(createGetRequest("Hello World"), CONTENT_CACHE_LIMIT); |
| 98 | + Field field = ReflectionUtils.findField(ContentCachingRequestWrapper.class, "cachedContent"); |
| 99 | + ReflectionUtils.makeAccessible(field); |
| 100 | + FastByteArrayOutputStream cachedContent = (FastByteArrayOutputStream) ReflectionUtils.getField(field, wrapper); |
| 101 | + field = ReflectionUtils.findField(FastByteArrayOutputStream.class, "initialBlockSize"); |
| 102 | + ReflectionUtils.makeAccessible(field); |
| 103 | + int blockSize = (int) ReflectionUtils.getField(field, cachedContent); |
| 104 | + assertThat(blockSize).isEqualTo(CONTENT_CACHE_LIMIT); |
| 105 | + } |
| 106 | + |
| 107 | + |
92 | 108 | @Test
|
93 | 109 | void cachedContentWithOverflow() throws Exception {
|
94 | 110 | ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper(
|
|
0 commit comments