/** * Tests that we can read non-partial content and return the expected contents. */ @Test public void testReadNonPartialFileContentByteForByteWithOffsetOk() throws Exception { File file = File.createTempFile("cris", ".tmp"); file.deleteOnExit(); FileOutputStream fos = new FileOutputStream(file); fos.write(m_content.getBytes()); fos.close(); ConnectionHandler handler = new TestConnectionHandler(file.toURI().toURL().openConnection()); ContentRangeInputStream is = new ContentRangeInputStream(handler, m_testURL, 48); assertEquals(slurpAsStringByteForByte(is), m_content.substring(48)); // try several additional reads, which should all return -1 (= EOF)... int tries = 5; while (--tries > 0) { assertEquals(is.read(), -1); } }
read = is.read(buffer);
/** * Tests that we can read non-partial content and return the expected contents. */ @Test public void testReadNonPartialFileContentByteForByteOk() throws Exception { File file = File.createTempFile("cris", ".tmp"); file.deleteOnExit(); FileOutputStream fos = new FileOutputStream(file); fos.write(m_content.getBytes()); fos.close(); ConnectionHandler handler = new TestConnectionHandler(file.toURI().toURL().openConnection()); ContentRangeInputStream is = new ContentRangeInputStream(handler, m_testURL); assertEquals(slurpAsStringByteForByte(is), m_content); // try several additional reads, which should all return -1 (= EOF)... int tries = 5; while (--tries > 0) { assertEquals(is.read(), -1); } }
/** * 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 server returns a complete body. */ @Test(expectedExceptions = IOException.class) public void testReadPartialContentWithCompleteBodyFail() throws Exception { ConnectionHandler handler = new TestConnectionHandler(new FailingContentConnection(m_content, Failure.PARTIAL_COMPLETE_BODY)); 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 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(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 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 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 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 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 the "Range" header is correctly set. */ @Test public void testRangeHeadersCorrectlySetOk() throws Exception { String content = m_content; PartialContentConnection conn; ContentRangeInputStream is; conn = new PartialContentConnection(content, false); // no offset causes no Range header to be set (initially)... is = new ContentRangeInputStream(new TestConnectionHandler(conn), m_testURL); is.read(); // read one byte... is.close(); // Make sure the proper request header is NOT set... assertRequestHeader(conn, "Range", null); conn = new PartialContentConnection(content, false); // start at 48 bytes and return the next complete chunk... is = new ContentRangeInputStream(new TestConnectionHandler(conn), m_testURL, 48); is.read(); // read one byte... is.close(); // Make sure the proper request header is set... assertRequestHeader(conn, "Range", "bytes=48-"); conn = new PartialContentConnection(content, false); // 4752 + 48 = 4800, causing only one chunk to be returned... is = new ContentRangeInputStream(new TestConnectionHandler(conn), m_testURL, 48, 4752); is.read(); // read one byte... is.close(); // Make sure the proper request header is set... assertRequestHeader(conn, "Range", "bytes=48-4800"); }