Search in sources :

Example 6 with HTTPSession

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="));
}
Also used : Response(org.nanohttpd.protocols.http.response.Response) ByteArrayInputStream(java.io.ByteArrayInputStream) HTTPSession(org.nanohttpd.protocols.http.HTTPSession) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CookieHandler(org.nanohttpd.protocols.http.content.CookieHandler) Test(org.junit.Test)

Example 7 with HTTPSession

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);
}
Also used : Response(org.nanohttpd.protocols.http.response.Response) ByteArrayInputStream(java.io.ByteArrayInputStream) HTTPSession(org.nanohttpd.protocols.http.HTTPSession) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) CookieHandler(org.nanohttpd.protocols.http.content.CookieHandler) Test(org.junit.Test)

Example 8 with HTTPSession

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());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) HTTPSession(org.nanohttpd.protocols.http.HTTPSession) ByteArrayOutputStream(java.io.ByteArrayOutputStream) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 9 with HTTPSession

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());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) HTTPSession(org.nanohttpd.protocols.http.HTTPSession) ByteArrayOutputStream(java.io.ByteArrayOutputStream) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 10 with HTTPSession

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"));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) HTTPSession(org.nanohttpd.protocols.http.HTTPSession) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HashSet(java.util.HashSet) CookieHandler(org.nanohttpd.protocols.http.content.CookieHandler) Test(org.junit.Test)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 HTTPSession (org.nanohttpd.protocols.http.HTTPSession)10 ByteArrayInputStream (java.io.ByteArrayInputStream)9 Test (org.junit.Test)8 InetAddress (java.net.InetAddress)4 CookieHandler (org.nanohttpd.protocols.http.content.CookieHandler)4 HashSet (java.util.HashSet)2 Response (org.nanohttpd.protocols.http.response.Response)2 IOException (java.io.IOException)1 PipedInputStream (java.io.PipedInputStream)1 PipedOutputStream (java.io.PipedOutputStream)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 IHTTPSession (org.nanohttpd.protocols.http.IHTTPSession)1 DefaultTempFileManager (org.nanohttpd.protocols.http.tempfiles.DefaultTempFileManager)1