55 * LICENSE file in the root directory of this source tree.
66 */
77
8+ @file:Suppress(" DEPRECATION_ERROR" ) // Conflicting okhttp versions
9+
810package com.facebook.react.modules.network
911
1012import com.facebook.react.bridge.Arguments
@@ -15,7 +17,13 @@ import com.facebook.react.internal.featureflags.ReactNativeFeatureFlags
1517import com.facebook.react.internal.featureflags.ReactNativeFeatureFlagsDefaults
1618import com.facebook.react.internal.featureflags.ReactNativeFeatureFlagsForTests
1719import com.facebook.testutils.shadows.ShadowArguments
20+ import java.io.ByteArrayInputStream
1821import java.net.SocketTimeoutException
22+ import okhttp3.MediaType
23+ import okhttp3.MediaType.Companion.toMediaTypeOrNull
24+ import okhttp3.MultipartBody
25+ import okhttp3.RequestBody
26+ import okhttp3.RequestBody.Companion.toRequestBody
1927import org.assertj.core.api.Assertions.assertThat
2028import org.junit.After
2129import org.junit.Before
@@ -292,6 +300,84 @@ class NetworkEventUtilTest {
292300 assertThat(args.getString(3 )).isEqualTo(url)
293301 }
294302
303+ @Test
304+ fun testGetRequestBodyPreviewReturnsNullForNullBody () {
305+ assertThat(NetworkEventUtil .getRequestBodyPreview(null )).isNull()
306+ }
307+
308+ @Test
309+ fun testGetRequestBodyPreviewReturnsBodyForStringRequest () {
310+ val payload = """ {"key":"value"}"""
311+ val body = payload.toRequestBody(" application/json" .toMediaTypeOrNull())
312+
313+ assertThat(NetworkEventUtil .getRequestBodyPreview(body)).isEqualTo(payload)
314+ }
315+
316+ @Test
317+ fun testGetRequestBodyPreviewUnwrapsProgressRequestBody () {
318+ val payload = " hello world"
319+ val inner = payload.toRequestBody(" text/plain" .toMediaTypeOrNull())
320+ val wrapped = ProgressRequestBody (inner) { _, _, _ -> }
321+
322+ assertThat(NetworkEventUtil .getRequestBodyPreview(wrapped)).isEqualTo(payload)
323+ }
324+
325+ @Test
326+ fun testGetRequestBodyPreviewMultipartWithTextParts () {
327+ val body =
328+ MultipartBody .Builder (" test-boundary" )
329+ .setType(MultipartBody .FORM )
330+ .addFormDataPart(" field1" , " value1" )
331+ .addFormDataPart(" field2" , " value2" )
332+ .build()
333+
334+ val preview = NetworkEventUtil .getRequestBodyPreview(body)
335+
336+ assertThat(preview).isNotNull()
337+ assertThat(preview).contains(" --test-boundary" )
338+ assertThat(preview).contains(" --test-boundary--" )
339+ assertThat(preview).contains(" name=\" field1\" " )
340+ assertThat(preview).contains(" value1" )
341+ assertThat(preview).contains(" name=\" field2\" " )
342+ assertThat(preview).contains(" value2" )
343+ assertThat(preview).doesNotContain(" [Preview unavailable]" )
344+ }
345+
346+ @Test
347+ fun testGetRequestBodyPreviewMultipartWithFilePartReplacesBinaryContent () {
348+ val fileBytes = ByteArray (2048 ) { it.toByte() }
349+ val streamingPart =
350+ RequestBodyUtil .create(MediaType .parse(" application/octet-stream" ), ByteArrayInputStream (fileBytes))
351+ val body =
352+ MultipartBody .Builder (" test-boundary" )
353+ .setType(MultipartBody .FORM )
354+ .addFormDataPart(" description" , " an image" )
355+ .addFormDataPart(" file" , " photo.jpg" , streamingPart)
356+ .build()
357+
358+ val preview = NetworkEventUtil .getRequestBodyPreview(body)
359+
360+ assertThat(preview).isNotNull()
361+ assertThat(preview).contains(" --test-boundary" )
362+ assertThat(preview).contains(" name=\" description\" " )
363+ assertThat(preview).contains(" an image" )
364+ assertThat(preview).contains(" name=\" file\" " )
365+ assertThat(preview).contains(" filename=\" photo.jpg\" " )
366+ assertThat(preview).contains(" [Binary data, 2048 bytes]" )
367+ assertThat(preview).doesNotContain(" [Preview unavailable]" )
368+ }
369+
370+ @Test
371+ fun testGetRequestBodyPreviewSingleOneShotBodyShowsPlaceholder () {
372+ val fileBytes = ByteArray (512 ) { it.toByte() }
373+ val body =
374+ RequestBodyUtil .create(MediaType .parse(" application/octet-stream" ), ByteArrayInputStream (fileBytes))
375+
376+ val preview = NetworkEventUtil .getRequestBodyPreview(body)
377+
378+ assertThat(preview).isEqualTo(" [Binary data, 512 bytes]" )
379+ }
380+
295381 @Test
296382 fun testNullReactContext () {
297383 val url = " http://example.com"
0 commit comments