Search in sources :

Example 6 with PipedInputStream

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();
    }
}
Also used : DefaultTempFileManager(org.nanohttpd.protocols.http.tempfiles.DefaultTempFileManager) HTTPSession(org.nanohttpd.protocols.http.HTTPSession) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 7 with PipedInputStream

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);
}
Also used : PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) BeforeClass(org.junit.BeforeClass)

Example 8 with PipedInputStream

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);
}
Also used : PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) BeforeClass(org.junit.BeforeClass)

Example 9 with PipedInputStream

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);
}
Also used : PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) BeforeClass(org.junit.BeforeClass)

Example 10 with PipedInputStream

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());
}
Also used : PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

PipedInputStream (java.io.PipedInputStream)122 PipedOutputStream (java.io.PipedOutputStream)118 IOException (java.io.IOException)38 Test (org.junit.Test)33 BinaryDecoder (co.cask.cdap.common.io.BinaryDecoder)21 BinaryEncoder (co.cask.cdap.common.io.BinaryEncoder)21 ReflectionDatumReader (co.cask.cdap.internal.io.ReflectionDatumReader)17 TypeToken (com.google.common.reflect.TypeToken)17 DataInputStream (java.io.DataInputStream)13 DataOutputStream (java.io.DataOutputStream)13 InputStream (java.io.InputStream)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 Before (org.junit.Before)10 OutputStream (java.io.OutputStream)9 ByteArrayInputStream (java.io.ByteArrayInputStream)7 PrintStream (java.io.PrintStream)7 InputStreamReader (java.io.InputStreamReader)6 ImmutableList (com.google.common.collect.ImmutableList)5 List (java.util.List)5 ISymmetricEngine (org.jumpmind.symmetric.ISymmetricEngine)5