use of org.apache.tomcat.util.buf.ByteChunk in project tomcat by apache.
the class TestStandardContext method testBug50015.
@Test
public void testBug50015() throws Exception {
// Test that configuring servlet security constraints programmatically
// does work.
// Set up a container
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
// Setup realm
TesterMapRealm realm = new TesterMapRealm();
realm.addUser("tomcat", "tomcat");
realm.addUserRole("tomcat", "tomcat");
ctx.setRealm(realm);
// Configure app for BASIC auth
LoginConfig lc = new LoginConfig();
lc.setAuthMethod("BASIC");
ctx.setLoginConfig(lc);
ctx.getPipeline().addValve(new BasicAuthenticator());
// Add ServletContainerInitializer
ServletContainerInitializer sci = new Bug50015SCI();
ctx.addServletContainerInitializer(sci, null);
// Start the context
tomcat.start();
// Request the first servlet
ByteChunk bc = new ByteChunk();
int rc = getUrl("http://localhost:" + getPort() + "/bug50015", bc, null);
// Check for a 401
assertNotSame("OK", bc.toString());
assertEquals(401, rc);
}
use of org.apache.tomcat.util.buf.ByteChunk in project tomcat by apache.
the class TestStandardContextAliases method testDirContextAliases.
@Test
public void testDirContextAliases() throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
File lib = new File("webapps/examples/WEB-INF/lib");
ctx.setResources(new StandardRoot(ctx));
ctx.getResources().createWebResourceSet(WebResourceRoot.ResourceSetType.POST, "/WEB-INF/lib", lib.getAbsolutePath(), null, "/");
Tomcat.addServlet(ctx, "test", new TestServlet());
ctx.addServletMappingDecoded("/", "test");
tomcat.start();
ByteChunk res = getUrl("http://localhost:" + getPort() + "/");
String result = res.toString();
if (result == null) {
result = "";
}
assertTrue(result.indexOf("00-PASS") > -1);
assertTrue(result.indexOf("01-PASS") > -1);
assertTrue(result.indexOf("02-PASS") > -1);
}
use of org.apache.tomcat.util.buf.ByteChunk in project tomcat by apache.
the class TestRestCsrfPreventionFilter2 method doTest.
private void doTest(String method, String uri, BasicCredentials credentials, byte[] body, boolean useCookie, int expectedRC, String expectedResponse, String nonce, boolean expectCsrfRH, String expectedCsrfRHV) throws Exception {
Map<String, List<String>> reqHeaders = new HashMap<>();
Map<String, List<String>> respHeaders = new HashMap<>();
addNonce(reqHeaders, nonce, n -> Objects.nonNull(n));
if (useCookie) {
addCookies(reqHeaders, l -> Objects.nonNull(l) && l.size() > 0);
}
addCredentials(reqHeaders, credentials, c -> Objects.nonNull(c));
ByteChunk bc = new ByteChunk();
int rc;
if (METHOD_GET.equals(method)) {
rc = getUrl(HTTP_PREFIX + getPort() + uri, bc, reqHeaders, respHeaders);
} else {
rc = postUrl(body, HTTP_PREFIX + getPort() + uri, bc, reqHeaders, respHeaders);
}
assertEquals(expectedRC, rc);
if (expectedRC == HttpServletResponse.SC_OK) {
assertEquals(expectedResponse, bc.toString());
List<String> newCookies = respHeaders.get(SERVER_COOKIE_HEADER);
saveCookies(newCookies, l -> Objects.nonNull(l) && l.size() > 0);
}
if (!expectCsrfRH) {
assertNull(respHeaders.get(Constants.CSRF_REST_NONCE_HEADER_NAME));
} else {
List<String> respHeaderValue = respHeaders.get(Constants.CSRF_REST_NONCE_HEADER_NAME);
assertNotNull(respHeaderValue);
if (Objects.nonNull(expectedCsrfRHV)) {
assertTrue(respHeaderValue.contains(expectedCsrfRHV));
} else {
validNonce = respHeaderValue.get(0);
}
}
}
use of org.apache.tomcat.util.buf.ByteChunk in project tomcat by apache.
the class TestReplicatedContext method testBug57425.
@Test
public void testBug57425() throws LifecycleException, IOException {
Tomcat tomcat = getTomcatInstance();
Host host = tomcat.getHost();
if (host instanceof StandardHost) {
((StandardHost) host).setContextClass(ReplicatedContext.class.getName());
}
File root = new File("test/webapp");
Context context = tomcat.addWebapp(host, "", root.getAbsolutePath());
Tomcat.addServlet(context, "test", new AccessContextServlet());
context.addServletMappingDecoded("/access", "test");
tomcat.start();
ByteChunk result = getUrl("http://localhost:" + getPort() + "/access");
Assert.assertEquals("OK", result.toString());
}
use of org.apache.tomcat.util.buf.ByteChunk in project tomcat by apache.
the class TestVirtualContext method assertPageContains.
private void assertPageContains(String pageUrl, String expectedBody, int expectedStatus) throws IOException {
ByteChunk res = new ByteChunk();
// Note: With a read timeout of 3s the ASF CI buildbot was consistently
// seeing failures with this test. The failures were due to the
// JSP initialisation taking longer than the read timeout. The
// root cause of this is the frequent poor IO performance of the
// VM running the buildbot instance. Increasing this to 10s should
// avoid these failures.
int sc = getUrl("http://localhost:" + getPort() + pageUrl, res, 10000, null, null);
assertEquals(expectedStatus, sc);
if (expectedStatus == 200) {
String result = res.toString();
assertTrue(result, result.indexOf(expectedBody) >= 0);
}
}
Aggregations