use of org.apache.catalina.startup.Tomcat in project tomcat by apache.
the class TestRequest method doBug56501.
private void doBug56501(String deployPath, String requestPath, String expected) throws Exception {
// Setup Tomcat instance
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext(deployPath, null);
Tomcat.addServlet(ctx, "servlet", new Bug56501Servlet());
ctx.addServletMappingDecoded("/*", "servlet");
tomcat.start();
ByteChunk res = getUrl("http://localhost:" + getPort() + requestPath);
String resultPath = res.toString();
if (resultPath == null) {
resultPath = "";
}
assertEquals(expected, resultPath);
}
use of org.apache.catalina.startup.Tomcat in project tomcat by apache.
the class TestAbortedUpload method configureAndStartWebApplication.
@Override
protected void configureAndStartWebApplication() throws LifecycleException {
Tomcat tomcat = getTomcatInstance();
// Retain '/simple' url-pattern since it enables code re-use
Context ctxt = tomcat.addContext("", null);
Tomcat.addServlet(ctxt, "abort", new AbortServlet());
ctxt.addServletMappingDecoded("/simple", "abort");
tomcat.start();
}
use of org.apache.catalina.startup.Tomcat in project tomcat by apache.
the class TestCookiesDefaultSysProps method testCookiesInstance.
@Override
@Test
public void testCookiesInstance() throws Exception {
Tomcat tomcat = getTomcatInstance();
addServlets(tomcat);
tomcat.start();
ByteChunk res = getUrl("http://localhost:" + getPort() + "/invalid");
assertEquals("Cookie name fail", res.toString());
res = getUrl("http://localhost:" + getPort() + "/null");
assertEquals("Cookie name fail", res.toString());
res = getUrl("http://localhost:" + getPort() + "/blank");
assertEquals("Cookie name fail", res.toString());
res = getUrl("http://localhost:" + getPort() + "/invalidFwd");
assertEquals("Cookie name fail", res.toString());
res = getUrl("http://localhost:" + getPort() + "/invalidStrict");
assertEquals("Cookie name ok", res.toString());
res = getUrl("http://localhost:" + getPort() + "/valid");
assertEquals("Cookie name ok", res.toString());
// Need to read response headers to test version switching
Map<String, List<String>> headers = new HashMap<>();
getUrl("http://localhost:" + getPort() + "/switch", res, headers);
List<String> cookieHeaders = headers.get("Set-Cookie");
for (String cookieHeader : cookieHeaders) {
assertEquals("name=\"val?ue\"; Version=1", cookieHeader);
}
}
use of org.apache.catalina.startup.Tomcat in project tomcat by apache.
the class TestNonBlockingAPI method testNonBlockingWriteError.
@Test
public void testNonBlockingWriteError() throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
TesterAccessLogValve alv = new TesterAccessLogValve();
ctx.getPipeline().addValve(alv);
NBWriteServlet servlet = new NBWriteServlet();
String servletName = NBWriteServlet.class.getName();
Tomcat.addServlet(ctx, servletName, servlet);
ctx.addServletMappingDecoded("/", servletName);
tomcat.getConnector().setProperty("socket.txBufSize", "1024");
tomcat.start();
SocketFactory factory = SocketFactory.getDefault();
Socket s = factory.createSocket("localhost", getPort());
ByteChunk result = new ByteChunk();
OutputStream os = s.getOutputStream();
os.write(("GET / HTTP/1.1\r\n" + "Host: localhost:" + getPort() + "\r\n" + "Connection: close\r\n" + "\r\n").getBytes(StandardCharsets.ISO_8859_1));
os.flush();
InputStream is = s.getInputStream();
byte[] buffer = new byte[8192];
int read = 0;
int readSinceLastPause = 0;
int readTotal = 0;
while (read != -1 && readTotal < WRITE_SIZE / 32) {
long start = System.currentTimeMillis();
read = is.read(buffer);
long end = System.currentTimeMillis();
log.info("Client read [" + read + "] bytes in [" + (end - start) + "] ms");
if (read > 0) {
result.append(buffer, 0, read);
}
readSinceLastPause += read;
readTotal += read;
if (readSinceLastPause > WRITE_SIZE / 64) {
readSinceLastPause = 0;
Thread.sleep(WRITE_PAUSE_MS);
}
}
os.close();
is.close();
s.close();
String resultString = result.toString();
log.info("Client read " + resultString.length() + " bytes");
int lineStart = 0;
int lineEnd = resultString.indexOf('\n', 0);
String line = resultString.substring(lineStart, lineEnd + 1);
Assert.assertEquals("HTTP/1.1 200 \r\n", line);
// Listeners are invoked and access valve entries created on a different
// thread so give that thread a chance to complete its work.
int count = 0;
while (count < 100 && !(servlet.wlistener.onErrorInvoked || servlet.rlistener.onErrorInvoked)) {
Thread.sleep(100);
count++;
}
while (count < 100 && alv.getEntryCount() < 1) {
Thread.sleep(100);
count++;
}
Assert.assertTrue("Error listener should have been invoked.", servlet.wlistener.onErrorInvoked || servlet.rlistener.onErrorInvoked);
// TODO Figure out why non-blocking writes with the NIO connector appear
// to be slower on Linux
alv.validateAccessLog(1, 500, WRITE_PAUSE_MS, WRITE_PAUSE_MS + 30 * 1000);
}
use of org.apache.catalina.startup.Tomcat in project tomcat by apache.
the class TestStandardWrapper method testBug51445AddServlet.
@Test
public void testBug51445AddServlet() throws Exception {
initLatch();
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
Tomcat.addServlet(ctx, "Bug51445", new Bug51445Servlet());
ctx.addServletMappingDecoded("/", "Bug51445");
tomcat.start();
// Start the threads
Bug51445Thread[] threads = new Bug51445Thread[5];
for (int i = 0; i < BUG51445_THREAD_COUNT; i++) {
threads[i] = new Bug51445Thread(getPort());
threads[i].start();
}
// Wait for threads to finish
for (int i = 0; i < BUG51445_THREAD_COUNT; i++) {
threads[i].join();
}
Set<String> servlets = new HashSet<>();
// Output the result
for (int i = 0; i < BUG51445_THREAD_COUNT; i++) {
System.out.println(threads[i].getResult());
}
// Check the result
for (int i = 0; i < BUG51445_THREAD_COUNT; i++) {
String[] results = threads[i].getResult().split(",");
assertEquals(2, results.length);
assertEquals("10", results[0]);
assertFalse(servlets.contains(results[1]));
servlets.add(results[1]);
}
}
Aggregations