env.js's XMLHttpRequest won't add message content to outgoing HTTP POST [fix included]

env.js's XMLHttpRequest won't add message content to outgoing HTTP POST [fix included]


While using http://dev.jquery.com/browser/trunk/jquery/build/runtest/env.js
for some unit tests, I noticed that outgoing HTTP POST requests could
not have body content. This prevents env.js from begin useful for
unit tests against APIs that use the POST body to contain JSON-RPC
information.
Specifically, look at function makeRequest() beginning on line 584 in
SVN trunk (link above). You'll see near line 590 that the file: PUT
case uses a FileWriter to output variable 'data' to the file. Now
look at the URLConnection-based code starting at line #605. 'data' is
never written to the outgoing HTTP request.
One possible way to fix it would be amend the block to be:
var connection = url.openConnection();
connection.setRequestMethod( self.method );
// Add headers to Java connection
for (var header in self.headers)
connection.addRequestProperty(header, self.headers[header]);
// Will we write content to the request?
var text = new java.lang.String( data || "" );
if ( text.length() > 0 ) {
connection.setDoOutput(true);
}
connection.connect();
if ( text.length() > 0 ) {
// Write content to the connection's stream
var out = connection.getOutputStream();
out.write( text.getBytes() );
out.flush();
out.close();
}
// Stick the response headers into responseHeaders
for (var i = 0; ; i++) {
var headerName = connection.getHeaderFieldKey(i);
var headerValue = connection.getHeaderField(i);
if (!headerName && !headerValue) break;
if (headerName)
self.responseHeaders[headerName] = headerValue;
}
handleResponse();
Tested it, and it works for me atop J2SE 1.5.
I'm happy to generate and submit a patch for this. Please advise.
- Rhys