First pass to fix #308851.

git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@1873 7e9141cc-0065-0410-87d8-b60c137991c4
This commit is contained in:
Simone Bordet 2010-05-26 10:24:11 +00:00
parent f728f9dfa3
commit 222826d2f7
5 changed files with 197 additions and 233 deletions

View File

@ -38,7 +38,7 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<archive>
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
</archive>
</configuration>
@ -56,16 +56,17 @@
<artifactId>jetty-server</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-security</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit4-version}</version>
<scope>test</scope>
</dependency>
<dependency>

View File

@ -19,44 +19,55 @@ import java.net.Socket;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import junit.framework.TestCase;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* @version $Revision$ $Date$
*/
public class ConnectionTest extends TestCase
public class ConnectionTest
{
@Test
public void testConnectionFailed() throws Exception
{
ServerSocket socket = new ServerSocket();
socket.bind(null);
int port=socket.getLocalPort();
socket.close();
ServerSocket serverSocket = new ServerSocket();
serverSocket.bind(null);
int port=serverSocket.getLocalPort();
serverSocket.close();
HttpClient httpClient = new HttpClient();
httpClient.start();
CountDownLatch latch = new CountDownLatch(1);
HttpExchange exchange = new ConnectionExchange(latch);
exchange.setAddress(new Address("localhost", port));
exchange.setURI("/");
httpClient.send(exchange);
boolean passed = latch.await(4000, TimeUnit.MILLISECONDS);
assertTrue(passed);
long wait = 100;
long maxWait = 10 * wait;
long curWait = wait;
while (curWait < maxWait && !exchange.isDone())
try
{
Thread.sleep(wait);
curWait += wait;
}
CountDownLatch latch = new CountDownLatch(1);
HttpExchange exchange = new ConnectionExchange(latch);
exchange.setAddress(new Address("localhost", port));
exchange.setURI("/");
httpClient.send(exchange);
assertEquals(HttpExchange.STATUS_EXCEPTED, exchange.getStatus());
boolean passed = latch.await(4000, TimeUnit.MILLISECONDS);
assertTrue(passed);
long wait = 100;
long maxWait = 10 * wait;
long curWait = wait;
while (curWait < maxWait && !exchange.isDone())
{
Thread.sleep(wait);
curWait += wait;
}
assertEquals(HttpExchange.STATUS_EXCEPTED, exchange.getStatus());
}
finally
{
httpClient.stop();
}
}
@Test
public void testConnectionTimeoutWithSocketConnector() throws Exception
{
HttpClient httpClient = new HttpClient();
@ -64,7 +75,6 @@ public class ConnectionTest extends TestCase
int connectTimeout = 5000;
httpClient.setConnectTimeout(connectTimeout);
httpClient.start();
try
{
CountDownLatch latch = new CountDownLatch(1);
@ -86,6 +96,7 @@ public class ConnectionTest extends TestCase
}
}
@Test
public void testConnectionTimeoutWithSelectConnector() throws Exception
{
HttpClient httpClient = new HttpClient();
@ -93,7 +104,6 @@ public class ConnectionTest extends TestCase
int connectTimeout = 5000;
httpClient.setConnectTimeout(connectTimeout);
httpClient.start();
try
{
CountDownLatch latch = new CountDownLatch(1);
@ -115,56 +125,62 @@ public class ConnectionTest extends TestCase
}
}
@Test
public void testIdleConnection() throws Exception
{
ServerSocket socket = new ServerSocket();
socket.bind(null);
int port=socket.getLocalPort();
ServerSocket serverSocket = new ServerSocket();
serverSocket.bind(null);
int port=serverSocket.getLocalPort();
HttpClient httpClient = new HttpClient();
httpClient.setIdleTimeout(700);
httpClient.start();
try
{
HttpExchange exchange = new ConnectionExchange();
exchange.setAddress(new Address("localhost", port));
exchange.setURI("/");
HttpDestination dest = httpClient.getDestination(new Address("localhost", port),false);
HttpExchange exchange = new ConnectionExchange();
exchange.setAddress(new Address("localhost", port));
exchange.setURI("/");
HttpDestination dest = httpClient.getDestination(new Address("localhost", port),false);
httpClient.send(exchange);
Socket s = serverSocket.accept();
byte[] buf = new byte[4096];
s.getInputStream().read(buf);
assertEquals(1,dest.getConnections());
assertEquals(0,dest.getIdleConnections());
httpClient.send(exchange);
Socket s = socket.accept();
byte[] buf = new byte[4096];
s.getInputStream().read(buf);
assertEquals(1,dest.getConnections());
assertEquals(0,dest.getIdleConnections());
s.getOutputStream().write("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n".getBytes());
s.getOutputStream().write("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n".getBytes());
Thread.sleep(300);
assertEquals(1,dest.getConnections());
assertEquals(1,dest.getIdleConnections());
Thread.sleep(300);
assertEquals(1,dest.getConnections());
assertEquals(1,dest.getIdleConnections());
exchange = new ConnectionExchange();
exchange.setAddress(new Address("localhost", port));
exchange.setURI("/");
exchange = new ConnectionExchange();
exchange.setAddress(new Address("localhost", port));
exchange.setURI("/");
httpClient.send(exchange);
s.getInputStream().read(buf);
assertEquals(1,dest.getConnections());
assertEquals(0,dest.getIdleConnections());
s.getOutputStream().write("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n".getBytes());
httpClient.send(exchange);
s.getInputStream().read(buf);
assertEquals(1,dest.getConnections());
assertEquals(0,dest.getIdleConnections());
s.getOutputStream().write("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n".getBytes());
Thread.sleep(500);
Thread.sleep(500);
assertEquals(1,dest.getConnections());
assertEquals(1,dest.getIdleConnections());
assertEquals(1,dest.getConnections());
assertEquals(1,dest.getIdleConnections());
Thread.sleep(500);
Thread.sleep(500);
assertEquals(0,dest.getConnections());
assertEquals(0,dest.getIdleConnections());
socket.close();
assertEquals(0,dest.getConnections());
assertEquals(0,dest.getIdleConnections());
serverSocket.close();
}
finally
{
httpClient.stop();
}
}
private class ConnectionExchange extends HttpExchange
@ -187,7 +203,7 @@ public class ConnectionTest extends TestCase
if (latch!=null)
latch.countDown();
}
@Override
protected void onException(Throwable x)
{

View File

@ -6,157 +6,108 @@ package org.eclipse.jetty.client;
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
// The Eclipse Public License is available at
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
// You may elect to redistribute this code under either of these licenses.
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import junit.framework.TestCase;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.util.log.Log;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/* Test expiring connections
*
import static org.junit.Assert.assertTrue;
/**
* Test contributed by: Michiel Thuys for JETTY-806
*/
public class ExpireTest extends TestCase
public class ExpireTest
{
HttpClient client;
private Server server;
private HttpClient client;
private int port;
Server server;
AtomicInteger expireCount = new AtomicInteger();
final String host = "localhost";
int _port;
@Override
protected void setUp() throws Exception
@Before
public void init() throws Exception
{
client = new HttpClient();
client.setConnectorType( HttpClient.CONNECTOR_SELECT_CHANNEL );
client.setTimeout( 200 );
client.setMaxRetries( 0 );
client.setMaxConnectionsPerAddress(100);
try
{
client.start();
}
catch ( Exception e )
{
throw new Error( "Cannot start HTTP client: " + e );
}
// Create server
server = new Server();
SelectChannelConnector connector = new SelectChannelConnector();
connector.setHost( host );
connector.setPort( 0 );
server.setConnectors( new Connector[] { connector } );
server.setHandler( new AbstractHandler()
connector.setHost("localhost");
connector.setPort(0);
server.addConnector(connector);
server.setHandler(new AbstractHandler()
{
public void handle( String target, Request baseRequest, HttpServletRequest servletRequest, HttpServletResponse response ) throws IOException,
ServletException
public void handle(String target, Request request, HttpServletRequest httpRequest, HttpServletResponse httpResponse)
throws IOException, ServletException
{
Request request = (Request) servletRequest;
request.setHandled(true);
try
{
Thread.sleep( 2000 );
Thread.sleep(2000);
}
catch ( InterruptedException e )
catch (InterruptedException x)
{
// TODO Auto-generated catch block
e.printStackTrace();
throw new ServletException(x);
}
request.setHandled( true );
}
} );
try
{
server.start();
_port = connector.getLocalPort();
}
catch ( Exception e )
{
Log.warn( "Cannot create server: " + e );
}
});
server.start();
port = connector.getLocalPort();
client = new HttpClient();
client.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
client.setTimeout(200);
client.setMaxRetries(0);
client.setMaxConnectionsPerAddress(100);
client.start();
}
@Override
protected void tearDown() throws Exception
@After
public void destroy() throws Exception
{
client.stop();
server.stop();
server.join();
}
public void testExpire() throws IOException
@Test
public void testExpire() throws Exception
{
String baseUrl = "http://" + host + ":" + _port + "/";
String baseUrl = "http://" + "localhost" + ":" + port + "/";
int count = 200;
expireCount.set( 0 );
Log.info( "Starting test on " + baseUrl );
final CountDownLatch expires = new CountDownLatch(count);
for (int i=0;i<count;i++)
{
if (i%10==0)
System.err.print('.');
expireCount.incrementAndGet();
final ContentExchange ex = new ContentExchange()
final ContentExchange exchange = new ContentExchange()
{
@Override
protected void onExpire()
{
expireCount.decrementAndGet();
expires.countDown();
}
};
ex.setMethod( "GET" );
ex.setURL( baseUrl );
exchange.setMethod("GET");
exchange.setURL(baseUrl);
client.send( ex );
try
{
Thread.sleep( 50 );
}
catch ( InterruptedException e )
{
break;
}
client.send(exchange);
Thread.sleep(50);
}
// Log.info("Test done");
// Wait to be sure that all exchanges have expired
try
{
Thread.sleep( 2000 );
int loops = 0;
while ( expireCount.get()>0 && loops < 10 ) // max out at 30 seconds
{
Log.info( "waiting for test to complete: "+expireCount.get()+" of "+count );
++loops;
Thread.sleep( 2000 );
}
Thread.sleep( 2000 );
}
catch ( InterruptedException e )
{
}
System.err.println('!');
assertEquals( 0, expireCount.get() );
assertTrue(expires.await(5, TimeUnit.SECONDS));
}
}

