Fix for #308854 (Update test suite to JUnit4 - Module jetty-http).

git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@1752 7e9141cc-0065-0410-87d8-b60c137991c4
This commit is contained in:
Simone Bordet 2010-05-11 15:16:01 +00:00
parent cddf630426
commit 15dc2319d1
8 changed files with 341 additions and 430 deletions

View File

@ -5,6 +5,7 @@ jetty-7.1.1-SNAPSHOT
+ 308860 Update test suite to JUnit4 - Module jetty-rewrite
+ 308850 Update test suite to JUnit4 - Module jetty-annotations
+ 308853 Update test suite to JUnit4 - Module jetty-deploy
+ 308854 Update test suite to JUnit4 - Module jetty-http
jetty-7.1.0 5 May 2010
+ 306353 fixed cross context dispatch to root context.

View File

@ -20,6 +20,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit4-version}</version>
<scope>test</scope>
</dependency>
</dependencies>
@ -61,7 +62,7 @@
</execution>
</executions>
<configuration>
<archive>
<archive>
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
</archive>
</configuration>

View File

@ -4,11 +4,11 @@
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
// The Eclipse Public License is available at
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
// You may elect to redistribute this code under either of these licenses.
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
package org.eclipse.jetty.http;
@ -17,31 +17,34 @@ import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;
import junit.framework.TestCase;
import org.eclipse.jetty.io.Buffer;
import org.eclipse.jetty.io.BufferCache.CachedBuffer;
import org.eclipse.jetty.io.ByteArrayBuffer;
import org.eclipse.jetty.io.View;
import org.eclipse.jetty.io.BufferCache.CachedBuffer;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
/* ------------------------------------------------------------------------------- */
/**
*
*
*/
public class HttpFieldsTest extends TestCase
public class HttpFieldsTest
{
public void testPut()
throws Exception
@Test
public void testPut() throws Exception
{
HttpFields header = new HttpFields();
header.put("name0", "value0");
header.put("name1", "value1");
assertEquals("value0",header.getStringField("name0"));
assertEquals("value1",header.getStringField("name1"));
assertNull(header.getStringField("name2"));
int matches=0;
Enumeration e = header.getFieldNames();
while (e.hasMoreElements())
@ -53,35 +56,34 @@ public class HttpFieldsTest extends TestCase
matches++;
}
assertEquals(2, matches);
matches=0;
e = header.getValues("name0");
assertEquals(true, e.hasMoreElements());
assertEquals(e.nextElement(), "value0");
assertEquals(false, e.hasMoreElements());
}
public void testCRLF()
throws Exception
@Test
public void testCRLF() throws Exception
{
HttpFields header = new HttpFields();
header.put("name0", "value\r\n0");
header.put("name\r\n1", "value1");
header.put("name:2", "value:\r\n2");
ByteArrayBuffer buffer = new ByteArrayBuffer(1024);
header.put(buffer);
assertTrue(buffer.toString().contains("name0: value0"));
assertTrue(buffer.toString().contains("name1: value1"));
assertTrue(buffer.toString().contains("name2: value:2"));
assertTrue(buffer.toString().contains("name2: value:2"));
}
public void testCachedPut()
throws Exception
@Test
public void testCachedPut() throws Exception
{
HttpFields header = new HttpFields();
header.put("Connection", "keep-alive");
assertEquals(HttpHeaderValues.KEEP_ALIVE, header.getStringField(HttpHeaders.CONNECTION));
@ -96,15 +98,13 @@ public class HttpFieldsTest extends TestCase
matches++;
}
assertEquals(1, matches);
}
public void testRePut()
throws Exception
@Test
public void testRePut() throws Exception
{
HttpFields header = new HttpFields();
header.put("name0", "value0");
header.put("name1", "xxxxxx");
header.put("name2", "value2");
@ -112,14 +112,14 @@ public class HttpFieldsTest extends TestCase
assertEquals("value0",header.getStringField("name0"));
assertEquals("xxxxxx",header.getStringField("name1"));
assertEquals("value2",header.getStringField("name2"));
header.put("name1", "value1");
assertEquals("value0",header.getStringField("name0"));
assertEquals("value1",header.getStringField("name1"));
assertEquals("value2",header.getStringField("name2"));
assertNull(header.getStringField("name3"));
int matches=0;
Enumeration e = header.getFieldNames();
while (e.hasMoreElements())
@ -133,19 +133,18 @@ public class HttpFieldsTest extends TestCase
matches++;
}
assertEquals(3, matches);
matches=0;
e = header.getValues("name1");
assertEquals(true, e.hasMoreElements());
assertEquals(e.nextElement(), "value1");
assertEquals(false, e.hasMoreElements());
}
public void testRemovePut()
throws Exception
@Test
public void testRemovePut() throws Exception
{
HttpFields header = new HttpFields();
header.put("name0", "value0");
header.put("name1", "value1");
header.put("name2", "value2");
@ -153,14 +152,14 @@ public class HttpFieldsTest extends TestCase
assertEquals("value0",header.getStringField("name0"));
assertEquals("value1",header.getStringField("name1"));
assertEquals("value2",header.getStringField("name2"));
header.remove("name1");
assertEquals("value0",header.getStringField("name0"));
assertNull(header.getStringField("name1"));
assertEquals("value2",header.getStringField("name2"));
assertNull(header.getStringField("name3"));
int matches=0;
Enumeration e = header.getFieldNames();
while (e.hasMoreElements())
@ -174,18 +173,16 @@ public class HttpFieldsTest extends TestCase
matches++;
}
assertEquals(2, matches);
matches=0;
e = header.getValues("name1");
assertEquals(false, e.hasMoreElements());
}
public void testAdd()
throws Exception
@Test
public void testAdd() throws Exception
{
HttpFields fields = new HttpFields();
fields.add("name0", "value0");
fields.add("name1", "valueA");
fields.add("name2", "value2");
@ -193,14 +190,14 @@ public class HttpFieldsTest extends TestCase
assertEquals("value0",fields.getStringField("name0"));
assertEquals("valueA",fields.getStringField("name1"));
assertEquals("value2",fields.getStringField("name2"));
fields.add("name1", "valueB");
assertEquals("value0",fields.getStringField("name0"));
assertEquals("valueA",fields.getStringField("name1"));
assertEquals("value2",fields.getStringField("name2"));
assertNull(fields.getStringField("name3"));
int matches=0;
Enumeration e = fields.getFieldNames();
while (e.hasMoreElements())
@ -214,8 +211,7 @@ public class HttpFieldsTest extends TestCase
matches++;
}
assertEquals(3, matches);
matches=0;
e = fields.getValues("name1");
assertEquals(true, e.hasMoreElements());
assertEquals(e.nextElement(), "valueA");
@ -223,9 +219,9 @@ public class HttpFieldsTest extends TestCase
assertEquals(e.nextElement(), "valueB");
assertEquals(false, e.hasMoreElements());
}
public void testReuse()
throws Exception
@Test
public void testReuse() throws Exception
{
HttpFields header = new HttpFields();
Buffer n1=new ByteArrayBuffer("name1");
@ -237,21 +233,21 @@ public class HttpFieldsTest extends TestCase
vb.put((byte)'u');
vb.put((byte)'e');
vb.put((byte)'1');
header.put("name0", "value0");
header.put(n1,va);
header.put("name2", "value2");
assertEquals("value0",header.getStringField("name0"));
assertEquals("value1",header.getStringField("name1"));
assertEquals("value2",header.getStringField("name2"));
assertNull(header.getStringField("name3"));
header.remove(n1);
assertNull(header.getStringField("name1"));
header.put(n1,vb);
assertEquals("value1",header.getStringField("name1"));
int matches=0;
Enumeration e = header.getFieldNames();
while (e.hasMoreElements())
@ -265,40 +261,38 @@ public class HttpFieldsTest extends TestCase
matches++;
}
assertEquals(3, matches);
matches=0;
e = header.getValues("name1");
assertEquals(true, e.hasMoreElements());
assertEquals(e.nextElement(), "value1");
assertEquals(false, e.hasMoreElements());
}
public void testDestroy()
throws Exception
@Test
public void testDestroy() throws Exception
{
HttpFields header = new HttpFields();
header.put(new ByteArrayBuffer("name0"), new View(new ByteArrayBuffer("value0")));
assertTrue(header.getFieldNames().hasMoreElements());
assertNotNull(header.getStringField("name0"));
assertNull(header.getStringField("name1"));
header.destroy();
assertNull(header.getStringField("name0"));
}
public void testCase()
throws Exception
@Test
public void testCase() throws Exception
{
HttpFields fields= new HttpFields();
Enumeration e;
Set s;
// 0123456789012345678901234567890
byte[] b ="Message-IDmessage-idvalueVALUE".getBytes();
ByteArrayBuffer buf= new ByteArrayBuffer(512);
buf.put(b);
View headUC= new View.CaseInsensitive(buf);
View headLC= new View.CaseInsensitive(buf);
View valUC = new View(buf);
@ -327,7 +321,7 @@ public class HttpFieldsTest extends TestCase
assertTrue(s.contains("message-id"));
assertEquals("value",fields.getStringField("Message-ID").toLowerCase());
assertEquals("value",fields.getStringField("message-id").toLowerCase());
fields.clear();
fields.add("header","value");
@ -349,19 +343,19 @@ public class HttpFieldsTest extends TestCase
assertTrue(s.contains("message-id"));
assertEquals("value",fields.getStringField("Message-ID").toLowerCase());
assertEquals("value",fields.getStringField("message-id").toLowerCase());
}
public void testHttpHeaderValues()
throws Exception
@Test
public void testHttpHeaderValues() throws Exception
{
assertTrue(((CachedBuffer)HttpHeaderValues.CACHE.lookup("unknown value")).getOrdinal()<0);
assertTrue(((CachedBuffer)HttpHeaderValues.CACHE.lookup("close")).getOrdinal()>=0);
assertTrue(((CachedBuffer)HttpHeaderValues.CACHE.lookup("Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)")).getOrdinal()>=0);
}
public void testSetCookie()
throws Exception
@Test
public void testSetCookie() throws Exception
{
HttpFields fields = new HttpFields();
fields.addSetCookie("minimal","value",null,null,-1,null,false,false,-1);
@ -370,7 +364,7 @@ public class HttpFieldsTest extends TestCase
fields.clear();
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"));
fields.clear();
fields.addSetCookie("ev erything","va lue","do main","pa th",1,"co mment",true,true,2);
assertEquals("\"ev erything\"=\"va lue\";Version=2;Comment=\"co mment\";Path=\"pa th\";Domain=\"do main\";Max-Age=1;Secure;HttpOnly",fields.getStringField("Set-Cookie"));
@ -388,22 +382,22 @@ public class HttpFieldsTest extends TestCase
fields.addSetCookie("foo","bar","domain",null,-1,null,false,false,-1);
fields.addSetCookie("foo","bob","domain",null,-1,null,false,false,-1);
assertEquals("name=more;Domain=domain",fields.getStringField("Set-Cookie"));
Enumeration e=fields.getValues("Set-Cookie");
assertEquals("name=more;Domain=domain",e.nextElement());
assertEquals("foo=bob;Domain=domain",e.nextElement());
}
private Set enum2set(Enumeration e)
private Set<String> enum2set(Enumeration<String> e)
{
HashSet s=new HashSet();
Set<String> s=new HashSet<String>();
while(e.hasMoreElements())
s.add(e.nextElement().toString().toLowerCase());
s.add(e.nextElement().toLowerCase());
return s;
}
public void testDateFields()
throws Exception
@Test
public void testDateFields() throws Exception
{
HttpFields fields = new HttpFields();
@ -427,7 +421,7 @@ public class HttpFieldsTest extends TestCase
assertEquals(d2,d3);
assertEquals(d3+2000,d4);
assertEquals(951825600000L,d5);
d1 = fields.getDateField("D1");
d2 = fields.getDateField("D2");
d3 = fields.getDateField("D3");
@ -439,28 +433,28 @@ public class HttpFieldsTest extends TestCase
assertEquals(d2,d3);
assertEquals(d3+2000,d4);
assertEquals(951825600000L,d5);
fields.putDateField("D2",d1);
assertEquals("Fri, 31 Dec 1999 23:59:59 GMT",fields.getStringField("D2"));
}
public void testLongFields()
throws Exception
@Test
public void testLongFields() throws Exception
{
HttpFields header = new HttpFields();
header.put("I1", "42");
header.put("I2", " 43 99");
header.put("I3", "-44;");
header.put("I4", " - 45abc");
header.put("N1", " - ");
header.put("N2", "xx");
long i1=header.getLongField("I1");
long i2=header.getLongField("I2");
long i3=header.getLongField("I3");
long i4=header.getLongField("I4");
try{
header.getLongField("N1");
assertTrue(false);
@ -469,7 +463,7 @@ public class HttpFieldsTest extends TestCase
{
assertTrue(true);
}
try{
header.getLongField("N2");
assertTrue(false);
@ -478,24 +472,24 @@ public class HttpFieldsTest extends TestCase
{
assertTrue(true);
}
assertEquals(42,i1);
assertEquals(43,i2);
assertEquals(-44,i3);
assertEquals(-45,i4);
header.putLongField("I5", 46);
header.putLongField("I6",-47);
assertEquals("46",header.getStringField("I5"));
assertEquals("-47",header.getStringField("I6"));
}
public void testToString()
throws Exception
@Test
public void testToString() throws Exception
{
HttpFields header = new HttpFields();
header.put(new ByteArrayBuffer("name0"), new View(new ByteArrayBuffer("value0")));
header.put(new ByteArrayBuffer("name1"), new View(new ByteArrayBuffer("value1".getBytes())));
String s1=header.toString();

View File

@ -4,119 +4,116 @@
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
// The Eclipse Public License is available at
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
// You may elect to redistribute this code under either of these licenses.
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
package org.eclipse.jetty.http;
import java.io.IOException;
import junit.framework.TestCase;
import org.eclipse.jetty.io.Buffer;
import org.eclipse.jetty.io.ByteArrayBuffer;
import org.eclipse.jetty.io.ByteArrayEndPoint;
import org.eclipse.jetty.io.SimpleBuffers;
import org.eclipse.jetty.io.View;
import org.junit.Test;
public class HttpGeneratorClientTest extends TestCase
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class HttpGeneratorClientTest
{
public final static String CONTENT="The quick brown fox jumped over the lazy dog.\nNow is the time for all good men to come to the aid of the party\nThe moon is blue to a fish in love.\n";
public final static String[] connect={null,"keep-alive","close"};
public HttpGeneratorClientTest(String arg0)
{
super(arg0);
}
public void testContentLength()
throws Exception
@Test
public void testContentLength() throws Exception
{
Buffer bb=new ByteArrayBuffer(8096);
Buffer sb=new ByteArrayBuffer(1500);
ByteArrayEndPoint endp = new ByteArrayEndPoint(new byte[0],4096);
HttpGenerator generator = new HttpGenerator(new SimpleBuffers(sb,bb),endp);
generator.setRequest("GET","/usr");
HttpFields fields = new HttpFields();
fields.add("Header","Value");
fields.add("Content-Type","text/plain");
String content = "The quick brown fox jumped over the lazy dog";
fields.addLongField("Content-Length",content.length());
generator.completeHeader(fields,false);
generator.addContent(new ByteArrayBuffer(content),true);
generator.flushBuffer();
generator.complete();
generator.flushBuffer();
String result=endp.getOut().toString().replace("\r\n","|").replace('\r','|').replace('\n','|');
assertEquals("GET /usr HTTP/1.1|Header: Value|Content-Type: text/plain|Content-Length: 44||"+content,result);
}
public void testAutoContentLength()
throws Exception
@Test
public void testAutoContentLength() throws Exception
{
Buffer bb=new ByteArrayBuffer(8096);
Buffer sb=new ByteArrayBuffer(1500);
ByteArrayEndPoint endp = new ByteArrayEndPoint(new byte[0],4096);
HttpGenerator generator = new HttpGenerator(new SimpleBuffers(sb,bb),endp);
generator.setRequest("GET","/usr");
HttpFields fields = new HttpFields();
fields.add("Header","Value");
fields.add("Content-Type","text/plain");
String content = "The quick brown fox jumped over the lazy dog";
generator.addContent(new ByteArrayBuffer(content),true);
generator.completeHeader(fields,true);
generator.flushBuffer();
generator.complete();
generator.flushBuffer();
String result=endp.getOut().toString().replace("\r\n","|").replace('\r','|').replace('\n','|');
assertEquals("GET /usr HTTP/1.1|Header: Value|Content-Type: text/plain|Content-Length: 44||"+content,result);
}
public void testChunked()
throws Exception
@Test
public void testChunked() throws Exception
{
Buffer bb=new ByteArrayBuffer(8096);
Buffer sb=new ByteArrayBuffer(1500);
ByteArrayEndPoint endp = new ByteArrayEndPoint(new byte[0],4096);
HttpGenerator generator = new HttpGenerator(new SimpleBuffers(sb,bb),endp);
generator.setRequest("GET","/usr");
HttpFields fields = new HttpFields();
fields.add("Header","Value");
fields.add("Content-Type","text/plain");
String content = "The quick brown fox jumped over the lazy dog";
generator.completeHeader(fields,false);
generator.addContent(new ByteArrayBuffer(content),false);
generator.flushBuffer();
generator.complete();
generator.flushBuffer();
String result=endp.getOut().toString().replace("\r\n","|").replace('\r','|').replace('\n','|');
assertEquals("GET /usr HTTP/1.1|Header: Value|Content-Type: text/plain|Transfer-Encoding: chunked||2C|"+content+"|0||",result);
}
public void testHTTP()
throws Exception
@Test
public void testHTTP() throws Exception
{
Buffer bb=new ByteArrayBuffer(8096);
Buffer sb=new ByteArrayBuffer(1500);
@ -125,7 +122,7 @@ public class HttpGeneratorClientTest extends TestCase
HttpGenerator hb = new HttpGenerator(new SimpleBuffers(sb,bb),endp);
Handler handler = new Handler();
HttpParser parser=null;
// For HTTP version
for (int v=9;v<=11;v++)
{
@ -140,13 +137,13 @@ public class HttpGeneratorClientTest extends TestCase
{
String t="v="+v+",r="+r+",chunks="+chunks+",c="+c+",tr="+tr[r];
// System.err.println(t);
hb.reset(true);
endp.reset();
fields.clear();
// System.out.println("TEST: "+t);
try
{
tr[r].build(v,hb,connect[c],null,chunks, fields);
@ -160,15 +157,15 @@ public class HttpGeneratorClientTest extends TestCase
}
String request=endp.getOut().toString();
// System.out.println(request+(hb.isPersistent()?"...\n":"---\n"));
assertTrue(t,hb.isPersistent());
if (v==9)
{
assertEquals(t,"GET /context/path/info\r\n", request);
continue;
}
parser=new HttpParser(new ByteArrayBuffer(request.getBytes()), handler);
try
{
@ -180,14 +177,14 @@ public class HttpGeneratorClientTest extends TestCase
throw e;
continue;
}
if (tr[r].body!=null)
assertEquals(t,tr[r].body, this.content);
if (v==10)
assertTrue(t,hb.isPersistent() || tr[r].values[1]==null || c==2 || c==0);
else
assertTrue(t,hb.isPersistent() || c==2);
assertTrue(t,tr[r].values[1]==null || content.length()==Integer.parseInt(tr[r].values[1]));
}
}
@ -195,23 +192,21 @@ public class HttpGeneratorClientTest extends TestCase
}
}
static final String[] headers= { "Content-Type","Content-Length","Connection","Transfer-Encoding","Other"};
class TR
private static final String[] headers= { "Content-Type","Content-Length","Connection","Transfer-Encoding","Other"};
private class TR
{
String[] values=new String[headers.length];
String body;
TR(String ct, String cl ,String content)
private String[] values=new String[headers.length];
private String body;
private TR(String ct, String cl ,String content)
{
values[0]=ct;
values[1]=cl;
values[4]="value";
this.body=content;
}
void build(int version,HttpGenerator hb, String connection, String te, int chunks, HttpFields fields)
private void build(int version,HttpGenerator hb, String connection, String te, int chunks, HttpFields fields)
throws Exception
{
values[2]=connection;
@ -219,14 +214,14 @@ public class HttpGeneratorClientTest extends TestCase
hb.setRequest(HttpMethods.GET,"/context/path/info");
hb.setVersion(version);
for (int i=0;i<headers.length;i++)
{
if (values[i]==null)
if (values[i]==null)
continue;
fields.put(new ByteArrayBuffer(headers[i]),new ByteArrayBuffer(values[i]));
}
if (body!=null)
{
int inc=1+body.length()/chunks;
@ -262,15 +257,15 @@ public class HttpGeneratorClientTest extends TestCase
}
hb.complete();
}
@Override
public String toString()
{
return "["+values[0]+","+values[1]+","+(body==null?"none":"_content")+"]";
}
}
private TR[] tr =
private final TR[] tr =
{
/* 0 */ new TR(null,null,null),
/* 1 */ new TR(null,null,CONTENT),
@ -279,20 +274,20 @@ public class HttpGeneratorClientTest extends TestCase
/* 5 */ new TR("text/html",null,CONTENT),
/* 7 */ new TR("text/html",""+CONTENT.length(),CONTENT),
};
String content;
String f0;
String f1;
String f2;
String[] hdr;
String[] val;
int h;
class Handler extends HttpParser.EventHandler
{
int index=0;
private String content;
private String f0;
private String f1;
private String f2;
private String[] hdr;
private String[] val;
private int h;
private class Handler extends HttpParser.EventHandler
{
private int index=0;
@Override
public void content(Buffer ref)
{
@ -302,7 +297,6 @@ public class HttpGeneratorClientTest extends TestCase
index+=ref.length();
}
@Override
public void startRequest(Buffer tok0, Buffer tok1, Buffer tok2)
{
@ -319,7 +313,6 @@ public class HttpGeneratorClientTest extends TestCase
// System.out.println(f0+" "+f1+" "+f2);
}
/* (non-Javadoc)
* @see org.eclipse.jetty.EventHandler#startResponse(org.eclipse.io.Buffer, int, org.eclipse.io.Buffer)
*/
@ -355,8 +348,5 @@ public class HttpGeneratorClientTest extends TestCase
public void messageComplete(long contentLength)
{
}
}
}

View File

@ -4,66 +4,62 @@
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
// The Eclipse Public License is available at
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
// You may elect to redistribute this code under either of these licenses.
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
package org.eclipse.jetty.http;
import java.io.IOException;
import junit.framework.TestCase;
import org.eclipse.jetty.io.Buffer;
import org.eclipse.jetty.io.ByteArrayBuffer;
import org.eclipse.jetty.io.ByteArrayEndPoint;
import org.eclipse.jetty.io.SimpleBuffers;
import org.eclipse.jetty.io.View;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
*
*
*
* To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
public class HttpGeneratorTest extends TestCase
public class HttpGeneratorTest
{
public final static String CONTENT="The quick brown fox jumped over the lazy dog.\nNow is the time for all good men to come to the aid of the party\nThe moon is blue to a fish in love.\n";
public final static String[] connect={null,"keep-alive","close","TE, close"};
public HttpGeneratorTest(String arg0)
{
super(arg0);
}
public void testRequest()
throws Exception
@Test
public void testRequest() throws Exception
{
Buffer bb=new ByteArrayBuffer(8096);
Buffer sb=new ByteArrayBuffer(1500);
HttpFields fields = new HttpFields();
ByteArrayEndPoint endp = new ByteArrayEndPoint(new byte[0],4096);
HttpGenerator hg = new HttpGenerator(new SimpleBuffers(sb,bb),endp);
fields.add("Host","something");
fields.add("User-Agent","test");
hg.setRequest("GET","/index.html");
hg.setVersion(11);
hg.completeHeader(fields,true);
hg.complete();
assertTrue(endp.getOut().toString().indexOf("GET /index.html HTTP/1.1")==0);
assertTrue(endp.getOut().toString().indexOf("Content-Length")==-1);
}
public void testHTTP()
throws Exception
@Test
public void testHTTP() throws Exception
{
Buffer bb=new ByteArrayBuffer(8096);
Buffer sb=new ByteArrayBuffer(1500);
@ -72,7 +68,7 @@ public class HttpGeneratorTest extends TestCase
HttpGenerator hb = new HttpGenerator(new SimpleBuffers(sb,bb),endp);
Handler handler = new Handler();
HttpParser parser=null;
// For HTTP version
for (int v=9;v<=11;v++)
{
@ -85,18 +81,18 @@ public class HttpGeneratorTest extends TestCase
// For none, keep-alive, close
for (int c=0;c<(v==11?connect.length:(connect.length-1));c++)
{
String t="v="+v+",r="+r+",chunks="+chunks+",connect="+connect[c]+",tr="+tr[r];
// System.err.println(t);
hb.reset(true);
endp.reset();
fields.clear();
tr[r].build(v,hb,"OK\r\nTest",connect[c],null,chunks, fields);
String response=endp.getOut().toString();
// System.out.println("RESPONSE: "+t+"\n"+response+(hb.isPersistent()?"...\n":"---\n"));
if (v==9)
{
assertFalse(t,hb.isPersistent());
@ -104,7 +100,7 @@ public class HttpGeneratorTest extends TestCase
assertEquals(t,tr[r].body, response);
continue;
}
parser=new HttpParser(new ByteArrayBuffer(response.getBytes()), handler);
try
{
@ -116,17 +112,17 @@ public class HttpGeneratorTest extends TestCase
throw e;
continue;
}
if (tr[r].body!=null)
assertEquals(t,tr[r].body, this.content);
if (v==10)
assertTrue(t,hb.isPersistent() || tr[r].values[1]==null || c==2 || c==0);
else
assertTrue(t,hb.isPersistent() || c==2 || c==3);
if (v>9)
assertEquals("OK Test",f2);
assertTrue(t,tr[r].values[1]==null || content.length()==Integer.parseInt(tr[r].values[1]));
}
}
@ -134,16 +130,14 @@ public class HttpGeneratorTest extends TestCase
}
}
static final String[] headers= { "Content-Type","Content-Length","Connection","Transfer-Encoding","Other"};
class TR
private static final String[] headers= { "Content-Type","Content-Length","Connection","Transfer-Encoding","Other"};
private class TR
{
int code;
String[] values=new String[headers.length];
String body;
TR(int code,String ct, String cl ,String content)
private int code;
private String[] values=new String[headers.length];
private String body;
private TR(int code,String ct, String cl ,String content)
{
this.code=code;
values[0]=ct;
@ -151,22 +145,21 @@ public class HttpGeneratorTest extends TestCase
values[4]="value";
this.body=content;
}
void build(int version,HttpGenerator hb,String reason, String connection, String te, int chunks, HttpFields fields)
throws Exception
private void build(int version,HttpGenerator hb,String reason, String connection, String te, int chunks, HttpFields fields) throws Exception
{
values[2]=connection;
values[3]=te;
hb.setVersion(version);
hb.setResponse(code,reason);
for (int i=0;i<headers.length;i++)
{
if (values[i]==null)
if (values[i]==null)
continue;
fields.put(new ByteArrayBuffer(headers[i]),new ByteArrayBuffer(values[i]));
}
if (body!=null)
{
int inc=1+body.length()/chunks;
@ -198,15 +191,15 @@ public class HttpGeneratorTest extends TestCase
}
hb.complete();
}
@Override
public String toString()
{
return "["+code+","+values[0]+","+values[1]+","+(body==null?"none":"_content")+"]";
}
}
private TR[] tr =
private final TR[] tr =
{
/* 0 */ new TR(200,null,null,null),
/* 1 */ new TR(200,null,null,CONTENT),
@ -217,19 +210,19 @@ public class HttpGeneratorTest extends TestCase
/* 6 */ new TR(200,"text/html",""+CONTENT.length(),null),
/* 7 */ new TR(200,"text/html",""+CONTENT.length(),CONTENT),
};
String content;
String f0;
String f1;
String f2;
String[] hdr;
String[] val;
int h;
class Handler extends HttpParser.EventHandler
{
int index=0;
private String content;
private String f0;
private String f1;
private String f2;
private String[] hdr;
private String[] val;
private int h;
private class Handler extends HttpParser.EventHandler
{
private int index=0;
@Override
public void content(Buffer ref)
{
@ -239,7 +232,6 @@ public class HttpGeneratorTest extends TestCase
index+=ref.length();
}
@Override
public void startRequest(Buffer tok0, Buffer tok1, Buffer tok2)
{
@ -256,7 +248,6 @@ public class HttpGeneratorTest extends TestCase
// System.out.println(f0+" "+f1+" "+f2);
}
/* (non-Javadoc)
* @see org.eclipse.jetty.EventHandler#startResponse(org.eclipse.io.Buffer, int, org.eclipse.io.Buffer)
*/
@ -292,8 +283,5 @@ public class HttpGeneratorTest extends TestCase
public void messageComplete(long contentLength)
{
}
}
}

View File

@ -4,67 +4,34 @@
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
// The Eclipse Public License is available at
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
// You may elect to redistribute this code under either of these licenses.
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
package org.eclipse.jetty.http;
import java.io.UnsupportedEncodingException;
import junit.framework.TestCase;
import org.eclipse.jetty.io.Buffer;
import org.eclipse.jetty.io.ByteArrayBuffer;
import org.eclipse.jetty.io.SimpleBuffers;
import org.eclipse.jetty.io.bio.StringEndPoint;
import org.eclipse.jetty.util.StringUtil;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
*
*
* To change this generated comment edit the template variable "typecomment":
* Window>Preferences>Java>Templates.
* To enable and disable the creation of type comments go to
* Window>Preferences>Java>Code Generation.
*/
public class HttpParserTest extends TestCase
public class HttpParserTest
{
/**
* Constructor for HttpParserTest.
* @param arg0
*/
public HttpParserTest(String arg0)
{
super(arg0);
}
public static void main(String[] args)
{
junit.textui.TestRunner.run(HttpParserTest.class);
}
/**
* @see TestCase#setUp()
*/
protected void setUp() throws Exception
{
super.setUp();
}
/**
* @see TestCase#tearDown()
*/
protected void tearDown() throws Exception
{
super.tearDown();
}
public void testLineParse0()
throws Exception
@Test
public void testLineParse0() throws Exception
{
StringEndPoint io=new StringEndPoint();
io.setInput("POST /foo HTTP/1.0\015\012" + "\015\012");
@ -80,8 +47,8 @@ public class HttpParserTest extends TestCase
assertEquals(-1, h);
}
public void testLineParse1()
throws Exception
@Test
public void testLineParse1() throws Exception
{
StringEndPoint io=new StringEndPoint();
io.setInput("GET /999\015\012");
@ -98,8 +65,8 @@ public class HttpParserTest extends TestCase
assertEquals(-1, h);
}
public void testLineParse2()
throws Exception
@Test
public void testLineParse2() throws Exception
{
StringEndPoint io=new StringEndPoint();
io.setInput("POST /222 \015\012");
@ -116,8 +83,8 @@ public class HttpParserTest extends TestCase
assertEquals(-1, h);
}
public void testLineParse3()
throws Exception
@Test
public void testLineParse3() throws Exception
{
StringEndPoint io=new StringEndPoint();
io.setInput("POST /fo\u0690 HTTP/1.0\015\012" + "\015\012");
@ -133,8 +100,8 @@ public class HttpParserTest extends TestCase
assertEquals(-1, h);
}
public void testLineParse4()
throws Exception
@Test
public void testLineParse4() throws Exception
{
StringEndPoint io=new StringEndPoint();
io.setInput("POST /foo?param=\u0690 HTTP/1.0\015\012" + "\015\012");
@ -150,8 +117,8 @@ public class HttpParserTest extends TestCase
assertEquals(-1, h);
}
public void testConnect()
throws Exception
@Test
public void testConnect() throws Exception
{
StringEndPoint io=new StringEndPoint();
io.setInput("CONNECT 192.168.1.2:80 HTTP/1.1\015\012" + "\015\012");
@ -168,8 +135,8 @@ public class HttpParserTest extends TestCase
assertEquals(-1, h);
}
public void testHeaderParse()
throws Exception
@Test
public void testHeaderParse() throws Exception
{
StringEndPoint io=new StringEndPoint();
io.setInput(
@ -207,14 +174,14 @@ public class HttpParserTest extends TestCase
assertEquals(5, h);
}
public void testChunkParse()
throws Exception
@Test
public void testChunkParse() throws Exception
{
StringEndPoint io=new StringEndPoint();
io.setInput(
"GET /chunk HTTP/1.0\015\012"
+ "Header1: value1\015\012"
+ "Transfer-Encoding: chunked\015\012"
+ "Transfer-Encoding: chunked\015\012"
+ "\015\012"
+ "a;\015\012"
+ "0123456789\015\012"
@ -237,14 +204,14 @@ public class HttpParserTest extends TestCase
assertEquals("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", _content);
}
public void testMultiParse()
throws Exception
@Test
public void testMultiParse() throws Exception
{
StringEndPoint io=new StringEndPoint();
io.setInput(
"GET /mp HTTP/1.0\015\012"
+ "Header1: value1\015\012"
+ "Transfer-Encoding: chunked\015\012"
+ "Transfer-Encoding: chunked\015\012"
+ "\015\012"
+ "a;\015\012"
+ "0123456789\015\012"
@ -253,11 +220,11 @@ public class HttpParserTest extends TestCase
+ "0\015\012"
+ "POST /foo HTTP/1.0\015\012"
+ "Header2: value2\015\012"
+ "Content-Length: 0\015\012"
+ "Content-Length: 0\015\012"
+ "\015\012"
+ "PUT /doodle HTTP/1.0\015\012"
+ "Header3: value3\015\012"
+ "Content-Length: 10\015\012"
+ "Content-Length: 10\015\012"
+ "\015\012"
+ "0123456789\015\012");
@ -293,15 +260,15 @@ public class HttpParserTest extends TestCase
assertEquals("Header3", hdr[0]);
assertEquals("value3", val[0]);
assertEquals("0123456789", _content);
}
@Test
public void testStreamParse() throws Exception
{
StringEndPoint io=new StringEndPoint();
String http="GET / HTTP/1.0\015\012"
+ "Header1: value1\015\012"
+ "Transfer-Encoding: chunked\015\012"
+ "Transfer-Encoding: chunked\015\012"
+ "\015\012"
+ "a;\015\012"
+ "0123456789\015\012"
@ -314,11 +281,10 @@ public class HttpParserTest extends TestCase
+ "\015\012"
+ "PUT /doodle HTTP/1.0\015\012"
+ "Header3: value3\015\012"
+ "Content-Length: 10\015\012"
+ "Content-Length: 10\015\012"
+ "\015\012"
+ "0123456789\015\012";
int[] tests=
{
1024,
@ -333,7 +299,7 @@ public class HttpParserTest extends TestCase
64,
32
};
for (int t= 0; t < tests.length; t++)
{
String tst="t"+tests[t];
@ -345,10 +311,9 @@ public class HttpParserTest extends TestCase
Handler handler = new Handler();
HttpParser parser= new HttpParser(buffers,io, handler);
io.setInput(http);
parser.parse();
assertEquals(tst,"GET", f0);
assertEquals(tst,"/", f1);
@ -357,7 +322,7 @@ public class HttpParserTest extends TestCase
assertEquals(tst,"Header1", hdr[0]);
assertEquals(tst,"value1", val[0]);
assertEquals(tst,"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", _content);
parser.parse();
assertEquals(tst,"POST", f0);
assertEquals(tst,"/foo", f1);
@ -366,7 +331,7 @@ public class HttpParserTest extends TestCase
assertEquals(tst,"Header2", hdr[0]);
assertEquals(tst,"value2", val[0]);
assertEquals(tst,null, _content);
parser.parse();
assertEquals(tst,"PUT", f0);
assertEquals(tst,"/doodle", f1);
@ -385,16 +350,16 @@ public class HttpParserTest extends TestCase
}
}
public void testResponseParse0()
throws Exception
@Test
public void testResponseParse0() throws Exception
{
StringEndPoint io=new StringEndPoint();
io.setInput(
"HTTP/1.1 200 Correct\015\012"
+ "Content-Length: 10\015\012"
+ "Content-Type: text/plain\015\012"
+ "\015\012"
+ "0123456789\015\012");
"HTTP/1.1 200 Correct\015\012"
+ "Content-Length: 10\015\012"
+ "Content-Type: text/plain\015\012"
+ "\015\012"
+ "0123456789\015\012");
ByteArrayBuffer buffer= new ByteArrayBuffer(4096);
SimpleBuffers buffers=new SimpleBuffers(buffer,null);
@ -404,19 +369,19 @@ public class HttpParserTest extends TestCase
assertEquals("HTTP/1.1", f0);
assertEquals("200", f1);
assertEquals("Correct", f2);
assertEquals(_content.length(), 10);
assertTrue(headerCompleted);
assertTrue(messageCompleted);
assertEquals(_content.length(), 10);
assertTrue(headerCompleted);
assertTrue(messageCompleted);
}
public void testResponseParse1()
throws Exception
@Test
public void testResponseParse1() throws Exception
{
StringEndPoint io=new StringEndPoint();
io.setInput(
"HTTP/1.1 304 Not-Modified\015\012"
+ "Connection: close\015\012"
+ "\015\012");
"HTTP/1.1 304 Not-Modified\015\012"
+ "Connection: close\015\012"
+ "\015\012");
ByteArrayBuffer buffer= new ByteArrayBuffer(4096);
SimpleBuffers buffers=new SimpleBuffers(buffer,null);
@ -426,23 +391,23 @@ public class HttpParserTest extends TestCase
assertEquals("HTTP/1.1", f0);
assertEquals("304", f1);
assertEquals("Not-Modified", f2);
assertTrue(headerCompleted);
assertTrue(messageCompleted);
assertTrue(headerCompleted);
assertTrue(messageCompleted);
}
public void testResponseParse2()
throws Exception
@Test
public void testResponseParse2() throws Exception
{
StringEndPoint io=new StringEndPoint();
io.setInput(
"HTTP/1.1 204 No-Content\015\012"
+ "Connection: close\015\012"
+ "\015\012"
+ "HTTP/1.1 200 Correct\015\012"
+ "Content-Length: 10\015\012"
+ "Content-Type: text/plain\015\012"
+ "\015\012"
+ "0123456789\015\012");
"HTTP/1.1 204 No-Content\015\012"
+ "Connection: close\015\012"
+ "\015\012"
+ "HTTP/1.1 200 Correct\015\012"
+ "Content-Length: 10\015\012"
+ "Content-Type: text/plain\015\012"
+ "\015\012"
+ "0123456789\015\012");
ByteArrayBuffer buffer= new ByteArrayBuffer(4096);
SimpleBuffers buffers=new SimpleBuffers(buffer,null);
@ -452,34 +417,34 @@ public class HttpParserTest extends TestCase
assertEquals("HTTP/1.1", f0);
assertEquals("204", f1);
assertEquals("No-Content", f2);
assertTrue(headerCompleted);
assertTrue(messageCompleted);
assertTrue(headerCompleted);
assertTrue(messageCompleted);
parser.parse();
assertEquals("HTTP/1.1", f0);
assertEquals("200", f1);
assertEquals("Correct", f2);
assertEquals(_content.length(), 10);
assertTrue(headerCompleted);
assertTrue(messageCompleted);
assertEquals(_content.length(), 10);
assertTrue(headerCompleted);
assertTrue(messageCompleted);
}
String _content;
String f0;
String f1;
String f2;
String[] hdr;
String[] val;
int h;
boolean headerCompleted;
boolean messageCompleted;
private String _content;
private String f0;
private String f1;
private String f2;
private String[] hdr;
private String[] val;
private int h;
private boolean headerCompleted;
private boolean messageCompleted;
private class Handler extends HttpParser.EventHandler
{
private HttpFields fields;
private boolean request;
class Handler extends HttpParser.EventHandler
{
HttpFields fields;
boolean request;
public void content(Buffer ref)
{
if (_content==null)
@ -487,7 +452,6 @@ public class HttpParserTest extends TestCase
_content= _content + ref;
}
public void startRequest(Buffer tok0, Buffer tok1, Buffer tok2)
{
try
@ -507,12 +471,11 @@ public class HttpParserTest extends TestCase
}
catch (UnsupportedEncodingException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException(e);
}
messageCompleted = false;
headerCompleted = false;
messageCompleted = false;
headerCompleted = false;
}
public void parsedHeader(Buffer name, Buffer value)
@ -533,28 +496,27 @@ public class HttpParserTest extends TestCase
throw new IllegalStateException();
}
headerCompleted = true;
headerCompleted = true;
}
public void messageComplete(long contentLength)
{
messageCompleted = true;
messageCompleted = true;
}
public void startResponse(Buffer version, int status, Buffer reason)
{
request=false;
f0 = version.toString();
f1 = Integer.toString(status);
f2 = reason.toString();
f1 = Integer.toString(status);
f2 = reason.toString();
fields=new HttpFields();
hdr= new String[9];
val= new String[9];
messageCompleted = false;
headerCompleted = false;
messageCompleted = false;
headerCompleted = false;
}
}
}

