Fixing InsufficientBytes test cases
+ Because of issues #1045 and #1185 insufficient bytes on a response results in a closed connection
This commit is contained in:
parent
472a40806e
commit
2bd1029d1b
|
@ -118,7 +118,7 @@ public class HttpTester
|
|||
parser.parseNext(ByteBuffer.wrap(contentStream.toByteArray()));
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
public abstract static class Input
|
||||
{
|
||||
final ByteBuffer _buffer;
|
||||
|
@ -216,27 +216,41 @@ public class HttpTester
|
|||
}
|
||||
|
||||
public static Response parseResponse(Input in) throws IOException
|
||||
{
|
||||
return parseResponse(in, false);
|
||||
}
|
||||
|
||||
public static Response parsePartialResponse(Input in) throws IOException
|
||||
{
|
||||
return parseResponse(in, true);
|
||||
}
|
||||
|
||||
private static Response parseResponse(Input in, boolean allowIncomplete) throws IOException
|
||||
{
|
||||
Response r;
|
||||
HttpParser parser=in.takeHttpParser();
|
||||
if (parser==null)
|
||||
{
|
||||
r=new Response();
|
||||
parser =new HttpParser(r);
|
||||
parser = new HttpParser(r);
|
||||
}
|
||||
else
|
||||
r=(Response)parser.getHandler();
|
||||
|
||||
parseResponse(in, parser, r);
|
||||
|
||||
if(r.isComplete())
|
||||
return r;
|
||||
|
||||
in.setHttpParser(parser);
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void parseResponse(Input in, Response response) throws IOException
|
||||
{
|
||||
HttpParser parser = in.takeHttpParser();
|
||||
if (parser == null)
|
||||
{
|
||||
parser = new HttpParser(response);
|
||||
}
|
||||
parseResponse(in, parser, response);
|
||||
|
||||
if (!response.isComplete())
|
||||
in.setHttpParser(parser);
|
||||
}
|
||||
|
||||
private static void parseResponse(Input in, HttpParser parser, Response r) throws IOException
|
||||
{
|
||||
ByteBuffer buffer = in.getBuffer();
|
||||
|
||||
while(true)
|
||||
|
@ -255,18 +269,12 @@ public class HttpTester
|
|||
}
|
||||
}
|
||||
|
||||
if (allowIncomplete || r.isComplete())
|
||||
return r;
|
||||
|
||||
LOG.info("Incomplete Response: (parser={}) {}", parser, r);
|
||||
|
||||
in.setHttpParser(parser);
|
||||
return null;
|
||||
System.out.printf("parseResponse() parser=%s%n%s%n", parser, r.toString());
|
||||
}
|
||||
|
||||
|
||||
public abstract static class Message extends HttpFields implements HttpParser.HttpHandler
|
||||
{
|
||||
boolean _earlyEOF;
|
||||
boolean _complete=false;
|
||||
ByteArrayOutputStream _content;
|
||||
HttpVersion _version=HttpVersion.HTTP_1_0;
|
||||
|
@ -379,8 +387,14 @@ public class HttpTester
|
|||
@Override
|
||||
public void earlyEOF()
|
||||
{
|
||||
_earlyEOF = true;
|
||||
}
|
||||
|
||||
|
||||
public boolean isEarlyEOF()
|
||||
{
|
||||
return _earlyEOF;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean content(ByteBuffer ref)
|
||||
{
|
||||
|
|
|
@ -66,7 +66,7 @@ public abstract class AbstractHttpTest
|
|||
{
|
||||
server = new Server();
|
||||
connector = new ServerConnector(server,null,null,new ArrayByteBufferPool(64,2048,64*1024),1,1,new HttpConnectionFactory());
|
||||
connector.setIdleTimeout(10000);
|
||||
connector.setIdleTimeout(100000);
|
||||
|
||||
server.addConnector(connector);
|
||||
stacklessChannelLogging =new StacklessLogging(HttpChannel.class);
|
||||
|
@ -92,9 +92,12 @@ public abstract class AbstractHttpTest
|
|||
writer.write("\r\n");
|
||||
writer.flush();
|
||||
|
||||
HttpTester.Response response = new HttpTester.Response();
|
||||
HttpTester.Input input = HttpTester.from(socket.getInputStream());
|
||||
HttpTester.Response response = HttpTester.parsePartialResponse(input);
|
||||
HttpTester.parseResponse(input, response);
|
||||
|
||||
if ("HTTP/1.1".equals(httpVersion)
|
||||
&& response.isComplete()
|
||||
&& response.get("content-length") == null
|
||||
&& response.get("transfer-encoding") == null
|
||||
&& !__noBodyCodes.contains(response.getStatus()))
|
||||
|
|
|
@ -21,10 +21,9 @@ package org.eclipse.jetty.server;
|
|||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.EOFException;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
|
@ -426,20 +425,14 @@ public class HttpManyWaysToCommitTest extends AbstractHttpTest
|
|||
{
|
||||
server.setHandler(new SetContentLengthAndWriteInsufficientBytesHandler(true));
|
||||
server.start();
|
||||
try
|
||||
{
|
||||
HttpTester.Response response = executeRequest();
|
||||
assertThat("response code", response.getStatus(), is(200));
|
||||
assertHeader(response, "content-length", "6");
|
||||
byte content[] = response.getContentBytes();
|
||||
assertThat("content bytes", content.length, is(6));
|
||||
String contentStr = new String(content, StandardCharsets.UTF_8);
|
||||
assertThat("content bytes as string", contentStr, is("foo"));
|
||||
}
|
||||
catch(EOFException e)
|
||||
{
|
||||
// possible good response
|
||||
}
|
||||
|
||||
HttpTester.Response response = executeRequest();
|
||||
System.out.println(response.toString());
|
||||
assertThat("response code", response.getStatus(), is(200));
|
||||
assertHeader(response, "content-length", "6");
|
||||
byte content[] = response.getContentBytes();
|
||||
assertThat("content bytes", content.length, is(0));
|
||||
assertTrue("response eof", response.isEarlyEOF());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -448,20 +441,9 @@ public class HttpManyWaysToCommitTest extends AbstractHttpTest
|
|||
server.setHandler(new SetContentLengthAndWriteInsufficientBytesHandler(false));
|
||||
server.start();
|
||||
|
||||
try
|
||||
{
|
||||
HttpTester.Response response = executeRequest();
|
||||
assertThat("response code is 200", response.getStatus(), is(200));
|
||||
assertHeader(response, "content-length", "6");
|
||||
byte content[] = response.getContentBytes();
|
||||
assertThat("content bytes", content.length, is(3));
|
||||
String contentStr = new String(content, StandardCharsets.UTF_8);
|
||||
assertThat("content bytes as string", contentStr, is("foo"));
|
||||
}
|
||||
catch(EOFException e)
|
||||
{
|
||||
// expected
|
||||
}
|
||||
HttpTester.Response response = executeRequest();
|
||||
assertThat("response has no status", response.getStatus(), is(0));
|
||||
assertTrue("response eof", response.isEarlyEOF());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
Loading…
Reference in New Issue