Skip to content

Commit

Permalink
Test header fields for null before accessing length
Browse files Browse the repository at this point in the history
  • Loading branch information
Acconut committed Apr 11, 2016
1 parent 9d07b59 commit 38a2c9c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/main/java/io/tus/java/client/TusClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public TusUploader createUpload(TusUpload upload) throws ProtocolException, IOEx
}

String urlStr = connection.getHeaderField("Location");
if(urlStr.length() == 0) {
if(urlStr == null || urlStr.length() == 0) {
throw new ProtocolException("missing upload URL in response for creating upload");
}

Expand Down Expand Up @@ -163,7 +163,7 @@ public TusUploader resumeUpload(TusUpload upload) throws FingerprintNotFoundExce
}

String offsetStr = connection.getHeaderField("Upload-Offset");
if(offsetStr.length() == 0) {
if(offsetStr == null || offsetStr.length() == 0) {
throw new ProtocolException("missing upload offset in response for resuming upload");
}
long offset = Long.parseLong(offsetStr);
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/io/tus/java/client/TestTusClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,30 @@ public void testCreateUpload() throws IOException, ProtocolException {
assertEquals(uploader.getUploadURL(), new URL(mockServerURL + "/foo"));
}

@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 testResumeUpload() throws ResumingNotEnabledException, FingerprintNotFoundException, IOException, ProtocolException {
mockServer.when(new HttpRequest()
Expand Down

0 comments on commit 38a2c9c

Please sign in to comment.