|
30 | 30 | import java.nio.file.FileSystem;
|
31 | 31 | import java.nio.file.Files;
|
32 | 32 | import java.nio.file.Path;
|
| 33 | +import java.util.Random; |
33 | 34 | import org.junit.Rule;
|
34 | 35 | import org.junit.Test;
|
35 | 36 | import org.junit.rules.TemporaryFolder;
|
| 37 | +import org.mockito.Mockito; |
| 38 | +import software.amazon.awssdk.checksums.DefaultChecksumAlgorithm; |
| 39 | +import software.amazon.awssdk.checksums.SdkChecksum; |
| 40 | +import software.amazon.awssdk.core.internal.sync.BufferingContentStreamProvider; |
36 | 41 | import software.amazon.awssdk.core.internal.util.Mimetype;
|
| 42 | +import software.amazon.awssdk.utils.BinaryUtils; |
37 | 43 | import software.amazon.awssdk.utils.IoUtils;
|
38 | 44 | import software.amazon.awssdk.utils.StringInputStream;
|
39 | 45 |
|
40 | 46 |
|
41 | 47 | public class RequestBodyTest {
|
| 48 | + private static final SdkChecksum CRC32 = SdkChecksum.forAlgorithm(DefaultChecksumAlgorithm.CRC32); |
42 | 49 |
|
43 | 50 | @Rule
|
44 | 51 | public TemporaryFolder folder = new TemporaryFolder();
|
@@ -140,4 +147,58 @@ public void remainingByteBufferConstructorOnlyRemainingBytesCopied() throws IOEx
|
140 | 147 | byte[] requestBodyBytes = IoUtils.toByteArray(requestBody.contentStreamProvider().newStream());
|
141 | 148 | assertThat(ByteBuffer.wrap(requestBodyBytes)).isEqualTo(bb);
|
142 | 149 | }
|
| 150 | + |
| 151 | + @Test |
| 152 | + public void fromInputStream_streamSupportMarkReset_doesNotBuffer() { |
| 153 | + byte[] newData = new byte[16536]; |
| 154 | + new Random().nextBytes(newData); |
| 155 | + |
| 156 | + ByteArrayInputStream stream = new ByteArrayInputStream(newData); |
| 157 | + |
| 158 | + RequestBody requestBody = RequestBody.fromInputStream(stream, newData.length); |
| 159 | + assertThat(requestBody.contentStreamProvider()).isNotInstanceOf(BufferingContentStreamProvider.class); |
| 160 | + } |
| 161 | + |
| 162 | + @Test |
| 163 | + public void fromInputStream_streamDoesNotSupportMarkReset_buffers() { |
| 164 | + byte[] newData = new byte[16536]; |
| 165 | + new Random().nextBytes(newData); |
| 166 | + |
| 167 | + ByteArrayInputStream stream = Mockito.spy(new ByteArrayInputStream(newData)); |
| 168 | + Mockito.when(stream.markSupported()).thenReturn(false); |
| 169 | + |
| 170 | + RequestBody requestBody = RequestBody.fromInputStream(stream, newData.length); |
| 171 | + assertThat(requestBody.contentStreamProvider()).isInstanceOf(BufferingContentStreamProvider.class); |
| 172 | + } |
| 173 | + |
| 174 | + @Test |
| 175 | + public void fromInputStream_streamSupportsReset_resetsTheStream() { |
| 176 | + byte[] newData = new byte[16536]; |
| 177 | + new Random().nextBytes(newData); |
| 178 | + |
| 179 | + String streamCrc32 = getCrc32(new ByteArrayInputStream(newData)); |
| 180 | + |
| 181 | + ByteArrayInputStream stream = new ByteArrayInputStream(newData); |
| 182 | + assertThat(stream.markSupported()).isTrue(); |
| 183 | + RequestBody requestBody = RequestBody.fromInputStream(stream, newData.length); |
| 184 | + |
| 185 | + assertThat(getCrc32(requestBody.contentStreamProvider().newStream())).isEqualTo(streamCrc32); |
| 186 | + assertThat(getCrc32(requestBody.contentStreamProvider().newStream())).isEqualTo(streamCrc32); |
| 187 | + } |
| 188 | + |
| 189 | + private static String getCrc32(InputStream inputStream) { |
| 190 | + byte[] buff = new byte[1024]; |
| 191 | + int read; |
| 192 | + |
| 193 | + CRC32.reset(); |
| 194 | + try { |
| 195 | + while ((read = inputStream.read(buff)) != -1) { |
| 196 | + CRC32.update(buff, 0, read); |
| 197 | + } |
| 198 | + } catch (IOException e) { |
| 199 | + throw new RuntimeException(e); |
| 200 | + } |
| 201 | + |
| 202 | + return BinaryUtils.toHex(CRC32.getChecksumBytes()); |
| 203 | + } |
143 | 204 | }
|
0 commit comments