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