jetty-9 progress on jetty-servlets test harnesses

This commit is contained in:
Greg Wilkins 2012-07-16 16:01:44 +10:00
parent acbf20af27
commit bc02ce4de7
7 changed files with 170 additions and 175 deletions

View File

@ -11,151 +11,14 @@ import org.eclipse.jetty.util.StringUtil;
public class HttpTester
{
private Message _message;
private Request _request;
private Response _response;
private HttpTester(){};
public void setMethod(String method)
public static Request newRequest()
{
if (_request==null)
_request=new Request();
_message=_request;
_request._method=method;
}
public void setVersion(HttpVersion version)
{
if (_message==null)
_message=_response=new Response();
_message._version=version;
return new Request();
}
public void setVersion(String version)
{
setVersion(HttpVersion.CACHE.get(version));
}
public void setHeader(String name, String value)
{
_message.put(name,value);
}
public void setURI(String uri)
{
if (_request==null)
_request=new Request();
_message=_request;
_request._uri=uri;
}
public void setContent(String content)
{
try
{
_message._content=new ByteArrayOutputStream();
_message._content.write(StringUtil.getBytes(content));
}
catch (IOException e)
{
throw new RuntimeIOException(e);
}
}
public void setContent(ByteBuffer content)
{
try
{
_message._content=new ByteArrayOutputStream();
_message._content.write(BufferUtil.toArray(content));
}
catch (IOException e)
{
throw new RuntimeIOException(e);
}
}
public ByteBuffer generate()
{
try
{
HttpGenerator generator = new HttpGenerator();
HttpGenerator.Info info = _message.getInfo();
System.err.println(info.getClass());
System.err.println(info);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteBuffer header=BufferUtil.allocate(8192);
ByteBuffer buffer=BufferUtil.allocate(8192);
ByteBuffer chunk=BufferUtil.allocate(16);
ByteBuffer content=_message._content==null?null:ByteBuffer.wrap(_message._content.toByteArray());
loop: while(true)
{
HttpGenerator.Result result = generator.generate(info,header,chunk,buffer,content,HttpGenerator.Action.COMPLETE);
switch(result)
{
case NEED_BUFFER:
case NEED_HEADER:
case NEED_CHUNK:
case NEED_INFO:
throw new IllegalStateException();
case FLUSH:
if (BufferUtil.hasContent(header))
{
out.write(BufferUtil.toArray(header));
BufferUtil.clear(header);
}
if (BufferUtil.hasContent(chunk))
{
out.write(BufferUtil.toArray(chunk));
BufferUtil.clear(chunk);
}
if (BufferUtil.hasContent(buffer))
{
out.write(BufferUtil.toArray(buffer));
BufferUtil.clear(buffer);
}
break;
case FLUSH_CONTENT:
if (BufferUtil.hasContent(header))
{
out.write(BufferUtil.toArray(header));
BufferUtil.clear(header);
}
if (BufferUtil.hasContent(chunk))
{
out.write(BufferUtil.toArray(chunk));
BufferUtil.clear(chunk);
}
if (BufferUtil.hasContent(content))
{
out.write(BufferUtil.toArray(content));
BufferUtil.clear(content);
}
break;
case OK:
case SHUTDOWN_OUT:
break loop;
}
}
return ByteBuffer.wrap(out.toByteArray());
}
catch (IOException e)
{
throw new RuntimeIOException(e);
}
}
public String toString()
{
return String.format("%s %s",_request,_response);
}
public static Request parseRequest(String request)
{
@ -199,7 +62,42 @@ public class HttpTester
{
return _version;
}
public void setVersion(String version)
{
setVersion(HttpVersion.CACHE.get(version));
}
public void setVersion(HttpVersion version)
{
_version=version;
}
public void setContent(String content)
{
try
{
_content=new ByteArrayOutputStream();
_content.write(StringUtil.getBytes(content));
}
catch (IOException e)
{
throw new RuntimeIOException(e);
}
}
public void setContent(ByteBuffer content)
{
try
{
_content=new ByteArrayOutputStream();
_content.write(BufferUtil.toArray(content));
}
catch (IOException e)
{
throw new RuntimeIOException(e);
}
}
@Override
public boolean parsedHeader(HttpHeader header, String name, String value)
{
@ -246,7 +144,83 @@ public class HttpTester
{
throw new RuntimeIOException(reason);
}
public ByteBuffer generate()
{
try
{
HttpGenerator generator = new HttpGenerator();
HttpGenerator.Info info = getInfo();
System.err.println(info.getClass());
System.err.println(info);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteBuffer header=BufferUtil.allocate(8192);
ByteBuffer buffer=BufferUtil.allocate(8192);
ByteBuffer chunk=BufferUtil.allocate(16);
ByteBuffer content=_content==null?null:ByteBuffer.wrap(_content.toByteArray());
loop: while(true)
{
HttpGenerator.Result result = generator.generate(info,header,chunk,buffer,content,HttpGenerator.Action.COMPLETE);
switch(result)
{
case NEED_BUFFER:
case NEED_HEADER:
case NEED_CHUNK:
case NEED_INFO:
throw new IllegalStateException();
case FLUSH:
if (BufferUtil.hasContent(header))
{
out.write(BufferUtil.toArray(header));
BufferUtil.clear(header);
}
if (BufferUtil.hasContent(chunk))
{
out.write(BufferUtil.toArray(chunk));
BufferUtil.clear(chunk);
}
if (BufferUtil.hasContent(buffer))
{
out.write(BufferUtil.toArray(buffer));
BufferUtil.clear(buffer);
}
break;
case FLUSH_CONTENT:
if (BufferUtil.hasContent(header))
{
out.write(BufferUtil.toArray(header));
BufferUtil.clear(header);
}
if (BufferUtil.hasContent(chunk))
{
out.write(BufferUtil.toArray(chunk));
BufferUtil.clear(chunk);
}
if (BufferUtil.hasContent(content))
{
out.write(BufferUtil.toArray(content));
BufferUtil.clear(content);
}
break;
case OK:
case SHUTDOWN_OUT:
break loop;
}
}
return ByteBuffer.wrap(out.toByteArray());
}
catch (IOException e)
{
throw new RuntimeIOException(e);
}
}
abstract public HttpGenerator.Info getInfo();
}
@ -275,6 +249,16 @@ public class HttpTester
return _uri;
}
public void setMethod(String method)
{
_method=method;
}
public void setURI(String uri)
{
_uri=uri;
}
@Override
public HttpGenerator.RequestInfo getInfo()
{
@ -285,6 +269,11 @@ public class HttpTester
{
return String.format("%s %s %s\n%s\n",_method,_uri,_version,super.toString());
}
public void setHeader(String name, String value)
{
put(name,value);
}
}
public static class Response extends Message implements HttpParser.ResponseHandler

View File

@ -323,7 +323,10 @@ public class Server extends HandlerWrapper implements Attributes
if (_connectors!=null)
{
for (int i=_connectors.length;i-->0;)
try{_connectors[i].stop();}catch(Throwable e){mex.add(e);}
{
if (_connectors[i]!=null)
try{_connectors[i].stop();}catch(Throwable e){mex.add(e);}
}
}
try {super.doStop(); } catch(Throwable e) { mex.add(e);}

View File

@ -197,6 +197,13 @@ public class ServletTester extends AggregateLifeCycle
InetAddress.getLocalHost().getHostAddress()
)+":"+connector.getLocalPort();
}
public LocalHttpConnector createLocalConnector()
{
LocalHttpConnector connector = new LocalHttpConnector();
_server.addConnector(connector);
return connector;
}