View File

@ -4,19 +4,22 @@
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
// The Eclipse Public License is available at
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
// You may elect to redistribute this code under either of these licenses.
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
package org.eclipse.jetty.http;
import junit.framework.TestCase;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
public class HttpStatusCodeTest extends TestCase
public class HttpStatusCodeTest
{
@Test
public void testInvalidGetCode()
{
assertNull("Invalid code: 800", HttpStatus.getCode(800));

View File

@ -4,53 +4,24 @@
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
// The Eclipse Public License is available at
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
// You may elect to redistribute this code under either of these licenses.
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
package org.eclipse.jetty.http;
import junit.framework.TestCase;
import org.junit.Test;
/* ------------------------------------------------------------ */
/**
* Top level test harness.
*
*
*
*/
public class PathMapTest extends TestCase
{
/**
* Constructor for HttpParserTest.
*
* @param arg0
*/
public PathMapTest(String arg0)
{
super(arg0);
}
/**
* @see TestCase#setUp()
*/
protected void setUp() throws Exception
{
super.setUp();
}
/**
* @see TestCase#tearDown()
*/
protected void tearDown() throws Exception
{
super.tearDown();
}
/* --------------------------------------------------------------- */
@Test
public void testPathMap() throws Exception
{
PathMap p = new PathMap();
@ -65,28 +36,28 @@ public class PathMapTest extends TestCase
p.put("/", "8");
p.put("/XXX:/YYY", "9");
String[][] tests = {
{ "/abs/path", "1"},
{ "/abs/path/xxx", "8"},
{ "/abs/pith", "8"},
{ "/abs/path/longer", "2"},
{ "/abs/path/", "8"},
String[][] tests = {
{ "/abs/path", "1"},
{ "/abs/path/xxx", "8"},
{ "/animal/bird/eagle/bald", "3"},
{ "/abs/pith", "8"},
{ "/abs/path/longer", "2"},
{ "/abs/path/", "8"},
{ "/abs/path/xxx", "8"},
{ "/animal/bird/eagle/bald", "3"},
{ "/animal/fish/shark/grey", "4"},
{ "/animal/insect/bug", "5"},
{ "/animal", "5"},
{ "/animal/insect/bug", "5"},
{ "/animal", "5"},
{ "/animal/", "5"},
{ "/animal/x", "5"},
{ "/animal/*", "5"},
{ "/suffix/path.tar.gz", "6"},
{ "/suffix/path.tar.gz", "6"},
{ "/suffix/path.gz", "7"},
{ "/animal/path.gz", "5"},
{ "/animal/path.gz", "5"},
{ "/Other/path", "8"},};
for (int i = 0; i < tests.length; i++)
for (String[] test : tests)
{
assertEquals(tests[i][0], tests[i][1], p.getMatch(tests[i][0]).getValue());
assertEquals(test[0], test[1], p.getMatch(test[0]).getValue());
}
assertEquals("Get absolute path", "1", p.get("/abs/path"));
@ -158,10 +129,11 @@ public class PathMapTest extends TestCase
/**
* See JIRA issue: JETTY-88.
*/
@Test
public void testPathMappingsOnlyMatchOnDirectoryNames() throws Exception
{
String spec = "/xyz/*";
assertMatch(spec, "/xyz");
assertMatch(spec, "/xyz/");
assertMatch(spec, "/xyz/123");