use of org.nanohttpd.protocols.http.HTTPSession in project nanohttpd by NanoHttpd.
the class HttpSessionTest method testSessionRemoteHostname.
@Test
public void testSessionRemoteHostname() throws UnknownHostException {
ByteArrayInputStream inputStream = new ByteArrayInputStream(HttpSessionTest.DUMMY_REQUEST_CONTENT.getBytes());
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
InetAddress inetAddress = InetAddress.getByName("google.com");
HTTPSession session = this.testServer.createSession(HttpSessionTest.TEST_TEMP_FILE_MANAGER, inputStream, outputStream, inetAddress);
assertEquals("google.com", session.getRemoteHostName());
}
use of org.nanohttpd.protocols.http.HTTPSession in project nanohttpd by NanoHttpd.
the class HttpServerTest method invokeServer.
protected ByteArrayOutputStream invokeServer(String request) {
ByteArrayInputStream inputStream = new ByteArrayInputStream(request.getBytes());
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
HTTPSession session = this.testServer.createSession(this.tempFileManager, inputStream, outputStream);
try {
session.execute();
} catch (IOException e) {
fail("" + e);
e.printStackTrace();
}
return outputStream;
}
use of org.nanohttpd.protocols.http.HTTPSession in project nanohttpd by NanoHttpd.
the class HttpSessionTest method testSessionRemoteIPAddress.
@Test
public void testSessionRemoteIPAddress() throws UnknownHostException {
ByteArrayInputStream inputStream = new ByteArrayInputStream(HttpSessionTest.DUMMY_REQUEST_CONTENT.getBytes());
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
HTTPSession session = this.testServer.createSession(HttpSessionTest.TEST_TEMP_FILE_MANAGER, inputStream, outputStream, inetAddress);
assertEquals("127.0.0.1", session.getRemoteIpAddress());
}
use of org.nanohttpd.protocols.http.HTTPSession 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.HTTPSession in project nanohttpd by NanoHttpd.
the class CookieHandlerTest method testCookieHeaderWithSpecialCharactersCorrectlyParsed.
@Test
public void testCookieHeaderWithSpecialCharactersCorrectlyParsed() throws IOException {
StringBuilder requestBuilder = new StringBuilder();
// not including ; = and ,
requestBuilder.append("GET " + HttpServerTest.URI + " HTTP/1.1").append(System.getProperty("line.separator")).append("Cookie: theme=light; sessionToken=abc123!@#$%^&*()-_+{}[]\\|:\"'<>.?/");
ByteArrayInputStream inputStream = new ByteArrayInputStream(requestBuilder.toString().getBytes());
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
HTTPSession session = this.testServer.createSession(this.tempFileManager, inputStream, outputStream);
session.execute();
Set<String> allCookies = new HashSet<String>();
CookieHandler cookieHandler = session.getCookies();
for (String cookie : cookieHandler) {
allCookies.add(cookie);
}
assertTrue("cookie specified in header not correctly parsed", allCookies.contains("theme"));
assertTrue("cookie specified in header not correctly parsed", allCookies.contains("sessionToken"));
assertEquals("cookie value not correctly parsed", "light", cookieHandler.read("theme"));
assertEquals("cookie value not correctly parsed", "abc123!@#$%^&*()-_+{}[]\\|:\"'<>.?/", cookieHandler.read("sessionToken"));
}
Aggregations