use of org.apache.tomcat.util.buf.ByteChunk in project tomcat by apache.
the class TesterAjpNonBlockingClient method doTestAJPNonBlockingRead.
@Test
public void doTestAJPNonBlockingRead() throws Exception {
Map<String, List<String>> resHeaders = new HashMap<>();
ByteChunk out = new ByteChunk();
int rc = postUrl(true, new DataWriter(2000), "http://localhost" + "/examples/servlets/nonblocking/bytecounter", out, resHeaders, null);
System.out.println(out.toString());
Assert.assertEquals(HttpServletResponse.SC_OK, rc);
}
use of org.apache.tomcat.util.buf.ByteChunk in project tomcat by apache.
the class TesterAjpNonBlockingClient method testNonBlockingWrite.
@Test
public void testNonBlockingWrite() throws Exception {
SocketFactory factory = SocketFactory.getDefault();
Socket s = factory.createSocket("localhost", 80);
ByteChunk result = new ByteChunk();
OutputStream os = s.getOutputStream();
os.write(("GET /examples/servlets/nonblocking/numberwriter HTTP/1.1\r\n" + "Host: localhost\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;
while (read != -1) {
read = is.read(buffer);
if (read > 0) {
result.append(buffer, 0, read);
}
readSinceLastPause += read;
if (readSinceLastPause > 40000) {
readSinceLastPause = 0;
Thread.sleep(500);
}
}
os.close();
is.close();
s.close();
// Validate the result
String resultString = result.toString();
log.info("Client read " + resultString.length() + " bytes");
System.out.println(resultString);
Assert.assertTrue(resultString.contains("00000000000000010000"));
}
use of org.apache.tomcat.util.buf.ByteChunk in project tomcat by apache.
the class TestMapperListener method testTomcatRestartListenerCount_Bug56717.
@Test
public void testTomcatRestartListenerCount_Bug56717() throws IOException, LifecycleException {
// The test runs Tomcat twice, tests that it has started successfully,
// and compares the counts of listeners registered on containers
// after the first and the second starts.
// Sample request is from TestTomcat#testSingleWebapp()
Tomcat tomcat = getTomcatInstance();
File appDir = new File(getBuildDirectory(), "webapps/examples");
// app dir is relative to server home
Context ctxt = tomcat.addWebapp(null, "/examples", appDir.getAbsolutePath());
ctxt.addApplicationListener(WsContextListener.class.getName());
tomcat.start();
ByteChunk res;
String text;
res = getUrl("http://localhost:" + getPort() + "/examples/servlets/servlet/HelloWorldExample");
text = res.toString();
Assert.assertTrue(text, text.contains("<a href=\"../helloworld.html\">"));
List<ListenersInfo> listenersFirst = new ArrayList<>();
populateListenersInfo(listenersFirst, tomcat.getEngine());
tomcat.stop();
tomcat.start();
res = getUrl("http://localhost:" + getPort() + "/examples/servlets/servlet/HelloWorldExample");
text = res.toString();
Assert.assertTrue(text, text.contains("<a href=\"../helloworld.html\">"));
List<ListenersInfo> listenersSecond = new ArrayList<>();
populateListenersInfo(listenersSecond, tomcat.getEngine());
Assert.assertEquals(listenersFirst.size(), listenersSecond.size());
for (int i = 0, len = listenersFirst.size(); i < len; i++) {
ListenersInfo a = listenersFirst.get(i);
ListenersInfo b = listenersSecond.get(i);
boolean equal = a.container.getClass() == b.container.getClass() && a.containerListeners.length == b.containerListeners.length && a.lifecycleListeners.length == b.lifecycleListeners.length;
if (!equal) {
Assert.fail("The lists of listeners differ:\n" + a + "\n" + b);
}
}
}
use of org.apache.tomcat.util.buf.ByteChunk in project tomcat by apache.
the class TestStandardContextResources method assertPageContains.
private void assertPageContains(String pageUrl, String expectedBody, int expectedStatus) throws IOException {
ByteChunk res = new ByteChunk();
int sc = getUrl("http://localhost:" + getPort() + pageUrl, res, null);
assertEquals(expectedStatus, sc);
if (expectedStatus == 200) {
String result = res.toString();
assertTrue(result, result.indexOf(expectedBody) > 0);
}
}
use of org.apache.tomcat.util.buf.ByteChunk in project tomcat by apache.
the class TestStandardContextValve method testBug51653a.
@Test
public void testBug51653a() throws Exception {
// Set up a container
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
// Traces order of events across multiple components
StringBuilder trace = new StringBuilder();
//Add the error page
Tomcat.addServlet(ctx, "errorPage", new Bug51653ErrorPage(trace));
ctx.addServletMappingDecoded("/error", "errorPage");
// And the handling for 404 responses
ErrorPage errorPage = new ErrorPage();
errorPage.setErrorCode(Response.SC_NOT_FOUND);
errorPage.setLocation("/error");
ctx.addErrorPage(errorPage);
// Add the request listener
Bug51653RequestListener reqListener = new Bug51653RequestListener(trace);
((StandardContext) ctx).addApplicationEventListener(reqListener);
tomcat.start();
// Request a page that does not exist
int rc = getUrl("http://localhost:" + getPort() + "/invalid", new ByteChunk(), null);
// Need to allow time (but not too long in case the test fails) for
// ServletRequestListener to complete
int i = 20;
while (i > 0) {
if (trace.toString().endsWith("Destroy")) {
break;
}
Thread.sleep(250);
i--;
}
assertEquals(Response.SC_NOT_FOUND, rc);
assertEquals("InitErrorDestroy", trace.toString());
}
Aggregations