Skip to content

Commit 38a2c9c

Browse files
committed
Test header fields for null before accessing length
1 parent 9d07b59 commit 38a2c9c

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/main/java/io/tus/java/client/TusClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public TusUploader createUpload(TusUpload upload) throws ProtocolException, IOEx
111111
}
112112

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

@@ -163,7 +163,7 @@ public TusUploader resumeUpload(TusUpload upload) throws FingerprintNotFoundExce
163163
}
164164

165165
String offsetStr = connection.getHeaderField("Upload-Offset");
166-
if(offsetStr.length() == 0) {
166+
if(offsetStr == null || offsetStr.length() == 0) {
167167
throw new ProtocolException("missing upload offset in response for resuming upload");
168168
}
169169
long offset = Long.parseLong(offsetStr);

src/test/java/io/tus/java/client/TestTusClient.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,30 @@ public void testCreateUpload() throws IOException, ProtocolException {
101101
assertEquals(uploader.getUploadURL(), new URL(mockServerURL + "/foo"));
102102
}
103103

104+
@Test
105+
public void testCreateUploadWithMissingLocationHeader() throws IOException, Exception {
106+
mockServer.when(new HttpRequest()
107+
.withMethod("POST")
108+
.withPath("/files")
109+
.withHeader("Tus-Resumable", TusClient.TUS_VERSION)
110+
.withHeader("Upload-Length", "10"))
111+
.respond(new HttpResponse()
112+
.withStatusCode(201)
113+
.withHeader("Tus-Resumable", TusClient.TUS_VERSION));
114+
115+
TusClient client = new TusClient();
116+
client.setUploadCreationURL(mockServerURL);
117+
TusUpload upload = new TusUpload();
118+
upload.setSize(10);
119+
upload.setInputStream(new ByteArrayInputStream(new byte[10]));
120+
try {
121+
TusUploader uploader = client.createUpload(upload);
122+
throw new Exception("unreachable code reached");
123+
} catch(ProtocolException e) {
124+
assertEquals(e.getMessage(), "missing upload URL in response for creating upload");
125+
}
126+
}
127+
104128
@Test
105129
public void testResumeUpload() throws ResumingNotEnabledException, FingerprintNotFoundException, IOException, ProtocolException {
106130
mockServer.when(new HttpRequest()

0 commit comments

Comments
 (0)