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();
}
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();
}
}
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);
}
}
Aggregations