use of org.apache.catalina.Context in project tomcat by apache.
the class TestTomcat method testEnableNaming.
/*
* Test for enabling JNDI.
*/
@Test
public void testEnableNaming() throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
// Enable JNDI - it is disabled by default
tomcat.enableNaming();
ContextEnvironment environment = new ContextEnvironment();
environment.setType("java.lang.String");
environment.setName(HelloWorldJndi.JNDI_ENV_NAME);
environment.setValue("Tomcat User");
ctx.getNamingResources().addEnvironment(environment);
Tomcat.addServlet(ctx, "jndiServlet", new HelloWorldJndi());
ctx.addServletMappingDecoded("/", "jndiServlet");
tomcat.start();
ByteChunk res = getUrl("http://localhost:" + getPort() + "/");
assertEquals("Hello, Tomcat User", res.toString());
}
use of org.apache.catalina.Context in project tomcat by apache.
the class TestHttp11Processor method testResponseWithErrorChunked.
@Test
public void testResponseWithErrorChunked() throws Exception {
Tomcat tomcat = getTomcatInstance();
// This setting means the connection will be closed at the end of the
// request
tomcat.getConnector().setAttribute("maxKeepAliveRequests", "1");
// No file system docBase required
Context ctx = tomcat.addContext("", null);
// Add protected servlet
Tomcat.addServlet(ctx, "ChunkedResponseWithErrorServlet", new ResponseWithErrorServlet(true));
ctx.addServletMappingDecoded("/*", "ChunkedResponseWithErrorServlet");
tomcat.start();
String request = "GET /anything HTTP/1.1" + SimpleHttpClient.CRLF + "Host: any" + SimpleHttpClient.CRLF + SimpleHttpClient.CRLF;
Client client = new Client(tomcat.getConnector().getLocalPort());
client.setRequest(new String[] { request });
client.connect();
client.processRequest();
// Expected response is a 200 response followed by an incomplete chunked
// body.
assertTrue(client.isResponse200());
// Should use chunked encoding
String transferEncoding = null;
for (String header : client.getResponseHeaders()) {
if (header.startsWith("Transfer-Encoding:")) {
transferEncoding = header.substring(18).trim();
}
}
Assert.assertEquals("chunked", transferEncoding);
// There should not be an end chunk
assertFalse(client.getResponseBody().endsWith("0"));
// The last portion of text should be there
assertTrue(client.getResponseBody().endsWith("line03"));
}
use of org.apache.catalina.Context in project tomcat by apache.
the class TestChunkedInputFilter method testNoTrailingHeaders.
@Test
public void testNoTrailingHeaders() throws Exception {
// Setup Tomcat instance
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
Tomcat.addServlet(ctx, "servlet", new EchoHeaderServlet(true));
ctx.addServletMappingDecoded("/", "servlet");
tomcat.start();
String request = "POST /echo-params.jsp HTTP/1.1" + SimpleHttpClient.CRLF + "Host: any" + SimpleHttpClient.CRLF + "Transfer-encoding: chunked" + SimpleHttpClient.CRLF + "Content-Type: application/x-www-form-urlencoded" + SimpleHttpClient.CRLF + "Connection: close" + SimpleHttpClient.CRLF + SimpleHttpClient.CRLF + "3" + SimpleHttpClient.CRLF + "a=0" + SimpleHttpClient.CRLF + "4" + SimpleHttpClient.CRLF + "&b=1" + SimpleHttpClient.CRLF + "0" + SimpleHttpClient.CRLF + SimpleHttpClient.CRLF;
TrailerClient client = new TrailerClient(tomcat.getConnector().getLocalPort());
client.setRequest(new String[] { request });
client.connect();
client.processRequest();
assertEquals("nullnull7nullnull", client.getResponseBody());
}
use of org.apache.catalina.Context in project tomcat by apache.
the class TestChunkedInputFilter method doTestChunkingCRLF.
private void doTestChunkingCRLF(boolean chunkHeaderUsesCRLF, boolean chunkUsesCRLF, boolean firstheaderUsesCRLF, boolean secondheaderUsesCRLF, boolean endUsesCRLF, boolean expectPass) throws Exception {
// Setup Tomcat instance
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
// Configure allowed trailer headers
tomcat.getConnector().setProperty("allowedTrailerHeaders", "X-Trailer1,X-Trailer2");
EchoHeaderServlet servlet = new EchoHeaderServlet(expectPass);
Tomcat.addServlet(ctx, "servlet", servlet);
ctx.addServletMappingDecoded("/", "servlet");
tomcat.start();
String[] request = new String[] { "POST /echo-params.jsp HTTP/1.1" + SimpleHttpClient.CRLF + "Host: any" + SimpleHttpClient.CRLF + "Transfer-encoding: chunked" + SimpleHttpClient.CRLF + "Content-Type: application/x-www-form-urlencoded" + SimpleHttpClient.CRLF + "Connection: close" + SimpleHttpClient.CRLF + SimpleHttpClient.CRLF + "3" + (chunkHeaderUsesCRLF ? SimpleHttpClient.CRLF : LF) + "a=0" + (chunkUsesCRLF ? SimpleHttpClient.CRLF : LF) + "4" + SimpleHttpClient.CRLF + "&b=1" + SimpleHttpClient.CRLF + "0" + SimpleHttpClient.CRLF + "x-trailer1: Test", "Value1" + (firstheaderUsesCRLF ? SimpleHttpClient.CRLF : LF) + "x-trailer2: TestValue2" + (secondheaderUsesCRLF ? SimpleHttpClient.CRLF : LF) + (endUsesCRLF ? SimpleHttpClient.CRLF : LF) };
TrailerClient client = new TrailerClient(tomcat.getConnector().getLocalPort());
client.setRequest(request);
client.connect();
Exception processException = null;
try {
client.processRequest();
} catch (Exception e) {
// Socket was probably closed before client had a chance to read
// response
processException = e;
}
if (expectPass) {
assertTrue(client.isResponse200());
assertEquals("nullnull7TestValue1TestValue2", client.getResponseBody());
assertNull(processException);
assertFalse(servlet.getExceptionDuringRead());
} else {
if (processException == null) {
assertTrue(client.getResponseLine(), client.isResponse500());
} else {
// Use fall-back for checking the error occurred
assertTrue(servlet.getExceptionDuringRead());
}
}
}
use of org.apache.catalina.Context in project tomcat by apache.
the class TestChunkedInputFilter method doTestExtensionSizeLimit.
private void doTestExtensionSizeLimit(int len, boolean ok) throws Exception {
// Setup Tomcat instance
Tomcat tomcat = getTomcatInstance();
tomcat.getConnector().setProperty("maxExtensionSize", Integer.toString(EXT_SIZE_LIMIT));
// No file system docBase required
Context ctx = tomcat.addContext("", null);
Tomcat.addServlet(ctx, "servlet", new EchoHeaderServlet(ok));
ctx.addServletMappingDecoded("/", "servlet");
tomcat.start();
String extName = ";foo=";
StringBuilder extValue = new StringBuilder(len);
for (int i = 0; i < (len - extName.length()); i++) {
extValue.append("x");
}
String[] request = new String[] { "POST /echo-params.jsp HTTP/1.1" + SimpleHttpClient.CRLF + "Host: any" + SimpleHttpClient.CRLF + "Transfer-encoding: chunked" + SimpleHttpClient.CRLF + "Content-Type: application/x-www-form-urlencoded" + SimpleHttpClient.CRLF + "Connection: close" + SimpleHttpClient.CRLF + SimpleHttpClient.CRLF + "3" + extName + extValue.toString() + SimpleHttpClient.CRLF + "a=0" + SimpleHttpClient.CRLF + "4" + SimpleHttpClient.CRLF + "&b=1" + SimpleHttpClient.CRLF + "0" + SimpleHttpClient.CRLF + SimpleHttpClient.CRLF };
TrailerClient client = new TrailerClient(tomcat.getConnector().getLocalPort());
client.setRequest(request);
client.connect();
client.processRequest();
if (ok) {
assertTrue(client.isResponse200());
} else {
assertTrue(client.isResponse500());
}
}
Aggregations