|
7 | 7 | import java.io.IOException; |
8 | 8 | import java.io.InputStream; |
9 | 9 | import java.net.URI; |
| 10 | +import java.nio.file.Files; |
| 11 | +import java.nio.file.Path; |
10 | 12 | import java.util.Random; |
11 | 13 | import org.junit.jupiter.api.DisplayName; |
12 | 14 | import org.junit.jupiter.api.Test; |
| 15 | +import org.junit.jupiter.api.io.TempDir; |
13 | 16 |
|
14 | 17 | /** Tests for streaming body support in ProxyRequest and ProxyResponse. */ |
15 | 18 | @DisplayName("Streaming Proxy Tests") |
@@ -167,6 +170,53 @@ public void testSupplierBasedBody() throws IOException { |
167 | 170 | assertThat(content2).isEqualTo("repeatable data"); |
168 | 171 | } |
169 | 172 |
|
| 173 | + @Test |
| 174 | + @DisplayName("materializeTo() saves body to file and returns re-readable response") |
| 175 | + public void testMaterializeTo(@TempDir Path tempDir) throws IOException { |
| 176 | + // Create a response with a single-use stream |
| 177 | + InputStream stream = new ByteArrayInputStream("test content for file".getBytes()); |
| 178 | + ProxyResponse response = ProxyResponse.fromStream(200, Headers.empty(), stream); |
| 179 | + |
| 180 | + // Materialize to a temp file |
| 181 | + Path tempFile = tempDir.resolve("response.bin"); |
| 182 | + ProxyResponse materialized = response.materializeTo(tempFile); |
| 183 | + |
| 184 | + // Verify file was created and contains the body |
| 185 | + assertThat(tempFile).exists(); |
| 186 | + String fileContent = Files.readString(tempFile); |
| 187 | + assertThat(fileContent).isEqualTo("test content for file"); |
| 188 | + |
| 189 | + // Verify the materialized response is re-readable |
| 190 | + String content1 = new String(materialized.bodyStream().readAllBytes()); |
| 191 | + assertThat(content1).isEqualTo("test content for file"); |
| 192 | + |
| 193 | + String content2 = new String(materialized.bodyStream().readAllBytes()); |
| 194 | + assertThat(content2).isEqualTo("test content for file"); |
| 195 | + |
| 196 | + // Verify status code and headers are preserved |
| 197 | + assertThat(materialized.statusCode()).isEqualTo(200); |
| 198 | + assertThat(materialized.headers()).isEqualTo(Headers.empty()); |
| 199 | + } |
| 200 | + |
| 201 | + @Test |
| 202 | + @DisplayName("materializeTo() replaces existing file") |
| 203 | + public void testMaterializeToReplacesFile(@TempDir Path tempDir) throws IOException { |
| 204 | + Path tempFile = tempDir.resolve("response.bin"); |
| 205 | + |
| 206 | + // Create a file with existing content |
| 207 | + Files.writeString(tempFile, "old content"); |
| 208 | + assertThat(Files.readString(tempFile)).isEqualTo("old content"); |
| 209 | + |
| 210 | + // Materialize new content to the same file |
| 211 | + ProxyResponse response = |
| 212 | + ProxyResponse.fromBytes(200, Headers.empty(), "new content".getBytes()); |
| 213 | + ProxyResponse materialized = response.materializeTo(tempFile); |
| 214 | + |
| 215 | + // Verify file was replaced |
| 216 | + assertThat(Files.readString(tempFile)).isEqualTo("new content"); |
| 217 | + assertThat(new String(materialized.bodyStream().readAllBytes())).isEqualTo("new content"); |
| 218 | + } |
| 219 | + |
170 | 220 | /** |
171 | 221 | * Virtual InputStream that generates data on-the-fly without allocating large byte arrays. This |
172 | 222 | * simulates a large response body for testing streaming without using excessive memory. |
|
0 commit comments