View File

@ -4,11 +4,11 @@
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
// The Eclipse Public License is available at
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
// You may elect to redistribute this code under either of these licenses.
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
package org.eclipse.jetty.client;
@ -18,13 +18,10 @@ import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import junit.framework.TestCase;
import org.eclipse.jetty.http.HttpMethods;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.server.Connector;
@ -32,10 +29,16 @@ import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class HttpHeadersTest extends TestCase
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class HttpHeadersTest
{
private static String _content = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. In quis felis nunc. "
private static final String CONTENT = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. In quis felis nunc. "
+ "Quisque suscipit mauris et ante auctor ornare rhoncus lacus aliquet. Pellentesque "
+ "habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. "
+ "Vestibulum sit amet felis augue, vel convallis dolor. Cras accumsan vehicula diam "
@ -48,75 +51,67 @@ public class HttpHeadersTest extends TestCase
+ "Aliquam purus mauris, consectetur nec convallis lacinia, porta sed ante. Suspendisse "
+ "et cursus magna. Donec orci enim, molestie a lobortis eu, imperdiet vitae neque.";
private File _docRoot;
private Server _server;
private Connector _connector;
private TestHeaderHandler _handler;
private int _port;
public void setUp() throws Exception
@Before
public void init() throws Exception
{
_docRoot = new File("target/test-output/docroot/");
_docRoot.mkdirs();
_docRoot.deleteOnExit();
File docRoot = new File("target/test-output/docroot/");
if (!docRoot.exists())
assertTrue(docRoot.mkdirs());
docRoot.deleteOnExit();
startServer();
}
public void tearDown() throws Exception
{
stopServer();
}
public void testHttpHeaders() throws Exception
{
HttpClient client = new HttpClient();
client.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
client.start();
String requestUrl = "http://localhost:" + _port + "/header";
ContentExchange exchange = new ContentExchange();
exchange.setURL(requestUrl);
exchange.setMethod(HttpMethods.GET);
exchange.addRequestHeader("User-Agent","Jetty-Client/7.0");
client.send(exchange);
int state = exchange.waitForDone();
String content = "";
int responseStatus = exchange.getResponseStatus();
if (responseStatus == HttpStatus.OK_200)
{
content = exchange.getResponseContent();
}
assertEquals(HttpStatus.OK_200,responseStatus);
assertEquals(_content,content);
assertEquals("Jetty-Client/7.0",_handler.headers.get("User-Agent"));
}
protected void startServer() throws Exception
{
_server = new Server(0);
_connector = new SelectChannelConnector();
_server.addConnector(_connector);
_server = new Server();
Connector connector = new SelectChannelConnector();
_server.addConnector(connector);
_handler = new TestHeaderHandler();
_server.setHandler(_handler);
_server.start();
_port = _connector.getLocalPort();
_port = connector.getLocalPort();
}
protected void stopServer() throws Exception
@After
public void destroy() throws Exception
{
if (_server != null)
_server.stop();
_server.join();
}
@Test
public void testHttpHeaders() throws Exception
{
HttpClient httpClient = new HttpClient();
httpClient.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
httpClient.start();
try
{
_server.stop();
_server = null;
String requestUrl = "http://localhost:" + _port + "/header";
ContentExchange exchange = new ContentExchange();
exchange.setURL(requestUrl);
exchange.setMethod(HttpMethods.GET);
exchange.addRequestHeader("User-Agent","Jetty-Client/7.0");
httpClient.send(exchange);
int state = exchange.waitForDone();
assertEquals(HttpExchange.STATUS_COMPLETED, state);
int responseStatus = exchange.getResponseStatus();
assertEquals(HttpStatus.OK_200,responseStatus);
String content = exchange.getResponseContent();
assertEquals(HttpHeadersTest.CONTENT,content);
assertEquals("Jetty-Client/7.0",_handler.headers.get("User-Agent"));
}
finally
{
httpClient.stop();
}
}
@ -138,7 +133,7 @@ public class HttpHeadersTest extends TestCase
response.setContentType("text/plain");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().print(_content);
response.getWriter().print(CONTENT);
baseRequest.setHandled(true);
}

View File

@ -4,35 +4,36 @@
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
// The Eclipse Public License is available at
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
// You may elect to redistribute this code under either of these licenses.
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
package org.eclipse.jetty.client.security;
import junit.framework.TestCase;
import org.junit.Test;
public class SecurityResolverTest extends TestCase
public class SecurityResolverTest
{
@Test
public void testNothing()
{
}
/* TODO
public void testCredentialParsing() throws Exception
{
SecurityListener resolver = new SecurityListener();
Buffer value = new ByteArrayBuffer("basic a=b".getBytes());
assertEquals( "basic", resolver.scrapeAuthenticationType( value.toString() ) );
assertEquals( 1, resolver.scrapeAuthenticationDetails( value.toString() ).size() );
value = new ByteArrayBuffer("digest a=boo, c=\"doo\" , egg=foo".getBytes());
assertEquals( "digest", resolver.scrapeAuthenticationType( value.toString() ) );
Map<String,String> testMap = resolver.scrapeAuthenticationDetails( value.toString() );
assertEquals( 3, testMap.size() );
@ -40,6 +41,6 @@ public class SecurityResolverTest extends TestCase
assertEquals( "doo", testMap.get("c") );
assertEquals( "foo", testMap.get("egg") );
}
*/
}