use of org.apache.catalina.Context in project tomcat by apache.
the class TestCoyoteAdapter method testBug54928.
@Test
public void testBug54928() throws Exception {
// Setup Tomcat instance
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
AsyncServlet servlet = new AsyncServlet();
Wrapper w = Tomcat.addServlet(ctx, "async", servlet);
w.setAsyncSupported(true);
ctx.addServletMappingDecoded("/async", "async");
tomcat.start();
SimpleHttpClient client = new SimpleHttpClient() {
@Override
public boolean isResponseBodyOK() {
return true;
}
};
String request = "GET /async HTTP/1.1" + SimpleHttpClient.CRLF + "Host: a" + SimpleHttpClient.CRLF + SimpleHttpClient.CRLF;
client.setPort(getPort());
client.setRequest(new String[] { request });
client.connect();
client.sendRequest();
for (int i = 0; i < 10; i++) {
String line = client.readLine();
if (line != null && line.length() > 20) {
log.info(line.subSequence(0, 20) + "...");
}
}
client.disconnect();
// Wait for server thread to stop
Thread t = servlet.getThread();
long startTime = System.nanoTime();
t.join(5000);
long endTime = System.nanoTime();
log.info("Waited for servlet thread to stop for " + (endTime - startTime) / 1000000 + " ms");
Assert.assertTrue(servlet.isCompleted());
}
use of org.apache.catalina.Context in project tomcat by apache.
the class TestCoyoteAdapter method pathParamExtensionTest.
private void pathParamExtensionTest(String path, String expected) throws Exception {
// Setup Tomcat instance
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("/testapp", null);
Tomcat.addServlet(ctx, "servlet", new PathParamServlet());
ctx.addServletMappingDecoded("*.txt", "servlet");
tomcat.start();
ByteChunk res = getUrl("http://localhost:" + getPort() + path);
Assert.assertEquals(expected, res.toString());
}
use of org.apache.catalina.Context in project tomcat by apache.
the class TestCoyoteAdapter method doTestUriDecoding.
private void doTestUriDecoding(String path, String encoding, String expectedPathInfo) throws Exception {
// Setup Tomcat instance
Tomcat tomcat = getTomcatInstance();
tomcat.getConnector().setURIEncoding(encoding);
// No file system docBase required
Context ctx = tomcat.addContext("", null);
PathInfoServlet servlet = new PathInfoServlet();
Tomcat.addServlet(ctx, "servlet", servlet);
ctx.addServletMappingDecoded("/*", "servlet");
tomcat.start();
int rc = getUrl("http://localhost:" + getPort() + path, new ByteChunk(), null);
Assert.assertEquals(HttpServletResponse.SC_OK, rc);
Assert.assertEquals(expectedPathInfo, servlet.getPathInfo());
}
use of org.apache.catalina.Context in project tomcat by apache.
the class TestApplicationContextGetRequestDispatcher method doTestGetRequestDispatcher.
private void doTestGetRequestDispatcher(boolean useEncodedDispatchPaths, String startPath, String startQueryString, String dispatchPath, String targetPath, String expectedBody) throws Exception {
// Setup Tomcat instance
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("/test", null);
ctx.setDispatchersUseEncodedPaths(useEncodedDispatchPaths);
// Add a default servlet to return 404 for not found resources
Tomcat.addServlet(ctx, "Default", new Default404Servlet());
ctx.addServletMappingDecoded("/*", "Default");
// Add a target servlet to dispatch to
Tomcat.addServlet(ctx, "target", new TargetServlet());
ctx.addServletMappingDecoded(UDecoder.URLDecode(targetPath, "UTF-8"), "target");
if (useAsync) {
Wrapper w = Tomcat.addServlet(ctx, "rd", new AsyncDispatcherServlet(dispatchPath, useEncodedDispatchPaths));
w.setAsyncSupported(true);
} else {
Tomcat.addServlet(ctx, "rd", new DispatcherServlet(dispatchPath));
}
ctx.addServletMappingDecoded(UDecoder.URLDecode(startPath, "UTF-8"), "rd");
tomcat.start();
StringBuilder url = new StringBuilder("http://localhost:");
url.append(getPort());
url.append("/test");
url.append(startPath);
if (startQueryString != null) {
url.append('?');
url.append(startQueryString);
}
ByteChunk bc = getUrl(url.toString());
String body = bc.toString();
Assert.assertEquals(expectedBody, body);
}
use of org.apache.catalina.Context in project tomcat by apache.
the class TestApplicationHttpRequest method testParameterImmutability.
@Test
public void testParameterImmutability() throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
Tomcat.addServlet(ctx, "forward", new ForwardServlet("/modify"));
ctx.addServletMappingDecoded("/forward", "forward");
Tomcat.addServlet(ctx, "modify", new ModifyParameterServlet());
ctx.addServletMappingDecoded("/modify", "modify");
tomcat.start();
ByteChunk response = new ByteChunk();
StringBuilder target = new StringBuilder("http://localhost:");
target.append(getPort());
target.append("/forward");
int rc = getUrl(target.toString(), response, null);
Assert.assertEquals(200, rc);
Assert.assertEquals("OK", response.toString());
}
Aggregations