Cleanup existing DispatcherTest (before fixing disabled tests)

This commit is contained in:
Joakim Erdfelt 2022-07-15 10:57:18 -05:00
parent 60b618d71d
commit 2a2e252de9
No known key found for this signature in database
GPG Key ID: 2D0E1FB8FE4B68B4
1 changed files with 397 additions and 200 deletions

View File

@ -44,6 +44,7 @@ import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletRequestWrapper; import jakarta.servlet.http.HttpServletRequestWrapper;
import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpServletResponseWrapper; import jakarta.servlet.http.HttpServletResponseWrapper;
import org.eclipse.jetty.http.HttpTester;
import org.eclipse.jetty.logging.StacklessLogging; import org.eclipse.jetty.logging.StacklessLogging;
import org.eclipse.jetty.server.HttpChannel; import org.eclipse.jetty.server.HttpChannel;
import org.eclipse.jetty.server.HttpConfiguration; import org.eclipse.jetty.server.HttpConfiguration;
@ -54,7 +55,7 @@ import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.server.handler.ResourceHandler; import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils; import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.util.MultiMap; import org.eclipse.jetty.util.MultiMap;
import org.eclipse.jetty.util.TypeUtil; import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.UrlEncoded; import org.eclipse.jetty.util.UrlEncoded;
import org.eclipse.jetty.util.resource.Resource; import org.eclipse.jetty.util.resource.Resource;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
@ -70,6 +71,7 @@ import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.startsWith; import static org.hamcrest.Matchers.startsWith;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
public class DispatcherTest public class DispatcherTest
@ -78,9 +80,7 @@ public class DispatcherTest
private Server _server; private Server _server;
private LocalConnector _connector; private LocalConnector _connector;
private ContextHandlerCollection _contextCollection;
private ServletContextHandler _contextHandler; private ServletContextHandler _contextHandler;
private ResourceHandler _resourceHandler;
@BeforeEach @BeforeEach
public void init() throws Exception public void init() throws Exception
@ -90,17 +90,17 @@ public class DispatcherTest
_connector.getConnectionFactory(HttpConfiguration.ConnectionFactory.class).getHttpConfiguration().setSendServerVersion(false); _connector.getConnectionFactory(HttpConfiguration.ConnectionFactory.class).getHttpConfiguration().setSendServerVersion(false);
_connector.getConnectionFactory(HttpConfiguration.ConnectionFactory.class).getHttpConfiguration().setSendDateHeader(false); _connector.getConnectionFactory(HttpConfiguration.ConnectionFactory.class).getHttpConfiguration().setSendDateHeader(false);
_contextCollection = new ContextHandlerCollection(); ContextHandlerCollection contextCollection = new ContextHandlerCollection();
_contextHandler = new ServletContextHandler(); _contextHandler = new ServletContextHandler();
_contextHandler.setContextPath("/context"); _contextHandler.setContextPath("/context");
_contextCollection.addHandler(_contextHandler); contextCollection.addHandler(_contextHandler);
_resourceHandler = new ResourceHandler(); ResourceHandler resourceHandler = new ResourceHandler();
Path basePath = MavenTestingUtils.getTestResourcePathDir("dispatchResourceTest"); Path basePath = MavenTestingUtils.getTestResourcePathDir("dispatchResourceTest");
_resourceHandler.setBaseResource(Resource.newResource(basePath)); resourceHandler.setBaseResource(Resource.newResource(basePath));
ContextHandler resourceContextHandler = new ContextHandler("/resource"); ContextHandler resourceContextHandler = new ContextHandler("/resource");
resourceContextHandler.setHandler(_resourceHandler); resourceContextHandler.setHandler(resourceHandler);
_contextCollection.addHandler(resourceContextHandler); contextCollection.addHandler(resourceContextHandler);
_server.setHandler(_contextCollection); _server.setHandler(contextCollection);
_server.addConnector(_connector); _server.addConnector(_connector);
_server.start(); _server.start();
@ -119,16 +119,22 @@ public class DispatcherTest
_contextHandler.addServlet(ForwardServlet.class, "/ForwardServlet/*"); _contextHandler.addServlet(ForwardServlet.class, "/ForwardServlet/*");
_contextHandler.addServlet(AssertForwardServlet.class, "/AssertForwardServlet/*"); _contextHandler.addServlet(AssertForwardServlet.class, "/AssertForwardServlet/*");
String expected = String rawResponse = _connector.getResponse("""
"HTTP/1.1 200 OK\r\n" + GET /context/ForwardServlet?do=assertforward&do=more&test=1 HTTP/1.1\r
"Content-Type: text/html\r\n" + Host: local\r
"Content-Length: 7\r\n" + Connection: close\r
"\r\n" + \r
"FORWARD"; """);
String responses = _connector.getResponse("GET /context/ForwardServlet?do=assertforward&do=more&test=1 HTTP/1.0\n\n"); String expected = """
HTTP/1.1 200 OK\r
Content-Type: text/html\r
Content-Length: 7\r
Connection: close\r
\r
FORWARD""";
assertEquals(expected, responses); assertEquals(expected, rawResponse);
} }
@Test @Test
@ -137,15 +143,21 @@ public class DispatcherTest
_contextHandler.addServlet(ForwardNonUTF8Servlet.class, "/ForwardServlet/*"); _contextHandler.addServlet(ForwardNonUTF8Servlet.class, "/ForwardServlet/*");
_contextHandler.addServlet(AssertNonUTF8ForwardServlet.class, "/AssertForwardServlet/*"); _contextHandler.addServlet(AssertNonUTF8ForwardServlet.class, "/AssertForwardServlet/*");
String expected = String rawResponse = _connector.getResponse("""
"HTTP/1.1 200 OK\r\n" + GET /context/ForwardServlet?do=assertforward&foreign=%d2%e5%ec%ef%e5%f0%e0%f2%f3%f0%e0&test=1 HTTP/1.1\r
"Content-Type: text/html\r\n" + Host: local\r
"Content-Length: 7\r\n" + Connection: close\r
"\r\n" + \r
"FORWARD"; """);
String responses = _connector.getResponse("GET /context/ForwardServlet?do=assertforward&foreign=%d2%e5%ec%ef%e5%f0%e0%f2%f3%f0%e0&test=1 HTTP/1.0\n\n");
assertEquals(expected, responses); String expected = """
HTTP/1.1 200 OK\r
Content-Type: text/html\r
Content-Length: 7\r
Connection: close\r
\r
FORWARD""";
assertEquals(expected, rawResponse);
} }
@Test @Test
@ -154,18 +166,24 @@ public class DispatcherTest
_contextHandler.addServlet(ForwardServlet.class, "/ForwardServlet/*"); _contextHandler.addServlet(ForwardServlet.class, "/ForwardServlet/*");
_contextHandler.addServlet(EchoURIServlet.class, "/EchoURI/*"); _contextHandler.addServlet(EchoURIServlet.class, "/EchoURI/*");
String expected = String responses = _connector.getResponse("""
"HTTP/1.1 200 OK\r\n" + GET /context/ForwardServlet;ignore=true?do=req.echo&uri=EchoURI%2Fx%2520x%3Ba=1%3Fb=2 HTTP/1.1\r
"Content-Type: text/plain\r\n" + Host: local\r
"Content-Length: 56\r\n" + Connection: close\r
"\r\n" + \r
"/context\r\n" + """);
"/EchoURI\r\n" +
"/x%20x\r\n" +
"/context/EchoURI/x%20x;a=1\r\n";
String responses = _connector.getResponse("GET /context/ForwardServlet;ignore=true?do=req.echo&uri=EchoURI%2Fx%2520x%3Ba=1%3Fb=2 HTTP/1.0\n\n");
String expected = """
HTTP/1.1 200 OK\r
Content-Type: text/plain\r
Content-Length: 56\r
Connection: close\r
\r
/context\r
/EchoURI\r
/x%20x\r
/context/EchoURI/x%20x;a=1\r
""";
assertEquals(expected, responses); assertEquals(expected, responses);
} }
@ -175,17 +193,26 @@ public class DispatcherTest
_contextHandler.addServlet(NamedForwardServlet.class, "/forward/*"); _contextHandler.addServlet(NamedForwardServlet.class, "/forward/*");
String echo = _contextHandler.addServlet(EchoURIServlet.class, "/echo/*").getName(); String echo = _contextHandler.addServlet(EchoURIServlet.class, "/echo/*").getName();
String expected = String rawResponse = _connector.getResponse(("""
"HTTP/1.1 200 OK\r\n" + GET /context/forward/info;param=value?name=@ECHO@ HTTP/1.1\r
"Content-Type: text/plain\r\n" + Host: local\r
"Content-Length: 62\r\n" + Connection: close\r
"\r\n" + \r
"/context\r\n" + """).replace("@ECHO@", echo));
"/forward\r\n" +
"/info\r\n" + String expected = """
"/context/forward/info;param=value\r\n"; HTTP/1.1 200 OK\r
String responses = _connector.getResponse("GET /context/forward/info;param=value?name=" + echo + " HTTP/1.0\n\n"); Content-Type: text/plain\r
assertEquals(expected, responses); Content-Length: 62\r
Connection: close\r
\r
/context\r
/forward\r
/info\r
/context/forward/info;param=value\r
""";
assertEquals(expected, rawResponse);
} }
@Test @Test
@ -194,15 +221,24 @@ public class DispatcherTest
_contextHandler.addServlet(NamedIncludeServlet.class, "/include/*"); _contextHandler.addServlet(NamedIncludeServlet.class, "/include/*");
String echo = _contextHandler.addServlet(EchoURIServlet.class, "/echo/*").getName(); String echo = _contextHandler.addServlet(EchoURIServlet.class, "/echo/*").getName();
String expected = String responses = _connector.getResponse("""
"HTTP/1.1 200 OK\r\n" + GET /context/include/info;param=value?name=@ECHO@ HTTP/1.1\r
"Content-Length: 62\r\n" + Host: local\r
"\r\n" + Connection: close\r
"/context\r\n" + \r
"/include\r\n" + """.replace("@ECHO@", echo));
"/info\r\n" +
"/context/include/info;param=value\r\n"; String expected = """
String responses = _connector.getResponse("GET /context/include/info;param=value?name=" + echo + " HTTP/1.0\n\n"); HTTP/1.1 200 OK\r
Content-Length: 62\r
Connection: close\r
\r
/context\r
/include\r
/info\r
/context/include/info;param=value\r
""";
assertEquals(expected, responses); assertEquals(expected, responses);
} }
@ -215,28 +251,58 @@ public class DispatcherTest
_contextHandler.addServlet(AlwaysForwardServlet.class, "/forward/*"); _contextHandler.addServlet(AlwaysForwardServlet.class, "/forward/*");
_contextHandler.addServlet(EchoServlet.class, "/echo/*"); _contextHandler.addServlet(EchoServlet.class, "/echo/*");
String response; String rawResponse;
response = _connector.getResponse("GET /context/forward/?echo=allgood HTTP/1.0\n\n"); rawResponse = _connector.getResponse("""
assertThat(response, containsString(" 200 OK")); GET /context/forward/?echo=allgood HTTP/1.1\r
assertThat(response, containsString("allgood")); Host: local\r
Connection: close\r
\r
""");
assertThat(rawResponse, containsString(" 200 OK"));
assertThat(rawResponse, containsString("allgood"));
response = _connector.getResponse("GET /context/forward/params?echo=allgood HTTP/1.0\n\n"); rawResponse = _connector.getResponse("""
assertThat(response, containsString(" 200 OK")); GET /context/forward/params?echo=allgood HTTP/1.1\r
assertThat(response, containsString("allgood")); Host: local\r
assertThat(response, containsString("forward")); Connection: close\r
\r
""");
assertThat(rawResponse, containsString(" 200 OK"));
assertThat(rawResponse, containsString("allgood"));
assertThat(rawResponse, containsString("forward"));
response = _connector.getResponse("GET /context/forward/badparams?echo=badparams HTTP/1.0\n\n"); rawResponse = _connector.getResponse("""
assertThat(response, containsString(" 500 ")); GET /context/forward/badparams?echo=badparams HTTP/1.1\r
Host: local\r
Connection: close\r
\r
""");
assertThat(rawResponse, containsString(" 500 "));
response = _connector.getResponse("GET /context/forward/?echo=badclient&bad=%88%A4 HTTP/1.0\n\n"); rawResponse = _connector.getResponse("""
assertThat(response, containsString(" 400 ")); GET /context/forward/?echo=badclient&bad=%88%A4 HTTP/1.1\r
Host: local\r
Connection: close\r
\r
""");
assertThat(rawResponse, containsString(" 400 "));
response = _connector.getResponse("GET /context/forward/params?echo=badclient&bad=%88%A4 HTTP/1.0\n\n"); rawResponse = _connector.getResponse("""
assertThat(response, containsString(" 400 ")); GET /context/forward/params?echo=badclient&bad=%88%A4 HTTP/1.1\r
Host: local\r
Connection: close\r
\r
""");
assertThat(rawResponse, containsString(" 400 "));
response = _connector.getResponse("GET /context/forward/badparams?echo=badclientandparam&bad=%88%A4 HTTP/1.0\n\n"); rawResponse = _connector.getResponse("""
assertThat(response, containsString(" 500 ")); GET /context/forward/badparams?echo=badclientandparam&bad=%88%A4 HTTP/1.1\r
Host: local\r
Connection: close\r
\r
""");
assertThat(rawResponse, containsString(" 500 "));
} }
} }
@ -246,24 +312,36 @@ public class DispatcherTest
_contextHandler.addServlet(IncludeServlet.class, "/IncludeServlet/*"); _contextHandler.addServlet(IncludeServlet.class, "/IncludeServlet/*");
_contextHandler.addServlet(AssertIncludeServlet.class, "/AssertIncludeServlet/*"); _contextHandler.addServlet(AssertIncludeServlet.class, "/AssertIncludeServlet/*");
String expected = String responses = _connector.getResponse("""
"HTTP/1.1 200 OK\r\n" + GET /context/IncludeServlet?do=assertinclude&do=more&test=1 HTTP/1.1\r
"Content-Length: 7\r\n" + Host: local\r
"\r\n" + Connection: close\r
"INCLUDE"; \r
""");
String responses = _connector.getResponse("GET /context/IncludeServlet?do=assertinclude&do=more&test=1 HTTP/1.0\n\n"); String expected = """
HTTP/1.1 200 OK\r
Content-Length: 7\r
Connection: close\r
\r
INCLUDE""";
assertEquals(expected, responses); assertEquals(expected, responses);
} }
@Test @Test
@Disabled // TODO
public void testForwardSendError() throws Exception public void testForwardSendError() throws Exception
{ {
_contextHandler.addServlet(ForwardServlet.class, "/forward/*"); _contextHandler.addServlet(ForwardServlet.class, "/forward/*");
_contextHandler.addServlet(SendErrorServlet.class, "/senderr/*"); _contextHandler.addServlet(SendErrorServlet.class, "/senderr/*");
String forwarded = _connector.getResponse("GET /context/forward?do=ctx.echo&uri=/senderr HTTP/1.0\n\n"); String forwarded = _connector.getResponse("""
GET /context/forward?do=ctx.echo&uri=/senderr HTTP/1.1\r
Host: local\r
Connection: close\r
\r
""");
assertThat(forwarded, containsString("HTTP/1.1 590 ")); assertThat(forwarded, containsString("HTTP/1.1 590 "));
assertThat(forwarded, containsString("<h2>HTTP ERROR 590 Five Nine Zero</h2>")); assertThat(forwarded, containsString("<h2>HTTP ERROR 590 Five Nine Zero</h2>"));
@ -274,16 +352,25 @@ public class DispatcherTest
{ {
_contextHandler.addServlet(RelativeDispatch2Servlet.class, "/RelDispatchServlet/*"); _contextHandler.addServlet(RelativeDispatch2Servlet.class, "/RelDispatchServlet/*");
_contextHandler.addServlet(ThrowServlet.class, "/include/throw/*"); _contextHandler.addServlet(ThrowServlet.class, "/include/throw/*");
String expected =
"HTTP/1.1 200 OK\r\n" +
"Content-Length: 56\r\n" +
"\r\n" +
"THROWING\r\n" +
"CAUGHT2 java.io.IOException: Expected\r\n" +
"AFTER\r\n";
String responses = _connector.getResponse("GET /context/RelDispatchServlet?path=include/throw HTTP/1.0\n\n"); String rawResponse = _connector.getResponse("""
assertEquals(expected, responses); GET /context/RelDispatchServlet?path=include/throw HTTP/1.1\r
Host: local\r
Connection: close\r
\r
""");
String expected = """
HTTP/1.1 200 OK\r
Content-Length: 56\r
Connection: close\r
\r
THROWING\r
CAUGHT2 java.io.IOException: Expected\r
AFTER\r
""";
assertEquals(expected, rawResponse);
} }
@Test @Test
@ -291,20 +378,29 @@ public class DispatcherTest
{ {
_contextHandler.addServlet(RelativeDispatch2Servlet.class, "/RelDispatchServlet/*"); _contextHandler.addServlet(RelativeDispatch2Servlet.class, "/RelDispatchServlet/*");
_contextHandler.addServlet(ThrowServlet.class, "/include/throw/*"); _contextHandler.addServlet(ThrowServlet.class, "/include/throw/*");
String expected =
"HTTP/1.1 200 OK\r\n" +
"Content-Length: 122\r\n" +
"\r\n" +
"BEFORE\r\n" +
"THROWING\r\n" +
"CAUGHT1 java.io.IOException: Expected\r\n" +
"BETWEEN\r\n" +
"THROWING\r\n" +
"CAUGHT2 java.io.IOException: Expected\r\n" +
"AFTER\r\n";
String responses = _connector.getResponse("GET /context/RelDispatchServlet?include=true&path=include/throw HTTP/1.0\n\n"); String rawResponse = _connector.getResponse("""
assertEquals(expected, responses); GET /context/RelDispatchServlet?include=true&path=include/throw HTTP/1.1\r
Host: local\r
Connection: close\r
\r
""");
String expected = """
HTTP/1.1 200 OK\r
Content-Length: 122\r
Connection: close\r
\r
BEFORE\r
THROWING\r
CAUGHT1 java.io.IOException: Expected\r
BETWEEN\r
THROWING\r
CAUGHT2 java.io.IOException: Expected\r
AFTER\r
""";
assertEquals(expected, rawResponse);
} }
@Test @Test
@ -314,15 +410,21 @@ public class DispatcherTest
_contextHandler.addServlet(IncludeServlet.class, "/IncludeServlet/*"); _contextHandler.addServlet(IncludeServlet.class, "/IncludeServlet/*");
_contextHandler.addServlet(AssertForwardIncludeServlet.class, "/AssertForwardIncludeServlet/*"); _contextHandler.addServlet(AssertForwardIncludeServlet.class, "/AssertForwardIncludeServlet/*");
String expected = String rawResponse = _connector.getResponse("""
"HTTP/1.1 200 OK\r\n" + GET /context/ForwardServlet/forwardpath?do=include HTTP/1.1\r
"Content-Length: 7\r\n" + Host: local\r
"\r\n" + Connection: close\r
"INCLUDE"; \r
""");
String responses = _connector.getResponse("GET /context/ForwardServlet/forwardpath?do=include HTTP/1.0\n\n"); String expected = """
HTTP/1.1 200 OK\r
Content-Length: 7\r
Connection: close\r
\r
INCLUDE""";
assertEquals(expected, responses); assertEquals(expected, rawResponse);
} }
@Test @Test
@ -332,15 +434,21 @@ public class DispatcherTest
_contextHandler.addServlet(ForwardServlet.class, "/ForwardServlet/*"); _contextHandler.addServlet(ForwardServlet.class, "/ForwardServlet/*");
_contextHandler.addServlet(AssertIncludeForwardServlet.class, "/AssertIncludeForwardServlet/*"); _contextHandler.addServlet(AssertIncludeForwardServlet.class, "/AssertIncludeForwardServlet/*");
String expected = String rawResponse = _connector.getResponse("""
"HTTP/1.1 200 OK\r\n" + GET /context/IncludeServlet/includepath?do=forward HTTP/1.1\r
"Content-Length: 7\r\n" + Host: local\r
"\r\n" + Connection: close\r
"FORWARD"; \r
""");
String responses = _connector.getResponse("GET /context/IncludeServlet/includepath?do=forward HTTP/1.0\n\n"); String expected = """
HTTP/1.1 200 OK\r
Content-Length: 7\r
Connection: close\r
\r
FORWARD""";
assertEquals(expected, responses); assertEquals(expected, rawResponse);
} }
@Test @Test
@ -349,15 +457,21 @@ public class DispatcherTest
_contextHandler.addServlet(DispatchServletServlet.class, "/dispatch/*"); _contextHandler.addServlet(DispatchServletServlet.class, "/dispatch/*");
_contextHandler.addServlet(RogerThatServlet.class, "/roger/*"); _contextHandler.addServlet(RogerThatServlet.class, "/roger/*");
String expected = String rawResponse = _connector.getResponse("""
"HTTP/1.1 200 OK\r\n" + GET /context/dispatch/test?forward=/roger/that HTTP/1.1\r
"Content-Length: 11\r\n" + Host: localhost\r
"\r\n" + Connection: close\r
"Roger That!"; \r
""");
String responses = _connector.getResponse("GET /context/dispatch/test?forward=/roger/that HTTP/1.0\n" + "Host: localhost\n\n"); String expected = """
HTTP/1.1 200 OK\r
Content-Length: 11\r
Connection: close\r
\r
Roger That!""";
assertEquals(expected, responses); assertEquals(expected, rawResponse);
} }
@Test @Test
@ -366,11 +480,16 @@ public class DispatcherTest
_contextHandler.addServlet(DispatchServletServlet.class, "/dispatch/*"); _contextHandler.addServlet(DispatchServletServlet.class, "/dispatch/*");
_contextHandler.addServlet(RogerThatServlet.class, "/roger/that"); _contextHandler.addServlet(RogerThatServlet.class, "/roger/that");
String requests = "GET /context/dispatch/test?forward=/%2e%2e/roger/that HTTP/1.0\n" + "Host: localhost\n\n"; String rawRequest = """
GET /context/dispatch/test?forward=/%2e%2e/roger/that HTTP/1.1\r
Host: localhost\r
Connection: close\r
\r
""";
String responses = _connector.getResponse(requests); String rawResponse = _connector.getResponse(rawRequest);
assertThat(responses, startsWith("HTTP/1.1 404 ")); assertThat(rawResponse, startsWith("HTTP/1.1 404 "));
} }
@Test @Test
@ -379,11 +498,16 @@ public class DispatcherTest
_contextHandler.addServlet(DispatchServletServlet.class, "/dispatch/*"); _contextHandler.addServlet(DispatchServletServlet.class, "/dispatch/*");
_contextHandler.addServlet(RogerThatServlet.class, "/roger/that"); _contextHandler.addServlet(RogerThatServlet.class, "/roger/that");
String requests = "GET /context/dispatch/test?forward=/%252e%252e/roger/that HTTP/1.0\n" + "Host: localhost\n\n"; String rawRequest = """
GET /context/dispatch/test?forward=/%252e%252e/roger/that HTTP/1.1\r
Host: localhost\r
Connection: close\r
\r
""";
String responses = _connector.getResponse(requests); String rawResponse = _connector.getResponse(rawRequest);
assertThat(responses, startsWith("HTTP/1.1 404 ")); assertThat(rawResponse, startsWith("HTTP/1.1 404 "));
} }
@Test @Test
@ -392,23 +516,36 @@ public class DispatcherTest
_contextHandler.addServlet(DispatchServletServlet.class, "/dispatch/*"); _contextHandler.addServlet(DispatchServletServlet.class, "/dispatch/*");
_contextHandler.addServlet(RogerThatServlet.class, "/roger/*"); _contextHandler.addServlet(RogerThatServlet.class, "/roger/*");
String expected = String rawResponse = _connector.getResponse("""
"HTTP/1.1 200 OK\r\n" + GET /context/dispatch/test?include=/roger/that HTTP/1.0\r
"Content-Length: 11\r\n" + Host: localhost\r
"\r\n" + Connection: close\r
"Roger That!"; \r
""");
String responses = _connector.getResponse("GET /context/dispatch/test?include=/roger/that HTTP/1.0\n" + "Host: localhost\n\n"); String expected = """
HTTP/1.1 200 OK\r
Content-Length: 11\r
\r
Roger That!""";
assertEquals(expected, responses); assertEquals(expected, rawResponse);
} }
@Test @Test
public void testWorkingResourceHandler() throws Exception public void testWorkingResourceHandler() throws Exception
{ {
String responses = _connector.getResponse("GET /resource/content.txt HTTP/1.0\n" + "Host: localhost\n\n"); String rawResponse = _connector.getResponse("""
GET /resource/content.txt HTTP/1.1\r
Host: localhost\r
Connection: close\r
\r
""");
assertThat(responses, containsString("content goes here")); // from inside the context.txt file HttpTester.Response response = HttpTester.parseResponse(rawResponse);
// from inside the context.txt file
assertThat(response.getContent(), containsString("content goes here")); // from inside the context.txt file
} }
@Test @Test
@ -417,12 +554,17 @@ public class DispatcherTest
{ {
_contextHandler.addServlet(DispatchToResourceServlet.class, "/resourceServlet/*"); _contextHandler.addServlet(DispatchToResourceServlet.class, "/resourceServlet/*");
String responses = _connector.getResponse("GET /context/resourceServlet/content.txt?do=include HTTP/1.0\n" + "Host: localhost\n\n"); String rawResponse = _connector.getResponse("""
GET /context/resourceServlet/content.txt?do=include HTTP/1.1\r
Host: localhost\r
Connection: close\r
\r
""");
HttpTester.Response response = HttpTester.parseResponse(rawResponse);
// from inside the context.txt file // from inside the context.txt file
assertNotNull(responses); assertThat(response.getContent(), containsString("content goes here"));
assertThat(responses, containsString("content goes here"));
} }
@Test @Test
@ -431,10 +573,17 @@ public class DispatcherTest
{ {
_contextHandler.addServlet(DispatchToResourceServlet.class, "/resourceServlet/*"); _contextHandler.addServlet(DispatchToResourceServlet.class, "/resourceServlet/*");
String responses = _connector.getResponse("GET /context/resourceServlet/content.txt?do=forward HTTP/1.0\n" + "Host: localhost\n\n"); String rawResponse = _connector.getResponse("""
GET /context/resourceServlet/content.txt?do=forward HTTP/1.1\r
Host: localhost\r
Connection: close\r
\r
""");
HttpTester.Response response = HttpTester.parseResponse(rawResponse);
// from inside the context.txt file // from inside the context.txt file
assertThat(responses, containsString("content goes here")); assertThat(response.getContent(), containsString("content goes here"));
} }
@Test @Test
@ -443,10 +592,17 @@ public class DispatcherTest
{ {
_contextHandler.addServlet(DispatchToResourceServlet.class, "/resourceServlet/*"); _contextHandler.addServlet(DispatchToResourceServlet.class, "/resourceServlet/*");
String responses = _connector.getResponse("GET /context/resourceServlet/content.txt?do=include&wrapped=true HTTP/1.0\n" + "Host: localhost\n\n"); String rawResponse = _connector.getResponse("""
GET /context/resourceServlet/content.txt?do=include&wrapped=true HTTP/1.1\r
Host: localhost\r
Connection: close\r
\r
""");
HttpTester.Response response = HttpTester.parseResponse(rawResponse);
// from inside the context.txt file // from inside the context.txt file
assertThat(responses, containsString("content goes here")); assertThat(response.getContent(), containsString("content goes here"));
} }
@Test @Test
@ -455,10 +611,17 @@ public class DispatcherTest
{ {
_contextHandler.addServlet(DispatchToResourceServlet.class, "/resourceServlet/*"); _contextHandler.addServlet(DispatchToResourceServlet.class, "/resourceServlet/*");
String responses = _connector.getResponse("GET /context/resourceServlet/content.txt?do=forward&wrapped=true HTTP/1.0\n" + "Host: localhost\n\n"); String rawResponse = _connector.getResponse("""
GET /context/resourceServlet/content.txt?do=forward&wrapped=true HTTP/1.1
Host: localhost\r
Connection: close\r
\r
""");
HttpTester.Response response = HttpTester.parseResponse(rawResponse);
// from inside the context.txt file // from inside the context.txt file
assertThat(responses, containsString("content goes here")); assertThat(response.getContent(), containsString("content goes here"));
} }
@Test @Test
@ -469,15 +632,38 @@ public class DispatcherTest
_contextHandler.addServlet(EchoServlet.class, "/echo/*"); _contextHandler.addServlet(EchoServlet.class, "/echo/*");
_contextHandler.addFilter(ForwardFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST)); _contextHandler.addFilter(ForwardFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
String rogerResponse = _connector.getResponse("GET /context/ HTTP/1.0\n" + "Host: localhost\n\n"); HttpTester.Response response;
String rawResponse;
String echoResponse = _connector.getResponse("GET /context/foo?echo=echoText HTTP/1.0\n" + "Host: localhost\n\n"); rawResponse = _connector.getResponse("""
GET /context/ HTTP/1.1\r
Host: localhost\r
Connection: close\r
\r
""");
String rechoResponse = _connector.getResponse("GET /context/?echo=echoText HTTP/1.0\n" + "Host: localhost\n\n"); response = HttpTester.parseResponse(rawResponse);
assertThat(response.getContent(), containsString("Roger That!"));
assertThat(rogerResponse, containsString("Roger That!")); rawResponse = _connector.getResponse("""
assertThat(echoResponse, containsString("echoText")); GET /context/foo?echo=echoText HTTP/1.1\r
assertThat(rechoResponse, containsString("txeTohce")); Host: localhost\r
Connection: close\r
\r
""");
response = HttpTester.parseResponse(rawResponse);
assertThat(response.getContent(), containsString("echoText"));
rawResponse = _connector.getResponse("""
GET /context/?echo=echoText HTTP/1.1\r
Host: localhost\r
Connection: close\r
\r
""");
response = HttpTester.parseResponse(rawResponse);
assertThat(response.getContent(), containsString("txeTohce"));
} }
@Test @Test
@ -496,13 +682,28 @@ public class DispatcherTest
_contextHandler.addServlet(new ServletHolder("DispatchServlet", AsyncDispatch2TestServlet.class), "/DispatchServlet"); _contextHandler.addServlet(new ServletHolder("DispatchServlet", AsyncDispatch2TestServlet.class), "/DispatchServlet");
_contextHandler.addServlet(new ServletHolder("DispatchServlet2", AsyncDispatch2TestServlet.class), "/DispatchServlet2"); _contextHandler.addServlet(new ServletHolder("DispatchServlet2", AsyncDispatch2TestServlet.class), "/DispatchServlet2");
HttpTester.Response response;
String rawResponse;
// TODO Test TCK hack for https://github.com/eclipse-ee4j/jakartaee-tck/issues/585 // TODO Test TCK hack for https://github.com/eclipse-ee4j/jakartaee-tck/issues/585
String response = _connector.getResponse("GET /context/DispatchServlet HTTP/1.0\n\n"); rawResponse = _connector.getResponse("""
assertThat(response, containsString("matchValue=DispatchServlet, pattern=/DispatchServlet, servletName=DispatchServlet, mappingMatch=EXACT")); GET /context/DispatchServlet HTTP/1.1\r
Host: local\r
Connection: close\r
\r
""");
response = HttpTester.parseResponse(rawResponse);
assertThat(response.getContent(), containsString("matchValue=DispatchServlet, pattern=/DispatchServlet, servletName=DispatchServlet, mappingMatch=EXACT"));
// TODO Test how it should work after fix for https://github.com/eclipse-ee4j/jakartaee-tck/issues/585 // TODO Test how it should work after fix for https://github.com/eclipse-ee4j/jakartaee-tck/issues/585
String response2 = _connector.getResponse("GET /context/DispatchServlet2 HTTP/1.0\n\n"); rawResponse = _connector.getResponse("""
assertThat(response2, containsString("matchValue=TestServlet, pattern=/TestServlet, servletName=TestServlet, mappingMatch=EXACT")); GET /context/DispatchServlet2 HTTP/1.1\r
Host: local\r
Connection: close\r
\r
""");
response = HttpTester.parseResponse(rawResponse);
assertThat(response.getContent(), containsString("matchValue=TestServlet, pattern=/TestServlet, servletName=TestServlet, mappingMatch=EXACT"));
} }
public static class WrappingFilter implements Filter public static class WrappingFilter implements Filter
@ -553,13 +754,13 @@ public class DispatcherTest
} }
@Override @Override
public void write(int b) throws IOException public void write(int b)
{ {
buffer.write(b); buffer.write(b);
} }
@Override @Override
public void write(byte[] b, int off, int len) throws IOException public void write(byte[] b, int off, int len)
{ {
buffer.write(b, off, len); buffer.write(b, off, len);
} }
@ -596,6 +797,7 @@ public class DispatcherTest
dispatcher = getServletContext().getRequestDispatcher(request.getParameter("uri")); dispatcher = getServletContext().getRequestDispatcher(request.getParameter("uri"));
else if (request.getParameter("do").equals("req.echo")) else if (request.getParameter("do").equals("req.echo"))
dispatcher = request.getRequestDispatcher(request.getParameter("uri")); dispatcher = request.getRequestDispatcher(request.getParameter("uri"));
assert dispatcher != null;
dispatcher.forward(request, response); dispatcher.forward(request, response);
} }
} }
@ -637,7 +839,7 @@ public class DispatcherTest
@Override @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{ {
RequestDispatcher dispatcher = null; RequestDispatcher dispatcher;
request.setAttribute("org.eclipse.jetty.server.Request.queryEncoding", "cp1251"); request.setAttribute("org.eclipse.jetty.server.Request.queryEncoding", "cp1251");
dispatcher = getServletContext().getRequestDispatcher("/AssertForwardServlet?do=end&else=%D0%B2%D1%8B%D0%B1%D1%80%D0%B0%D0%BD%D0%BE%3D%D0%A2%D0%B5%D0%BC%D0%BF%D0%B5%D1%80%D0%B0%D1%82%D1%83%D1%80%D0%B0"); dispatcher = getServletContext().getRequestDispatcher("/AssertForwardServlet?do=end&else=%D0%B2%D1%8B%D0%B1%D1%80%D0%B0%D0%BD%D0%BE%3D%D0%A2%D0%B5%D0%BC%D0%BF%D0%B5%D1%80%D0%B0%D1%82%D1%83%D1%80%D0%B0");
dispatcher.forward(request, response); dispatcher.forward(request, response);
@ -666,15 +868,12 @@ public class DispatcherTest
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
{ {
if (servletContext == null || !(request instanceof HttpServletRequest) || !(response instanceof HttpServletResponse)) if (servletContext == null || !(request instanceof HttpServletRequest req) || !(response instanceof HttpServletResponse))
{ {
chain.doFilter(request, response); chain.doFilter(request, response);
return; return;
} }
HttpServletRequest req = (HttpServletRequest)request;
HttpServletResponse resp = (HttpServletResponse)response;
if (req.getParameter("echo") != null && "/".equals(req.getPathInfo())) if (req.getParameter("echo") != null && "/".equals(req.getPathInfo()))
{ {
RequestDispatcher dispatcher = servletContext.getRequestDispatcher("/recho"); RequestDispatcher dispatcher = servletContext.getRequestDispatcher("/recho");
@ -688,7 +887,6 @@ public class DispatcherTest
else else
{ {
chain.doFilter(request, response); chain.doFilter(request, response);
return;
} }
} }
@ -704,7 +902,7 @@ public class DispatcherTest
@Override @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{ {
RequestDispatcher dispatcher = null; RequestDispatcher dispatcher;
if (request.getParameter("include") != null) if (request.getParameter("include") != null)
{ {
@ -735,6 +933,7 @@ public class DispatcherTest
dispatcher = getServletContext().getRequestDispatcher("/AssertForwardIncludeServlet/assertpath?do=end"); dispatcher = getServletContext().getRequestDispatcher("/AssertForwardIncludeServlet/assertpath?do=end");
else if (request.getParameter("do").equals("assertinclude")) else if (request.getParameter("do").equals("assertinclude"))
dispatcher = getServletContext().getRequestDispatcher("/AssertIncludeServlet?do=end&do=the"); dispatcher = getServletContext().getRequestDispatcher("/AssertIncludeServlet?do=end&do=the");
assert dispatcher != null;
dispatcher.include(request, response); dispatcher.include(request, response);
} }
} }
@ -744,7 +943,6 @@ public class DispatcherTest
@Override @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{ {
RequestDispatcher dispatcher = null;
String path = request.getParameter("path"); String path = request.getParameter("path");
String include = request.getParameter("include"); String include = request.getParameter("include");
ServletOutputStream out = response.getOutputStream(); ServletOutputStream out = response.getOutputStream();
@ -845,7 +1043,7 @@ public class DispatcherTest
} }
else else
{ {
res.getWriter().print(new StringBuffer(echoText).reverse().toString()); res.getWriter().print(new StringBuffer(echoText).reverse());
} }
} }
} }
@ -914,7 +1112,7 @@ public class DispatcherTest
assertEquals("/context/ForwardServlet", request.getAttribute(Dispatcher.FORWARD_REQUEST_URI)); assertEquals("/context/ForwardServlet", request.getAttribute(Dispatcher.FORWARD_REQUEST_URI));
assertEquals("/context", request.getAttribute(Dispatcher.FORWARD_CONTEXT_PATH)); assertEquals("/context", request.getAttribute(Dispatcher.FORWARD_CONTEXT_PATH));
assertEquals("/ForwardServlet", request.getAttribute(Dispatcher.FORWARD_SERVLET_PATH)); assertEquals("/ForwardServlet", request.getAttribute(Dispatcher.FORWARD_SERVLET_PATH));
assertEquals(null, request.getAttribute(Dispatcher.FORWARD_PATH_INFO)); assertNull(request.getAttribute(Dispatcher.FORWARD_PATH_INFO));
assertEquals("do=assertforward&do=more&test=1", request.getAttribute(Dispatcher.FORWARD_QUERY_STRING)); assertEquals("do=assertforward&do=more&test=1", request.getAttribute(Dispatcher.FORWARD_QUERY_STRING));
HttpServletMapping fwdMapping = (HttpServletMapping)request.getAttribute(Dispatcher.FORWARD_MAPPING); HttpServletMapping fwdMapping = (HttpServletMapping)request.getAttribute(Dispatcher.FORWARD_MAPPING);
assertNotNull(fwdMapping); assertNotNull(fwdMapping);
@ -925,8 +1123,8 @@ public class DispatcherTest
List<String> requestAttributeNames = Collections.list(request.getAttributeNames()); List<String> requestAttributeNames = Collections.list(request.getAttributeNames());
assertTrue(requestAttributeNames.containsAll(expectedAttributeNames)); assertTrue(requestAttributeNames.containsAll(expectedAttributeNames));
assertEquals(null, request.getPathInfo()); assertNull(request.getPathInfo());
assertEquals(null, request.getPathTranslated()); assertNull(request.getPathTranslated());
assertEquals("do=end&do=the", request.getQueryString()); assertEquals("do=end&do=the", request.getQueryString());
assertEquals("/context/AssertForwardServlet", request.getRequestURI()); assertEquals("/context/AssertForwardServlet", request.getRequestURI());
assertEquals("/context", request.getContextPath()); assertEquals("/context", request.getContextPath());
@ -943,13 +1141,13 @@ public class DispatcherTest
@Override @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{ {
byte[] cp1251Bytes = TypeUtil.fromHexString("d2e5ecefe5f0e0f2f3f0e0"); byte[] cp1251Bytes = StringUtil.fromHexString("d2e5ecefe5f0e0f2f3f0e0");
String expectedCP1251String = new String(cp1251Bytes, "cp1251"); String expectedCP1251String = new String(cp1251Bytes, "cp1251");
assertEquals("/context/ForwardServlet", request.getAttribute(Dispatcher.FORWARD_REQUEST_URI)); assertEquals("/context/ForwardServlet", request.getAttribute(Dispatcher.FORWARD_REQUEST_URI));
assertEquals("/context", request.getAttribute(Dispatcher.FORWARD_CONTEXT_PATH)); assertEquals("/context", request.getAttribute(Dispatcher.FORWARD_CONTEXT_PATH));
assertEquals("/ForwardServlet", request.getAttribute(Dispatcher.FORWARD_SERVLET_PATH)); assertEquals("/ForwardServlet", request.getAttribute(Dispatcher.FORWARD_SERVLET_PATH));
assertEquals(null, request.getAttribute(Dispatcher.FORWARD_PATH_INFO)); assertNull(request.getAttribute(Dispatcher.FORWARD_PATH_INFO));
assertEquals("do=assertforward&foreign=%d2%e5%ec%ef%e5%f0%e0%f2%f3%f0%e0&test=1", request.getAttribute(Dispatcher.FORWARD_QUERY_STRING)); assertEquals("do=assertforward&foreign=%d2%e5%ec%ef%e5%f0%e0%f2%f3%f0%e0&test=1", request.getAttribute(Dispatcher.FORWARD_QUERY_STRING));
HttpServletMapping fwdMapping = (HttpServletMapping)request.getAttribute(Dispatcher.FORWARD_MAPPING); HttpServletMapping fwdMapping = (HttpServletMapping)request.getAttribute(Dispatcher.FORWARD_MAPPING);
assertNotNull(fwdMapping); assertNotNull(fwdMapping);
@ -960,8 +1158,8 @@ public class DispatcherTest
List<String> requestAttributeNames = Collections.list(request.getAttributeNames()); List<String> requestAttributeNames = Collections.list(request.getAttributeNames());
assertTrue(requestAttributeNames.containsAll(expectedAttributeNames)); assertTrue(requestAttributeNames.containsAll(expectedAttributeNames));
assertEquals(null, request.getPathInfo()); assertNull(request.getPathInfo());
assertEquals(null, request.getPathTranslated()); assertNull(request.getPathTranslated());
MultiMap<String> query = new MultiMap<>(); MultiMap<String> query = new MultiMap<>();
UrlEncoded.decodeTo(request.getQueryString(), query, UrlEncoded.ENCODING); UrlEncoded.decodeTo(request.getQueryString(), query, UrlEncoded.ENCODING);
@ -976,7 +1174,7 @@ public class DispatcherTest
assertThat(query.containsKey("foreign"), is(false)); assertThat(query.containsKey("foreign"), is(false));
String[] vals = request.getParameterValues("foreign"); String[] vals = request.getParameterValues("foreign");
assertTrue(vals != null); assertNotNull(vals);
assertEquals(1, vals.length); assertEquals(1, vals.length);
assertEquals(expectedCP1251String, vals[0]); assertEquals(expectedCP1251String, vals[0]);
@ -998,19 +1196,19 @@ public class DispatcherTest
assertEquals("/context/AssertIncludeServlet", request.getAttribute(Dispatcher.INCLUDE_REQUEST_URI)); assertEquals("/context/AssertIncludeServlet", request.getAttribute(Dispatcher.INCLUDE_REQUEST_URI));
assertEquals("/context", request.getAttribute(Dispatcher.INCLUDE_CONTEXT_PATH)); assertEquals("/context", request.getAttribute(Dispatcher.INCLUDE_CONTEXT_PATH));
assertEquals("/AssertIncludeServlet", request.getAttribute(Dispatcher.INCLUDE_SERVLET_PATH)); assertEquals("/AssertIncludeServlet", request.getAttribute(Dispatcher.INCLUDE_SERVLET_PATH));
assertEquals(null, request.getAttribute(Dispatcher.INCLUDE_PATH_INFO)); assertNull(request.getAttribute(Dispatcher.INCLUDE_PATH_INFO));
assertEquals("do=end&do=the", request.getAttribute(Dispatcher.INCLUDE_QUERY_STRING)); assertEquals("do=end&do=the", request.getAttribute(Dispatcher.INCLUDE_QUERY_STRING));
HttpServletMapping incMapping = (HttpServletMapping)request.getAttribute(Dispatcher.INCLUDE_MAPPING); HttpServletMapping incMapping = (HttpServletMapping)request.getAttribute(Dispatcher.INCLUDE_MAPPING);
assertNotNull(incMapping); assertNotNull(incMapping);
assertEquals("AssertIncludeServlet", incMapping.getMatchValue()); assertEquals("AssertIncludeServlet", incMapping.getMatchValue());
List expectedAttributeNames = Arrays.asList(Dispatcher.INCLUDE_REQUEST_URI, Dispatcher.INCLUDE_CONTEXT_PATH, List<String> expectedAttributeNames = Arrays.asList(Dispatcher.INCLUDE_REQUEST_URI, Dispatcher.INCLUDE_CONTEXT_PATH,
Dispatcher.INCLUDE_SERVLET_PATH, Dispatcher.INCLUDE_QUERY_STRING, Dispatcher.INCLUDE_MAPPING); Dispatcher.INCLUDE_SERVLET_PATH, Dispatcher.INCLUDE_QUERY_STRING, Dispatcher.INCLUDE_MAPPING);
List requestAttributeNames = Collections.list(request.getAttributeNames()); List<String> requestAttributeNames = Collections.list(request.getAttributeNames());
assertTrue(requestAttributeNames.containsAll(expectedAttributeNames)); assertTrue(requestAttributeNames.containsAll(expectedAttributeNames));
assertEquals(null, request.getPathInfo()); assertNull(request.getPathInfo());
assertEquals(null, request.getPathTranslated()); assertNull(request.getPathTranslated());
assertEquals("do=assertinclude&do=more&test=1", request.getQueryString()); assertEquals("do=assertinclude&do=more&test=1", request.getQueryString());
assertEquals("/context/IncludeServlet", request.getRequestURI()); assertEquals("/context/IncludeServlet", request.getRequestURI());
assertEquals("/context", request.getContextPath()); assertEquals("/context", request.getContextPath());
@ -1046,15 +1244,15 @@ public class DispatcherTest
assertNotNull(incMapping); assertNotNull(incMapping);
assertEquals("AssertForwardIncludeServlet", incMapping.getMatchValue()); assertEquals("AssertForwardIncludeServlet", incMapping.getMatchValue());
List expectedAttributeNames = Arrays.asList(Dispatcher.FORWARD_REQUEST_URI, Dispatcher.FORWARD_CONTEXT_PATH, Dispatcher.FORWARD_SERVLET_PATH, List<String> expectedAttributeNames = Arrays.asList(Dispatcher.FORWARD_REQUEST_URI, Dispatcher.FORWARD_CONTEXT_PATH, Dispatcher.FORWARD_SERVLET_PATH,
Dispatcher.FORWARD_PATH_INFO, Dispatcher.FORWARD_QUERY_STRING, Dispatcher.FORWARD_MAPPING, Dispatcher.FORWARD_PATH_INFO, Dispatcher.FORWARD_QUERY_STRING, Dispatcher.FORWARD_MAPPING,
Dispatcher.INCLUDE_REQUEST_URI, Dispatcher.INCLUDE_CONTEXT_PATH, Dispatcher.INCLUDE_SERVLET_PATH, Dispatcher.INCLUDE_REQUEST_URI, Dispatcher.INCLUDE_CONTEXT_PATH, Dispatcher.INCLUDE_SERVLET_PATH,
Dispatcher.INCLUDE_PATH_INFO, Dispatcher.INCLUDE_QUERY_STRING, Dispatcher.INCLUDE_MAPPING); Dispatcher.INCLUDE_PATH_INFO, Dispatcher.INCLUDE_QUERY_STRING, Dispatcher.INCLUDE_MAPPING);
List requestAttributeNames = Collections.list(request.getAttributeNames()); List<String> requestAttributeNames = Collections.list(request.getAttributeNames());
assertTrue(requestAttributeNames.containsAll(expectedAttributeNames)); assertTrue(requestAttributeNames.containsAll(expectedAttributeNames));
assertEquals("/includepath", request.getPathInfo()); assertEquals("/includepath", request.getPathInfo());
assertEquals(null, request.getPathTranslated()); assertNull(request.getPathTranslated());
assertEquals("do=assertforwardinclude", request.getQueryString()); assertEquals("do=assertforwardinclude", request.getQueryString());
assertEquals("/context/IncludeServlet/includepath", request.getRequestURI()); assertEquals("/context/IncludeServlet/includepath", request.getRequestURI());
assertEquals("/context", request.getContextPath()); assertEquals("/context", request.getContextPath());
@ -1072,12 +1270,12 @@ public class DispatcherTest
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{ {
// forward hides include // forward hides include
assertEquals(null, request.getAttribute(Dispatcher.INCLUDE_REQUEST_URI)); assertNull(request.getAttribute(Dispatcher.INCLUDE_REQUEST_URI));
assertEquals(null, request.getAttribute(Dispatcher.INCLUDE_CONTEXT_PATH)); assertNull(request.getAttribute(Dispatcher.INCLUDE_CONTEXT_PATH));
assertEquals(null, request.getAttribute(Dispatcher.INCLUDE_SERVLET_PATH)); assertNull(request.getAttribute(Dispatcher.INCLUDE_SERVLET_PATH));
assertEquals(null, request.getAttribute(Dispatcher.INCLUDE_PATH_INFO)); assertNull(request.getAttribute(Dispatcher.INCLUDE_PATH_INFO));
assertEquals(null, request.getAttribute(Dispatcher.INCLUDE_QUERY_STRING)); assertNull(request.getAttribute(Dispatcher.INCLUDE_QUERY_STRING));
assertEquals(null, request.getAttribute(Dispatcher.INCLUDE_MAPPING)); assertNull(request.getAttribute(Dispatcher.INCLUDE_MAPPING));
assertEquals("/context/IncludeServlet/includepath", request.getAttribute(Dispatcher.FORWARD_REQUEST_URI)); assertEquals("/context/IncludeServlet/includepath", request.getAttribute(Dispatcher.FORWARD_REQUEST_URI));
assertEquals("/context", request.getAttribute(Dispatcher.FORWARD_CONTEXT_PATH)); assertEquals("/context", request.getAttribute(Dispatcher.FORWARD_CONTEXT_PATH));
@ -1088,13 +1286,13 @@ public class DispatcherTest
assertNotNull(fwdMapping); assertNotNull(fwdMapping);
assertEquals("IncludeServlet", fwdMapping.getMatchValue()); assertEquals("IncludeServlet", fwdMapping.getMatchValue());
List expectedAttributeNames = Arrays.asList(Dispatcher.FORWARD_REQUEST_URI, Dispatcher.FORWARD_CONTEXT_PATH, Dispatcher.FORWARD_SERVLET_PATH, List<String> expectedAttributeNames = Arrays.asList(Dispatcher.FORWARD_REQUEST_URI, Dispatcher.FORWARD_CONTEXT_PATH, Dispatcher.FORWARD_SERVLET_PATH,
Dispatcher.FORWARD_PATH_INFO, Dispatcher.FORWARD_QUERY_STRING, Dispatcher.FORWARD_MAPPING); Dispatcher.FORWARD_PATH_INFO, Dispatcher.FORWARD_QUERY_STRING, Dispatcher.FORWARD_MAPPING);
List requestAttributeNames = Collections.list(request.getAttributeNames()); List<String> requestAttributeNames = Collections.list(request.getAttributeNames());
assertTrue(requestAttributeNames.containsAll(expectedAttributeNames)); assertTrue(requestAttributeNames.containsAll(expectedAttributeNames));
assertEquals("/assertpath", request.getPathInfo()); assertEquals("/assertpath", request.getPathInfo());
assertEquals(null, request.getPathTranslated()); assertNull(request.getPathTranslated());
assertEquals("do=end", request.getQueryString()); assertEquals("do=end", request.getQueryString());
assertEquals("/context/AssertIncludeForwardServlet/assertpath", request.getRequestURI()); assertEquals("/context/AssertIncludeForwardServlet/assertpath", request.getRequestURI());
assertEquals("/context", request.getContextPath()); assertEquals("/context", request.getContextPath());
@ -1119,12 +1317,11 @@ public class DispatcherTest
} }
else else
{ {
StringBuilder sb = new StringBuilder(); String sb = "matchValue=" + mapping.getMatchValue() +
sb.append("matchValue=" + mapping.getMatchValue()) ", pattern=" + mapping.getPattern() +
.append(", pattern=" + mapping.getPattern()) ", servletName=" + mapping.getServletName() +
.append(", servletName=" + mapping.getServletName()) ", mappingMatch=" + mapping.getMappingMatch();
.append(", mappingMatch=" + mapping.getMappingMatch()); resp.getWriter().println(sb);
resp.getWriter().println(sb.toString());
} }
} }