13
13
import org .junit .Test ;
14
14
import org .mockito .Mock ;
15
15
16
+ import java .io .ByteArrayInputStream ;
16
17
import java .io .ByteArrayOutputStream ;
17
18
import java .io .IOException ;
18
19
import java .io .InputStream ;
@@ -374,7 +375,7 @@ public void getOutputStreamThrowsWhenNotEnabled() throws IOException {
374
375
// Should throw exception since doOutput is not enabled
375
376
mAdapter .getOutputStream ();
376
377
}
377
-
378
+
378
379
@ Test
379
380
public void setDoOutputEnablesOutput () {
380
381
mAdapter = new HttpResponseConnectionAdapter (mTestUrl , mMockResponse , mTestCertificates );
@@ -386,15 +387,15 @@ public void setDoOutputEnablesOutput() {
386
387
mAdapter .setDoOutput (true );
387
388
assertEquals (true , mAdapter .getDoOutput ());
388
389
}
389
-
390
+
390
391
@ Test
391
392
public void getOutputStreamAfterEnablingOutput () throws IOException {
392
393
mAdapter = new HttpResponseConnectionAdapter (mTestUrl , mMockResponse , mTestCertificates );
393
394
mAdapter .setDoOutput (true );
394
395
395
396
assertNotNull ("Output stream should not be null when doOutput is enabled" , mAdapter .getOutputStream ());
396
397
}
397
-
398
+
398
399
@ Test
399
400
public void writeToOutputStream () throws IOException {
400
401
// Create a ByteArrayOutputStream to capture the written data
@@ -411,15 +412,15 @@ public void writeToOutputStream() throws IOException {
411
412
// Verify that the data was written correctly
412
413
assertEquals ("Written data should match the input" , testData , testOutputStream .toString (StandardCharsets .UTF_8 .name ()));
413
414
}
414
-
415
+
415
416
@ Test
416
417
public void disconnectClosesOutputStream () throws IOException {
417
418
// Create a custom OutputStream that tracks if it's been closed
418
419
TestOutputStream testOutputStream = new TestOutputStream ();
419
-
420
+
420
421
mAdapter = new HttpResponseConnectionAdapter (mTestUrl , mMockResponse , mTestCertificates , testOutputStream );
421
422
mAdapter .setDoOutput (true );
422
-
423
+
423
424
// Get the output stream and write some data
424
425
mAdapter .getOutputStream ().write ("Test" .getBytes (StandardCharsets .UTF_8 ));
425
426
@@ -433,6 +434,37 @@ public void disconnectClosesOutputStream() throws IOException {
433
434
assertTrue ("Output stream should be closed after disconnect" , testOutputStream .isClosed ());
434
435
}
435
436
437
+ @ Test
438
+ public void disconnectClosesInputStream () throws IOException {
439
+ // Create a custom InputStream that tracks if it's been closed
440
+ TestInputStream testInputStream = new TestInputStream ("Test response data" .getBytes (StandardCharsets .UTF_8 ));
441
+ TestOutputStream testOutputStream = new TestOutputStream ();
442
+
443
+ // Create adapter with injected test input stream
444
+ when (mMockResponse .getHttpStatus ()).thenReturn (200 );
445
+ mAdapter = new HttpResponseConnectionAdapter (
446
+ mTestUrl ,
447
+ mMockResponse ,
448
+ mTestCertificates ,
449
+ testOutputStream ,
450
+ testInputStream ,
451
+ null );
452
+
453
+ // Get the input stream and read some data to simulate usage
454
+ InputStream stream = mAdapter .getInputStream ();
455
+ byte [] buffer = new byte [10 ];
456
+ stream .read (buffer );
457
+
458
+ // Verify the stream is not closed yet
459
+ assertFalse ("Input stream should not be closed before disconnect" , testInputStream .isClosed ());
460
+
461
+ // Disconnect should close the input stream
462
+ mAdapter .disconnect ();
463
+
464
+ // Verify the stream was closed
465
+ assertTrue ("Input stream should be closed after disconnect" , testInputStream .isClosed ());
466
+ }
467
+
436
468
/**
437
469
* Custom OutputStream implementation for testing that tracks if it's been closed.
438
470
*/
@@ -449,4 +481,48 @@ public boolean isClosed() {
449
481
return mClosed ;
450
482
}
451
483
}
484
+
485
+ private static class TestInputStream extends ByteArrayInputStream {
486
+ private boolean mClosed = false ;
487
+
488
+ public TestInputStream (byte [] data ) {
489
+ super (data );
490
+ }
491
+
492
+ @ Override
493
+ public void close () throws IOException {
494
+ super .close ();
495
+ mClosed = true ;
496
+ }
497
+
498
+ public boolean isClosed () {
499
+ return mClosed ;
500
+ }
501
+ }
502
+
503
+ @ Test
504
+ public void disconnectClosesErrorStream () throws IOException {
505
+ TestInputStream testErrorStream = new TestInputStream ("Error data" .getBytes (StandardCharsets .UTF_8 ));
506
+ TestOutputStream testOutputStream = new TestOutputStream ();
507
+
508
+ when (mMockResponse .getHttpStatus ()).thenReturn (404 ); // Error status
509
+ mAdapter = new HttpResponseConnectionAdapter (
510
+ mTestUrl ,
511
+ mMockResponse ,
512
+ mTestCertificates ,
513
+ testOutputStream ,
514
+ null ,
515
+ testErrorStream );
516
+
517
+ // Get the error stream and read some data to simulate usage
518
+ InputStream stream = mAdapter .getErrorStream ();
519
+ byte [] buffer = new byte [10 ];
520
+ stream .read (buffer );
521
+
522
+ assertFalse ("Error stream should not be closed before disconnect" , testErrorStream .isClosed ());
523
+
524
+ mAdapter .disconnect ();
525
+
526
+ assertTrue ("Error stream should be closed after disconnect" , testErrorStream .isClosed ());
527
+ }
452
528
}
0 commit comments