Skip to content

Commit

Permalink
Add connection timeouts to TusClient
Browse files Browse the repository at this point in the history
  • Loading branch information
Acconut committed May 12, 2016
1 parent 893678f commit dc7931f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/main/java/io/tus/java/client/TusClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class TusClient {
private boolean resumingEnabled;
private TusURLStore urlStore;
private Map<String, String> headers;
private int connectTimeout = 5000;

/**
* Create a new tus client.
Expand Down Expand Up @@ -112,6 +113,14 @@ public Map<String, String> getHeaders() {
return headers;
}

public void setConnectTimeout(int timeout) {
connectTimeout = timeout;
}

public int getConnectTimeout() {
return connectTimeout;
}

/**
* Create a new upload using the Creation extension. Before calling this function, an "upload
* creation URL" must be defined using {@link #setUploadCreationURL(URL)} or else this
Expand Down Expand Up @@ -231,6 +240,7 @@ public TusUploader resumeOrCreateUpload(@NotNull TusUpload upload) throws Protoc
* @param connection The connection whose headers will be modified.
*/
public void prepareConnection(@NotNull URLConnection connection) {
connection.setConnectTimeout(connectTimeout);
connection.addRequestProperty("Tus-Resumable", TUS_VERSION);

if(headers != null) {
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/io/tus/java/client/TestTusClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,18 @@ public void testSetHeaders() throws IOException {
assertEquals(connection.getRequestProperty("Greeting"), "Hello");
assertEquals(connection.getRequestProperty("Important"), "yes");
}

@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);
}
}

0 comments on commit dc7931f

Please sign in to comment.