Search in sources :

Example 1 with DefaultTempFileManager

use of org.nanohttpd.protocols.http.tempfiles.DefaultTempFileManager in project nanohttpd by NanoHttpd.

the class JavaIOTempDirExistTest method testJavaIoTempDefault.

@Test
public void testJavaIoTempDefault() throws Exception {
    String tmpdir = System.getProperty("java.io.tmpdir");
    DefaultTempFileManager manager = new DefaultTempFileManager();
    DefaultTempFile tempFile = (DefaultTempFile) manager.createTempFile("xx");
    File tempFileBackRef = new File(tempFile.getName());
    Assert.assertEquals(tempFileBackRef.getParentFile(), new File(tmpdir));
    // force an exception
    tempFileBackRef.delete();
    Exception e = null;
    try {
        tempFile.delete();
    } catch (Exception ex) {
        e = ex;
    }
    Assert.assertNotNull(e);
    manager.clear();
}
Also used : DefaultTempFileManager(org.nanohttpd.protocols.http.tempfiles.DefaultTempFileManager) DefaultTempFile(org.nanohttpd.protocols.http.tempfiles.DefaultTempFile) DefaultTempFile(org.nanohttpd.protocols.http.tempfiles.DefaultTempFile) File(java.io.File) IOException(java.io.IOException) Test(org.junit.Test)

Example 2 with DefaultTempFileManager

use of org.nanohttpd.protocols.http.tempfiles.DefaultTempFileManager 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 3 with DefaultTempFileManager

use of org.nanohttpd.protocols.http.tempfiles.DefaultTempFileManager in project nanohttpd by NanoHttpd.

the class JavaIOTempDirExistTest method testJavaIoTempSpecific.

@Test
public void testJavaIoTempSpecific() throws IOException {
    final String tmpdir = System.getProperty("java.io.tmpdir");
    try {
        String tempFileName = UUID.randomUUID().toString();
        File newDir = new File("target", tempFileName);
        System.setProperty("java.io.tmpdir", newDir.getAbsolutePath());
        Assert.assertEquals(false, newDir.exists());
        new DefaultTempFileManager();
        Assert.assertEquals(true, newDir.exists());
        newDir.delete();
    } finally {
        System.setProperty("java.io.tmpdir", tmpdir);
    }
}
Also used : DefaultTempFileManager(org.nanohttpd.protocols.http.tempfiles.DefaultTempFileManager) DefaultTempFile(org.nanohttpd.protocols.http.tempfiles.DefaultTempFile) File(java.io.File) Test(org.junit.Test)

Aggregations

DefaultTempFileManager (org.nanohttpd.protocols.http.tempfiles.DefaultTempFileManager)3 File (java.io.File)2 Test (org.junit.Test)2 DefaultTempFile (org.nanohttpd.protocols.http.tempfiles.DefaultTempFile)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 PipedInputStream (java.io.PipedInputStream)1 PipedOutputStream (java.io.PipedOutputStream)1 HTTPSession (org.nanohttpd.protocols.http.HTTPSession)1