Search in sources :

Example 6 with Response

use of org.nanohttpd.protocols.http.response.Response 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 Response

use of org.nanohttpd.protocols.http.response.Response 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 Response

use of org.nanohttpd.protocols.http.response.Response in project MyPet by xXKeyleXx.

the class WebServer method newFixedFileResponse.

public static Response newFixedFileResponse(File file, String mime) throws FileNotFoundException {
    Response res = Response.newFixedLengthResponse(Status.OK, mime, new FileInputStream(file), (long) ((int) file.length()));
    res.addHeader("Accept-Ranges", "bytes");
    return res;
}
Also used : Response(org.nanohttpd.protocols.http.response.Response) FileInputStream(java.io.FileInputStream)

Example 9 with Response

use of org.nanohttpd.protocols.http.response.Response in project MyPet by xXKeyleXx.

the class WebServer method newResourceResponse.

public static Response newResourceResponse(String file) throws FileNotFoundException {
    String mime = getMimeType(file);
    Response res;
    try {
        res = Response.newChunkedResponse(Status.OK, mime, ClassLoader.getSystemResource(file).openStream());
    } catch (Exception e) {
        throw new FileNotFoundException(e.getMessage());
    }
    res.addHeader("Accept-Ranges", "bytes");
    return res;
}
Also used : Response(org.nanohttpd.protocols.http.response.Response) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 10 with Response

use of org.nanohttpd.protocols.http.response.Response in project nanohttpd by NanoHttpd.

the class HttpServerTest method testMultipartFormData.

@Test
public void testMultipartFormData() throws IOException {
    final int testPort = 4589;
    NanoHTTPD server = null;
    try {
        server = new NanoHTTPD(testPort) {

            final Map<String, String> files = new HashMap<String, String>();

            @Override
            public Response serve(IHTTPSession session) {
                StringBuilder responseMsg = new StringBuilder();
                try {
                    session.parseBody(this.files);
                    for (String key : files.keySet()) {
                        responseMsg.append(key);
                    }
                } catch (Exception e) {
                    responseMsg.append(e.getMessage());
                }
                return Response.newFixedLengthResponse(responseMsg.toString());
            }
        };
        server.start(NanoHTTPD.SOCKET_READ_TIMEOUT, false);
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://localhost:" + testPort);
        final String fileName = "file-upload-test.htm";
        FileBody bin = new FileBody(new File(getClass().getClassLoader().getResource(fileName).getFile()));
        StringBody comment = new StringBody("Filename: " + fileName);
        MultipartEntity reqEntity = new MultipartEntity();
        reqEntity.addPart("bin", bin);
        reqEntity.addPart("comment", comment);
        httppost.setEntity(reqEntity);
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            InputStream instream = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(instream, "UTF-8"));
            String line = reader.readLine();
            assertNotNull(line, "Invalid server reponse");
            assertEquals("Server failed multi-part data parse" + line, "bincomment", line);
            reader.close();
            instream.close();
        }
    } finally {
        if (server != null) {
            server.stop();
        }
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) FileBody(org.apache.http.entity.mime.content.FileBody) HttpEntity(org.apache.http.HttpEntity) InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) NanoHTTPD(org.nanohttpd.protocols.http.NanoHTTPD) HttpResponse(org.apache.http.HttpResponse) IHTTPSession(org.nanohttpd.protocols.http.IHTTPSession) IOException(java.io.IOException) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) Response(org.nanohttpd.protocols.http.response.Response) HttpResponse(org.apache.http.HttpResponse) StringBody(org.apache.http.entity.mime.content.StringBody) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) BufferedReader(java.io.BufferedReader) File(java.io.File) Test(org.junit.Test)

Aggregations

Response (org.nanohttpd.protocols.http.response.Response)17 Test (org.junit.Test)9 ByteArrayInputStream (java.io.ByteArrayInputStream)4 IOException (java.io.IOException)4 FileInputStream (java.io.FileInputStream)3 CookieHandler (org.nanohttpd.protocols.http.content.CookieHandler)3 BufferedReader (java.io.BufferedReader)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 InputStreamReader (java.io.InputStreamReader)2 HashMap (java.util.HashMap)2 HTTPSession (org.nanohttpd.protocols.http.HTTPSession)2 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 SocketException (java.net.SocketException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1