use of org.apache.tomcat.util.buf.ByteChunk 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.tomcat.util.buf.ByteChunk in project tomcat by apache.
the class TestHttp11Processor method doTestNon2xxResponseAndExpectation.
private void doTestNon2xxResponseAndExpectation(boolean useExpectation) throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
Tomcat.addServlet(ctx, "echo", new EchoBodyServlet());
ctx.addServletMappingDecoded("/echo", "echo");
SecurityCollection collection = new SecurityCollection("All", "");
collection.addPatternDecoded("/*");
SecurityConstraint constraint = new SecurityConstraint();
constraint.addAuthRole("Any");
constraint.addCollection(collection);
ctx.addConstraint(constraint);
tomcat.start();
byte[] requestBody = "HelloWorld".getBytes(StandardCharsets.UTF_8);
Map<String, List<String>> reqHeaders = null;
if (useExpectation) {
reqHeaders = new HashMap<>();
List<String> expectation = new ArrayList<>();
expectation.add("100-continue");
reqHeaders.put("Expect", expectation);
}
ByteChunk responseBody = new ByteChunk();
Map<String, List<String>> responseHeaders = new HashMap<>();
int rc = postUrl(requestBody, "http://localhost:" + getPort() + "/echo", responseBody, reqHeaders, responseHeaders);
Assert.assertEquals(HttpServletResponse.SC_FORBIDDEN, rc);
List<String> connectionHeaders = responseHeaders.get("Connection");
if (useExpectation) {
Assert.assertEquals(1, connectionHeaders.size());
Assert.assertEquals("close", connectionHeaders.get(0).toLowerCase(Locale.ENGLISH));
} else {
Assert.assertNull(connectionHeaders);
}
}
use of org.apache.tomcat.util.buf.ByteChunk 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.tomcat.util.buf.ByteChunk 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.tomcat.util.buf.ByteChunk 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());
}
Aggregations