|
1 | 1 | package io.split.android.client.network;
|
2 | 2 |
|
3 | 3 | import static org.junit.Assert.assertEquals;
|
| 4 | +import static org.junit.Assert.assertFalse; |
4 | 5 | import static org.junit.Assert.assertNotNull;
|
5 | 6 | import static org.junit.Assert.assertNull;
|
6 | 7 | import static org.junit.Assert.assertSame;
|
|
12 | 13 | import org.junit.Test;
|
13 | 14 | import org.mockito.Mock;
|
14 | 15 |
|
| 16 | +import java.io.ByteArrayOutputStream; |
15 | 17 | import java.io.IOException;
|
16 | 18 | import java.io.InputStream;
|
17 | 19 | import java.net.MalformedURLException;
|
@@ -367,8 +369,84 @@ public void urlCanBeRetrieved() {
|
367 | 369 | }
|
368 | 370 |
|
369 | 371 | @Test(expected = IOException.class)
|
370 |
| - public void getOutputStreamThrows() throws IOException { |
| 372 | + public void getOutputStreamThrowsWhenNotEnabled() throws IOException { |
371 | 373 | mAdapter = new HttpResponseConnectionAdapter(mTestUrl, mMockResponse, mTestCertificates);
|
| 374 | + // Should throw exception since doOutput is not enabled |
372 | 375 | mAdapter.getOutputStream();
|
373 | 376 | }
|
| 377 | + |
| 378 | + @Test |
| 379 | + public void setDoOutputEnablesOutput() { |
| 380 | + mAdapter = new HttpResponseConnectionAdapter(mTestUrl, mMockResponse, mTestCertificates); |
| 381 | + |
| 382 | + // Initially doOutput should be false |
| 383 | + assertEquals(false, mAdapter.getDoOutput()); |
| 384 | + |
| 385 | + // After setting doOutput to true, getDoOutput should return true |
| 386 | + mAdapter.setDoOutput(true); |
| 387 | + assertEquals(true, mAdapter.getDoOutput()); |
| 388 | + } |
| 389 | + |
| 390 | + @Test |
| 391 | + public void getOutputStreamAfterEnablingOutput() throws IOException { |
| 392 | + mAdapter = new HttpResponseConnectionAdapter(mTestUrl, mMockResponse, mTestCertificates); |
| 393 | + mAdapter.setDoOutput(true); |
| 394 | + |
| 395 | + assertNotNull("Output stream should not be null when doOutput is enabled", mAdapter.getOutputStream()); |
| 396 | + } |
| 397 | + |
| 398 | + @Test |
| 399 | + public void writeToOutputStream() throws IOException { |
| 400 | + // Create a ByteArrayOutputStream to capture the written data |
| 401 | + ByteArrayOutputStream testOutputStream = new ByteArrayOutputStream(); |
| 402 | + |
| 403 | + // Use the constructor that accepts a custom OutputStream |
| 404 | + mAdapter = new HttpResponseConnectionAdapter(mTestUrl, mMockResponse, mTestCertificates, testOutputStream); |
| 405 | + mAdapter.setDoOutput(true); |
| 406 | + |
| 407 | + // Write test data to the output stream |
| 408 | + String testData = "Test output data"; |
| 409 | + mAdapter.getOutputStream().write(testData.getBytes(StandardCharsets.UTF_8)); |
| 410 | + |
| 411 | + // Verify that the data was written correctly |
| 412 | + assertEquals("Written data should match the input", testData, testOutputStream.toString(StandardCharsets.UTF_8.name())); |
| 413 | + } |
| 414 | + |
| 415 | + @Test |
| 416 | + public void disconnectClosesOutputStream() throws IOException { |
| 417 | + // Create a custom OutputStream that tracks if it's been closed |
| 418 | + TestOutputStream testOutputStream = new TestOutputStream(); |
| 419 | + |
| 420 | + mAdapter = new HttpResponseConnectionAdapter(mTestUrl, mMockResponse, mTestCertificates, testOutputStream); |
| 421 | + mAdapter.setDoOutput(true); |
| 422 | + |
| 423 | + // Get the output stream and write some data |
| 424 | + mAdapter.getOutputStream().write("Test".getBytes(StandardCharsets.UTF_8)); |
| 425 | + |
| 426 | + // Verify the stream is not closed yet |
| 427 | + assertFalse("Output stream should not be closed before disconnect", testOutputStream.isClosed()); |
| 428 | + |
| 429 | + // Disconnect should close the output stream |
| 430 | + mAdapter.disconnect(); |
| 431 | + |
| 432 | + // Verify the stream was closed |
| 433 | + assertTrue("Output stream should be closed after disconnect", testOutputStream.isClosed()); |
| 434 | + } |
| 435 | + |
| 436 | + /** |
| 437 | + * Custom OutputStream implementation for testing that tracks if it's been closed. |
| 438 | + */ |
| 439 | + private static class TestOutputStream extends ByteArrayOutputStream { |
| 440 | + private boolean mClosed = false; |
| 441 | + |
| 442 | + @Override |
| 443 | + public void close() throws IOException { |
| 444 | + super.close(); |
| 445 | + mClosed = true; |
| 446 | + } |
| 447 | + |
| 448 | + public boolean isClosed() { |
| 449 | + return mClosed; |
| 450 | + } |
| 451 | + } |
374 | 452 | }
|
0 commit comments