CVS Commit: src/xdocs: Javascript: XMLHttpRequest now accepts non xml

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 now accepts non xml

mguillem
Log Message:
-----------
Javascript: XMLHttpRequest now accepts non xml responses. Fix for bug 1209682.

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.3
retrieving revision 1.4
diff -Lsrc/test/java/com/gargoylesoftware/htmlunit/javascript/host/XMLHttpRequestTest.java -Lsrc/test/java/com/gargoylesoftware/htmlunit/javascript/host/XMLHttpRequestTest.java -u -d -r1.3 -r1.4
--- src/test/java/com/gargoylesoftware/htmlunit/javascript/host/XMLHttpRequestTest.java
+++ src/test/java/com/gargoylesoftware/htmlunit/javascript/host/XMLHttpRequestTest.java
@@ -277,11 +277,46 @@
         final MockWebConnection webConnection = new MockWebConnection( client );
         webConnection.setResponse(URL_FIRST, html);
         final URL urlPage2 = new URL(URL_FIRST.toExternalForm() + "/foo.xml");
-        webConnection.setResponse(urlPage2, xml, 200, "OK", "text/xml", Collections.EMPTY_LIST);
+        webConnection.setResponse(urlPage2, xml, "text/xml");
         client.setWebConnection( webConnection );
         client.getPage(URL_FIRST);
 
         final List alerts = Arrays.asList( new String[] { COMPLETED, xml } );
         assertEquals( alerts, collectedAlerts );
     }
+
+    /**
+     * @throws Exception if the test fails.
+     */
+    public void testResponseText_NotXml() 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('');"
+            + "  alert(request.responseText);"
+            + "}"
+            + "</script>"
+            + "</head>"
+            + "<body onload='test()'></body></html>";
+
+        final WebClient client = new WebClient();
+        final List collectedAlerts = new ArrayList();
+        client.setAlertHandler( new CollectingAlertHandler( collectedAlerts ) );
+        final MockWebConnection webConnection = new MockWebConnection( client );
+        webConnection.setResponse(URL_FIRST, html);
+        final URL urlPage2 = new URL(URL_FIRST.toExternalForm() + "/foo.txt");
+        webConnection.setResponse(urlPage2, "bla bla", "text/plain");
+        client.setWebConnection( webConnection );
+        client.getPage(URL_FIRST);
+
+        final List alerts = Arrays.asList( new String[] { "bla bla" } );
+        assertEquals( alerts, collectedAlerts );
+    }
 }
Index: XMLHttpRequest.java
===================================================================
RCS file: /cvsroot/htmlunit/htmlunit/src/java/com/gargoylesoftware/htmlunit/javascript/host/XMLHttpRequest.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -Lsrc/java/com/gargoylesoftware/htmlunit/javascript/host/XMLHttpRequest.java -Lsrc/java/com/gargoylesoftware/htmlunit/javascript/host/XMLHttpRequest.java -u -d -r1.5 -r1.6
--- src/java/com/gargoylesoftware/htmlunit/javascript/host/XMLHttpRequest.java
+++ src/java/com/gargoylesoftware/htmlunit/javascript/host/XMLHttpRequest.java
@@ -61,6 +61,7 @@
  * A JavaScript object for a XMLHttpRequest.
  *
  * @author Daniel Gredler
+ * @author Marc Guillemot
  * @version $Revision$
  * @see <a href="http://developer.apple.com/internet/webcontent/xmlhttpreq.html">Safari documentation</a>
  */
@@ -84,7 +85,7 @@
     private WebRequestSettings requestSettings_;
     private boolean async_;
     private Thread requestThread_;
-    private XmlPage page_;
+    private Page page_;
 
     /**
      * Creates a new instance. JavaScript objects must have a default constructor.
@@ -105,7 +106,7 @@
      * Sets the event handler that fires on every state change.
      * @param stateChangeHandler The event handler that fires on every state change.
      */
-    public void jsxSet_onreadystatechange( Function stateChangeHandler ) {
+    public void jsxSet_onreadystatechange( final Function stateChangeHandler ) {
         stateChangeHandler_ = stateChangeHandler;
     }
 
@@ -153,7 +154,7 @@
      */
     public String jsxGet_responseText() {
         if( page_ != null ) {
-            return page_.getContent();
+            return page_.getWebResponse().getContentAsString();
         }
         else {
             getLog().debug( "XMLHttpRequest.responseText was retrieved before the response was available." );
@@ -166,11 +167,11 @@
      * @return A DOM-compatible document object version of the data retrieved from the server.
      */
     public Object jsxGet_responseXML() {
-        if( page_ != null ) {
-            return page_.getXmlDocument();
+        if (page_ != null && page_ instanceof XmlPage) {
+            return ((XmlPage) page_).getXmlDocument();
         }
         else {
-            getLog().error( "XMLHttpRequest.responseXML was retrieved before the response was available." );
+            getLog().debug("XMLHttpRequest.responseXML was called but the response is " + page_);
             return null;
         }
     }
@@ -302,15 +303,7 @@
                     if (content != null && !"undefined".equals(content) && content.length() > 0) {
                         requestSettings_.setRequestBody( content );
                     }
-                    final Page page = wc.getPage( requestSettings_ );
-                    final String contentType = page.getWebResponse().getContentType();
-                    if( "text/xml".equals( contentType ) == false ) {
-                        setState( STATE_LOADING, context );
-                        final String msg = "The Content-Type of the data returned from the server must be 'text/xml'; "
-                            + "actual content type was '" + contentType + "'.";
-                        throw Context.reportRuntimeError( msg );
-                    }
-                    page_ = (XmlPage) page;
+                    page_ = wc.getPage( requestSettings_ );
                     setState( STATE_INTERACTIVE, context );
                     setState( STATE_COMPLETED, context );
                 }
Index: changes.xml
===================================================================
RCS file: /cvsroot/htmlunit/htmlunit/src/xdocs/changes.xml,v
retrieving revision 1.443
retrieving revision 1.444
diff -Lsrc/xdocs/changes.xml -Lsrc/xdocs/changes.xml -u -d -r1.443 -r1.444
--- src/xdocs/changes.xml
+++ src/xdocs/changes.xml
@@ -7,6 +7,9 @@
 
     <body>
         <release version="NextRelease" date="Unknown">
+            <action type="update" dev="mguillem" id="1209682">
+                Javascript: XMLHttpRequest now accepts non xml responses.
+            </action>
             <action type="update" dev="mguillem" id="1323425">
                 Javascript: allow to change selected attribute of options created with
  document.createElement('option') and not yet added to a select.


-------------------------------------------------------
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