View File

@ -75,8 +75,8 @@ public class MultipartFilterTest
public void testBadPost() throws Exception
{
// generated and parsed test
HttpTester request = new HttpTester();
HttpTester response = new HttpTester();
HttpTester.Request request = HttpTester.newRequest();
HttpTester.Response response;
// test GET
request.setMethod("POST");
@ -97,8 +97,7 @@ public class MultipartFilterTest
request.setContent(content);
response.parse(tester.getResponses(request.generate()));
assertTrue(response.getMethod()==null);
response = HttpTester.parseResponse(tester.getResponses(request.generate()));
assertEquals(HttpServletResponse.SC_OK,response.getStatus());
}
@ -107,8 +106,8 @@ public class MultipartFilterTest
public void testPost() throws Exception
{
// generated and parsed test
HttpTester request = new HttpTester();
HttpTester response = new HttpTester();
HttpTester.Request request = HttpTester.newRequest();
HttpTester.Response response;
// test GET
request.setMethod("POST");
@ -128,8 +127,7 @@ public class MultipartFilterTest
request.setContent(content);
response.parse(tester.getResponses(request.generate()));
assertTrue(response.getMethod()==null);
response = HttpTester.parseResponse(tester.getResponses(request.generate()));
assertEquals(HttpServletResponse.SC_OK,response.getStatus());
assertTrue(response.getContent().indexOf("brown cow")>=0);
}
@ -139,8 +137,8 @@ public class MultipartFilterTest
public void testEncodedPost() throws Exception
{
// generated and parsed test
HttpTester request = new HttpTester();
HttpTester response = new HttpTester();
HttpTester.Request request = HttpTester.newRequest();
HttpTester.Response response;
// test GET
request.setMethod("POST");
@ -160,8 +158,7 @@ public class MultipartFilterTest
request.setContent(content);
response.parse(tester.getResponses(request.generate()));
assertTrue(response.getMethod()==null);
response = HttpTester.parseResponse(tester.getResponses(request.generate()));
assertEquals(HttpServletResponse.SC_OK,response.getStatus());
assertTrue(response.getContent().indexOf("brown cow")>=0);
}
@ -172,8 +169,8 @@ public class MultipartFilterTest
@Test
public void testPostWithContentTransferEncodingBase64() throws Exception {
// generated and parsed test
HttpTester request = new HttpTester();
HttpTester response = new HttpTester();
HttpTester.Request request = HttpTester.newRequest();
HttpTester.Response response;
// test GET
request.setMethod("POST");
@ -194,8 +191,7 @@ public class MultipartFilterTest
request.setContent(content);
response.parse(tester.getResponses(request.generate()));
assertTrue(response.getMethod()==null);
response = HttpTester.parseResponse(tester.getResponses(request.generate()));
assertEquals(HttpServletResponse.SC_OK,response.getStatus());
assertTrue(response.getContent().indexOf("brown cow")>=0);
}
@ -206,8 +202,8 @@ public class MultipartFilterTest
@Test
public void testPostWithContentTransferEncodingQuotedPrintable() throws Exception {
// generated and parsed test
HttpTester request = new HttpTester();
HttpTester response = new HttpTester();
HttpTester.Request request = HttpTester.newRequest();
HttpTester.Response response;
// test GET
request.setMethod("POST");
@ -231,8 +227,7 @@ public class MultipartFilterTest
request.setContent(content);
response.parse(tester.getResponses(request.generate()));
assertTrue(response.getMethod()==null);
response = HttpTester.parseResponse(tester.getResponses(request.generate()));
assertEquals(HttpServletResponse.SC_OK,response.getStatus());
assertTrue(response.getContent().indexOf("brown cow")>=0);
}

