Skip to content

Commit

Permalink
Fix MultipartBasicAuthTest
Browse files Browse the repository at this point in the history
It’s expected to get an Broken Pipe when server forcefully closes
connection before reading full request.
  • Loading branch information
slandelle committed Sep 14, 2017
1 parent ab9deda commit 58e590f
Showing 1 changed file with 44 additions and 15 deletions.
Expand Up @@ -13,18 +13,22 @@
*/
package org.asynchttpclient.request.body.multipart;

import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_LENGTH;
import static io.netty.handler.codec.http.HttpHeaderValues.APPLICATION_OCTET_STREAM;
import static io.netty.handler.codec.http.HttpHeaderNames.*;
import static io.netty.handler.codec.http.HttpHeaderValues.*;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.asynchttpclient.Dsl.*;
import static org.asynchttpclient.test.TestUtils.*;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.*;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.function.Function;

import org.asynchttpclient.AbstractBasicTest;
import org.asynchttpclient.AsyncHttpClient;
import org.asynchttpclient.BasicAuthTest;
import org.asynchttpclient.BoundRequestBuilder;
import org.asynchttpclient.Response;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
Expand All @@ -37,7 +41,6 @@ public class MultipartBasicAuthTest extends AbstractBasicTest {
@BeforeClass(alwaysRun = true)
@Override
public void setUpGlobal() throws Exception {

server = new Server();
ServerConnector connector1 = addHttpConnector(server);
addBasicAuthHandler(server, configureHandler());
Expand All @@ -51,31 +54,57 @@ public AbstractHandler configureHandler() throws Exception {
return new BasicAuthTest.SimpleHandler();
}

@Test(groups = "standalone", enabled = false)
public void testNoRealm() throws Exception {
private void expectBrokenPipe(Function<BoundRequestBuilder, BoundRequestBuilder> f) throws Exception {
File file = createTempFile(1024 * 1024);

Throwable cause = null;
try (AsyncHttpClient client = asyncHttpClient()) {
for (int i = 0; i < 20; i++) {
Response response = client.preparePut(getTargetUrl())//
.addBodyPart(new FilePart("test", file, APPLICATION_OCTET_STREAM.toString(), UTF_8)).execute().get();
assertEquals(response.getStatusCode(), 401);
try {
for (int i = 0; i < 20 && cause == null; i++) {
f.apply(client.preparePut(getTargetUrl())//
.addBodyPart(new FilePart("test", file, APPLICATION_OCTET_STREAM.toString(), UTF_8)))//
.execute().get();
}
} catch (ExecutionException e) {
cause = e.getCause();
}
}

assertTrue(cause instanceof IOException, "Expected an IOException");
assertEquals(cause.getMessage(), "Broken pipe");
}

@Test(groups = "standalone", enabled = false)
public void testAuthorizedRealm() throws Exception {
@Test(groups = "standalone")
public void noRealmCausesServerToCloseSocket() throws Exception {
expectBrokenPipe(rb -> rb);
}

@Test(groups = "standalone")
public void unauthorizedNonPreemptiveRealmCausesServerToCloseSocket() throws Exception {
expectBrokenPipe(rb -> rb.setRealm(basicAuthRealm(USER, ADMIN)));
}

private void expectSuccess(Function<BoundRequestBuilder, BoundRequestBuilder> f) throws Exception {
File file = createTempFile(1024 * 1024);

try (AsyncHttpClient client = asyncHttpClient()) {
for (int i = 0; i < 20; i++) {
Response response = client.preparePut(getTargetUrl())//
.setRealm(basicAuthRealm(USER, ADMIN).build())//
.addBodyPart(new FilePart("test", file, APPLICATION_OCTET_STREAM.toString(), UTF_8)).execute().get();
Response response = f.apply(client.preparePut(getTargetUrl())//
.addBodyPart(new FilePart("test", file, APPLICATION_OCTET_STREAM.toString(), UTF_8)))//
.execute().get();
assertEquals(response.getStatusCode(), 200);
assertEquals(response.getResponseBodyAsBytes().length, Integer.valueOf(response.getHeader("X-" + CONTENT_LENGTH)).intValue());
}
}
}

@Test(groups = "standalone")
public void authorizedPreemptiveRealmWorks() throws Exception {
expectSuccess(rb -> rb.setRealm(basicAuthRealm(USER, ADMIN).setUsePreemptiveAuth(true)));
}

@Test(groups = "standalone")
public void authorizedNonPreemptiveRealmWorksWithExpectContinue() throws Exception {
expectSuccess(rb -> rb.setRealm(basicAuthRealm(USER, ADMIN)).setHeader(EXPECT, CONTINUE));
}
}

0 comments on commit 58e590f

Please sign in to comment.