Skip to content

Commit 942f805

Browse files
committed
Improve tests (still not working)
1 parent 213a559 commit 942f805

File tree

3 files changed

+91
-82
lines changed

3 files changed

+91
-82
lines changed

client/src/test/java/org/asynchttpclient/BasicAuthTest.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public void handle(String s, Request r, HttpServletRequest request, HttpServletR
131131
}
132132
}
133133

134-
private static class SimpleHandler extends AbstractHandler {
134+
public static class SimpleHandler extends AbstractHandler {
135135

136136
public void handle(String s, Request r, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
137137

@@ -142,20 +142,21 @@ public void handle(String s, Request r, HttpServletRequest request, HttpServletR
142142
} else {
143143
response.addHeader("X-Auth", request.getHeader("Authorization"));
144144
response.addHeader("X-" + CONTENT_LENGTH, String.valueOf(request.getContentLength()));
145+
response.setIntHeader("X-" + CONTENT_LENGTH, request.getContentLength());
145146
response.setStatus(200);
146147

147148
int size = 10 * 1024;
148-
if (request.getContentLength() > 0) {
149-
size = request.getContentLength();
150-
}
151149
byte[] bytes = new byte[size];
152150
int contentLength = 0;
153151
if (bytes.length > 0) {
154-
int read = request.getInputStream().read(bytes);
155-
if (read > 0) {
156-
contentLength = read;
157-
response.getOutputStream().write(bytes, 0, read);
158-
}
152+
int read = 0;
153+
do {
154+
read = request.getInputStream().read(bytes);
155+
if (read > 0) {
156+
contentLength += read;
157+
response.getOutputStream().write(bytes, 0, read);
158+
}
159+
} while (read >= 0);
159160
}
160161
response.setContentLength(contentLength);
161162
}

client/src/test/java/org/asynchttpclient/request/body/FastUnauthorizedMultipartTest.java

Lines changed: 0 additions & 73 deletions
This file was deleted.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright (c) 2017 AsyncHttpClient Project. 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
7+
* http://www.apache.org/licenses/LICENSE-2.0.
8+
*
9+
* Unless required by applicable law or agreed to in writing,
10+
* software distributed under the Apache License Version 2.0 is distributed on an
11+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
13+
*/
14+
package org.asynchttpclient.request.body.multipart;
15+
16+
import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_LENGTH;
17+
import static io.netty.handler.codec.http.HttpHeaderValues.APPLICATION_OCTET_STREAM;
18+
import static java.nio.charset.StandardCharsets.UTF_8;
19+
import static org.asynchttpclient.Dsl.*;
20+
import static org.asynchttpclient.test.TestUtils.*;
21+
import static org.testng.Assert.assertEquals;
22+
23+
import java.io.File;
24+
25+
import org.asynchttpclient.AbstractBasicTest;
26+
import org.asynchttpclient.AsyncHttpClient;
27+
import org.asynchttpclient.BasicAuthTest;
28+
import org.asynchttpclient.Response;
29+
import org.eclipse.jetty.server.Server;
30+
import org.eclipse.jetty.server.ServerConnector;
31+
import org.eclipse.jetty.server.handler.AbstractHandler;
32+
import org.testng.annotations.BeforeClass;
33+
import org.testng.annotations.Test;
34+
35+
public class MultipartBasicAuthTest extends AbstractBasicTest {
36+
37+
@BeforeClass(alwaysRun = true)
38+
@Override
39+
public void setUpGlobal() throws Exception {
40+
41+
server = new Server();
42+
ServerConnector connector1 = addHttpConnector(server);
43+
addBasicAuthHandler(server, configureHandler());
44+
server.start();
45+
port1 = connector1.getLocalPort();
46+
logger.info("Local HTTP server started successfully");
47+
}
48+
49+
@Override
50+
public AbstractHandler configureHandler() throws Exception {
51+
return new BasicAuthTest.SimpleHandler();
52+
}
53+
54+
@Test(groups = "standalone", enabled = false)
55+
public void testNoRealm() throws Exception {
56+
File file = createTempFile(1024 * 1024);
57+
58+
try (AsyncHttpClient client = asyncHttpClient()) {
59+
for (int i = 0; i < 20; i++) {
60+
Response response = client.preparePut(getTargetUrl())//
61+
.addBodyPart(new FilePart("test", file, APPLICATION_OCTET_STREAM.toString(), UTF_8)).execute().get();
62+
assertEquals(response.getStatusCode(), 401);
63+
}
64+
}
65+
}
66+
67+
@Test(groups = "standalone", enabled = false)
68+
public void testAuthorizedRealm() throws Exception {
69+
File file = createTempFile(1024 * 1024);
70+
71+
try (AsyncHttpClient client = asyncHttpClient()) {
72+
for (int i = 0; i < 20; i++) {
73+
Response response = client.preparePut(getTargetUrl())//
74+
.setRealm(basicAuthRealm(USER, ADMIN).build())//
75+
.addBodyPart(new FilePart("test", file, APPLICATION_OCTET_STREAM.toString(), UTF_8)).execute().get();
76+
assertEquals(response.getStatusCode(), 200);
77+
assertEquals(response.getResponseBodyAsBytes().length, Integer.valueOf(response.getHeader("X-" + CONTENT_LENGTH)).intValue());
78+
}
79+
}
80+
}
81+
}

0 commit comments

Comments
 (0)