Search in sources :

Example 1 with HTTPSession

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());
}
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 2 with HTTPSession

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

Example 3 with HTTPSession

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());
}
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 4 with HTTPSession

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();
    }
}
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 5 with HTTPSession

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"));
}
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