/** * Tests that we can read non-partial content and return the expected contents. */ @Test public void testReadPartialContentWithOffsetAndChunkSizeOk() throws Exception { String content = m_content; PartialContentConnection conn = new PartialContentConnection(content, false); ConnectionHandler handler = new TestConnectionHandler(conn); // 4752 + 48 = 4800, causing only one chunk to be returned... ContentRangeInputStream is = new ContentRangeInputStream(handler, m_testURL, 48, 4752); assertEquals(slurpAsStringWithBuffer(is), content.substring(48)); }
/** * Tests that we can read non-partial content and return the expected contents. */ @Test public void testReadPartialContentWithOffsetOk() throws Exception { String content = m_content; ConnectionHandler handler = new TestConnectionHandler(new PartialContentConnection(content, false)); ContentRangeInputStream is = new ContentRangeInputStream(handler, m_testURL, 48); assertEquals(slurpAsStringWithBuffer(is), content.substring(48)); }
/** * Tests that we can read partial content and return the expected contents. */ @Test public void testReadPartialContentWithUnknownChunkSizeOk() throws Exception { String content = m_content; ConnectionHandler handler = new TestConnectionHandler(new FailingContentConnection(content, Failure.PARTIAL_UNKNOWN_CHUNK_SIZE)); ContentRangeInputStream is = new ContentRangeInputStream(handler, m_testURL); assertEquals(slurpAsStringWithBuffer(is), content.substring(48)); }
/** * Tests that we cannot read partial content if the server returns a complete body. */ @Test(expectedExceptions = IOException.class) public void testReadPartialContentWithChangingContentLengthFail() throws Exception { ConnectionHandler handler = new TestConnectionHandler(new FailingContentConnection(m_content, Failure.PARTIAL_CHANGING_CONTENT_LENGTH)); ContentRangeInputStream is = null; try { is = new ContentRangeInputStream(handler, m_testURL); is.read(new byte[1024]); // should succeed. is.read(new byte[1024]); // should fail! } finally { if (is != null) { is.close(); } } }
/** * Tests that we cannot read partial content if the content is not available. */ @Test(expectedExceptions = IOException.class) public void testReadPartialContentNotFoundFail() throws Exception { ConnectionHandler handler = new TestConnectionHandler(new FailingContentConnection(m_content, Failure.CONTENT_NOT_FOUND)); ContentRangeInputStream is = null; try { is = new ContentRangeInputStream(handler, m_testURL); is.read(); // should fail! } finally { if (is != null) { is.close(); } } }
/** * Tests that we cannot read partial content if given a non-byte range value in the Content-Range header. */ @Test(expectedExceptions = IOException.class) public void testReadPartialContentWithoutByteRangeValueFail() throws Exception { ConnectionHandler handler = new TestConnectionHandler(new FailingContentConnection(m_content, Failure.PARTIAL_NON_BYTE_RANGE)); ContentRangeInputStream is = null; try { is = new ContentRangeInputStream(handler, m_testURL); is.read(); // should fail! } finally { if (is != null) { is.close(); } } }
/** * Tests that we cannot read partial content if the server is not available. */ @Test(expectedExceptions = RetryAfterException.class) public void testReadPartialContentServerUnavailableFail() throws Exception { ConnectionHandler handler = new TestConnectionHandler(new FailingContentConnection(m_content, Failure.SERVER_UNAVAILABLE)); ContentRangeInputStream is = null; try { is = new ContentRangeInputStream(handler, m_testURL); is.read(); // should fail! } finally { if (is != null) { is.close(); } } }
/** * Tests that we cannot read partial content without a Content-Range header. */ @Test(expectedExceptions = IOException.class) public void testReadPartialContentWithoutContentRangeHeaderFail() throws Exception { ConnectionHandler handler = new TestConnectionHandler(new FailingContentConnection(m_content, Failure.PARTIAL_NO_CONTENT_RANGE)); ContentRangeInputStream is = null; try { is = new ContentRangeInputStream(handler, m_testURL); is.read(); // should fail! } finally { if (is != null) { is.close(); } } }
/** * Tests that we can read non-partial content and return the expected contents. */ @Test public void testReadNonPartialContentByteForByteOk() throws Exception { String content = m_content; ConnectionHandler handler = new TestConnectionHandler(new CompleteContentConnection(content, true)); ContentRangeInputStream is = new ContentRangeInputStream(handler, m_testURL); assertEquals(slurpAsStringByteForByte(is), content); // try several additional reads, which should all return -1 (= EOF)... int tries = 5; while (--tries > 0) { assertEquals(is.read(), -1); } }
/** * Tests that we can read non-partial content and return the expected contents. */ @Test public void testReadPartialContentWithChunkSizeOk() throws Exception { String content = m_content; PartialContentConnection conn = new PartialContentConnection(content, false); ConnectionHandler handler = new TestConnectionHandler(conn); // should cause chunks of 1024 bytes to be used, which means 4 complete chunks and one chunk of 704 bytes... ContentRangeInputStream is = new ContentRangeInputStream(handler, m_testURL, 0, 1024); assertEquals(slurpAsStringWithBuffer(is), content); assertResponseHeader(conn, "Content-Range", "bytes 4096-4800/4800"); }
/** * Tests that we can read partial content and return the expected contents. */ @Test public void testReadPartialEmptyContentByteForByteOk() throws Exception { String content = ""; ConnectionHandler handler = new TestConnectionHandler(new PartialContentConnection(content, false)); ContentRangeInputStream is = new ContentRangeInputStream(handler, m_testURL); assertEquals(slurpAsStringByteForByte(is), content); }
/** * Tests that we can read partial content and return the expected contents. */ @Test public void testReadPartialContentOk() throws Exception { String content = m_content; ConnectionHandler handler = new TestConnectionHandler(new PartialContentConnection(content, false)); ContentRangeInputStream is = new ContentRangeInputStream(handler, m_testURL); assertEquals(slurpAsStringWithBuffer(is), content); }
/** * Tests that we can read partial content and return the expected contents. */ @Test public void testReadPartialContentByteForByteOk() throws Exception { String content = m_content; ConnectionHandler handler = new TestConnectionHandler(new PartialContentConnection(content, false)); ContentRangeInputStream is = new ContentRangeInputStream(handler, m_testURL); assertEquals(slurpAsStringByteForByte(is), content); }
/** * Tests that we can read partial content and return the expected contents. */ @Test public void testReadPartialEmptyContentOk() throws Exception { String content = ""; ConnectionHandler handler = new TestConnectionHandler(new PartialContentConnection(content, false)); ContentRangeInputStream is = new ContentRangeInputStream(handler, m_testURL); assertEquals(slurpAsStringWithBuffer(is), content); }
/** * Tests that we can read non-partial content and return the expected contents. */ @Test public void testReadNonPartialWithoutContentLengthOk() throws Exception { String content = ""; ConnectionHandler handler = new TestConnectionHandler(new CompleteContentConnection(content, false)); ContentRangeInputStream is = new ContentRangeInputStream(handler, m_testURL); assertEquals(slurpAsStringWithBuffer(is), content); }
/** * Tests that we can read non-partial content and return the expected contents. */ @Test public void testReadNonPartialContentOk() throws Exception { String content = m_content; ConnectionHandler handler = new TestConnectionHandler(new CompleteContentConnection(content, true)); ContentRangeInputStream is = new ContentRangeInputStream(handler, m_testURL); assertEquals(slurpAsStringWithBuffer(is), content); }
/** * Tests that we call {@link InputStream#close()} multiple times. */ @Test public void testDoubleClosedStreamOk() throws Exception { ConnectionHandler handler = new TestConnectionHandler(new CompleteContentConnection(m_content, true)); ContentRangeInputStream is = new ContentRangeInputStream(handler, m_testURL); is.close(); // simulate an early close... is.close(); // not a problem... }
/** * Tests that we can read partial content and return the expected contents. */ @Test public void testReadPartialContentWithDeferredTotalLengthOk() throws Exception { String content = m_content; ConnectionHandler handler = new TestConnectionHandler(new PartialContentConnection(content, true)); ContentRangeInputStream is = new ContentRangeInputStream(handler, m_testURL); assertEquals(slurpAsStringWithBuffer(is), content); }
/** * Tests that we can read non-partial content and return the expected contents. */ @Test(expectedExceptions = IOException.class) public void testReadClosedStreamFail() throws Exception { ConnectionHandler handler = new TestConnectionHandler(new CompleteContentConnection(m_content, true)); ContentRangeInputStream is = new ContentRangeInputStream(handler, m_testURL); is.close(); // simulate an early close... is.read(); // should fail! }
/** * Tests that we can read non-partial content and return the expected contents. */ @Test public void testReadNonPartialEmptyContentOk() throws Exception { String content = ""; ConnectionHandler handler = new TestConnectionHandler(new CompleteContentConnection(content, true)); ContentRangeInputStream is = new ContentRangeInputStream(handler, m_testURL); assertEquals(slurpAsStringWithBuffer(is), content); }