CVS Commit: src/xdocs: JavaScript: XMLHttpRequest.send now accepts null

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

CVS Commit: src/xdocs: JavaScript: XMLHttpRequest.send now accepts null

mguillem
Log Message:
-----------
JavaScript: XMLHttpRequest.send now accepts null as parameter and fails when simulating Mozilla if called with no parameter like the browser does.

Modified Files:
--------------
    htmlunit/src/java/com/gargoylesoftware/htmlunit/javascript/host:
        XMLHttpRequest.java
        (http://cvs.sourceforge.net/viewcvs.py/htmlunit/htmlunit/src/java/com/gargoylesoftware/htmlunit/javascript/host/XMLHttpRequest.java)
    htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host:
        XMLHttpRequestTest.java
        (http://cvs.sourceforge.net/viewcvs.py/htmlunit/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/XMLHttpRequestTest.java)
    htmlunit/src/xdocs:
        changes.xml
        (http://cvs.sourceforge.net/viewcvs.py/htmlunit/htmlunit/src/xdocs/changes.xml)

Revision Data
-------------
Index: XMLHttpRequestTest.java
===================================================================
RCS file: /cvsroot/htmlunit/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/XMLHttpRequestTest.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -Lsrc/test/java/com/gargoylesoftware/htmlunit/javascript/host/XMLHttpRequestTest.java -Lsrc/test/java/com/gargoylesoftware/htmlunit/javascript/host/XMLHttpRequestTest.java -u -d -r1.4 -r1.5
--- src/test/java/com/gargoylesoftware/htmlunit/javascript/host/XMLHttpRequestTest.java
+++ src/test/java/com/gargoylesoftware/htmlunit/javascript/host/XMLHttpRequestTest.java
@@ -217,7 +217,7 @@
         client.setAlertHandler( new CollectingAlertHandler( collectedAlerts ) );
         final MockWebConnection webConnection = new MockWebConnection( client );
         webConnection.setResponse( URL_FIRST, html );
-        webConnection.setResponse( URL_SECOND, xml, 200, "OK", "text/xml", Collections.EMPTY_LIST );
+        webConnection.setResponse(URL_SECOND, xml, "text/xml");
         client.setWebConnection( webConnection );
         client.getPage( URL_FIRST );
 
@@ -319,4 +319,77 @@
         final List alerts = Arrays.asList( new String[] { "bla bla" } );
         assertEquals( alerts, collectedAlerts );
     }
+
+    /**
+     * @throws Exception if the test fails.
+     */
+    public void testSendNull() throws Exception {
+        final String html = "<html><head>"
+            + "<script>"
+            + "function test()"
+            + "{"
+            + "  var request;"
+            + "  if (window.XMLHttpRequest)"
+            + "    request = new XMLHttpRequest();"
+            + "  else if (window.ActiveXObject)"
+            + "    request = new ActiveXObject('Microsoft.XMLHTTP');"
+            + "  request.open('GET', 'foo.txt', false);"
+            + "  request.send(null);"
+            + "}"
+            + "</script>"
+            + "</head>"
+            + "<body onload='test()'></body></html>";
+
+        final WebClient client = new WebClient();
+        final MockWebConnection webConnection = new MockWebConnection( client );
+        webConnection.setResponse(URL_FIRST, html);
+        webConnection.setDefaultResponse("");
+        client.setWebConnection( webConnection );
+        client.getPage(URL_FIRST);
+    }
+
+    /**
+     * Test calls to send() without any argument
+     * @throws Exception if the test fails.
+     */
+    public void testSendNoArg() throws Exception {
+        testSendNoArg(BrowserVersion.INTERNET_EXPLORER_6_0);
+        // Mozilla fails if no arg is provided.
+        try {
+            testSendNoArg(BrowserVersion.MOZILLA_1_0);
+            fail("Should have thrown");
+        }
+        catch (final Exception e) {
+            // nothing
+            assertTrue(e.getMessage().indexOf("not enough arguments") != -1);
+        }
+
+    }
+    /**
+     * @throws Exception if the test fails.
+     */
+    private void testSendNoArg(final BrowserVersion browserVersion) throws Exception {
+        final String html = "<html><head>"
+            + "<script>"
+            + "function test()"
+            + "{"
+            + "  var request;"
+            + "  if (window.XMLHttpRequest)"
+            + "    request = new XMLHttpRequest();"
+            + "  else if (window.ActiveXObject)"
+            + "    request = new ActiveXObject('Microsoft.XMLHTTP');"
+            + "  request.open('GET', 'foo.txt', false);"
+            + "  request.send();"
+            + "}"
+            + "</script>"
+            + "</head>"
+            + "<body onload='test()'></body></html>";
+
+        final WebClient client = new WebClient(browserVersion);
+        final MockWebConnection webConnection = new MockWebConnection( client );
+        webConnection.setResponse(URL_FIRST, html);
+        webConnection.setDefaultResponse("");
+        client.setWebConnection( webConnection );
+        client.getPage(URL_FIRST);
+    }
 }
Index: XMLHttpRequest.java
===================================================================
RCS file: /cvsroot/htmlunit/htmlunit/src/java/com/gargoylesoftware/htmlunit/javascript/host/XMLHttpRequest.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -Lsrc/java/com/gargoylesoftware/htmlunit/javascript/host/XMLHttpRequest.java -Lsrc/java/com/gargoylesoftware/htmlunit/javascript/host/XMLHttpRequest.java -u -d -r1.6 -r1.7
--- src/java/com/gargoylesoftware/htmlunit/javascript/host/XMLHttpRequest.java
+++ src/java/com/gargoylesoftware/htmlunit/javascript/host/XMLHttpRequest.java
@@ -292,37 +292,56 @@
      * Sends the specified content to the server in an HTTP request and receives the response.
      * @param content The body of the message being sent with the request.
      */
-    public void jsxFunction_send( final String content ) {
-        // Create and start a thread in which to execute the request.
-        final WebClient wc = getWindow().getWebWindow().getWebClient();
-        final Context context = Context.getCurrentContext();
-        final Thread t = new Thread( "XMLHttpRequest.send() Thread" ) {
-            public void run() {
-                try {
-                    setState( STATE_LOADED, context );
-                    if (content != null && !"undefined".equals(content) && content.length() > 0) {
-                        requestSettings_.setRequestBody( content );
+    public void jsxFunction_send( final Object content ) {
+        if (async_) {
+            // Create and start a thread in which to execute the request.
+            final Thread t = new Thread( "XMLHttpRequest.send() Thread" ) {
+                public void run() {
+                    final Context context = Context.enter();
+                    try {
+                        doSend(content, context);
+                    }
+                    finally {
+                        Context.exit();
                     }
-                    page_ = wc.getPage( requestSettings_ );
-                    setState( STATE_INTERACTIVE, context );
-                    setState( STATE_COMPLETED, context );
-                }
-                catch( final IOException e ) {
-                    setState( STATE_LOADING, context );
-                    throw Context.reportRuntimeError( "Unable to send the XMLHttpRequest: " + e );
                 }
+            };
+            requestThread_ = t;
+            getLog().debug("Starting XMLHttpRequest thread for asynchronous request");
+            requestThread_.start();
+        }
+        else {
+            doSend(content, Context.getCurrentContext());
+        }
+    }
+
+    /**
+     * The real send job
+     * @param content the content to send
+     * @param context the current context
+     */
+    private void doSend(final Object content, final Context context) {
+        final WebClient wc = getWindow().getWebWindow().getWebClient();
+        try {
+            setState( STATE_LOADED, context );
+            if (Context.getUndefinedValue().equals(content) && wc.getBrowserVersion().isNetscape()) {
+                throw Context.reportRuntimeError("XMLHttpRequest.send: not enough arguments");
             }
-        };
-        requestThread_ = t;
-        requestThread_.start();
-        // If the call is to be synchronous, wait for the thread to return.
-        if( ! async_ ) {
-            try {
-                requestThread_.join();
-            }
-            catch( final InterruptedException e ) {
-                getLog().info( t.getName() + " interrupted; abort() probably called." );
+
+            if (content != null && !Context.getUndefinedValue().equals(content)) {
+                final String body = Context.toString(content);
+                if (body.length() > 0) {
+                    getLog().debug("Setting request body to: " + body);
+                    requestSettings_.setRequestBody(body);
+                }
             }
+            page_ = wc.getPage( requestSettings_ );
+            setState( STATE_INTERACTIVE, context );
+            setState( STATE_COMPLETED, context );
+        }
+        catch( final IOException e ) {
+            setState( STATE_LOADING, context );
+            throw Context.reportRuntimeError( "Unable to send the XMLHttpRequest: " + e );
         }
     }
 
Index: changes.xml
===================================================================
RCS file: /cvsroot/htmlunit/htmlunit/src/xdocs/changes.xml,v
retrieving revision 1.446
retrieving revision 1.447
diff -Lsrc/xdocs/changes.xml -Lsrc/xdocs/changes.xml -u -d -r1.446 -r1.447
--- src/xdocs/changes.xml
+++ src/xdocs/changes.xml
@@ -8,6 +8,10 @@
     <body>
         <release version="NextRelease" date="Unknown">
             <action type="update" dev="mguillem">
+                JavaScript: XMLHttpRequest.send now accepts null as parameter
+ and fails when simulating Mozilla if called with no parameter like the browser does.
+            </action>
+            <action type="update" dev="mguillem">
                 Upgraded commons-httpclient to version 3.0rc4.
             </action>
             <action type="update" dev="mguillem" id="1260369">


-------------------------------------------------------
This SF.Net email is sponsored by:
Power Architecture Resource Center: Free content, downloads, discussions,
and more. http://solutions.newsforge.com/ibmarch.tmpl
_______________________________________________
HtmlUnit-develop mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/htmlunit-develop