Fix for #308863 (Update test suite to JUnit4 - Module jetty-servlet).
git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@1755 7e9141cc-0065-0410-87d8-b60c137991c4
This commit is contained in:
parent
2a37d805b3
commit
4d72f12fa1
|
@ -8,6 +8,7 @@ jetty-7.1.1-SNAPSHOT
|
|||
+ 308854 Update test suite to JUnit4 - Module jetty-http
|
||||
+ 308859 Update test suite to JUnit4 - Module jetty-policy
|
||||
+ 308858 Update test suite to JUnit4 - Module jetty-plus
|
||||
+ 308863 Update test suite to JUnit4 - Module jetty-servlet
|
||||
|
||||
jetty-7.1.0 5 May 2010
|
||||
+ 306353 fixed cross context dispatch to root context.
|
||||
|
|
|
@ -53,6 +53,7 @@
|
|||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit4-version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
|
|
@ -5,26 +5,28 @@ import java.io.FileOutputStream;
|
|||
import java.io.IOException;
|
||||
|
||||
import junit.framework.AssertionFailedError;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.eclipse.jetty.server.LocalConnector;
|
||||
import org.eclipse.jetty.server.Server;
|
||||
import org.eclipse.jetty.util.IO;
|
||||
import org.eclipse.jetty.util.StringUtil;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class DefaultServletTest extends TestCase
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class DefaultServletTest
|
||||
{
|
||||
private boolean _runningOnWindows;
|
||||
private Server server;
|
||||
private LocalConnector connector;
|
||||
private ServletContextHandler context;
|
||||
|
||||
protected void setUp() throws Exception
|
||||
@Before
|
||||
public void init() throws Exception
|
||||
{
|
||||
_runningOnWindows = System.getProperty( "os.name" ).startsWith( "Windows" );
|
||||
|
||||
super.setUp();
|
||||
|
||||
server = new Server();
|
||||
server.setSendServerVersion(false);
|
||||
|
||||
|
@ -32,7 +34,7 @@ public class DefaultServletTest extends TestCase
|
|||
|
||||
context = new ServletContextHandler();
|
||||
context.setContextPath("/context");
|
||||
context.setWelcomeFiles(new String[] {"index.html","index.jsp","index.htm"});
|
||||
context.setWelcomeFiles(new String[] {"index.html","index.jsp","index.htm"});
|
||||
|
||||
server.setHandler(context);
|
||||
server.addConnector(connector);
|
||||
|
@ -40,16 +42,14 @@ public class DefaultServletTest extends TestCase
|
|||
server.start();
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception
|
||||
@After
|
||||
public void destroy() throws Exception
|
||||
{
|
||||
super.tearDown();
|
||||
|
||||
if (server != null)
|
||||
{
|
||||
server.stop();
|
||||
}
|
||||
server.stop();
|
||||
server.join();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListingWithSession() throws Exception
|
||||
{
|
||||
ServletHolder defholder = context.addServlet(DefaultServlet.class,"/*");
|
||||
|
@ -57,15 +57,15 @@ public class DefaultServletTest extends TestCase
|
|||
defholder.setInitParameter("redirectWelcome","false");
|
||||
defholder.setInitParameter("gzip","false");
|
||||
|
||||
File testDir = new File("target/tests/" + getName());
|
||||
File testDir = new File("target/tests/" + DefaultServletTest.class.getSimpleName());
|
||||
prepareEmptyTestDir(testDir);
|
||||
|
||||
/* create some content in the docroot */
|
||||
File resBase = new File(testDir, "docroot");
|
||||
resBase.mkdirs();
|
||||
new File(resBase, "one").mkdir();
|
||||
new File(resBase, "two").mkdir();
|
||||
new File(resBase, "three").mkdir();
|
||||
assertTrue(resBase.mkdirs());
|
||||
assertTrue(new File(resBase, "one").mkdir());
|
||||
assertTrue(new File(resBase, "two").mkdir());
|
||||
assertTrue(new File(resBase, "three").mkdir());
|
||||
|
||||
String resBasePath = resBase.getAbsolutePath();
|
||||
defholder.setInitParameter("resourceBase",resBasePath);
|
||||
|
@ -84,6 +84,7 @@ public class DefaultServletTest extends TestCase
|
|||
assertResponseNotContains("<script>",response);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListingXSS() throws Exception
|
||||
{
|
||||
ServletHolder defholder = context.addServlet(DefaultServlet.class,"/*");
|
||||
|
@ -91,15 +92,15 @@ public class DefaultServletTest extends TestCase
|
|||
defholder.setInitParameter("redirectWelcome","false");
|
||||
defholder.setInitParameter("gzip","false");
|
||||
|
||||
File testDir = new File("target/tests/" + getName());
|
||||
File testDir = new File("target/tests/" + DefaultServletTest.class.getSimpleName());
|
||||
prepareEmptyTestDir(testDir);
|
||||
|
||||
/* create some content in the docroot */
|
||||
File resBase = new File(testDir, "docroot");
|
||||
resBase.mkdirs();
|
||||
new File(resBase, "one").mkdir();
|
||||
new File(resBase, "two").mkdir();
|
||||
new File(resBase, "three").mkdir();
|
||||
assertTrue(resBase.mkdirs());
|
||||
assertTrue(new File(resBase, "one").mkdir());
|
||||
assertTrue(new File(resBase, "two").mkdir());
|
||||
assertTrue(new File(resBase, "three").mkdir());
|
||||
if ( !_runningOnWindows )
|
||||
assertTrue("Creating dir 'f??r' (Might not work in Windows)", new File(resBase, "f??r").mkdir());
|
||||
|
||||
|
@ -126,6 +127,7 @@ public class DefaultServletTest extends TestCase
|
|||
assertResponseNotContains( "<script>", response );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListingProperUrlEncoding() throws Exception
|
||||
{
|
||||
ServletHolder defholder = context.addServlet(DefaultServlet.class,"/*");
|
||||
|
@ -133,18 +135,18 @@ public class DefaultServletTest extends TestCase
|
|||
defholder.setInitParameter("redirectWelcome","false");
|
||||
defholder.setInitParameter("gzip","false");
|
||||
|
||||
File testDir = new File("target/tests/" + getName());
|
||||
File testDir = new File("target/tests/" + DefaultServletTest.class.getSimpleName());
|
||||
prepareEmptyTestDir(testDir);
|
||||
|
||||
/* create some content in the docroot */
|
||||
File resBase = new File(testDir, "docroot");
|
||||
resBase.mkdirs();
|
||||
assertTrue(resBase.mkdirs());
|
||||
File wackyDir = new File(resBase, "dir;"); // this should not be double-encoded.
|
||||
assertTrue(wackyDir.mkdirs());
|
||||
|
||||
new File(wackyDir, "four").mkdir();
|
||||
new File(wackyDir, "five").mkdir();
|
||||
new File(wackyDir, "six").mkdir();
|
||||
assertTrue(new File(wackyDir, "four").mkdir());
|
||||
assertTrue(new File(wackyDir, "five").mkdir());
|
||||
assertTrue(new File(wackyDir, "six").mkdir());
|
||||
|
||||
/* At this point we have the following
|
||||
* testListingProperUrlEncoding/
|
||||
|
@ -163,7 +165,6 @@ public class DefaultServletTest extends TestCase
|
|||
|
||||
assertResponseContains("HTTP/1.1 404 Not Found", response);
|
||||
|
||||
|
||||
// Now send request in proper, encoded format.
|
||||
response = connector.getResponses("GET /context/dir%3B/ HTTP/1.0\r\n\r\n");
|
||||
|
||||
|
@ -178,6 +179,7 @@ public class DefaultServletTest extends TestCase
|
|||
assertResponseContains("/dir%3B/six/",response);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListingContextBreakout() throws Exception
|
||||
{
|
||||
ServletHolder defholder = context.addServlet(DefaultServlet.class,"/");
|
||||
|
@ -186,12 +188,12 @@ public class DefaultServletTest extends TestCase
|
|||
defholder.setInitParameter("gzip","false");
|
||||
defholder.setInitParameter("aliases","true");
|
||||
|
||||
File testDir = new File("target/tests/" + getName());
|
||||
File testDir = new File("target/tests/" + DefaultServletTest.class.getSimpleName());
|
||||
prepareEmptyTestDir(testDir);
|
||||
|
||||
/* create some content in the docroot */
|
||||
File resBase = new File(testDir, "docroot");
|
||||
resBase.mkdirs();
|
||||
assertTrue(resBase.mkdirs());
|
||||
|
||||
File index = new File(resBase, "index.html");
|
||||
createFile(index, "<h1>Hello Index</h1>");
|
||||
|
@ -207,7 +209,7 @@ public class DefaultServletTest extends TestCase
|
|||
|
||||
/* create some content outside of the docroot */
|
||||
File sekret = new File(testDir, "sekret");
|
||||
sekret.mkdirs();
|
||||
assertTrue(sekret.mkdirs());
|
||||
File pass = new File(sekret, "pass");
|
||||
createFile(pass, "Sssh, you shouldn't be seeing this");
|
||||
|
||||
|
@ -286,17 +288,15 @@ public class DefaultServletTest extends TestCase
|
|||
assertResponseNotContains("Sssh",response);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
public void testWelcome() throws Exception
|
||||
{
|
||||
File testDir = new File("target/tests/" + getName());
|
||||
File testDir = new File("target/tests/" + DefaultServletTest.class.getSimpleName());
|
||||
prepareEmptyTestDir(testDir);
|
||||
File resBase = new File(testDir, "docroot");
|
||||
resBase.mkdirs();
|
||||
assertTrue(resBase.mkdirs());
|
||||
File inde = new File(resBase, "index.htm");
|
||||
File index = new File(resBase, "index.html");
|
||||
|
||||
|
||||
String resBasePath = resBase.getAbsolutePath();
|
||||
|
||||
|
@ -306,41 +306,39 @@ public class DefaultServletTest extends TestCase
|
|||
defholder.setInitParameter("welcomeServlets","false");
|
||||
defholder.setInitParameter("gzip","false");
|
||||
defholder.setInitParameter("resourceBase",resBasePath);
|
||||
|
||||
|
||||
ServletHolder jspholder = context.addServlet(NoJspServlet.class,"*.jsp");
|
||||
|
||||
String response;
|
||||
|
||||
response= connector.getResponses("GET /context/ HTTP/1.0\r\n\r\n");
|
||||
String response = connector.getResponses("GET /context/ HTTP/1.0\r\n\r\n");
|
||||
assertResponseContains("403",response);
|
||||
|
||||
createFile(index, "<h1>Hello Index</h1>");
|
||||
response= connector.getResponses("GET /context/ HTTP/1.0\r\n\r\n");
|
||||
assertResponseContains("<h1>Hello Index</h1>",response);
|
||||
|
||||
|
||||
createFile(inde, "<h1>Hello Inde</h1>");
|
||||
response= connector.getResponses("GET /context/ HTTP/1.0\r\n\r\n");
|
||||
assertResponseContains("<h1>Hello Index</h1>",response);
|
||||
|
||||
index.delete();
|
||||
assertTrue(index.delete());
|
||||
response= connector.getResponses("GET /context/ HTTP/1.0\r\n\r\n");
|
||||
assertResponseContains("<h1>Hello Inde</h1>",response);
|
||||
|
||||
inde.delete();
|
||||
|
||||
assertTrue(inde.delete());
|
||||
response= connector.getResponses("GET /context/ HTTP/1.0\r\n\r\n");
|
||||
assertResponseContains("403",response);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWelcomeServlet() throws Exception
|
||||
{
|
||||
File testDir = new File("target/tests/" + getName());
|
||||
File testDir = new File("target/tests/" + DefaultServletTest.class.getSimpleName());
|
||||
prepareEmptyTestDir(testDir);
|
||||
File resBase = new File(testDir, "docroot");
|
||||
resBase.mkdirs();
|
||||
assertTrue(resBase.mkdirs());
|
||||
File inde = new File(resBase, "index.htm");
|
||||
File index = new File(resBase, "index.html");
|
||||
|
||||
|
||||
|
||||
String resBasePath = resBase.getAbsolutePath();
|
||||
|
||||
|
@ -350,7 +348,7 @@ public class DefaultServletTest extends TestCase
|
|||
defholder.setInitParameter("welcomeServlets","true");
|
||||
defholder.setInitParameter("gzip","false");
|
||||
defholder.setInitParameter("resourceBase",resBasePath);
|
||||
|
||||
|
||||
ServletHolder jspholder = context.addServlet(NoJspServlet.class,"*.jsp");
|
||||
|
||||
String response;
|
||||
|
@ -361,30 +359,29 @@ public class DefaultServletTest extends TestCase
|
|||
createFile(index, "<h1>Hello Index</h1>");
|
||||
response= connector.getResponses("GET /context/ HTTP/1.0\r\n\r\n");
|
||||
assertResponseContains("<h1>Hello Index</h1>",response);
|
||||
|
||||
|
||||
createFile(inde, "<h1>Hello Inde</h1>");
|
||||
response= connector.getResponses("GET /context/ HTTP/1.0\r\n\r\n");
|
||||
assertResponseContains("<h1>Hello Index</h1>",response);
|
||||
|
||||
index.delete();
|
||||
assertTrue(index.delete());
|
||||
response= connector.getResponses("GET /context/ HTTP/1.0\r\n\r\n");
|
||||
assertResponseContains("<h1>Hello Inde</h1>",response);
|
||||
|
||||
inde.delete();
|
||||
|
||||
assertTrue(inde.delete());
|
||||
response= connector.getResponses("GET /context/ HTTP/1.0\r\n\r\n");
|
||||
assertResponseContains("JSP support not configured",response);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWelcomeExactServlet() throws Exception
|
||||
{
|
||||
File testDir = new File("target/tests/" + getName());
|
||||
File testDir = new File("target/tests/" + DefaultServletTest.class.getSimpleName());
|
||||
prepareEmptyTestDir(testDir);
|
||||
File resBase = new File(testDir, "docroot");
|
||||
resBase.mkdirs();
|
||||
assertTrue(resBase.mkdirs());
|
||||
File inde = new File(resBase, "index.htm");
|
||||
File index = new File(resBase, "index.html");
|
||||
|
||||
|
||||
String resBasePath = resBase.getAbsolutePath();
|
||||
|
||||
|
@ -394,7 +391,7 @@ public class DefaultServletTest extends TestCase
|
|||
defholder.setInitParameter("welcomeServlets","exact");
|
||||
defholder.setInitParameter("gzip","false");
|
||||
defholder.setInitParameter("resourceBase",resBasePath);
|
||||
|
||||
|
||||
ServletHolder jspholder = context.addServlet(NoJspServlet.class,"*.jsp");
|
||||
context.addServlet(jspholder,"/index.jsp");
|
||||
|
||||
|
@ -406,46 +403,42 @@ public class DefaultServletTest extends TestCase
|
|||
createFile(index, "<h1>Hello Index</h1>");
|
||||
response= connector.getResponses("GET /context/ HTTP/1.0\r\n\r\n");
|
||||
assertResponseContains("<h1>Hello Index</h1>",response);
|
||||
|
||||
|
||||
createFile(inde, "<h1>Hello Inde</h1>");
|
||||
response= connector.getResponses("GET /context/ HTTP/1.0\r\n\r\n");
|
||||
assertResponseContains("<h1>Hello Index</h1>",response);
|
||||
|
||||
index.delete();
|
||||
assertTrue(index.delete());
|
||||
response= connector.getResponses("GET /context/ HTTP/1.0\r\n\r\n");
|
||||
assertResponseContains("<h1>Hello Inde</h1>",response);
|
||||
|
||||
inde.delete();
|
||||
|
||||
assertTrue(inde.delete());
|
||||
response= connector.getResponses("GET /context/ HTTP/1.0\r\n\r\n");
|
||||
assertResponseContains("JSP support not configured",response);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRangeRequests() throws Exception
|
||||
{
|
||||
File testDir = new File("target/tests/" + getName());
|
||||
File testDir = new File("target/tests/" + DefaultServletTest.class.getSimpleName());
|
||||
prepareEmptyTestDir(testDir);
|
||||
File resBase = new File(testDir, "docroot");
|
||||
resBase.mkdirs();
|
||||
assertTrue(resBase.mkdirs());
|
||||
File data = new File(resBase, "data.txt");
|
||||
createFile(data,"01234567890123456789012345678901234567890123456789012345678901234567890123456789");
|
||||
String resBasePath = resBase.getAbsolutePath();
|
||||
|
||||
|
||||
|
||||
ServletHolder defholder = context.addServlet(DefaultServlet.class,"/");
|
||||
defholder.setInitParameter("acceptRanges","true");
|
||||
defholder.setInitParameter("resourceBase",resBasePath);
|
||||
|
||||
String response;
|
||||
|
||||
response= connector.getResponses(
|
||||
"GET /context/data.txt HTTP/1.1\r\n"+
|
||||
"Host: localhost\r\n"+
|
||||
"\r\n");
|
||||
String response = connector.getResponses(
|
||||
"GET /context/data.txt HTTP/1.1\r\n" +
|
||||
"Host: localhost\r\n" +
|
||||
"\r\n");
|
||||
assertResponseContains("200 OK",response);
|
||||
assertResponseContains("Accept-Ranges: bytes",response);
|
||||
|
||||
|
||||
response= connector.getResponses(
|
||||
"GET /context/data.txt HTTP/1.1\r\n"+
|
||||
"Host: localhost\r\n"+
|
||||
|
@ -486,7 +479,7 @@ public class DefaultServletTest extends TestCase
|
|||
assertResponseContains("Content-Range: bytes 70-79/80",response);
|
||||
assertResponseContains("Content-Length: "+body.length(),response);
|
||||
assertTrue(body.endsWith(boundary+"--\r\n"));
|
||||
|
||||
|
||||
response= connector.getResponses(
|
||||
"GET /context/data.txt HTTP/1.1\r\n"+
|
||||
"Host: localhost\r\n"+
|
||||
|
@ -503,10 +496,8 @@ public class DefaultServletTest extends TestCase
|
|||
assertResponseContains("Content-Range: bytes 70-79/80",response);
|
||||
assertResponseContains("Content-Length: "+body.length(),response);
|
||||
assertTrue(body.endsWith(boundary+"--\r\n"));
|
||||
|
||||
}
|
||||
|
||||
|
||||
private void createFile(File file, String str) throws IOException
|
||||
{
|
||||
FileOutputStream out = null;
|
||||
|
@ -527,7 +518,7 @@ public class DefaultServletTest extends TestCase
|
|||
}
|
||||
else
|
||||
{
|
||||
testdir.mkdirs();
|
||||
assertTrue(testdir.mkdirs());
|
||||
}
|
||||
|
||||
assertTrue("test dir should exists",testdir.exists());
|
||||
|
@ -538,9 +529,7 @@ public class DefaultServletTest extends TestCase
|
|||
private boolean isEmpty(File dir)
|
||||
{
|
||||
if (!dir.isDirectory())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return dir.list().length == 0;
|
||||
}
|
||||
|
@ -548,10 +537,8 @@ public class DefaultServletTest extends TestCase
|
|||
private void emptyDir(File dir)
|
||||
{
|
||||
File entries[] = dir.listFiles();
|
||||
for (int i = 0; i < entries.length; i++)
|
||||
{
|
||||
deletePath(entries[i]);
|
||||
}
|
||||
for (File entry : entries)
|
||||
deletePath(entry);
|
||||
}
|
||||
|
||||
private void deletePath(File path)
|
||||
|
@ -559,10 +546,8 @@ public class DefaultServletTest extends TestCase
|
|||
if (path.isDirectory())
|
||||
{
|
||||
File entries[] = path.listFiles();
|
||||
for (int i = 0; i < entries.length; i++)
|
||||
{
|
||||
deletePath(entries[i]);
|
||||
}
|
||||
for (File entry : entries)
|
||||
deletePath(entry);
|
||||
}
|
||||
|
||||
assertTrue("Deleting: " + path.getAbsolutePath(),path.delete());
|
||||
|
|
|
@ -4,21 +4,19 @@
|
|||
// 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.servlet;
|
||||
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import javax.servlet.RequestDispatcher;
|
||||
import javax.servlet.Servlet;
|
||||
import javax.servlet.ServletException;
|
||||
|
@ -26,19 +24,24 @@ import javax.servlet.http.HttpServlet;
|
|||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.eclipse.jetty.server.Dispatcher;
|
||||
import org.eclipse.jetty.server.LocalConnector;
|
||||
import org.eclipse.jetty.server.Server;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class DispatcherTest extends TestCase
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class DispatcherTest
|
||||
{
|
||||
private Server _server = new Server();
|
||||
private Server _server;
|
||||
private LocalConnector _connector;
|
||||
private ServletContextHandler _context;
|
||||
|
||||
protected void setUp() throws Exception
|
||||
@Before
|
||||
public void init() throws Exception
|
||||
{
|
||||
_server = new Server();
|
||||
_server.setSendServerVersion(false);
|
||||
|
@ -51,6 +54,14 @@ public class DispatcherTest extends TestCase
|
|||
_server.start();
|
||||
}
|
||||
|
||||
@After
|
||||
public void destroy() throws Exception
|
||||
{
|
||||
_server.stop();
|
||||
_server.join();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testForward() throws Exception
|
||||
{
|
||||
_context.addServlet(ForwardServlet.class, "/ForwardServlet/*");
|
||||
|
@ -63,10 +74,11 @@ public class DispatcherTest extends TestCase
|
|||
"\r\n";
|
||||
|
||||
String responses = _connector.getResponses("GET /context/ForwardServlet?do=assertforward&do=more&test=1 HTTP/1.1\n" + "Host: localhost\n\n");
|
||||
|
||||
assertEquals(expected, responses);
|
||||
|
||||
assertEquals(expected, responses);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testInclude() throws Exception
|
||||
{
|
||||
_context.addServlet(IncludeServlet.class, "/IncludeServlet/*");
|
||||
|
@ -78,10 +90,11 @@ public class DispatcherTest extends TestCase
|
|||
"\r\n";
|
||||
|
||||
String responses = _connector.getResponses("GET /context/IncludeServlet?do=assertinclude&do=more&test=1 HTTP/1.1\n" + "Host: localhost\n\n");
|
||||
|
||||
assertEquals(expected, responses);
|
||||
|
||||
assertEquals(expected, responses);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testForwardThenInclude() throws Exception
|
||||
{
|
||||
_context.addServlet(ForwardServlet.class, "/ForwardServlet/*");
|
||||
|
@ -94,16 +107,17 @@ public class DispatcherTest extends TestCase
|
|||
"\r\n";
|
||||
|
||||
String responses = _connector.getResponses("GET /context/ForwardServlet/forwardpath?do=include HTTP/1.1\n" + "Host: localhost\n\n");
|
||||
|
||||
|
||||
assertEquals(expected, responses);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testIncludeThenForward() throws Exception
|
||||
{
|
||||
_context.addServlet(IncludeServlet.class, "/IncludeServlet/*");
|
||||
_context.addServlet(ForwardServlet.class, "/ForwardServlet/*");
|
||||
_context.addServlet(AssertIncludeForwardServlet.class, "/AssertIncludeForwardServlet/*");
|
||||
|
||||
|
||||
|
||||
String expected=
|
||||
"HTTP/1.1 200 OK\r\n"+
|
||||
|
@ -113,16 +127,16 @@ public class DispatcherTest extends TestCase
|
|||
"\r\n";
|
||||
|
||||
String responses = _connector.getResponses("GET /context/IncludeServlet/includepath?do=forward HTTP/1.1\n" + "Host: localhost\n\n");
|
||||
|
||||
|
||||
assertEquals(expected, responses);
|
||||
}
|
||||
|
||||
public static class ForwardServlet extends HttpServlet implements Servlet
|
||||
|
||||
public static class ForwardServlet extends HttpServlet implements Servlet
|
||||
{
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
|
||||
{
|
||||
RequestDispatcher dispatcher = null;
|
||||
|
||||
|
||||
if(request.getParameter("do").equals("include"))
|
||||
dispatcher = getServletContext().getRequestDispatcher("/IncludeServlet/includepath?do=assertforwardinclude");
|
||||
else if(request.getParameter("do").equals("assertincludeforward"))
|
||||
|
@ -130,15 +144,15 @@ public class DispatcherTest extends TestCase
|
|||
else if(request.getParameter("do").equals("assertforward"))
|
||||
dispatcher = getServletContext().getRequestDispatcher("/AssertForwardServlet?do=end&do=the");
|
||||
dispatcher.forward(request, response);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class IncludeServlet extends HttpServlet implements Servlet
|
||||
|
||||
public static class IncludeServlet extends HttpServlet implements Servlet
|
||||
{
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
|
||||
{
|
||||
RequestDispatcher dispatcher = null;
|
||||
|
||||
|
||||
if(request.getParameter("do").equals("forward"))
|
||||
dispatcher = getServletContext().getRequestDispatcher("/ForwardServlet/forwardpath?do=assertincludeforward");
|
||||
else if(request.getParameter("do").equals("assertforwardinclude"))
|
||||
|
@ -146,7 +160,7 @@ public class DispatcherTest extends TestCase
|
|||
else if(request.getParameter("do").equals("assertinclude"))
|
||||
dispatcher = getServletContext().getRequestDispatcher("/AssertIncludeServlet?do=end&do=the");
|
||||
dispatcher.include(request, response);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class AssertForwardServlet extends HttpServlet implements Servlet
|
||||
|
@ -159,29 +173,24 @@ public class DispatcherTest extends TestCase
|
|||
assertEquals( null, request.getAttribute(Dispatcher.FORWARD_PATH_INFO));
|
||||
assertEquals( "do=assertforward&do=more&test=1", request.getAttribute(Dispatcher.FORWARD_QUERY_STRING) );
|
||||
|
||||
|
||||
List expectedAttributeNames = Arrays.asList(new String[] {
|
||||
Dispatcher.FORWARD_REQUEST_URI, Dispatcher.FORWARD_CONTEXT_PATH,
|
||||
Dispatcher.FORWARD_SERVLET_PATH, Dispatcher.FORWARD_QUERY_STRING
|
||||
});
|
||||
List expectedAttributeNames = Arrays.asList(Dispatcher.FORWARD_REQUEST_URI, Dispatcher.FORWARD_CONTEXT_PATH,
|
||||
Dispatcher.FORWARD_SERVLET_PATH, Dispatcher.FORWARD_QUERY_STRING);
|
||||
List requestAttributeNames = Collections.list(request.getAttributeNames());
|
||||
assertTrue(requestAttributeNames.containsAll(expectedAttributeNames));
|
||||
|
||||
|
||||
|
||||
assertEquals(null, request.getPathInfo());
|
||||
assertEquals(null, request.getPathTranslated());
|
||||
assertEquals("do=end&do=the&test=1", request.getQueryString());
|
||||
assertEquals("/context/AssertForwardServlet", request.getRequestURI());
|
||||
assertEquals("/context", request.getContextPath());
|
||||
assertEquals("/AssertForwardServlet", request.getServletPath());
|
||||
|
||||
|
||||
response.setContentType("text/html");
|
||||
response.setStatus(HttpServletResponse.SC_OK);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public static class AssertIncludeServlet extends HttpServlet implements Servlet
|
||||
public static class AssertIncludeServlet extends HttpServlet implements Servlet
|
||||
{
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
|
||||
{
|
||||
|
@ -189,34 +198,28 @@ public class DispatcherTest extends TestCase
|
|||
assertEquals( "/context", request.getAttribute(Dispatcher.INCLUDE_CONTEXT_PATH) );
|
||||
assertEquals( "/AssertIncludeServlet", request.getAttribute(Dispatcher.INCLUDE_SERVLET_PATH));
|
||||
assertEquals( null, request.getAttribute(Dispatcher.INCLUDE_PATH_INFO));
|
||||
assertEquals( "do=end&do=the", request.getAttribute(Dispatcher.INCLUDE_QUERY_STRING));
|
||||
|
||||
List expectedAttributeNames = Arrays.asList(new String[] {
|
||||
Dispatcher.INCLUDE_REQUEST_URI, Dispatcher.INCLUDE_CONTEXT_PATH,
|
||||
Dispatcher.INCLUDE_SERVLET_PATH, Dispatcher.INCLUDE_QUERY_STRING
|
||||
});
|
||||
assertEquals( "do=end&do=the", request.getAttribute(Dispatcher.INCLUDE_QUERY_STRING));
|
||||
|
||||
List expectedAttributeNames = Arrays.asList(Dispatcher.INCLUDE_REQUEST_URI, Dispatcher.INCLUDE_CONTEXT_PATH,
|
||||
Dispatcher.INCLUDE_SERVLET_PATH, Dispatcher.INCLUDE_QUERY_STRING);
|
||||
List requestAttributeNames = Collections.list(request.getAttributeNames());
|
||||
assertTrue(requestAttributeNames.containsAll(expectedAttributeNames));
|
||||
|
||||
|
||||
|
||||
assertEquals(null, request.getPathInfo());
|
||||
assertEquals(null, request.getPathTranslated());
|
||||
assertEquals("do=assertinclude&do=more&test=1", request.getQueryString());
|
||||
assertEquals("/context/IncludeServlet", request.getRequestURI());
|
||||
assertEquals("/context", request.getContextPath());
|
||||
assertEquals("/IncludeServlet", request.getServletPath());
|
||||
|
||||
|
||||
response.setContentType("text/html");
|
||||
response.setStatus(HttpServletResponse.SC_OK);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class AssertForwardIncludeServlet extends HttpServlet implements Servlet
|
||||
|
||||
public static class AssertForwardIncludeServlet extends HttpServlet implements Servlet
|
||||
{
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
|
||||
{
|
||||
// include doesn't hide forward
|
||||
assertEquals( "/context/ForwardServlet/forwardpath", request.getAttribute(Dispatcher.FORWARD_REQUEST_URI));
|
||||
|
@ -224,64 +227,56 @@ public class DispatcherTest extends TestCase
|
|||
assertEquals( "/ForwardServlet", request.getAttribute(Dispatcher.FORWARD_SERVLET_PATH));
|
||||
assertEquals( "/forwardpath", request.getAttribute(Dispatcher.FORWARD_PATH_INFO));
|
||||
assertEquals( "do=include", request.getAttribute(Dispatcher.FORWARD_QUERY_STRING) );
|
||||
|
||||
|
||||
assertEquals( "/context/AssertForwardIncludeServlet/assertpath", request.getAttribute(Dispatcher.INCLUDE_REQUEST_URI));
|
||||
assertEquals( "/context", request.getAttribute(Dispatcher.INCLUDE_CONTEXT_PATH) );
|
||||
assertEquals( "/AssertForwardIncludeServlet", request.getAttribute(Dispatcher.INCLUDE_SERVLET_PATH));
|
||||
assertEquals( "/assertpath", request.getAttribute(Dispatcher.INCLUDE_PATH_INFO));
|
||||
assertEquals( "do=end", request.getAttribute(Dispatcher.INCLUDE_QUERY_STRING));
|
||||
|
||||
|
||||
List expectedAttributeNames = Arrays.asList(new String[] {
|
||||
Dispatcher.FORWARD_REQUEST_URI, Dispatcher.FORWARD_CONTEXT_PATH, Dispatcher.FORWARD_SERVLET_PATH,
|
||||
Dispatcher.FORWARD_PATH_INFO, Dispatcher.FORWARD_QUERY_STRING,
|
||||
Dispatcher.INCLUDE_REQUEST_URI, Dispatcher.INCLUDE_CONTEXT_PATH, Dispatcher.INCLUDE_SERVLET_PATH,
|
||||
Dispatcher.INCLUDE_PATH_INFO, Dispatcher.INCLUDE_QUERY_STRING
|
||||
});
|
||||
assertEquals( "do=end", request.getAttribute(Dispatcher.INCLUDE_QUERY_STRING));
|
||||
|
||||
List expectedAttributeNames = Arrays.asList(Dispatcher.FORWARD_REQUEST_URI, Dispatcher.FORWARD_CONTEXT_PATH, Dispatcher.FORWARD_SERVLET_PATH,
|
||||
Dispatcher.FORWARD_PATH_INFO, Dispatcher.FORWARD_QUERY_STRING,
|
||||
Dispatcher.INCLUDE_REQUEST_URI, Dispatcher.INCLUDE_CONTEXT_PATH, Dispatcher.INCLUDE_SERVLET_PATH,
|
||||
Dispatcher.INCLUDE_PATH_INFO, Dispatcher.INCLUDE_QUERY_STRING);
|
||||
List requestAttributeNames = Collections.list(request.getAttributeNames());
|
||||
assertTrue(requestAttributeNames.containsAll(expectedAttributeNames));
|
||||
|
||||
|
||||
assertTrue(requestAttributeNames.containsAll(expectedAttributeNames));
|
||||
|
||||
assertEquals("/includepath", request.getPathInfo());
|
||||
assertEquals(null, request.getPathTranslated());
|
||||
assertEquals("do=assertforwardinclude", request.getQueryString());
|
||||
assertEquals("/context/IncludeServlet/includepath", request.getRequestURI());
|
||||
assertEquals("/context", request.getContextPath());
|
||||
assertEquals("/IncludeServlet", request.getServletPath());
|
||||
|
||||
|
||||
response.setContentType("text/html");
|
||||
response.setStatus(HttpServletResponse.SC_OK);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class AssertIncludeForwardServlet extends HttpServlet implements Servlet
|
||||
{
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
|
||||
{
|
||||
// forward hides include
|
||||
assertEquals( null, request.getAttribute(Dispatcher.INCLUDE_REQUEST_URI));
|
||||
assertEquals( null, request.getAttribute(Dispatcher.INCLUDE_CONTEXT_PATH) );
|
||||
assertEquals( null, request.getAttribute(Dispatcher.INCLUDE_SERVLET_PATH));
|
||||
assertEquals( null, request.getAttribute(Dispatcher.INCLUDE_PATH_INFO));
|
||||
assertEquals( null, request.getAttribute(Dispatcher.INCLUDE_QUERY_STRING));
|
||||
assertEquals( null, request.getAttribute(Dispatcher.INCLUDE_QUERY_STRING));
|
||||
|
||||
assertEquals( "/context/IncludeServlet/includepath", request.getAttribute(Dispatcher.FORWARD_REQUEST_URI));
|
||||
assertEquals( "/context", request.getAttribute(Dispatcher.FORWARD_CONTEXT_PATH) );
|
||||
assertEquals( "/IncludeServlet", request.getAttribute(Dispatcher.FORWARD_SERVLET_PATH));
|
||||
assertEquals( "/includepath", request.getAttribute(Dispatcher.FORWARD_PATH_INFO));
|
||||
assertEquals( "do=forward", request.getAttribute(Dispatcher.FORWARD_QUERY_STRING) );
|
||||
|
||||
|
||||
List expectedAttributeNames = Arrays.asList(new String[] {
|
||||
Dispatcher.FORWARD_REQUEST_URI, Dispatcher.FORWARD_CONTEXT_PATH, Dispatcher.FORWARD_SERVLET_PATH,
|
||||
Dispatcher.FORWARD_PATH_INFO, Dispatcher.FORWARD_QUERY_STRING,
|
||||
});
|
||||
|
||||
List expectedAttributeNames = Arrays.asList(Dispatcher.FORWARD_REQUEST_URI, Dispatcher.FORWARD_CONTEXT_PATH, Dispatcher.FORWARD_SERVLET_PATH,
|
||||
Dispatcher.FORWARD_PATH_INFO, Dispatcher.FORWARD_QUERY_STRING);
|
||||
List requestAttributeNames = Collections.list(request.getAttributeNames());
|
||||
assertTrue(requestAttributeNames.containsAll(expectedAttributeNames));
|
||||
|
||||
|
||||
|
||||
assertEquals("/assertpath", request.getPathInfo());
|
||||
assertEquals(null, request.getPathTranslated());
|
||||
assertEquals(null, request.getPathTranslated());
|
||||
assertEquals("do=end", request.getQueryString());
|
||||
assertEquals("/context/AssertIncludeForwardServlet/assertpath", request.getRequestURI());
|
||||
assertEquals("/context", request.getContextPath());
|
||||
|
@ -289,7 +284,6 @@ public class DispatcherTest extends TestCase
|
|||
|
||||
response.setContentType("text/html");
|
||||
response.setStatus(HttpServletResponse.SC_OK);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -14,46 +14,54 @@
|
|||
package org.eclipse.jetty.servlet;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.Servlet;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.eclipse.jetty.server.LocalConnector;
|
||||
import org.eclipse.jetty.server.Server;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
*/
|
||||
public class InvokerTest extends TestCase
|
||||
public class InvokerTest
|
||||
{
|
||||
Server _server;
|
||||
LocalConnector _connector;
|
||||
ServletContextHandler _context;
|
||||
private Server _server;
|
||||
private LocalConnector _connector;
|
||||
|
||||
protected void setUp() throws Exception
|
||||
@Before
|
||||
public void init() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
_server = new Server();
|
||||
_connector = new LocalConnector();
|
||||
_context = new ServletContextHandler(ServletContextHandler.SESSIONS);
|
||||
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
|
||||
|
||||
_server.setSendServerVersion(false);
|
||||
_server.addConnector(_connector);
|
||||
_server.setHandler(_context);
|
||||
_server.setHandler(context);
|
||||
|
||||
_context.setContextPath("/");
|
||||
context.setContextPath("/");
|
||||
|
||||
ServletHolder holder = _context.addServlet(Invoker.class, "/servlet/*");
|
||||
ServletHolder holder = context.addServlet(Invoker.class, "/servlet/*");
|
||||
holder.setInitParameter("nonContextServlets","true");
|
||||
_server.start();
|
||||
}
|
||||
|
||||
@After
|
||||
public void destroy() throws Exception
|
||||
{
|
||||
_server.stop();
|
||||
_server.join();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvoker() throws Exception
|
||||
{
|
||||
String requestPath = "/servlet/"+TestServlet.class.getName();
|
||||
|
|
|
@ -4,31 +4,31 @@
|
|||
// 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.servlet;
|
||||
|
||||
import junit.framework.AssertionFailedError;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.eclipse.jetty.server.LocalConnector;
|
||||
import org.eclipse.jetty.server.Server;
|
||||
import org.eclipse.jetty.server.handler.StatisticsHandler;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class StatisticsServletTest extends TestCase
|
||||
public class StatisticsServletTest
|
||||
{
|
||||
Server server;
|
||||
LocalConnector connector;
|
||||
ServletContextHandler context;
|
||||
|
||||
protected void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
private Server server;
|
||||
private LocalConnector connector;
|
||||
private ServletContextHandler context;
|
||||
|
||||
@Before
|
||||
public void init() throws Exception
|
||||
{
|
||||
server = new Server();
|
||||
server.setSendServerVersion(false);
|
||||
context = new ServletContextHandler();
|
||||
|
@ -37,25 +37,22 @@ public class StatisticsServletTest extends TestCase
|
|||
holder.setServlet(new org.eclipse.jetty.servlet.StatisticsServlet());
|
||||
holder.setInitParameter("restrictToLocalhost", "false");
|
||||
context.addServlet(holder, "/stats");
|
||||
|
||||
|
||||
server.setHandler(context);
|
||||
connector = new LocalConnector();
|
||||
server.addConnector(connector);
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception
|
||||
@After
|
||||
public void destroy() throws Exception
|
||||
{
|
||||
super.tearDown();
|
||||
|
||||
if (server != null)
|
||||
{
|
||||
server.stop();
|
||||
}
|
||||
server.stop();
|
||||
server.join();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
public void testNoHandler () throws Exception
|
||||
{
|
||||
{
|
||||
server.start();
|
||||
|
||||
StringBuffer req1 = new StringBuffer();
|
||||
|
@ -64,25 +61,25 @@ public class StatisticsServletTest extends TestCase
|
|||
req1.append("\n");
|
||||
|
||||
String response = connector.getResponses(req1.toString());
|
||||
assertResponseContains("503", response);
|
||||
assertResponseContains("503", response);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testWithHandler () throws Exception
|
||||
{
|
||||
StatisticsHandler statsHandler = new StatisticsHandler();
|
||||
statsHandler.setHandler(context);
|
||||
server.setHandler(statsHandler);
|
||||
server.start();
|
||||
|
||||
|
||||
StringBuffer req1 = new StringBuffer();
|
||||
req1.append("GET /stats HTTP/1.1\n");
|
||||
req1.append("Host: localhost\n");
|
||||
req1.append("\n");
|
||||
|
||||
|
||||
String response = connector.getResponses(req1.toString());
|
||||
assertResponseContains("Statistics gathering started ", response);
|
||||
assertResponseContains("Statistics gathering started ", response);
|
||||
}
|
||||
|
||||
|
||||
private void assertResponseContains(String expected, String response)
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue