Skip to content

Commit 2244dba

Browse files
committed
added two regression-guard tests
1 parent b93f953 commit 2244dba

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

packages/react-native/ReactAndroid/src/test/java/com/facebook/react/modules/network/NetworkEventUtilTest.kt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,40 @@ class NetworkEventUtilTest {
378378
assertThat(preview).isEqualTo("[Binary data, 512 bytes]")
379379
}
380380

381+
@Test
382+
fun testGetRequestBodyPreviewDoesNotConsumeMultipartOneShotStream() {
383+
// Regression guard: previewMultipartWithBinaryParts must not call writeTo() on a
384+
// one-shot part. If it ever does, the underlying stream will be drained and the
385+
// real upload will fail at request time.
386+
val fileBytes = ByteArray(2048) { it.toByte() }
387+
val stream = ByteArrayInputStream(fileBytes)
388+
val streamingPart =
389+
RequestBodyUtil.create(MediaType.parse("application/octet-stream"), stream)
390+
val body =
391+
MultipartBody.Builder("test-boundary")
392+
.setType(MultipartBody.FORM)
393+
.addFormDataPart("description", "an image")
394+
.addFormDataPart("file", "photo.jpg", streamingPart)
395+
.build()
396+
397+
NetworkEventUtil.getRequestBodyPreview(body)
398+
399+
assertThat(stream.available()).isEqualTo(fileBytes.size)
400+
}
401+
402+
@Test
403+
fun testGetRequestBodyPreviewDoesNotConsumeSingleOneShotStream() {
404+
// Regression guard for the top-level one-shot branch: getRequestBodyPreview must
405+
// only read contentLength() / contentType() on a one-shot body, never writeTo().
406+
val fileBytes = ByteArray(512) { it.toByte() }
407+
val stream = ByteArrayInputStream(fileBytes)
408+
val body = RequestBodyUtil.create(MediaType.parse("application/octet-stream"), stream)
409+
410+
NetworkEventUtil.getRequestBodyPreview(body)
411+
412+
assertThat(stream.available()).isEqualTo(fileBytes.size)
413+
}
414+
381415
@Test
382416
fun testNullReactContext() {
383417
val url = "http://example.com"

0 commit comments

Comments
 (0)