/** * Set the source from which will be read if the file will be later uploaded. * * @param inputStream The stream which will be read. */ public void setInputStream(InputStream inputStream) { input = inputStream; tusInputStream = new TusInputStream(inputStream); }
@Test(expected = IllegalStateException.class) public void testSetRequestPayloadSizeThrows() throws Exception { byte[] content = "hello world".getBytes(); TusClient client = new TusClient(); URL uploadUrl = new URL(mockServerURL + "/payloadException"); TusInputStream input = new TusInputStream(new ByteArrayInputStream(content)); TusUploader uploader = new TusUploader(client, uploadUrl, input, 0); uploader.setChunkSize(4); uploader.uploadChunk(); // Throws IllegalStateException uploader.setRequestPayloadSize(100); }
@Test public void testTusUploaderFailedExpectation() throws IOException, ProtocolException { Assume.assumeFalse(isOpenJDK6); FailingExpectationServer server = new FailingExpectationServer(); server.start(); byte[] content = "hello world".getBytes(); TusClient client = new TusClient(); URL uploadUrl = new URL(server.getURL() + "/expect"); TusInputStream input = new TusInputStream(new ByteArrayInputStream(content)); long offset = 3; boolean exceptionThrown = false; TusUploader uploader = new TusUploader(client, uploadUrl, input, offset); try { uploader.uploadChunk(); } catch(ProtocolException e) { assertTrue(e.getMessage().contains("500")); exceptionThrown = true; } finally { assertTrue(exceptionThrown); } }
@Test public void testMissingUploadOffsetHeader() throws Exception { byte[] content = "hello world".getBytes(); mockServer.when(new HttpRequest() .withPath("/files/missingHeader")) .respond(new HttpResponse() .withStatusCode(204) .withHeader("Tus-Resumable", TusClient.TUS_VERSION)); TusClient client = new TusClient(); URL uploadUrl = new URL(mockServerURL + "/missingHeader"); TusInputStream input = new TusInputStream(new ByteArrayInputStream(content)); TusUploader uploader = new TusUploader(client, uploadUrl, input, 0); boolean exceptionThrown = false; try { assertEquals(11, uploader.uploadChunk()); uploader.finish(); } catch(ProtocolException e) { assertTrue(e.getMessage().contains("no or invalid Upload-Offset header")); exceptionThrown = true; } finally { assertTrue(exceptionThrown); } }
@Test public void testUnmatchingUploadOffsetHeader() throws Exception { byte[] content = "hello world".getBytes(); mockServer.when(new HttpRequest() .withPath("/files/unmatchingHeader")) .respond(new HttpResponse() .withStatusCode(204) .withHeader("Tus-Resumable", TusClient.TUS_VERSION) .withHeader("Upload-Offset", "44")); TusClient client = new TusClient(); URL uploadUrl = new URL(mockServerURL + "/unmatchingHeader"); TusInputStream input = new TusInputStream(new ByteArrayInputStream(content)); TusUploader uploader = new TusUploader(client, uploadUrl, input, 0); boolean exceptionThrown = false; try { assertEquals(11, uploader.uploadChunk()); uploader.finish(); } catch(ProtocolException e) { assertTrue(e.getMessage().contains("different Upload-Offset value (44) than expected (11)")); exceptionThrown = true; } finally { assertTrue(exceptionThrown); } } }
@Test public void testTusUploader() throws IOException, ProtocolException { byte[] content = "hello world".getBytes(); mockServer.when(new HttpRequest() .withPath("/files/foo") .withHeader("Tus-Resumable", TusClient.TUS_VERSION) .withHeader("Upload-Offset", "3") .withHeader("Content-Type", "application/offset+octet-stream") .withHeader(isOpenJDK6 ? "": "Expect: 100-continue") .withBody(Arrays.copyOfRange(content, 3, 11))) .respond(new HttpResponse() .withStatusCode(204) .withHeader("Tus-Resumable", TusClient.TUS_VERSION) .withHeader("Upload-Offset", "11")); TusClient client = new TusClient(); URL uploadUrl = new URL(mockServerURL + "/foo"); TusInputStream input = new TusInputStream(new ByteArrayInputStream(content)); long offset = 3; TusUploader uploader = new TusUploader(client, uploadUrl, input, offset); uploader.setChunkSize(5); assertEquals(uploader.getChunkSize(), 5); assertEquals(5, uploader.uploadChunk()); assertEquals(3, uploader.uploadChunk(5)); assertEquals(-1, uploader.uploadChunk()); assertEquals(-1, uploader.uploadChunk(5)); assertEquals(11, uploader.getOffset()); uploader.finish(); }
TusInputStream input = new TusInputStream(new ByteArrayInputStream(content));