use of org.nanohttpd.protocols.http.HTTPSession in project nanohttpd by NanoHttpd.
the class CookieHandlerTest method testUnloadQueue.
@Test
public void testUnloadQueue() throws IOException {
StringBuilder requestBuilder = new StringBuilder();
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();
CookieHandler cookieHandler = session.getCookies();
Response response = Response.newFixedLengthResponse("");
cookieHandler.set("name", "value", 30);
cookieHandler.unloadQueue(response);
String setCookieHeader = response.getCookieHeaders().get(0);
assertTrue("unloadQueue did not set the cookies correctly", setCookieHeader.startsWith("name=value; expires="));
}
use of org.nanohttpd.protocols.http.HTTPSession in project nanohttpd by NanoHttpd.
the class CookieHandlerTest method testDelete.
@Test
public void testDelete() throws IOException, ParseException {
StringBuilder requestBuilder = new StringBuilder();
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();
CookieHandler cookieHandler = session.getCookies();
Response response = Response.newFixedLengthResponse("");
cookieHandler.delete("name");
cookieHandler.unloadQueue(response);
String setCookieHeader = response.getCookieHeaders().get(0);
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
String dateString = setCookieHeader.split(";")[1].split("=")[1].trim();
Date date = dateFormat.parse(dateString);
assertTrue("Deleted cookie's expiry time should be a time in the past", date.compareTo(new Date()) < 0);
}
use of org.nanohttpd.protocols.http.HTTPSession in project nanohttpd by NanoHttpd.
the class HttpSessionTest method testSessionRemoteHostnameLocalhost.
@Test
public void testSessionRemoteHostnameLocalhost() 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("localhost", session.getRemoteHostName());
}
use of org.nanohttpd.protocols.http.HTTPSession in project nanohttpd by NanoHttpd.
the class HttpSessionHeadersTest method testHeadersRemoteIp.
@Test
public void testHeadersRemoteIp() throws Exception {
ByteArrayInputStream inputStream = new ByteArrayInputStream(HttpSessionHeadersTest.DUMMY_REQUEST_CONTENT.getBytes());
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
String[] ipAddresses = { "127.0.0.1", "8.8.8.8" };
for (String ipAddress : ipAddresses) {
InetAddress inetAddress = InetAddress.getByName(ipAddress);
HTTPSession session = this.testServer.createSession(HttpSessionHeadersTest.TEST_TEMP_FILE_MANAGER, inputStream, outputStream, inetAddress);
assertEquals(ipAddress, session.getRemoteIpAddress());
}
}
use of org.nanohttpd.protocols.http.HTTPSession in project nanohttpd by NanoHttpd.
the class CookieHandlerTest method testCookieHeaderCorrectlyParsed.
@Test
public void testCookieHeaderCorrectlyParsed() throws IOException {
StringBuilder requestBuilder = new StringBuilder();
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