use of org.apache.catalina.Context in project tomcat by apache.
the class TestHttp11Processor method doTestBug53677.
private void doTestBug53677(boolean flush) throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
Tomcat.addServlet(ctx, "LargeHeaderServlet", new LargeHeaderServlet(flush));
ctx.addServletMappingDecoded("/test", "LargeHeaderServlet");
tomcat.start();
ByteChunk responseBody = new ByteChunk();
Map<String, List<String>> responseHeaders = new HashMap<>();
int rc = getUrl("http://localhost:" + getPort() + "/test", responseBody, responseHeaders);
assertEquals(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, rc);
if (responseBody.getLength() > 0) {
// It will be >0 if the standard error page handling has been
// triggered
assertFalse(responseBody.toString().contains("FAIL"));
}
}
use of org.apache.catalina.Context in project tomcat by apache.
the class TestHttp11Processor method testBug59310.
@Test
public void testBug59310() throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
Tomcat.addServlet(ctx, "Bug59310", new Bug59310Servlet());
ctx.addServletMappingDecoded("/test", "Bug59310");
tomcat.start();
ByteChunk responseBody = new ByteChunk();
Map<String, List<String>> responseHeaders = new HashMap<>();
int rc = headUrl("http://localhost:" + getPort() + "/test", responseBody, responseHeaders);
assertEquals(HttpServletResponse.SC_OK, rc);
assertEquals(0, responseBody.getLength());
assertFalse(responseHeaders.containsKey("Content-Length"));
}
use of org.apache.catalina.Context in project tomcat by apache.
the class TestHttp11Processor method testNoChunking11NoContentLengthConnectionClose.
@Test
public void testNoChunking11NoContentLengthConnectionClose() throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
Tomcat.addServlet(ctx, "NoContentLengthConnectionCloseFlushingServlet", new NoContentLengthConnectionCloseFlushingServlet());
ctx.addServletMappingDecoded("/test", "NoContentLengthConnectionCloseFlushingServlet");
tomcat.start();
ByteChunk responseBody = new ByteChunk();
Map<String, List<String>> responseHeaders = new HashMap<>();
int rc = getUrl("http://localhost:" + getPort() + "/test", responseBody, responseHeaders);
assertEquals(HttpServletResponse.SC_OK, rc);
assertTrue(responseHeaders.containsKey("Connection"));
List<String> connections = responseHeaders.get("Connection");
assertEquals(1, connections.size());
assertEquals("close", connections.get(0));
assertFalse(responseHeaders.containsKey("Transfer-Encoding"));
assertEquals("OK", responseBody.toString());
}
use of org.apache.catalina.Context in project tomcat by apache.
the class TestHttp11Processor method testRequestBodySwallowing.
/*
* Tests what happens if a request is completed during a dispatch but the
* request body has not been fully read.
*/
@Test
public void testRequestBodySwallowing() throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
DispatchingServlet servlet = new DispatchingServlet();
Wrapper w = Tomcat.addServlet(ctx, "Test", servlet);
w.setAsyncSupported(true);
ctx.addServletMappingDecoded("/test", "Test");
tomcat.start();
// Hand-craft the client so we have complete control over the timing
SocketAddress addr = new InetSocketAddress("localhost", getPort());
Socket socket = new Socket();
socket.setSoTimeout(300000);
socket.connect(addr, 300000);
OutputStream os = socket.getOutputStream();
Writer writer = new OutputStreamWriter(os, "ISO-8859-1");
InputStream is = socket.getInputStream();
Reader r = new InputStreamReader(is, "ISO-8859-1");
BufferedReader reader = new BufferedReader(r);
// Write the headers
writer.write("POST /test HTTP/1.1\r\n");
writer.write("Host: localhost:8080\r\n");
writer.write("Transfer-Encoding: chunked\r\n");
writer.write("\r\n");
writer.flush();
validateResponse(reader);
// Write the request body
writer.write("2\r\n");
writer.write("AB\r\n");
writer.write("0\r\n");
writer.write("\r\n");
writer.flush();
// Write the 2nd request
writer.write("POST /test HTTP/1.1\r\n");
writer.write("Host: localhost:8080\r\n");
writer.write("Transfer-Encoding: chunked\r\n");
writer.write("\r\n");
writer.flush();
// Read the 2nd response
validateResponse(reader);
// Write the 2nd request body
writer.write("2\r\n");
writer.write("AB\r\n");
writer.write("0\r\n");
writer.write("\r\n");
writer.flush();
// Done
socket.close();
}
use of org.apache.catalina.Context in project tomcat by apache.
the class TestUpgradeInternalHandler method doUpgrade.
private UpgradeConnection doUpgrade(Class<? extends HttpUpgradeHandler> upgradeHandlerClass) throws Exception {
// Setup Tomcat instance
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
UpgradeServlet servlet = new UpgradeServlet(upgradeHandlerClass);
Tomcat.addServlet(ctx, "servlet", servlet);
ctx.addServletMappingDecoded("/", "servlet");
tomcat.start();
// Use raw socket so the necessary control is available after the HTTP
// upgrade
Socket socket = SocketFactory.getDefault().createSocket("localhost", getPort());
socket.setSoTimeout(5000);
UpgradeConnection uc = new UpgradeConnection(socket);
uc.getWriter().write("GET / HTTP/1.1" + CRLF);
uc.getWriter().write("Host: whatever" + CRLF);
uc.getWriter().write(CRLF);
uc.getWriter().flush();
String status = uc.getReader().readLine();
Assert.assertNotNull(status);
Assert.assertEquals("101", getStatusCode(status));
// Skip the remaining response headers
String line = uc.getReader().readLine();
while (line != null && line.length() > 0) {
// Skip
line = uc.getReader().readLine();
}
return uc;
}
Aggregations