Search in sources :

Example 1 with IHTTPSession

use of org.nanohttpd.protocols.http.IHTTPSession 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)

Example 2 with IHTTPSession

use of org.nanohttpd.protocols.http.IHTTPSession in project nanohttpd by NanoHttpd.

the class HttpServerTest method testTempFileInterface.

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

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

        @Override
        public Response serve(IHTTPSession session) {
            String responseMsg = "pass";
            try {
                session.parseBody(this.files);
                for (String key : files.keySet()) {
                    if (!(new File(files.get(key))).exists()) {
                        responseMsg = "fail";
                    }
                }
            } catch (Exception e) {
                responseMsg = 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 file check failed: " + line, "pass", line);
        reader.close();
        instream.close();
    } else {
        fail("No server response");
    }
    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) 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) 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) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File) Test(org.junit.Test)

Aggregations

BufferedReader (java.io.BufferedReader)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 File (java.io.File)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 HashMap (java.util.HashMap)2 HttpEntity (org.apache.http.HttpEntity)2 HttpResponse (org.apache.http.HttpResponse)2 HttpClient (org.apache.http.client.HttpClient)2 HttpPost (org.apache.http.client.methods.HttpPost)2 MultipartEntity (org.apache.http.entity.mime.MultipartEntity)2 FileBody (org.apache.http.entity.mime.content.FileBody)2 StringBody (org.apache.http.entity.mime.content.StringBody)2 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)2 Test (org.junit.Test)2 IHTTPSession (org.nanohttpd.protocols.http.IHTTPSession)2 NanoHTTPD (org.nanohttpd.protocols.http.NanoHTTPD)2 Map (java.util.Map)1 Response (org.nanohttpd.protocols.http.response.Response)1