Skip to content

Commit 3fe2f05

Browse files
committed
Report upload progress when sending a byte array
1 parent 3437d83 commit 3fe2f05

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

lib/src/main/java/com/github/kevinsawicki/http/HttpRequest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2925,6 +2925,8 @@ public HttpRequest send(final File input) throws HttpRequestException {
29252925
* @throws HttpRequestException
29262926
*/
29272927
public HttpRequest send(final byte[] input) throws HttpRequestException {
2928+
if (input != null)
2929+
totalSize += input.length;
29282930
return send(new ByteArrayInputStream(input));
29292931
}
29302932

lib/src/test/java/com/github/kevinsawicki/http/HttpRequestTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3520,6 +3520,35 @@ public void onUpload(int transferred, int total) {
35203520
assertEquals(file.length(), tx.get());
35213521
}
35223522

3523+
/**
3524+
* Verify progress callback when sending from a byte array
3525+
*
3526+
* @throws Exception
3527+
*/
3528+
@Test
3529+
public void uploadProgressSendByteArray() throws Exception {
3530+
final AtomicReference<String> body = new AtomicReference<String>();
3531+
handler = new RequestHandler() {
3532+
3533+
@Override
3534+
public void handle(Request request, HttpServletResponse response) {
3535+
body.set(new String(read()));
3536+
response.setStatus(HTTP_OK);
3537+
}
3538+
};
3539+
3540+
final byte[] bytes = "hello".getBytes(CHARSET_UTF8);
3541+
final AtomicInteger tx = new AtomicInteger(0);
3542+
UploadProgress progress = new UploadProgress() {
3543+
public void onUpload(int transferred, int total) {
3544+
assertEquals(bytes.length, total);
3545+
assertEquals(tx.incrementAndGet(), transferred);
3546+
}
3547+
};
3548+
post(url).bufferSize(1).progress(progress).send(bytes).code();
3549+
assertEquals(bytes.length, tx.get());
3550+
}
3551+
35233552
/**
35243553
* Verify progress callback when sending from a Reader
35253554
*

0 commit comments

Comments
 (0)