jetty-9 more progress with server tests

This commit is contained in:
Greg Wilkins 2012-05-23 11:49:21 +02:00
parent 294150f485
commit 8b98e3918c
7 changed files with 91 additions and 143 deletions

View File

@ -22,8 +22,8 @@
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.eclipse.jetty.toolchain</groupId>
<artifactId>jetty-test-helper</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

View File

@ -288,7 +288,6 @@ public class HttpFields implements Iterable<HttpFields.Field>
/* -------------------------------------------------------------- */
private final ArrayList<Field> _fields = new ArrayList<Field>(20);
private final StringMap<Field> _names = new StringMap<Field>(true);
private final int _maxCookieVersion;
/* ------------------------------------------------------------ */
/**
@ -296,16 +295,6 @@ public class HttpFields implements Iterable<HttpFields.Field>
*/
public HttpFields()
{
_maxCookieVersion=1;
}
/* ------------------------------------------------------------ */
/**
* Constructor.
*/
public HttpFields(int maxCookieVersion)
{
_maxCookieVersion=maxCookieVersion;
}
@ -820,7 +809,7 @@ public class HttpFields implements Iterable<HttpFields.Field>
final boolean isHttpOnly,
int version)
{
String delim=_maxCookieVersion==0?"":__COOKIE_DELIM;
String delim=__COOKIE_DELIM;
// Check arguments
if (name == null || name.length() == 0)
@ -835,23 +824,7 @@ public class HttpFields implements Iterable<HttpFields.Field>
if (value != null && value.length() > 0)
quoted|=QuotedStringTokenizer.quoteIfNeeded(buf, value, delim);
// upgrade to version 1 cookies if quoted.
if (quoted&&version==0 && _maxCookieVersion>=1)
version=1;
if (version>_maxCookieVersion)
version=_maxCookieVersion;
if (version > 0)
{
buf.append(";Version=");
buf.append(version);
if (comment != null && comment.length() > 0)
{
buf.append(";Comment=");
QuotedStringTokenizer.quoteIfNeeded(buf, comment, delim);
}
}
if (path != null && path.length() > 0)
{
buf.append(";Path=");
@ -875,15 +848,8 @@ public class HttpFields implements Iterable<HttpFields.Field>
else
formatCookieDate(buf, System.currentTimeMillis() + 1000L * maxAge);
if (version >0)
{
buf.append(";Max-Age=");
buf.append(maxAge);
}
}
else if (version > 0)
{
buf.append(";Discard");
buf.append(";Max-Age=");
buf.append(maxAge);
}
if (isSecure)
@ -891,6 +857,12 @@ public class HttpFields implements Iterable<HttpFields.Field>
if (isHttpOnly)
buf.append(";HttpOnly");
if (comment != null && comment.length() > 0)
{
buf.append(";Comment=");
QuotedStringTokenizer.quoteIfNeeded(buf, comment, delim);
}
name_value_params = buf.toString();
// remove existing set-cookie of same name

View File

@ -16,6 +16,7 @@ package org.eclipse.jetty.http;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import java.nio.ByteBuffer;
@ -26,7 +27,7 @@ import java.util.Set;
import org.eclipse.jetty.util.BufferUtil;
import org.junit.Assert;
import org.junit.Test;
import org.junit.matchers.JUnitMatchers;
import org.hamcrest.Matchers;
/**
*
@ -111,9 +112,9 @@ public class HttpFieldsTest
buffer.flip();
String out = BufferUtil.toString(buffer);
Assert.assertThat(out,JUnitMatchers.containsString(HttpHeader.CONNECTION+": "+HttpHeaderValue.KEEP_ALIVE));
Assert.assertThat(out,JUnitMatchers.containsString(HttpHeader.TRANSFER_ENCODING+": "+HttpHeaderValue.CHUNKED));
Assert.assertThat(out,JUnitMatchers.containsString(HttpHeader.CONTENT_ENCODING+": "+HttpHeaderValue.GZIP));
Assert.assertThat(out,Matchers.containsString(HttpHeader.CONNECTION+": "+HttpHeaderValue.KEEP_ALIVE));
Assert.assertThat(out,Matchers.containsString(HttpHeader.TRANSFER_ENCODING+": "+HttpHeaderValue.CHUNKED));
Assert.assertThat(out,Matchers.containsString(HttpHeader.CONTENT_ENCODING+": "+HttpHeaderValue.GZIP));
@ -251,10 +252,10 @@ public class HttpFieldsTest
fields.clear();
fields.addSetCookie("everything","wrong","wrong","wrong",0,"to be replaced",true,true,0);
fields.addSetCookie("everything","value","domain","path",0,"comment",true,true,0);
assertEquals("everything=value;Path=path;Domain=domain;Expires=Thu, 01-Jan-1970 00:00:00 GMT;Secure;HttpOnly",fields.getStringField("Set-Cookie"));
assertEquals("everything=value;Path=path;Domain=domain;Expires=Thu, 01-Jan-1970 00:00:00 GMT;Max-Age=0;Secure;HttpOnly;Comment=comment",fields.getStringField("Set-Cookie"));
Enumeration<String> e =fields.getValues("Set-Cookie");
assertTrue(e.hasMoreElements());
assertEquals("everything=value;Path=path;Domain=domain;Expires=Thu, 01-Jan-1970 00:00:00 GMT;Secure;HttpOnly",e.nextElement());
assertEquals("everything=value;Path=path;Domain=domain;Expires=Thu, 01-Jan-1970 00:00:00 GMT;Max-Age=0;Secure;HttpOnly;Comment=comment",e.nextElement());
assertFalse(e.hasMoreElements());
assertEquals("Thu, 01 Jan 1970 00:00:00 GMT",fields.getStringField("Expires"));
@ -262,8 +263,8 @@ public class HttpFieldsTest
fields.clear();
fields.addSetCookie("ev erything","va lue","do main","pa th",1,"co mment",true,true,2);
String setCookie=fields.getStringField("Set-Cookie");
assertTrue(setCookie.startsWith("\"ev erything\"=\"va lue\";Version=1;Comment=\"co mment\";Path=\"pa th\";Domain=\"do main\";Expires="));
assertTrue(setCookie.endsWith("GMT;Max-Age=1;Secure;HttpOnly"));
assertThat(setCookie,Matchers.startsWith("\"ev erything\"=\"va lue\";Path=\"pa th\";Domain=\"do main\";Expires="));
assertThat(setCookie,Matchers.endsWith(" GMT;Max-Age=1;Secure;HttpOnly;Comment=\"co mment\""));
fields.clear();
fields.addSetCookie("name","value",null,null,-1,null,false,false,0);
@ -272,7 +273,6 @@ public class HttpFieldsTest
fields.clear();
fields.addSetCookie("name","v a l u e",null,null,-1,null,false,false,0);
setCookie=fields.getStringField("Set-Cookie");
assertEquals(17,setCookie.indexOf("Version=1"));
fields.clear();
fields.addSetCookie("json","{\"services\":[\"cwa\", \"aa\"]}",null,null,-1,null,false,false,-1);
@ -292,10 +292,10 @@ public class HttpFieldsTest
assertEquals("name=more;Domain=domain",e.nextElement());
assertEquals("foo=bob;Domain=domain",e.nextElement());
fields=new HttpFields(0);
fields=new HttpFields();
fields.addSetCookie("name","value==",null,null,-1,null,false,false,0);
setCookie=fields.getStringField("Set-Cookie");
assertEquals("name=value==",setCookie);
assertEquals("name=\"value==\"",setCookie);
}

View File

@ -13,16 +13,15 @@
package org.eclipse.jetty.http;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.either;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.matchers.JUnitMatchers.containsString;
import static org.junit.matchers.JUnitMatchers.either;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
@ -30,6 +29,7 @@ import java.util.List;
import org.eclipse.jetty.http.HttpGenerator.Action;
import org.eclipse.jetty.http.HttpGenerator.ResponseInfo;
import org.eclipse.jetty.util.BufferUtil;
import org.hamcrest.Matchers;
import org.junit.Test;
/**

View File

@ -109,7 +109,7 @@ public abstract class HttpChannel
_connection = connection;
_uri = new HttpURI(URIUtil.__CHARSET);
_requestFields = new HttpFields();
_responseFields = new HttpFields(server.getMaxCookieVersion());
_responseFields = new HttpFields();
_state = new HttpChannelState(this);
_request = new Request(this);
_response = new Response(this);

View File

@ -44,7 +44,7 @@ import org.junit.Test;
public class AsyncRequestReadTest
{
private static Server server;
private static Connector connector;
private static ChannelHttpConnector connector;
private final static Exchanger<Long> __total=new Exchanger<Long>();
@BeforeClass

View File

@ -5,84 +5,97 @@ import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Timer;
import org.eclipse.jetty.http.HttpFields;
import org.eclipse.jetty.http.HttpGenerator;
import org.eclipse.jetty.http.HttpGenerator.ResponseInfo;
import org.eclipse.jetty.io.AsyncConnection;
import org.eclipse.jetty.io.ByteArrayEndPoint;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.TypeUtil;
import org.eclipse.jetty.util.Utf8StringBuilder;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class HttpWriterTest
{
private HttpWriter _writer;
private ByteArrayBuffer _bytes;
private ByteBuffer _bytes;
@Before
public void init() throws Exception
{
_bytes = new ByteArrayBuffer(2048);
_bytes = BufferUtil.allocate(2048);
Buffers buffers = new SimpleBuffers(new ByteArrayBuffer(1024),new ByteArrayBuffer(1024));
ByteArrayEndPoint endp = new ByteArrayEndPoint();
AbstractGenerator generator = new AbstractGenerator(buffers,endp)
HttpChannel channel = new HttpChannel(null,null,null)
{
@Override
public boolean isRequest()
public HttpConnector getHttpConnector()
{
return false;
return null;
}
@Override
public boolean isResponse()
protected int write(ByteBuffer content) throws IOException
{
return true;
return BufferUtil.append(content,_bytes);
}
@Override
public void completeHeader(HttpFields fields, boolean allContentAdded) throws IOException
protected void commit(ResponseInfo info, ByteBuffer content) throws IOException
{
}
@Override
public int flushBuffer() throws IOException
protected int getContentBufferSize()
{
return 0;
}
@Override
public int prepareUncheckedAddContent() throws IOException
protected void increaseContentBufferSize(int size)
{
return 1024;
}
public void addContent(ByteBuffer content, boolean last) throws IOException
{
_bytes.put(content);
content.clear();
}
public boolean addContent(byte b) throws IOException
{
return false;
}
};
AbstractHttpConnection connection = new AbstractHttpConnection(null,endp,new Server(),null,generator,null)
{
@Override
public AsyncConnection handle() throws IOException
protected void resetBuffer()
{
BufferUtil.clear(_bytes);
}
@Override
protected void flushResponse() throws IOException
{
}
@Override
protected void completeResponse() throws IOException
{
}
@Override
protected void completed()
{
}
@Override
protected void execute(Runnable task)
{
task.run();
}
@Override
public Timer getTimer()
{
return null;
}
};
endp.setMaxIdleTime(60000);
HttpOutput httpOut = new HttpOutput(connection);
HttpOutput httpOut = new HttpOutput(channel);
_writer = new HttpWriter(httpOut);
}
@ -91,7 +104,7 @@ public class HttpWriterTest
{
_writer.setCharacterEncoding(StringUtil.__UTF8);
_writer.write("Now is the time");
assertArrayEquals("Now is the time".getBytes(StringUtil.__UTF8),_bytes.asArray());
assertArrayEquals("Now is the time".getBytes(StringUtil.__UTF8),BufferUtil.toArray(_bytes));
}
@Test
@ -99,7 +112,7 @@ public class HttpWriterTest
{
_writer.setCharacterEncoding(StringUtil.__UTF8);
_writer.write("How now \uFF22rown cow");
assertArrayEquals("How now \uFF22rown cow".getBytes(StringUtil.__UTF8),_bytes.asArray());
assertArrayEquals("How now \uFF22rown cow".getBytes(StringUtil.__UTF8),BufferUtil.toArray(_bytes));
}
@Test
@ -108,12 +121,12 @@ public class HttpWriterTest
_writer.setCharacterEncoding(StringUtil.__UTF8);
String data="xxx\uD801\uDC00xxx";
_writer.write(data);
assertEquals("787878F0909080787878",TypeUtil.toHexString(_bytes.asArray()));
assertArrayEquals(data.getBytes(StringUtil.__UTF8),_bytes.asArray());
assertEquals(3+4+3,_bytes.length());
assertEquals("787878F0909080787878",TypeUtil.toHexString(BufferUtil.toArray(_bytes)));
assertArrayEquals(data.getBytes(StringUtil.__UTF8),BufferUtil.toArray(_bytes));
assertEquals(3+4+3,_bytes.remaining());
Utf8StringBuilder buf = new Utf8StringBuilder();
buf.append(_bytes.asArray(),0,_bytes.length());
buf.append(BufferUtil.toArray(_bytes),0,_bytes.remaining());
assertEquals(data,buf.toString());
}
@ -142,7 +155,7 @@ public class HttpWriterTest
_writer.write(buf, 0, length);
assertEquals(sb.toString(),new String(_bytes.asArray(),StringUtil.__UTF8));
assertEquals(sb.toString(),new String(BufferUtil.toArray(_bytes),StringUtil.__UTF8));
}
@Test
@ -150,46 +163,9 @@ public class HttpWriterTest
{
_writer.setCharacterEncoding(StringUtil.__ISO_8859_1);
_writer.write("How now \uFF22rown cow");
assertEquals("How now ?rown cow",new String(_bytes.asArray(),StringUtil.__ISO_8859_1));
assertEquals("How now ?rown cow",new String(BufferUtil.toArray(_bytes),StringUtil.__ISO_8859_1));
}
@Test
public void testOutput() throws Exception
{
ByteBuffer sb=new ByteArrayBuffer(1500);
ByteBuffer bb=new ByteArrayBuffer(8096);
HttpFields fields = new HttpFields();
ByteArrayEndPoint endp = new ByteArrayEndPoint(new byte[0],4096);
HttpGenerator hb = new HttpGenerator(new SimpleBuffers(sb,bb),endp);
hb.setResponse(200,"OK");
AbstractHttpConnection connection = new AbstractHttpConnection(null,endp,new Server(),null,hb,null)
{
@Override
public AsyncConnection handle() throws IOException
{
return null;
}
};
endp.setMaxIdleTime(10000);
hb.setSendServerVersion(false);
HttpOutput output = new HttpOutput(connection);
HttpWriter writer = new HttpWriter(output);
writer.setCharacterEncoding(StringUtil.__UTF8);
char[] chars = new char[1024];
for (int i=0;i<chars.length;i++)
chars[i]=(char)('0'+(i%10));
chars[0]='\u0553';
writer.write(chars);
hb.completeHeader(fields,true);
hb.flush(10000);
String response = new String(endp.getOutput().asArray(),StringUtil.__UTF8);
assertTrue(response.startsWith("HTTP/1.1 200 OK\r\nContent-Length: 1025\r\n\r\n\u05531234567890"));
}
@Test
public void testUTF16x2() throws Exception
@ -208,10 +184,10 @@ public class HttpWriterTest
myReportBytes(bytes);
myReportBytes(baos.toByteArray());
myReportBytes(_bytes.asArray());
myReportBytes(BufferUtil.toArray(_bytes));
assertArrayEquals(bytes,_bytes.asArray());
assertArrayEquals(baos.toByteArray(),_bytes.asArray());
assertArrayEquals(bytes,BufferUtil.toArray(_bytes));
assertArrayEquals(baos.toByteArray(),BufferUtil.toArray(_bytes));
}
@Test
@ -246,10 +222,10 @@ public class HttpWriterTest
myReportBytes(bytes);
myReportBytes(baos.toByteArray());
myReportBytes(_bytes.asArray());
myReportBytes(BufferUtil.toArray(_bytes));
assertArrayEquals(bytes,_bytes.asArray());
assertArrayEquals(baos.toByteArray(),_bytes.asArray());
assertArrayEquals(bytes,BufferUtil.toArray(_bytes));
assertArrayEquals(baos.toByteArray(),BufferUtil.toArray(_bytes));
}
@Test
@ -284,10 +260,10 @@ public class HttpWriterTest
myReportBytes(bytes);
myReportBytes(baos.toByteArray());
myReportBytes(_bytes.asArray());
myReportBytes(BufferUtil.toArray(_bytes));
assertArrayEquals(bytes,_bytes.asArray());
assertArrayEquals(baos.toByteArray(),_bytes.asArray());
assertArrayEquals(bytes,BufferUtil.toArray(_bytes));
assertArrayEquals(baos.toByteArray(),BufferUtil.toArray(_bytes));
}
private void myReportBytes(byte[] bytes) throws Exception