From: <mgu...@us...> - 2013-02-04 12:56:11
|
Revision: 8090 http://sourceforge.net/p/htmlunit/code/8090 Author: mguillem Date: 2013-02-04 12:56:08 +0000 (Mon, 04 Feb 2013) Log Message: ----------- - XHR error handler should be called, when CORS header is missing - avoid exception when reading responseXML from a XHR where the requested host does't exist Modified Paths: -------------- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/xml/XMLHttpRequest.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/xml/XMLHttpRequestCORSTest.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/xml/XMLHttpRequestTest.java Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/xml/XMLHttpRequest.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/xml/XMLHttpRequest.java 2013-02-03 15:09:16 UTC (rev 8089) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/xml/XMLHttpRequest.java 2013-02-04 12:56:08 UTC (rev 8090) @@ -336,6 +336,9 @@ if (webResponse_ == null) { return null; // send() has not been called } + if (webResponse_ instanceof NetworkErrorWebResponse) { + return null; + } final String contentType = webResponse_.getContentType(); if (contentType.isEmpty() || contentType.contains("xml")) { try { @@ -667,8 +670,7 @@ if (LOG.isDebugEnabled()) { LOG.debug("No permitted \"Access-Control-Allow-Origin\" header for URL " + webRequest_.getUrl()); } - Context.throwAsScriptRuntimeEx( - new RuntimeException("No permitted \"Access-Control-Allow-Origin\" header.")); + throw new IOException("No permitted \"Access-Control-Allow-Origin\" header."); } } catch (final IOException e) { @@ -677,7 +679,12 @@ } webResponse_ = new NetworkErrorWebResponse(webRequest_); setState(STATE_COMPLETED, context); - processError(context); + if (async_) { + processError(context); + } + else { + Context.throwAsScriptRuntimeEx(e); + } } } Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/xml/XMLHttpRequestCORSTest.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/xml/XMLHttpRequestCORSTest.java 2013-02-03 15:09:16 UTC (rev 8089) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/xml/XMLHttpRequestCORSTest.java 2013-02-04 12:56:08 UTC (rev 8090) @@ -50,6 +50,29 @@ * @throws Exception if the test fails. */ @Test + @Alerts(DEFAULT = "error", IE = { }) + public void noCorsHeaderCallsErrorHandler() throws Exception { + final String html = "<html><head>\n" + + "<script>\n" + + "var xhr = " + XHRInstantiation_ + ";\n" + + "function test() {\n" + + " try {\n" + + " var url = '" + URL_THIRD + "';\n" + + " xhr.open('GET', url, true);\n" + + " xhr.onerror = function() { alert('error'); };\n" + + " xhr.send();\n" + + " } catch(e) { alert('exception'); }\n" + + "}\n" + + "</script>\n" + + "</head>\n" + + "<body onload='test()'></body></html>"; + loadPageWithAlerts2(html); + } + + /** + * @throws Exception if the test fails. + */ + @Test @Alerts(IE = { "4", "200", "No Origin!" }, DEFAULT = { "4", "200", "§§URL§§" }) public void simple() throws Exception { SimpleServerServlet.ACCESS_CONTROL_ALLOW_ORIGIN_ = "*"; Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/xml/XMLHttpRequestTest.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/xml/XMLHttpRequestTest.java 2013-02-03 15:09:16 UTC (rev 8089) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/xml/XMLHttpRequestTest.java 2013-02-04 12:56:08 UTC (rev 8090) @@ -229,6 +229,34 @@ * @throws Exception if the test fails */ @Test + @Alerts("received: null") + public void responseXML_siteNotExisting() throws Exception { + final String html = "<html><head>\n" + + "<script>\n" + + "function test() {\n" + + " var request;\n" + + " if (window.XMLHttpRequest)\n" + + " request = new XMLHttpRequest();\n" + + " else if (window.ActiveXObject)\n" + + " request = new ActiveXObject('Microsoft.XMLHTTP');\n" + + "try {\n" + + " request.open('GET', 'http://this.doesnt.exist/foo.xml', false);\n" + + " request.send('');\n" + + "} catch(e) {\n" + + " alert('received: ' + request.responseXML);\n" + + "}\n" + + "}\n" + + "</script>\n" + + "</head>\n" + + "<body onload='test()'></body></html>"; + + loadPageWithAlerts2(html); + } + + /** + * @throws Exception if the test fails + */ + @Test public void sendNull() throws Exception { final String html = "<html><head>\n" + "<script>\n" |