package org.apache.harmony.luni.tests.java.net;
import dalvik.annotation.BrokenTest;
-import dalvik.annotation.KnownFailure;
import dalvik.annotation.TestLevel;
import dalvik.annotation.TestTargetClass;
import dalvik.annotation.TestTargetNew;
import dalvik.annotation.TestTargets;
-
import junit.framework.TestCase;
-
import tests.support.Support_Configuration;
import tests.support.Support_PortManager;
import tests.support.Support_TestWebData;
import java.net.URLStreamHandler;
import java.net.UnknownServiceException;
import java.security.Permission;
-import java.text.DateFormat;
import java.text.ParseException;
import java.util.Arrays;
import java.util.Calendar;
}
}
+ public void testHttpPostHeaders() throws IOException {
+ String path = "/" + Math.random();
+ HttpURLConnection connection = (HttpURLConnection)
+ new URL("http://localhost:" + port + path).openConnection();
+
+ // post a request
+ connection.setDoOutput(true);
+ OutputStreamWriter writer
+ = new OutputStreamWriter(connection.getOutputStream());
+ writer.write("hello");
+ writer.flush();
+ assertEquals(200, connection.getResponseCode());
+
+ // validate the request by asking the server what was received
+ Map<String, String> headers = server.pathToRequest().get(path).getHeaders();
+ assertEquals("*, */*", headers.get("Accept"));
+ assertEquals("application/x-www-form-urlencoded", headers.get("Content-Type"));
+ assertEquals("5", headers.get("Content-Length"));
+ assertEquals("localhost:" + port, headers.get("Host"));
+ // TODO: test User-Agent?
+ }
+
/**
* @throws IOException
* @tests {@link java.net.URLConnection#getAllowUserInteraction()}
import java.io.*;
import java.lang.Thread;
import java.net.*;
-import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
+import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Logger;
/**
/* Where worker threads stand idle */
Vector threads = new Vector();
+ /** maps the recently requested URLs to the full request snapshot */
+ private final Map<String, Request> pathToRequest
+ = new ConcurrentHashMap<String, Request>();
+
/* List of all active worker threads */
Vector activeThreads = new Vector();
* a redirect code with the Location response header set to the value
* specified.
* @param redirect The location to be redirected to
- * @param redirectCode The code to send when redirecting
+ * @param code The code to send when redirecting
*/
public void setRedirect(String redirect, int code) {
redirectHost = redirect;
}
/**
+ * Returns a map from recently-requested paths (like "/index.html") to a
+ * snapshot of the request data.
+ */
+ public Map<String, Request> pathToRequest() {
+ return pathToRequest;
+ }
+
+ /**
* Cause the thread accepting connections on the server socket to close
*/
public void close() {
static final byte[] EOL = {(byte)'\r', (byte)'\n' };
/**
+ * An immutable snapshot of an HTTP request.
+ */
+ public static class Request {
+ private final String path;
+ private final Map<String, String> headers;
+ // TODO: include posted content?
+
+ public Request(String path, Map<String, String> headers) {
+ this.path = path;
+ this.headers = new LinkedHashMap<String, String>(headers);
+ }
+
+ public String getPath() {
+ return path;
+ }
+
+ public Map<String, String> getHeaders() {
+ return headers;
+ }
+ }
+
+ /**
* The worker thread handles all interactions with a current open
* connection. If pipelining is turned on, this will allow this
* thread to continuously operate on numerous requests before the
/* Reference to current requests test file/data */
private String testID;
+ /* The requested path, such as "/test1" */
+ private String path;
+
/* Reference to test number from testID */
private int testNum;
boolean running = false;
/* Request headers are stored here */
- private Hashtable<String, String> headers = new Hashtable<String, String>();
+ private Map<String, String> headers = new LinkedHashMap<String, String>();
/* Create a new worker thread */
Worker() {
i++;
}
- testID = new String(buf, 0, index, i-index);
- if (testID.startsWith("/")) {
- testID = testID.substring(1);
- }
+ path = new String(buf, 0, index, i-index);
+ testID = path.substring(1);
return nread;
}
while (buf[i] == ' ') {
i++;
}
- String headerValue = new String(buf, i, nread-1);
+ String headerValue = new String(buf, i, nread-i);
headers.put(headerName, headerValue);
return nread;
// If status line found, read any headers
nread = readHeaders(is);
+ pathToRequest().put(path, new Request(path, headers));
+
// Then read content (if any)
// TODO handle chunked encoding from the client
if (headers.get(requestHeaders[REQ_CONTENT_LENGTH]) != null) {