View File

@ -74,7 +74,7 @@ public class PutFilterTest
public void testHandlePut() throws Exception
{
// generated and parsed test
HttpTester request = new HttpTester();
HttpTester.Request request = HttpTester.newRequest();
HttpTester.Response response;
// test GET
@ -171,7 +171,7 @@ public class PutFilterTest
public void testHandleDelete() throws Exception
{
// generated and parsed test
HttpTester request = new HttpTester();
HttpTester.Request request = HttpTester.newRequest();
HttpTester.Response response;
// test PUT1
@ -208,7 +208,7 @@ public class PutFilterTest
public void testHandleMove() throws Exception
{
// generated and parsed test
HttpTester request = new HttpTester();
HttpTester.Request request = HttpTester.newRequest();
HttpTester.Response response;
// test PUT1
@ -246,13 +246,13 @@ public class PutFilterTest
public void testHandleOptions() throws Exception
{
// generated and parsed test
HttpTester request = new HttpTester();
HttpTester.Request request = HttpTester.newRequest();
HttpTester.Response response;
// test PUT1
request.setMethod("OPTIONS");
request.setVersion("HTTP/1.0");
request.setHeader("Host","tester");
request.put("Host","tester");
request.setURI("/context/file.txt");
response = HttpTester.parseResponse(tester.getResponses(request.generate()));
assertEquals(HttpServletResponse.SC_OK,response.getStatus());

View File

@ -28,10 +28,12 @@ import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.LocalHttpConnector;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.FilterMapping;
import org.eclipse.jetty.http.HttpTester;
import org.eclipse.jetty.servlet.ServletTester;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.junit.After;
@ -43,7 +45,7 @@ public class QoSFilterTest
private static final Logger LOG = Log.getLogger(QoSFilterTest.class);
private ServletTester _tester;
private LocalConnector[] _connectors;
private LocalHttpConnector[] _connectors;
private CountDownLatch _doneRequests;
private final int NUM_CONNECTIONS = 8;
private final int NUM_LOOPS = 6;
@ -58,7 +60,7 @@ public class QoSFilterTest
TestServlet.__maxSleepers=0;
TestServlet.__sleepers=0;
_connectors = new LocalConnector[NUM_CONNECTIONS];
_connectors = new LocalHttpConnector[NUM_CONNECTIONS];
for(int i = 0; i < _connectors.length; ++i)
_connectors[i] = _tester.createLocalConnector();
@ -133,7 +135,7 @@ public class QoSFilterTest
{
for (int i=0;i<NUM_LOOPS;i++)
{
HttpTester request = new HttpTester();
HttpTester.Request request = HttpTester.newRequest();
request.setMethod("GET");
request.setHeader("host", "tester");
@ -141,7 +143,7 @@ public class QoSFilterTest
request.setHeader("num", _num+"");
try
{
String responseString = _tester.getResponses(request.generate(), _connectors[_num]);
String responseString = _connectors[_num].getResponses(BufferUtil.toString(request.generate()));
if(responseString.indexOf("HTTP")!=-1)
{
_doneRequests.countDown();
@ -167,7 +169,7 @@ public class QoSFilterTest
URL url=null;
try
{
String addr = _tester.createSocketConnector(true);
String addr = _tester.createConnector(true);
for (int i=0;i<NUM_LOOPS;i++)
{
url=new URL(addr+"/context/test?priority="+(_num%QoSFilter.__DEFAULT_MAX_PRIORITY)+"&n="+_num+"&l="+i);

View File

@ -18,12 +18,11 @@ import javax.servlet.DispatcherType;
import javax.servlet.Servlet;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.io.ByteArrayBuffer;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.servlets.GzipFilter;
import org.eclipse.jetty.testing.HttpTester;
import org.eclipse.jetty.testing.ServletTester;
import org.eclipse.jetty.http.HttpTester;
import org.eclipse.jetty.servlet.ServletTester;
import org.eclipse.jetty.toolchain.test.IO;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.toolchain.test.TestingDir;