use of java.io.PipedInputStream in project nanohttpd by NanoHttpd.
the class HttpKeepAliveTest method testManyRequests.
/**
* Issue the given request many times to check whether an error occurs. For
* this test, a small stack size is used, since a stack overflow is among
* the possible errors.
*
* @param request
* The request to issue
* @param expected
* The expected response
*/
public void testManyRequests(final String request, final String[] expected) throws Exception {
Runnable r = new Runnable() {
@Override
public void run() {
try {
PipedOutputStream requestStream = new PipedOutputStream();
PipedInputStream inputStream = new PipedInputStream(requestStream);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
DefaultTempFileManager tempFileManager = new DefaultTempFileManager();
try {
HTTPSession session = HttpKeepAliveTest.this.testServer.createSession(tempFileManager, inputStream, outputStream);
for (int i = 0; i < 2048; i++) {
requestStream.write(request.getBytes());
requestStream.flush();
outputStream.reset();
session.execute();
assertResponse(outputStream, expected);
}
// Finally, try "Connection: Close"
String closeReq = request.replaceAll("HTTP/1.1", "HTTP/1.1\r\nConnection: Close");
expected[3] = "Connection: close";
requestStream.write(closeReq.getBytes());
outputStream.reset();
requestStream.flush();
// SocketException:
try {
session.execute();
} catch (java.net.SocketException se) {
junit.framework.Assert.assertEquals(se.getMessage(), "NanoHttpd Shutdown");
}
assertResponse(outputStream, expected);
} finally {
tempFileManager.clear();
}
} catch (Throwable t) {
HttpKeepAliveTest.this.error = t;
}
}
};
Thread t = new Thread(null, r, "Request Thread", 1 << 17);
t.start();
t.join();
if (this.error != null) {
fail("" + this.error);
this.error.printStackTrace();
}
}
use of java.io.PipedInputStream in project nanohttpd by NanoHttpd.
the class TestHttpServer method setUp.
@BeforeClass
public static void setUp() throws Exception {
stdIn = new PipedOutputStream();
System.setIn(new PipedInputStream(stdIn));
serverStartThread = new Thread(new Runnable() {
@Override
public void run() {
String[] args = { "--host", "localhost", "--port", "9090", "--dir", "src/test/resources" };
SimpleWebServer.main(args);
}
});
serverStartThread.start();
// give the server some tine to start.
Thread.sleep(100);
}
use of java.io.PipedInputStream in project nanohttpd by NanoHttpd.
the class TestCorsHttpServer method setUp.
@BeforeClass
public static void setUp() throws Exception {
stdIn = new PipedOutputStream();
System.setIn(new PipedInputStream(stdIn));
serverStartThread = new Thread(new Runnable() {
@Override
public void run() {
String[] args = { "--host", "localhost", "--port", "9090", "--dir", "src/test/resources", "--cors" };
SimpleWebServer.main(args);
}
});
serverStartThread.start();
// give the server some tine to start.
Thread.sleep(100);
}
use of java.io.PipedInputStream in project nanohttpd by NanoHttpd.
the class TestNanolets method setUp.
@BeforeClass
public static void setUp() throws Exception {
stdIn = new PipedOutputStream();
System.setIn(new PipedInputStream(stdIn));
serverStartThread = new Thread(new Runnable() {
@Override
public void run() {
String[] args = {};
AppNanolets.main(args);
}
});
serverStartThread.start();
// give the server some tine to start.
Thread.sleep(200);
}
use of java.io.PipedInputStream in project nanohttpd by NanoHttpd.
the class EchoWebSocketsTest method testDirectoryArgument.
@Test
public void testDirectoryArgument() throws IOException, InterruptedException {
final String testPort = "9458";
PipedOutputStream stdIn = new PipedOutputStream();
System.setIn(new PipedInputStream(stdIn));
Thread testServer = new Thread(new Runnable() {
@Override
public void run() {
String[] args = { testPort, "-d" };
try {
EchoSocketSample.main(args);
} catch (IOException e) {
fail("Exception: " + e.getMessage());
}
}
});
testServer.start();
Thread.sleep(1000);
stdIn.write(System.getProperty("line.separator").getBytes());
testServer.join(1000);
assertFalse("Test server failed to close", testServer.isAlive());
}
Aggregations