Skip to content

Commit 366d247

Browse files
committed
Fix PutFileTest that was closing socket while upload was still in progress
1 parent d131458 commit 366d247

File tree

1 file changed

+22
-23
lines changed

1 file changed

+22
-23
lines changed

client/src/test/java/org/asynchttpclient/request/body/PutLargeFileTest.java renamed to client/src/test/java/org/asynchttpclient/request/body/PutFileTest.java

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.File;
2020
import java.io.IOException;
21+
import java.io.InputStream;
2122

2223
import javax.servlet.ServletException;
2324
import javax.servlet.http.HttpServletRequest;
@@ -30,46 +31,44 @@
3031
import org.eclipse.jetty.server.handler.AbstractHandler;
3132
import org.testng.annotations.Test;
3233

33-
/**
34-
* @author Benjamin Hanzelmann
35-
*/
36-
public class PutLargeFileTest extends AbstractBasicTest {
37-
38-
@Test(groups = "standalone")
39-
public void testPutLargeFile() throws Exception {
40-
41-
File file = createTempFile(1024 * 1024);
34+
public class PutFileTest extends AbstractBasicTest {
4235

36+
private void put(int fileSize) throws Exception {
37+
File file = createTempFile(fileSize);
4338
int timeout = (int) file.length() / 1000;
44-
45-
try (AsyncHttpClient client = asyncHttpClient(config().setConnectTimeout(timeout))) {
39+
try (AsyncHttpClient client = asyncHttpClient(config().setRequestTimeout(timeout))) {
4640
Response response = client.preparePut(getTargetUrl()).setBody(file).execute().get();
4741
assertEquals(response.getStatusCode(), 200);
4842
}
4943
}
5044

5145
@Test(groups = "standalone")
52-
public void testPutSmallFile() throws Exception {
53-
54-
File file = createTempFile(1024);
46+
public void testPutLargeFile() throws Exception {
47+
put(1024 * 1024);
48+
}
5549

56-
try (AsyncHttpClient client = asyncHttpClient()) {
57-
Response response = client.preparePut(getTargetUrl()).setBody(file).execute().get();
58-
assertEquals(response.getStatusCode(), 200);
59-
}
50+
@Test(groups = "standalone")
51+
public void testPutSmallFile() throws Exception {
52+
put(1024);
6053
}
6154

6255
@Override
6356
public AbstractHandler configureHandler() throws Exception {
6457
return new AbstractHandler() {
6558

66-
public void handle(String arg0, Request arg1, HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
59+
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
6760

68-
resp.setStatus(200);
69-
resp.getOutputStream().flush();
70-
resp.getOutputStream().close();
61+
InputStream is = baseRequest.getInputStream();
62+
int read = 0;
63+
do {
64+
// drain upload
65+
read = is.read();
66+
} while (read >= 0);
7167

72-
arg1.setHandled(true);
68+
response.setStatus(200);
69+
response.getOutputStream().flush();
70+
response.getOutputStream().close();
71+
baseRequest.setHandled(true);
7372
}
7473
};
7574
}

0 commit comments

Comments
 (0)