Skip to content

Commit 468ff38

Browse files
author
Stephane Landelle
committed
Add a test about the server replying 401 while the client uploads a big file
1 parent eda2bae commit 468ff38

File tree

3 files changed

+97
-10
lines changed

3 files changed

+97
-10
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright (c) 2010-2012 Sonatype, Inc. All rights reserved.
3+
*
4+
* This program is licensed to you under the Apache License Version 2.0,
5+
* and you may not use this file except in compliance with the Apache License Version 2.0.
6+
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
7+
*
8+
* Unless required by applicable law or agreed to in writing,
9+
* software distributed under the Apache License Version 2.0 is distributed on an
10+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
12+
*/
13+
package com.ning.http.client.async;
14+
15+
import java.io.File;
16+
import java.io.IOException;
17+
18+
import javax.servlet.ServletException;
19+
import javax.servlet.http.HttpServletRequest;
20+
import javax.servlet.http.HttpServletResponse;
21+
22+
import org.eclipse.jetty.server.Request;
23+
import org.eclipse.jetty.server.handler.AbstractHandler;
24+
import org.testng.Assert;
25+
import org.testng.annotations.Test;
26+
27+
import com.ning.http.client.AsyncHttpClient;
28+
import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder;
29+
import com.ning.http.client.FilePart;
30+
import com.ning.http.client.Response;
31+
32+
public abstract class FastUnauthorizedUploadTest extends AbstractBasicTest {
33+
34+
@Override
35+
public AbstractHandler configureHandler() throws Exception {
36+
return new AbstractHandler() {
37+
38+
public void handle(String target, Request baseRequest, HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
39+
40+
resp.setStatus(401);
41+
resp.getOutputStream().flush();
42+
resp.getOutputStream().close();
43+
44+
baseRequest.setHandled(true);
45+
}
46+
};
47+
}
48+
49+
@Test(groups = { "standalone", "default_provider" }, enabled = true)
50+
public void testUnauthorizedWhileUploading() throws Exception {
51+
byte[] bytes = "RatherLargeFileRatherLargeFileRatherLargeFileRatherLargeFile".getBytes("UTF-16");
52+
long repeats = (1024 * 1024 / bytes.length) + 1;
53+
File largeFile = FilePartLargeFileTest.createTempFile(bytes, (int) repeats);
54+
55+
AsyncHttpClient client = getAsyncHttpClient(null);
56+
try {
57+
BoundRequestBuilder rb = client.preparePut(getTargetUrl());
58+
59+
rb.addBodyPart(new FilePart("test", largeFile, "application/octet-stream", "UTF-8"));
60+
61+
Response response = rb.execute().get();
62+
Assert.assertEquals(401, response.getStatusCode());
63+
} finally {
64+
client.close();
65+
}
66+
}
67+
}

src/test/java/com/ning/http/client/async/FilePartLargeFileTest.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.eclipse.jetty.server.Request;
2929
import org.eclipse.jetty.server.handler.AbstractHandler;
3030
import org.testng.Assert;
31-
import org.testng.annotations.AfterMethod;
3231
import org.testng.annotations.Test;
3332

3433
import com.ning.http.client.AsyncHttpClient;
@@ -39,11 +38,9 @@
3938

4039
public abstract class FilePartLargeFileTest extends AbstractBasicTest {
4140

42-
private File largeFile;
43-
4441
@Test(groups = { "standalone", "default_provider" }, enabled = true)
4542
public void testPutImageFile() throws Exception {
46-
largeFile = getTestFile();
43+
File largeFile = getTestFile();
4744
AsyncHttpClientConfig config = new AsyncHttpClientConfig.Builder().setRequestTimeoutInMs(100 * 6000).build();
4845
AsyncHttpClient client = getAsyncHttpClient(config);
4946
try {
@@ -62,7 +59,7 @@ public void testPutImageFile() throws Exception {
6259
public void testPutLargeTextFile() throws Exception {
6360
byte[] bytes = "RatherLargeFileRatherLargeFileRatherLargeFileRatherLargeFile".getBytes("UTF-16");
6461
long repeats = (1024 * 1024 / bytes.length) + 1;
65-
largeFile = createTempFile(bytes, (int) repeats);
62+
File largeFile = createTempFile(bytes, (int) repeats);
6663

6764
AsyncHttpClient client = getAsyncHttpClient(null);
6865
try {
@@ -93,11 +90,6 @@ private static File getTestFile() {
9390
return testResource1File;
9491
}
9592

96-
@AfterMethod
97-
public void after() {
98-
largeFile.delete();
99-
}
100-
10193
@Override
10294
public AbstractHandler configureHandler() throws Exception {
10395
return new AbstractHandler() {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (c) 2010-2012 Sonatype, Inc. All rights reserved.
3+
*
4+
* This program is licensed to you under the Apache License Version 2.0,
5+
* and you may not use this file except in compliance with the Apache License Version 2.0.
6+
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
7+
*
8+
* Unless required by applicable law or agreed to in writing,
9+
* software distributed under the Apache License Version 2.0 is distributed on an
10+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
12+
*/
13+
package com.ning.http.client.async.netty;
14+
15+
import org.testng.annotations.Test;
16+
17+
import com.ning.http.client.AsyncHttpClient;
18+
import com.ning.http.client.AsyncHttpClientConfig;
19+
import com.ning.http.client.async.FastUnauthorizedUploadTest;
20+
import com.ning.http.client.async.ProviderUtil;
21+
22+
@Test
23+
public class NettyFastUnauthorizedUploadTest extends FastUnauthorizedUploadTest {
24+
@Override
25+
public AsyncHttpClient getAsyncHttpClient(AsyncHttpClientConfig config) {
26+
return ProviderUtil.nettyProvider(config);
27+
}
28+
}

0 commit comments

Comments
 (0)