@Test public void testEnableResuming() { TusClient client = new TusClient(); assertEquals(client.resumingEnabled(), false); TusURLStore store = new TusURLMemoryStore(); client.enableResuming(store); assertEquals(client.resumingEnabled(), true); client.disableResuming(); assertEquals(client.resumingEnabled(), false); }
/** * Try to resume an upload using {@link #resumeUpload(TusUpload)}. If the method call throws * an {@link ResumingNotEnabledException} or {@link FingerprintNotFoundException}, a new upload * will be created using {@link #createUpload(TusUpload)}. * * @param upload The file for which an upload will be resumed * @throws ProtocolException Thrown if the remote server sent an unexpected response, e.g. * wrong status codes or missing/invalid headers. * @throws IOException Thrown if an exception occurs while issuing the HTTP request. */ public TusUploader resumeOrCreateUpload(@NotNull TusUpload upload) throws ProtocolException, IOException { try { return resumeUpload(upload); } catch(FingerprintNotFoundException e) { return createUpload(upload); } catch(ResumingNotEnabledException e) { return createUpload(upload); } catch(ProtocolException e) { // If the attempt to resume returned a 404 Not Found, we immediately try to create a new // one since TusExectuor would not retry this operation. HttpURLConnection connection = e.getCausingConnection(); if(connection != null && connection.getResponseCode() == 404) { return createUpload(upload); } throw e; } }
@Test public void testSetConnectionTimeout() throws IOException { HttpURLConnection connection = (HttpURLConnection) mockServerURL.openConnection(); TusClient client = new TusClient(); assertEquals(client.getConnectTimeout(), 5000); client.setConnectTimeout(3000); assertEquals(client.getConnectTimeout(), 3000); client.prepareConnection(connection); assertEquals(connection.getConnectTimeout(), 3000); }
@Test public void testTusClientURL() throws MalformedURLException { TusClient client = new TusClient(); client.setUploadCreationURL(creationUrl); assertEquals(client.getUploadCreationURL(), creationUrl); }
@Test public void testCreateUpload() throws IOException, ProtocolException { mockServer.when(new HttpRequest() .withMethod("POST") .withPath("/files") .withHeader("Tus-Resumable", TusClient.TUS_VERSION) .withHeader("Upload-Metadata", "foo aGVsbG8=,bar d29ybGQ=") .withHeader("Upload-Length", "10")) .respond(new HttpResponse() .withStatusCode(201) .withHeader("Tus-Resumable", TusClient.TUS_VERSION) .withHeader("Location", mockServerURL + "/foo")); Map<String, String> metadata = new LinkedHashMap<String, String>(); metadata.put("foo", "hello"); metadata.put("bar", "world"); TusClient client = new TusClient(); client.setUploadCreationURL(mockServerURL); TusUpload upload = new TusUpload(); upload.setSize(10); upload.setInputStream(new ByteArrayInputStream(new byte[10])); upload.setMetadata(metadata); TusUploader uploader = client.createUpload(upload); assertEquals(uploader.getUploadURL(), new URL(mockServerURL + "/foo")); }
@Test public void testResumeUpload() throws ResumingNotEnabledException, FingerprintNotFoundException, IOException, ProtocolException { mockServer.when(new HttpRequest() .withMethod("HEAD") .withPath("/files/foo") .withHeader("Tus-Resumable", TusClient.TUS_VERSION)) .respond(new HttpResponse() .withStatusCode(204) .withHeader("Tus-Resumable", TusClient.TUS_VERSION) .withHeader("Upload-Offset", "3")); TusClient client = new TusClient(); client.setUploadCreationURL(mockServerURL); client.enableResuming(new TestResumeUploadStore()); TusUpload upload = new TusUpload(); upload.setSize(10); upload.setInputStream(new ByteArrayInputStream(new byte[10])); upload.setFingerprint("test-fingerprint"); TusUploader uploader = client.resumeUpload(upload); assertEquals(uploader.getUploadURL(), new URL(mockServerURL.toString() + "/foo")); assertEquals(uploader.getOffset(), 3); }
@Test public void testFollowRedirects() throws Exception { HttpURLConnection connection = (HttpURLConnection) mockServerURL.openConnection(); TusClient client = new TusClient(); client.prepareConnection(connection); assertFalse(connection.getInstanceFollowRedirects()); client.prepareConnection(connection); assertTrue(connection.getInstanceFollowRedirects()); .withHeader("Location", mockServerURL + "/foo")); client.setUploadCreationURL(new URL(mockServerURL + "Redirect")); TusUpload upload = new TusUpload(); upload.setSize(10); upload.setInputStream(new ByteArrayInputStream(new byte[10])); TusUploader uploader = client.createUpload(upload);
@Test public void testResumeOrCreateUploadNotFound() throws IOException, ProtocolException { mockServer.when(new HttpRequest() .withMethod("HEAD") .withPath("/files/not_found") .withHeader("Tus-Resumable", TusClient.TUS_VERSION)) .respond(new HttpResponse() .withStatusCode(404)); mockServer.when(new HttpRequest() .withMethod("POST") .withPath("/files") .withHeader("Tus-Resumable", TusClient.TUS_VERSION) .withHeader("Upload-Length", "10")) .respond(new HttpResponse() .withStatusCode(201) .withHeader("Tus-Resumable", TusClient.TUS_VERSION) .withHeader("Location", mockServerURL + "/foo")); TusClient client = new TusClient(); client.setUploadCreationURL(mockServerURL); TusURLStore store = new TusURLMemoryStore(); store.set("fingerprint", new URL(mockServerURL + "/not_found")); client.enableResuming(store); TusUpload upload = new TusUpload(); upload.setSize(10); upload.setInputStream(new ByteArrayInputStream(new byte[10])); upload.setFingerprint("fingerprint"); TusUploader uploader = client.resumeOrCreateUpload(upload); assertEquals(uploader.getUploadURL(), new URL(mockServerURL + "/foo")); }
final TusClient client = new TusClient(); client.setUploadCreationURL(new URL("https://master.tus.io/files/")); client.enableResuming(new TusURLMemoryStore());
@Test public void testSetHeaders() throws IOException { HttpURLConnection connection = (HttpURLConnection) mockServerURL.openConnection(); TusClient client = new TusClient(); Map<String, String> headers = new HashMap<String, String>(); headers.put("Greeting", "Hello"); headers.put("Important", "yes"); headers.put("Tus-Resumable", "evil"); assertNull(client.getHeaders()); client.setHeaders(headers); assertEquals(headers, client.getHeaders()); client.prepareConnection(connection); assertEquals(connection.getRequestProperty("Greeting"), "Hello"); assertEquals(connection.getRequestProperty("Important"), "yes"); }
@Test public void testResumeOrCreateUpload() throws IOException, ProtocolException { mockServer.when(new HttpRequest() .withMethod("POST") .withPath("/files") .withHeader("Tus-Resumable", TusClient.TUS_VERSION) .withHeader("Upload-Length", "10")) .respond(new HttpResponse() .withStatusCode(201) .withHeader("Tus-Resumable", TusClient.TUS_VERSION) .withHeader("Location", mockServerURL + "/foo")); TusClient client = new TusClient(); client.setUploadCreationURL(mockServerURL); TusUpload upload = new TusUpload(); upload.setSize(10); upload.setInputStream(new ByteArrayInputStream(new byte[10])); TusUploader uploader = client.resumeOrCreateUpload(upload); assertEquals(uploader.getUploadURL(), new URL(mockServerURL + "/foo")); }
@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 testPrepareConnection() throws IOException { HttpURLConnection connection = (HttpURLConnection) mockServerURL.openConnection(); TusClient client = new TusClient(); client.prepareConnection(connection); assertEquals(connection.getRequestProperty("Tus-Resumable"), TusClient.TUS_VERSION); }
@Test public void testBeginOrResumeUploadFromURL() throws IOException, ProtocolException { mockServer.when(new HttpRequest() .withMethod("HEAD") .withPath("/files/fooFromURL") .withHeader("Tus-Resumable", TusClient.TUS_VERSION)) .respond(new HttpResponse() .withStatusCode(204) .withHeader("Tus-Resumable", TusClient.TUS_VERSION) .withHeader("Upload-Offset", "3")); TusClient client = new TusClient(); URL uploadURL = new URL(mockServerURL.toString() + "/fooFromURL"); TusUpload upload = new TusUpload(); upload.setSize(10); upload.setInputStream(new ByteArrayInputStream(new byte[10])); TusUploader uploader = client.beginOrResumeUploadFromURL(upload, uploadURL); assertEquals(uploader.getUploadURL(), uploadURL); assertEquals(uploader.getOffset(), 3); }
@Test public void testTusClient() { TusClient client = new TusClient(); assertEquals(client.getUploadCreationURL(), null); }
client.prepareConnection(connection); connection.setRequestProperty("Upload-Offset", Long.toString(offset)); connection.setRequestProperty("Content-Type", "application/offset+octet-stream");
@Override protected void makeAttempt() throws ProtocolException, IOException { // First try to resume an upload. If that's not possible we will create a new // upload and get a TusUploader in return. This class is responsible for opening // a connection to the remote server and doing the uploading. TusUploader uploader = client.resumeOrCreateUpload(upload); // Upload the file in chunks of 1KB sizes. uploader.setChunkSize(1024); // Upload the file as long as data is available. Once the // file has been fully uploaded the method will return -1 do { // Calculate the progress using the total size of the uploading file and // the current offset. long totalBytes = upload.getSize(); long bytesUploaded = uploader.getOffset(); double progress = (double) bytesUploaded / totalBytes * 100; System.out.printf("Upload at %06.2f%%.\n", progress); } while(uploader.uploadChunk() > -1); // Allow the HTTP connection to be closed and cleaned up uploader.finish(); System.out.println("Upload finished."); System.out.format("Upload available at: %s", uploader.getUploadURL().toString()); } };
/** * Try to resume an already started upload. Before call this function, resuming must be * enabled using {@link #enableResuming(TusURLStore)}. This method will look up the URL for this * upload in the {@link TusURLStore} using the upload's fingerprint (see * {@link TusUpload#getFingerprint()}). After a successful lookup a HEAD request will be issued * to find the current offset without uploading the file, yet. * * @param upload The file for which an upload will be resumed * @return Use {@link TusUploader} to upload the remaining file's chunks. * @throws FingerprintNotFoundException Thrown if no matching fingerprint has been found in * {@link TusURLStore}. Use {@link #createUpload(TusUpload)} to create a new upload. * @throws ResumingNotEnabledException Throw if resuming has not been enabled using {@link * #enableResuming(TusURLStore)}. * @throws ProtocolException Thrown if the remote server sent an unexpected response, e.g. * wrong status codes or missing/invalid headers. * @throws IOException Thrown if an exception occurs while issuing the HTTP request. */ public TusUploader resumeUpload(@NotNull TusUpload upload) throws FingerprintNotFoundException, ResumingNotEnabledException, ProtocolException, IOException { if (!resumingEnabled) { throw new ResumingNotEnabledException(); } URL uploadURL = urlStore.get(upload.getFingerprint()); if (uploadURL == null) { throw new FingerprintNotFoundException(upload.getFingerprint()); } return beginOrResumeUploadFromURL(upload, uploadURL); }
@Test public void testCreateUploadWithMissingLocationHeader() throws IOException, Exception { mockServer.when(new HttpRequest() .withMethod("POST") .withPath("/files") .withHeader("Tus-Resumable", TusClient.TUS_VERSION) .withHeader("Upload-Length", "10")) .respond(new HttpResponse() .withStatusCode(201) .withHeader("Tus-Resumable", TusClient.TUS_VERSION)); TusClient client = new TusClient(); client.setUploadCreationURL(mockServerURL); TusUpload upload = new TusUpload(); upload.setSize(10); upload.setInputStream(new ByteArrayInputStream(new byte[10])); try { TusUploader uploader = client.createUpload(upload); throw new Exception("unreachable code reached"); } catch(ProtocolException e) { assertEquals(e.getMessage(), "missing upload URL in response for creating upload"); } }
@Test public void testSetUploadCreationURL() throws MalformedURLException { TusClient client = new TusClient(); client.setUploadCreationURL(new URL("http://master.tus.io")); assertEquals(client.getUploadCreationURL(), new URL("http://master.tus.io")); }