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,23 +11,56 @@ 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;
return new Request();
}
public void setVersion(HttpVersion version)
public static Request parseRequest(String request)
{
if (_message==null)
_message=_response=new Response();
_message._version=version;
Request r=new Request();
HttpParser parser =new HttpParser(r);
parser.parseNext(BufferUtil.toBuffer(request));
return r;
}
public static Request parseRequest(ByteBuffer request)
{
Request r=new Request();
HttpParser parser =new HttpParser(r);
parser.parseNext(request);
return r;
}
public static Response parseResponse(String response)
{
Response r=new Response();
HttpParser parser =new HttpParser(r);
parser.parseNext(BufferUtil.toBuffer(response));
return r;
}
public static Response parseResponse(ByteBuffer response)
{
Response r=new Response();
HttpParser parser =new HttpParser(r);
parser.parseNext(response);
return r;
}
public abstract static class Message extends HttpFields implements HttpParser.HttpHandler
{
ByteArrayOutputStream _content;
HttpVersion _version=HttpVersion.HTTP_1_0;
public HttpVersion getVersion()
{
return _version;
}
public void setVersion(String version)
@ -35,26 +68,17 @@ public class HttpTester
setVersion(HttpVersion.CACHE.get(version));
}
public void setHeader(String name, String value)
public void setVersion(HttpVersion version)
{
_message.put(name,value);
}
public void setURI(String uri)
{
if (_request==null)
_request=new Request();
_message=_request;
_request._uri=uri;
_version=version;
}
public void setContent(String content)
{
try
{
_message._content=new ByteArrayOutputStream();
_message._content.write(StringUtil.getBytes(content));
_content=new ByteArrayOutputStream();
_content.write(StringUtil.getBytes(content));
}
catch (IOException e)
{
@ -66,21 +90,67 @@ public class HttpTester
{
try
{
_message._content=new ByteArrayOutputStream();
_message._content.write(BufferUtil.toArray(content));
_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)
{
put(name,value);
return false;
}
@Override
public boolean messageComplete(long contentLength)
{
return true;
}
@Override
public boolean headerComplete(boolean hasBody, boolean persistent)
{
if (hasBody)
_content=new ByteArrayOutputStream();
return false;
}
@Override
public boolean earlyEOF()
{
return true;
}
@Override
public boolean content(ByteBuffer ref)
{
try
{
_content.write(BufferUtil.toArray(ref));
}
catch (IOException e)
{
throw new RuntimeIOException(e);
}
return false;
}
@Override
public void badMessage(int status, String reason)
{
throw new RuntimeIOException(reason);
}
public ByteBuffer generate()
{
try
{
HttpGenerator generator = new HttpGenerator();
HttpGenerator.Info info = _message.getInfo();
HttpGenerator.Info info = getInfo();
System.err.println(info.getClass());
System.err.println(info);
@ -88,7 +158,7 @@ public class HttpTester
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());
ByteBuffer content=_content==null?null:ByteBuffer.wrap(_content.toByteArray());
loop: while(true)
@ -151,102 +221,6 @@ public class HttpTester
}
}
public String toString()
{
return String.format("%s %s",_request,_response);
}
public static Request parseRequest(String request)
{
Request r=new Request();
HttpParser parser =new HttpParser(r);
parser.parseNext(BufferUtil.toBuffer(request));
return r;
}
public static Request parseRequest(ByteBuffer request)
{
Request r=new Request();
HttpParser parser =new HttpParser(r);
parser.parseNext(request);
return r;
}
public static Response parseResponse(String response)
{
Response r=new Response();
HttpParser parser =new HttpParser(r);
parser.parseNext(BufferUtil.toBuffer(response));
return r;
}
public static Response parseResponse(ByteBuffer response)
{
Response r=new Response();
HttpParser parser =new HttpParser(r);
parser.parseNext(response);
return r;
}
public abstract static class Message extends HttpFields implements HttpParser.HttpHandler
{
ByteArrayOutputStream _content;
HttpVersion _version=HttpVersion.HTTP_1_0;
public HttpVersion getVersion()
{
return _version;
}
@Override
public boolean parsedHeader(HttpHeader header, String name, String value)
{
put(name,value);
return false;
}
@Override
public boolean messageComplete(long contentLength)
{
return true;
}
@Override
public boolean headerComplete(boolean hasBody, boolean persistent)
{
if (hasBody)
_content=new ByteArrayOutputStream();
return false;
}
@Override
public boolean earlyEOF()
{
return true;
}
@Override
public boolean content(ByteBuffer ref)
{
try
{
_content.write(BufferUtil.toArray(ref));
}
catch (IOException e)
{
throw new RuntimeIOException(e);
}
return false;
}
@Override
public void badMessage(int status, String reason)
{
throw new RuntimeIOException(reason);
}
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,8 +323,11 @@ public class Server extends HandlerWrapper implements Attributes
if (_connectors!=null)
{
for (int i=_connectors.length;i-->0;)
{
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

@ -198,6 +198,13 @@ public class ServletTester extends AggregateLifeCycle
)+":"+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;