Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'jetty-server/src/test/java/org/eclipse/jetty/server/HttpWriterTest.java')
-rw-r--r--jetty-server/src/test/java/org/eclipse/jetty/server/HttpWriterTest.java194
1 files changed, 136 insertions, 58 deletions
diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/HttpWriterTest.java b/jetty-server/src/test/java/org/eclipse/jetty/server/HttpWriterTest.java
index 1b30841ec6..5f133b5fd1 100644
--- a/jetty-server/src/test/java/org/eclipse/jetty/server/HttpWriterTest.java
+++ b/jetty-server/src/test/java/org/eclipse/jetty/server/HttpWriterTest.java
@@ -19,13 +19,19 @@
package org.eclipse.jetty.server;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
import java.io.IOException;
-import java.nio.ByteBuffer;
-import org.eclipse.jetty.io.ByteBufferPool;
-import org.eclipse.jetty.io.MappedByteBufferPool;
-import org.eclipse.jetty.util.BufferUtil;
+import org.eclipse.jetty.http.AbstractGenerator;
+import org.eclipse.jetty.http.HttpFields;
+import org.eclipse.jetty.http.HttpGenerator;
+import org.eclipse.jetty.io.Buffer;
+import org.eclipse.jetty.io.Buffers;
+import org.eclipse.jetty.io.ByteArrayBuffer;
+import org.eclipse.jetty.io.ByteArrayEndPoint;
+import org.eclipse.jetty.io.Connection;
+import org.eclipse.jetty.io.SimpleBuffers;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.TypeUtil;
import org.eclipse.jetty.util.Utf8StringBuilder;
@@ -34,77 +40,110 @@ import org.junit.Test;
public class HttpWriterTest
{
- private HttpOutput _httpOut;
- private ByteBuffer _bytes;
+ private HttpWriter _writer;
+ private ByteArrayBuffer _bytes;
@Before
public void init() throws Exception
{
- _bytes = BufferUtil.allocate(2048);
+ _bytes = new ByteArrayBuffer(2048);
- final ByteBufferPool bufferPool = new MappedByteBufferPool();
- HttpChannel<?> channel = new HttpChannel<ByteBuffer>(null,new HttpConfiguration(),null,null,null)
+ Buffers buffers = new SimpleBuffers(new ByteArrayBuffer(1024),new ByteArrayBuffer(1024));
+ ByteArrayEndPoint endp = new ByteArrayEndPoint();
+ AbstractGenerator generator = new AbstractGenerator(buffers,endp)
{
@Override
- public ByteBufferPool getByteBufferPool()
+ public boolean isRequest()
{
- return bufferPool;
+ return false;
}
+
+ @Override
+ public boolean isResponse()
+ {
+ return true;
+ }
+
+ @Override
+ public void completeHeader(HttpFields fields, boolean allContentAdded) throws IOException
+ {
+ }
+
+ @Override
+ public int flushBuffer() throws IOException
+ {
+ return 0;
+ }
+
+ @Override
+ public int prepareUncheckedAddContent() throws IOException
+ {
+ return 1024;
+ }
+
+ public void addContent(Buffer content, boolean last) throws IOException
+ {
+ _bytes.put(content);
+ content.clear();
+ }
+
+ public boolean addContent(byte b) throws IOException
+ {
+ return false;
+ }
+
};
- _httpOut = new HttpOutput(channel)
+ AbstractHttpConnection connection = new AbstractHttpConnection(null,endp,new Server(),null,generator,null)
{
@Override
- public void write(byte[] b, int off, int len) throws IOException
+ public Connection handle() throws IOException
{
- BufferUtil.append(_bytes, b, off, len);
+ return null;
}
};
+ endp.setMaxIdleTime(60000);
+
+ HttpOutput httpOut = new HttpOutput(connection);
+ _writer = new HttpWriter(httpOut);
}
@Test
public void testSimpleUTF8() throws Exception
{
- HttpWriter _writer = new Utf8HttpWriter(_httpOut);
+ _writer.setCharacterEncoding(StringUtil.__UTF8);
_writer.write("Now is the time");
- assertArrayEquals("Now is the time".getBytes(StringUtil.__UTF8),BufferUtil.toArray(_bytes));
+ assertArrayEquals("Now is the time".getBytes(StringUtil.__UTF8),_bytes.asArray());
}
@Test
public void testUTF8() throws Exception
{
- HttpWriter _writer = new Utf8HttpWriter(_httpOut);
+ _writer.setCharacterEncoding(StringUtil.__UTF8);
_writer.write("How now \uFF22rown cow");
- assertArrayEquals("How now \uFF22rown cow".getBytes(StringUtil.__UTF8),BufferUtil.toArray(_bytes));
+ assertArrayEquals("How now \uFF22rown cow".getBytes(StringUtil.__UTF8),_bytes.asArray());
}
-
- @Test
- public void testUTF16() throws Exception
- {
- HttpWriter _writer = new EncodingHttpWriter(_httpOut,StringUtil.__UTF16);
- _writer.write("How now \uFF22rown cow");
- assertArrayEquals("How now \uFF22rown cow".getBytes(StringUtil.__UTF16),BufferUtil.toArray(_bytes));
- }
-
+
@Test
public void testNotCESU8() throws Exception
{
- HttpWriter _writer = new Utf8HttpWriter(_httpOut);
+ _writer.setCharacterEncoding(StringUtil.__UTF8);
String data="xxx\uD801\uDC00xxx";
_writer.write(data);
- assertEquals("787878F0909080787878",TypeUtil.toHexString(BufferUtil.toArray(_bytes)));
- assertArrayEquals(data.getBytes(StringUtil.__UTF8),BufferUtil.toArray(_bytes));
- assertEquals(3+4+3,_bytes.remaining());
-
+ assertEquals("787878F0909080787878",TypeUtil.toHexString(_bytes.asArray()));
+ assertArrayEquals(data.getBytes(StringUtil.__UTF8),_bytes.asArray());
+ assertEquals(3+4+3,_bytes.length());
+
Utf8StringBuilder buf = new Utf8StringBuilder();
- buf.append(BufferUtil.toArray(_bytes),0,_bytes.remaining());
+ buf.append(_bytes.asArray(),0,_bytes.length());
assertEquals(data,buf.toString());
+
}
@Test
public void testMultiByteOverflowUTF8() throws Exception
{
- HttpWriter _writer = new Utf8HttpWriter(_httpOut);
+ _writer.setCharacterEncoding(StringUtil.__UTF8);
final String singleByteStr = "a";
final String multiByteDuplicateStr = "\uFF22";
int remainSize = 1;
@@ -125,21 +164,59 @@ public class HttpWriterTest
_writer.write(buf, 0, length);
- assertEquals(sb.toString(),new String(BufferUtil.toArray(_bytes),StringUtil.__UTF8));
+ assertEquals(sb.toString(),new String(_bytes.asArray(),StringUtil.__UTF8));
}
@Test
public void testISO8859() throws Exception
{
- HttpWriter _writer = new Iso88591HttpWriter(_httpOut);
+ _writer.setCharacterEncoding(StringUtil.__ISO_8859_1);
_writer.write("How now \uFF22rown cow");
- assertEquals("How now ?rown cow",new String(BufferUtil.toArray(_bytes),StringUtil.__ISO_8859_1));
+ assertEquals("How now ?rown cow",new String(_bytes.asArray(),StringUtil.__ISO_8859_1));
+ }
+
+ @Test
+ public void testOutput() throws Exception
+ {
+ Buffer sb=new ByteArrayBuffer(1500);
+ Buffer 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 Connection 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.getOut().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
{
- HttpWriter _writer = new Utf8HttpWriter(_httpOut);
+ _writer.setCharacterEncoding(StringUtil.__UTF8);
String source = "\uD842\uDF9F";
@@ -153,20 +230,20 @@ public class HttpWriterTest
myReportBytes(bytes);
myReportBytes(baos.toByteArray());
- myReportBytes(BufferUtil.toArray(_bytes));
+ myReportBytes(_bytes.asArray());
- assertArrayEquals(bytes,BufferUtil.toArray(_bytes));
- assertArrayEquals(baos.toByteArray(),BufferUtil.toArray(_bytes));
+ assertArrayEquals(bytes,_bytes.asArray());
+ assertArrayEquals(baos.toByteArray(),_bytes.asArray());
}
@Test
public void testMultiByteOverflowUTF16x2() throws Exception
{
- HttpWriter _writer = new Utf8HttpWriter(_httpOut);
+ _writer.setCharacterEncoding(StringUtil.__UTF8);
final String singleByteStr = "a";
int remainSize = 1;
- final String multiByteDuplicateStr = "\uD842\uDF9F";
+ final String multiByteDuplicateStr = "\uD842\uDF9F";
int adjustSize = -1;
StringBuilder sb = new StringBuilder();
@@ -191,21 +268,21 @@ public class HttpWriterTest
myReportBytes(bytes);
myReportBytes(baos.toByteArray());
- myReportBytes(BufferUtil.toArray(_bytes));
+ myReportBytes(_bytes.asArray());
- assertArrayEquals(bytes,BufferUtil.toArray(_bytes));
- assertArrayEquals(baos.toByteArray(),BufferUtil.toArray(_bytes));
+ assertArrayEquals(bytes,_bytes.asArray());
+ assertArrayEquals(baos.toByteArray(),_bytes.asArray());
}
-
+
@Test
public void testMultiByteOverflowUTF16x2_2() throws Exception
{
- HttpWriter _writer = new Utf8HttpWriter(_httpOut);
+ _writer.setCharacterEncoding(StringUtil.__UTF8);
final String singleByteStr = "a";
int remainSize = 1;
- final String multiByteDuplicateStr = "\uD842\uDF9F";
- int adjustSize = -2;
+ final String multiByteDuplicateStr = "\uD842\uDF9F";
+ int adjustSize = -2;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < HttpWriter.MAX_OUTPUT_CHARS + adjustSize; i++)
@@ -229,21 +306,22 @@ public class HttpWriterTest
myReportBytes(bytes);
myReportBytes(baos.toByteArray());
- myReportBytes(BufferUtil.toArray(_bytes));
+ myReportBytes(_bytes.asArray());
- assertArrayEquals(bytes,BufferUtil.toArray(_bytes));
- assertArrayEquals(baos.toByteArray(),BufferUtil.toArray(_bytes));
+ assertArrayEquals(bytes,_bytes.asArray());
+ assertArrayEquals(baos.toByteArray(),_bytes.asArray());
}
private void myReportBytes(byte[] bytes) throws Exception
{
-// for (int i = 0; i < bytes.length; i++)
-// {
-// System.err.format("%s%x",(i == 0)?"[":(i % (HttpWriter.MAX_OUTPUT_CHARS) == 0)?"][":",",bytes[i]);
-// }
-// System.err.format("]->%s\n",new String(bytes,StringUtil.__UTF8));
+ for (int i = 0; i < bytes.length; i++)
+ {
+ // System.err.format("%s%x",(i == 0)?"[":(i % (HttpWriter.MAX_OUTPUT_CHARS) == 0)?"][":",",bytes[i]);
+ }
+ // System.err.format("]->%s\n",new String(bytes,StringUtil.__UTF8));
}
+
private void assertArrayEquals(byte[] b1, byte[] b2)
{
String test=new String(b1)+"=="+new String(b2);

Back to the top