mirror of
https://github.com/jetty/jetty.project.git
synced 2025-03-03 20:39:18 +00:00
jetty-9: HTTP client: more tests.
This commit is contained in:
parent
2e02f153ce
commit
f81dc0161e
@ -249,4 +249,52 @@ public class HttpClientAuthenticationTest extends AbstractHttpClientServerTest
|
||||
Assert.assertEquals(3, requests.get());
|
||||
client.getRequestListeners().remove(requestListener);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_BasicAuthentication_WithAuthenticationRemoved() throws Exception
|
||||
{
|
||||
startBasic(new EmptyServerHandler());
|
||||
|
||||
final AtomicInteger requests = new AtomicInteger();
|
||||
Request.Listener.Empty requestListener = new Request.Listener.Empty()
|
||||
{
|
||||
@Override
|
||||
public void onSuccess(Request request)
|
||||
{
|
||||
requests.incrementAndGet();
|
||||
}
|
||||
};
|
||||
client.getRequestListeners().add(requestListener);
|
||||
|
||||
AuthenticationStore authenticationStore = client.getAuthenticationStore();
|
||||
BasicAuthentication authentication = new BasicAuthentication(scheme + "://localhost:" + connector.getLocalPort(), realm, "basic", "basic");
|
||||
authenticationStore.addAuthentication(authentication);
|
||||
|
||||
Request request = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/secure");
|
||||
ContentResponse response = request.send().get(5, TimeUnit.SECONDS);
|
||||
Assert.assertNotNull(response);
|
||||
Assert.assertEquals(200, response.status());
|
||||
Assert.assertEquals(2, requests.get());
|
||||
requests.set(0);
|
||||
|
||||
authenticationStore.removeAuthentication(authentication);
|
||||
|
||||
request = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/secure");
|
||||
response = request.send().get(5, TimeUnit.SECONDS);
|
||||
Assert.assertNotNull(response);
|
||||
Assert.assertEquals(200, response.status());
|
||||
Assert.assertEquals(1, requests.get());
|
||||
requests.set(0);
|
||||
|
||||
Authentication.Result result = authenticationStore.findAuthenticationResult(request.uri());
|
||||
Assert.assertNotNull(result);
|
||||
authenticationStore.removeAuthenticationResult(result);
|
||||
|
||||
request = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/secure");
|
||||
response = request.send().get(5, TimeUnit.SECONDS);
|
||||
Assert.assertNotNull(response);
|
||||
Assert.assertEquals(401, response.status());
|
||||
Assert.assertEquals(1, requests.get());
|
||||
requests.set(0);
|
||||
}
|
||||
}
|
||||
|
@ -117,10 +117,12 @@ public class HttpClientLoadTest extends AbstractHttpClientServerTest
|
||||
HttpMethod method = random.nextBoolean() ? HttpMethod.GET : HttpMethod.POST;
|
||||
request.method(method);
|
||||
|
||||
boolean ssl = "https".equalsIgnoreCase(scheme);
|
||||
|
||||
// Choose randomly whether to close the connection on the client or on the server
|
||||
if (random.nextBoolean())
|
||||
if (!ssl && random.nextBoolean())
|
||||
request.header("Connection", "close");
|
||||
else if (random.nextBoolean())
|
||||
else if (!ssl && random.nextBoolean())
|
||||
request.header("X-Close", "true");
|
||||
|
||||
switch (method)
|
||||
|
@ -176,6 +176,19 @@ public class HttpClientRedirectTest extends AbstractHttpClientServerTest
|
||||
Assert.assertFalse(response.headers().containsKey(HttpHeader.LOCATION.asString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDontFollowRedirects() throws Exception
|
||||
{
|
||||
Response response = client.newRequest("localhost", connector.getLocalPort())
|
||||
.scheme(scheme)
|
||||
.followRedirects(false)
|
||||
.path("/303/localhost/done?close=true")
|
||||
.send().get(5, TimeUnit.SECONDS);
|
||||
Assert.assertNotNull(response);
|
||||
Assert.assertEquals(303, response.status());
|
||||
Assert.assertTrue(response.headers().containsKey(HttpHeader.LOCATION.asString()));
|
||||
}
|
||||
|
||||
private class RedirectHandler extends AbstractHandler
|
||||
{
|
||||
@Override
|
||||
|
@ -34,6 +34,8 @@ import org.eclipse.jetty.client.api.Result;
|
||||
import org.eclipse.jetty.client.util.ByteBufferContentProvider;
|
||||
import org.eclipse.jetty.http.HttpHeader;
|
||||
import org.eclipse.jetty.server.handler.AbstractHandler;
|
||||
import org.eclipse.jetty.util.log.Log;
|
||||
import org.eclipse.jetty.util.log.StdErrLog;
|
||||
import org.eclipse.jetty.util.ssl.SslContextFactory;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
@ -302,44 +304,54 @@ public class HttpConnectionLifecycleTest extends AbstractHttpClientServerTest
|
||||
@Test
|
||||
public void test_BigRequestContent_ResponseWithConnectionCloseHeader_RemovesConnection() throws Exception
|
||||
{
|
||||
start(new AbstractHandler()
|
||||
StdErrLog logger = (StdErrLog)Log.getLogger(org.eclipse.jetty.server.HttpConnection.class);
|
||||
// logger.setHideStacks(true);
|
||||
try
|
||||
{
|
||||
@Override
|
||||
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
|
||||
start(new AbstractHandler()
|
||||
{
|
||||
response.setHeader("Connection", "close");
|
||||
baseRequest.setHandled(true);
|
||||
}
|
||||
});
|
||||
|
||||
String host = "localhost";
|
||||
int port = connector.getLocalPort();
|
||||
HttpDestination destination = (HttpDestination)client.getDestination(scheme, host, port);
|
||||
|
||||
final BlockingQueue<Connection> idleConnections = destination.getIdleConnections();
|
||||
Assert.assertEquals(0, idleConnections.size());
|
||||
|
||||
final BlockingQueue<Connection> activeConnections = destination.getActiveConnections();
|
||||
Assert.assertEquals(0, activeConnections.size());
|
||||
|
||||
final CountDownLatch latch = new CountDownLatch(1);
|
||||
client.newRequest(host, port)
|
||||
.scheme(scheme)
|
||||
.content(new ByteBufferContentProvider(ByteBuffer.allocate(16 * 1024 * 1024)))
|
||||
.send(new Response.Listener.Empty()
|
||||
@Override
|
||||
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
|
||||
{
|
||||
@Override
|
||||
public void onComplete(Result result)
|
||||
response.setHeader("Connection", "close");
|
||||
baseRequest.setHandled(true);
|
||||
// Don't read request content; this causes the server parser to be closed
|
||||
}
|
||||
});
|
||||
|
||||
String host = "localhost";
|
||||
int port = connector.getLocalPort();
|
||||
HttpDestination destination = (HttpDestination)client.getDestination(scheme, host, port);
|
||||
|
||||
final BlockingQueue<Connection> idleConnections = destination.getIdleConnections();
|
||||
Assert.assertEquals(0, idleConnections.size());
|
||||
|
||||
final BlockingQueue<Connection> activeConnections = destination.getActiveConnections();
|
||||
Assert.assertEquals(0, activeConnections.size());
|
||||
|
||||
final CountDownLatch latch = new CountDownLatch(1);
|
||||
client.newRequest(host, port)
|
||||
.scheme(scheme)
|
||||
.content(new ByteBufferContentProvider(ByteBuffer.allocate(16 * 1024 * 1024)))
|
||||
.send(new Response.Listener.Empty()
|
||||
{
|
||||
Assert.assertEquals(0, idleConnections.size());
|
||||
Assert.assertEquals(0, activeConnections.size());
|
||||
latch.countDown();
|
||||
}
|
||||
});
|
||||
@Override
|
||||
public void onComplete(Result result)
|
||||
{
|
||||
Assert.assertEquals(0, idleConnections.size());
|
||||
Assert.assertEquals(0, activeConnections.size());
|
||||
latch.countDown();
|
||||
}
|
||||
});
|
||||
|
||||
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
|
||||
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
|
||||
|
||||
Assert.assertEquals(0, idleConnections.size());
|
||||
Assert.assertEquals(0, activeConnections.size());
|
||||
Assert.assertEquals(0, idleConnections.size());
|
||||
Assert.assertEquals(0, activeConnections.size());
|
||||
}
|
||||
finally
|
||||
{
|
||||
// logger.setHideStacks(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user