diff options
Diffstat (limited to 'jetty-client/src/test/java')
4 files changed, 116 insertions, 28 deletions
diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/HostnameVerificationTest.java b/jetty-client/src/test/java/org/eclipse/jetty/client/HostnameVerificationTest.java index cf0d813ed9..7fe669c423 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/HostnameVerificationTest.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/HostnameVerificationTest.java @@ -19,6 +19,8 @@ package org.eclipse.jetty.client; import java.io.IOException; +import java.nio.channels.ClosedChannelException; +import java.security.cert.CertificateException; import java.util.concurrent.ExecutionException; import javax.net.ssl.SSLHandshakeException; import javax.servlet.ServletException; @@ -37,12 +39,12 @@ import org.junit.Before; import org.junit.Test; import static junit.framework.Assert.fail; -import static org.hamcrest.CoreMatchers.instanceOf; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.instanceOf; +import static org.junit.Assert.assertThat; /** - * This test class runs tests to make sure that hostname verification (http://www.ietf.org/rfc/rfc2818.txt section 3 - * .1) is configurable in SslContextFactory and works as expected. + * This test class runs tests to make sure that hostname verification (http://www.ietf.org/rfc/rfc2818.txt + * section 3.1) is configurable in SslContextFactory and works as expected. */ public class HostnameVerificationTest { @@ -107,10 +109,20 @@ public class HostnameVerificationTest client.GET(uri); fail("sending request to client should have failed with an Exception!"); } - catch (ExecutionException e) + catch (ExecutionException x) { - assertThat("We got a SSLHandshakeException as localhost doesn't match the hostname of the certificate", - e.getCause().getCause(), instanceOf(SSLHandshakeException.class)); + // The test may fail in 2 ways, since the CertificateException thrown because of the hostname + // verification failure is not rethrown immediately by the JDK SSL implementation, but only + // rethrown on the next read or write. + // Therefore this test may catch a SSLHandshakeException, or a ClosedChannelException. + // If it is the former, we verify that its cause is a CertificateException. + + // ExecutionException wraps an EofException that wraps the SSLHandshakeException + Throwable cause = x.getCause().getCause(); + if (cause instanceof SSLHandshakeException) + assertThat(cause.getCause().getCause(), instanceOf(CertificateException.class)); + else + assertThat(cause, instanceOf(ClosedChannelException.class)); } } diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientAuthenticationTest.java b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientAuthenticationTest.java index 8247c49123..80c3d0b930 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientAuthenticationTest.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientAuthenticationTest.java @@ -49,7 +49,6 @@ import org.eclipse.jetty.toolchain.test.MavenTestingUtils; import org.eclipse.jetty.util.security.Constraint; import org.eclipse.jetty.util.ssl.SslContextFactory; import org.junit.Assert; -import org.junit.Ignore; import org.junit.Test; public class HttpClientAuthenticationTest extends AbstractHttpClientServerTest @@ -104,7 +103,6 @@ public class HttpClientAuthenticationTest extends AbstractHttpClientServerTest test_Authentication(new BasicAuthentication(uri, realm, "basic", "basic")); } - @Ignore @Test public void test_DigestAuthentication() throws Exception { @@ -135,6 +133,7 @@ public class HttpClientAuthenticationTest extends AbstractHttpClientServerTest Assert.assertEquals(401, response.getStatus()); Assert.assertTrue(requests.get().await(5, TimeUnit.SECONDS)); client.getRequestListeners().remove(requestListener); + Assert.assertNull(client.getConversation(request.getConversationID(), false)); authenticationStore.addAuthentication(authentication); diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientTest.java b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientTest.java index bb9bea6d97..225078050b 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientTest.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientTest.java @@ -28,6 +28,7 @@ import java.nio.channels.UnresolvedAddressException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.Arrays; import java.util.Iterator; import java.util.List; import java.util.NoSuchElementException; @@ -35,6 +36,7 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; +import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; import java.util.zip.GZIPOutputStream; import javax.servlet.ServletException; @@ -286,6 +288,59 @@ public class HttpClientTest extends AbstractHttpClientServerTest } @Test + public void test_POST_WithContent_NotifiesRequestContentListener() throws Exception + { + final byte[] content = {0, 1, 2, 3}; + start(new EmptyServerHandler()); + + ContentResponse response = client.POST(scheme + "://localhost:" + connector.getLocalPort()) + .onRequestContent(new Request.ContentListener() + { + @Override + public void onContent(Request request, ByteBuffer buffer) + { + byte[] bytes = new byte[buffer.remaining()]; + buffer.get(bytes); + if (!Arrays.equals(content, bytes)) + request.abort(new Exception()); + } + }) + .content(new BytesContentProvider(content)) + .timeout(5, TimeUnit.SECONDS) + .send(); + + Assert.assertNotNull(response); + Assert.assertEquals(200, response.getStatus()); + } + + @Test + public void test_POST_WithContent_TracksProgress() throws Exception + { + start(new EmptyServerHandler()); + + final AtomicInteger progress = new AtomicInteger(); + ContentResponse response = client.POST(scheme + "://localhost:" + connector.getLocalPort()) + .onRequestContent(new Request.ContentListener() + { + @Override + public void onContent(Request request, ByteBuffer buffer) + { + byte[] bytes = new byte[buffer.remaining()]; + Assert.assertEquals(1, bytes.length); + buffer.get(bytes); + Assert.assertEquals(bytes[0], progress.getAndIncrement()); + } + }) + .content(new BytesContentProvider(new byte[]{0}, new byte[]{1}, new byte[]{2}, new byte[]{3}, new byte[]{4})) + .timeout(5, TimeUnit.SECONDS) + .send(); + + Assert.assertNotNull(response); + Assert.assertEquals(200, response.getStatus()); + Assert.assertEquals(5, progress.get()); + } + + @Test public void test_QueuedRequest_IsSent_WhenPreviousRequestSucceeded() throws Exception { start(new EmptyServerHandler()); diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpRequestAbortTest.java b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpRequestAbortTest.java index 293e772c38..5d1a2b1279 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpRequestAbortTest.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpRequestAbortTest.java @@ -18,7 +18,6 @@ package org.eclipse.jetty.client; -import java.io.EOFException; import java.io.IOException; import java.nio.ByteBuffer; import java.util.concurrent.CountDownLatch; @@ -26,7 +25,6 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; - import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -36,11 +34,9 @@ import org.eclipse.jetty.client.api.Request; import org.eclipse.jetty.client.api.Response; import org.eclipse.jetty.client.api.Result; import org.eclipse.jetty.client.util.ByteBufferContentProvider; -import org.eclipse.jetty.server.HttpChannel; import org.eclipse.jetty.server.handler.AbstractHandler; import org.eclipse.jetty.toolchain.test.annotation.Slow; import org.eclipse.jetty.util.IO; -import org.eclipse.jetty.util.log.StdErrLog; import org.eclipse.jetty.util.ssl.SslContextFactory; import org.junit.Assert; import org.junit.Test; @@ -226,7 +222,6 @@ public class HttpRequestAbortTest extends AbstractHttpClientServerTest } }); - StdErrLog.getLogger(HttpChannel.class).setHideStacks(true); final Throwable cause = new Exception(); try { @@ -254,24 +249,51 @@ public class HttpRequestAbortTest extends AbstractHttpClientServerTest } catch (ExecutionException x) { - Throwable abort = x.getCause(); - if (abort instanceof EOFException) - { - // Server closed abruptly - System.err.println("C"); - } - else if (abort == cause) - { - // Expected - } - else + Assert.assertSame(cause, x.getCause()); + } + } + + @Test + public void testAbortOnContent() throws Exception + { + start(new EmptyServerHandler() + { + @Override + public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { - throw x; + super.handle(target, baseRequest, request, response); + IO.copy(request.getInputStream(), response.getOutputStream()); } + }); + + final Throwable cause = new Exception(); + try + { + client.newRequest("localhost", connector.getLocalPort()) + .scheme(scheme) + .onRequestContent(new Request.ContentListener() + { + @Override + public void onContent(Request request, ByteBuffer content) + { + request.abort(cause); + } + }) + .content(new ByteBufferContentProvider(ByteBuffer.wrap(new byte[]{0}), ByteBuffer.wrap(new byte[]{1})) + { + @Override + public long getLength() + { + return -1; + } + }) + .timeout(5, TimeUnit.SECONDS) + .send(); + Assert.fail(); } - finally + catch (ExecutionException x) { - StdErrLog.getLogger(HttpChannel.class).setHideStacks(false); + Assert.assertSame(cause, x.getCause()); } } |